blob: 31d52c51acc374c471c6c50d12c91e1bdbfc27c6 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040037#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050038#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040039#include <linux/posix_acl.h>
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040046#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040047#include "ordered-data.h"
Chris Mason39279cc2007-06-12 06:35:45 -040048
49struct btrfs_iget_args {
50 u64 ino;
51 struct btrfs_root *root;
52};
53
54static struct inode_operations btrfs_dir_inode_operations;
55static struct inode_operations btrfs_symlink_inode_operations;
56static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040057static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040058static struct inode_operations btrfs_file_inode_operations;
59static struct address_space_operations btrfs_aops;
60static struct address_space_operations btrfs_symlink_aops;
61static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050062static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040063
64static struct kmem_cache *btrfs_inode_cachep;
65struct kmem_cache *btrfs_trans_handle_cachep;
66struct kmem_cache *btrfs_transaction_cachep;
67struct kmem_cache *btrfs_bit_radix_cachep;
68struct kmem_cache *btrfs_path_cachep;
69
70#define S_SHIFT 12
71static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
72 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
73 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
74 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
75 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
76 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
77 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
78 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
79};
80
Josef Bacik7b128762008-07-24 12:17:14 -040081static void btrfs_truncate(struct inode *inode);
82
Chris Mason1832a6d2007-12-21 16:27:21 -050083int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
84 int for_del)
85{
Chris Masona2135012008-06-25 16:01:30 -040086 u64 total;
87 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050088 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040089 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050090 int ret = 0;
91
Chris Masona2135012008-06-25 16:01:30 -040092 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
93 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
94 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050095 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050096 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050097 else
Chris Masonf9ef6602008-01-03 09:22:38 -050098 thresh = total * 85;
99
100 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500101
Chris Mason1832a6d2007-12-21 16:27:21 -0500102 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
103 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400104 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500105 return ret;
106}
107
Chris Masonbe20aa92007-12-17 20:14:01 -0500108static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400109{
110 struct btrfs_root *root = BTRFS_I(inode)->root;
111 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400112 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400113 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500114 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400115 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500116 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500117 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400118 struct extent_map *em;
119 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
120 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400121
Chris Masonf9295742008-07-17 12:54:14 -0400122 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400123 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500124 btrfs_set_trans_block_group(trans, inode);
125
Chris Masondb945352007-10-15 16:15:53 -0400126 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500127 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500128 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400129
Chris Mason179e29e2007-11-01 11:28:41 -0400130 if (alloc_hint == EXTENT_MAP_INLINE)
131 goto out;
132
Chris Mason3b951512008-04-17 11:29:12 -0400133 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone5a22172008-07-18 20:42:20 -0400134 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400135 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Masone5a22172008-07-18 20:42:20 -0400136 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400137
Chris Masonc59f8952007-12-17 20:14:04 -0500138 while(num_bytes > 0) {
139 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400140 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
141 root->sectorsize, 0, 0,
142 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500143 if (ret) {
144 WARN_ON(1);
145 goto out;
146 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400147 em = alloc_extent_map(GFP_NOFS);
148 em->start = start;
149 em->len = ins.offset;
150 em->block_start = ins.objectid;
151 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masone5a22172008-07-18 20:42:20 -0400152 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400153 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400154 while(1) {
155 spin_lock(&em_tree->lock);
156 ret = add_extent_mapping(em_tree, em);
157 spin_unlock(&em_tree->lock);
158 if (ret != -EEXIST) {
159 free_extent_map(em);
160 break;
161 }
162 btrfs_drop_extent_cache(inode, start,
163 start + ins.offset - 1);
164 }
Chris Masone5a22172008-07-18 20:42:20 -0400165 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400166
Chris Mason98d20f62008-04-14 09:46:10 -0400167 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400168 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
169 ins.offset);
170 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400171 if (num_bytes < cur_alloc_size) {
172 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
173 cur_alloc_size);
174 break;
175 }
Chris Masonc59f8952007-12-17 20:14:04 -0500176 num_bytes -= cur_alloc_size;
177 alloc_hint = ins.objectid + ins.offset;
178 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400179 }
Chris Masonb888db22007-08-27 16:49:44 -0400180out:
181 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500182 return ret;
183}
184
185static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
186{
187 u64 extent_start;
188 u64 extent_end;
189 u64 bytenr;
190 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500191 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500192 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500193 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400194 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500195 struct extent_buffer *leaf;
196 int found_type;
197 struct btrfs_path *path;
198 struct btrfs_file_extent_item *item;
199 int ret;
200 int err;
201 struct btrfs_key found_key;
202
Chris Masonc31f8832008-01-08 15:46:31 -0500203 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500204 path = btrfs_alloc_path();
205 BUG_ON(!path);
206again:
207 ret = btrfs_lookup_file_extent(NULL, root, path,
208 inode->i_ino, start, 0);
209 if (ret < 0) {
210 btrfs_free_path(path);
211 return ret;
212 }
213
214 cow_end = end;
215 if (ret != 0) {
216 if (path->slots[0] == 0)
217 goto not_found;
218 path->slots[0]--;
219 }
220
221 leaf = path->nodes[0];
222 item = btrfs_item_ptr(leaf, path->slots[0],
223 struct btrfs_file_extent_item);
224
225 /* are we inside the extent that was found? */
226 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
227 found_type = btrfs_key_type(&found_key);
228 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400229 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500230 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500231
232 found_type = btrfs_file_extent_type(leaf, item);
233 extent_start = found_key.offset;
234 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500235 u64 extent_num_bytes;
236
237 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
238 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500239 err = 0;
240
Chris Mason1832a6d2007-12-21 16:27:21 -0500241 if (loops && start != extent_start)
242 goto not_found;
243
Chris Masonbe20aa92007-12-17 20:14:01 -0500244 if (start < extent_start || start >= extent_end)
245 goto not_found;
246
247 cow_end = min(end, extent_end - 1);
248 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
249 if (bytenr == 0)
250 goto not_found;
251
Yan Zhengf321e492008-07-30 09:26:11 -0400252 if (btrfs_cross_ref_exists(root, &found_key, bytenr))
Chris Masona68d5932008-05-08 14:11:56 -0400253 goto not_found;
Chris Masonc31f8832008-01-08 15:46:31 -0500254 /*
255 * we may be called by the resizer, make sure we're inside
256 * the limits of the FS
257 */
Chris Masona68d5932008-05-08 14:11:56 -0400258 block_group = btrfs_lookup_block_group(root->fs_info,
259 bytenr);
260 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500261 goto not_found;
262
Chris Masonbe20aa92007-12-17 20:14:01 -0500263 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500264 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500265 goto not_found;
266 }
267loop:
268 if (start > end) {
269 btrfs_free_path(path);
270 return 0;
271 }
272 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500273 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500274 goto again;
275
276not_found:
Yan Zhengf321e492008-07-30 09:26:11 -0400277 btrfs_release_path(root, path);
Chris Masonbbaf5492008-05-08 16:31:21 -0400278 cow_file_range(inode, start, end);
279 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500280 goto loop;
281}
282
283static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
284{
285 struct btrfs_root *root = BTRFS_I(inode)->root;
286 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400287
Yanb98b6762008-01-08 15:54:37 -0500288 if (btrfs_test_opt(root, NODATACOW) ||
289 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500290 ret = run_delalloc_nocow(inode, start, end);
291 else
292 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500293
Chris Masonb888db22007-08-27 16:49:44 -0400294 return ret;
295}
296
Chris Mason291d6732008-01-29 15:55:23 -0500297int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500298 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500299{
Chris Masonbcbfce82008-04-22 13:26:47 -0400300 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500301 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500302 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400303 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500304 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500305 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400306 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500307 }
308 return 0;
309}
310
311int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500312 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500313{
Chris Masonb0c68f82008-01-31 11:05:37 -0500314 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500315 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400316 unsigned long flags;
317
318 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500319 if (end - start + 1 > root->fs_info->delalloc_bytes) {
320 printk("warning: delalloc account %Lu %Lu\n",
321 end - start + 1, root->fs_info->delalloc_bytes);
322 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500323 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500324 } else {
325 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500326 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500327 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400328 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500329 }
330 return 0;
331}
332
Chris Mason239b14b2008-03-24 15:02:07 -0400333int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
334 size_t size, struct bio *bio)
335{
336 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
337 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400338 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400339 u64 length = 0;
340 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400341 int ret;
342
Chris Masonf2d8d742008-04-21 10:03:05 -0400343 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400344 map_tree = &root->fs_info->mapping_tree;
345 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400346 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400347 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400348
Chris Mason239b14b2008-03-24 15:02:07 -0400349 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400350 return 1;
351 }
352 return 0;
353}
354
Chris Mason44b8bd72008-04-16 11:14:51 -0400355int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400356 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500357{
Chris Mason065631f2008-02-20 12:07:25 -0500358 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500359 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400360
Chris Mason3edf7d32008-07-18 06:17:13 -0400361 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400362 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400363
Chris Mason8b712842008-06-11 16:50:36 -0400364 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400365}
366
367int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
368 int mirror_num)
369{
370 struct btrfs_root *root = BTRFS_I(inode)->root;
371 int ret = 0;
372
Chris Masone6dcd2d2008-07-17 12:53:50 -0400373 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
374 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500375
Chris Masone6dcd2d2008-07-17 12:53:50 -0400376 if (!(rw & (1 << BIO_RW))) {
Chris Mason61b49442008-07-31 15:42:53 -0400377 if (!btrfs_test_opt(root, NODATASUM) &&
378 !btrfs_test_flag(inode, NODATASUM)) {
379 btrfs_lookup_bio_sums(root, inode, bio);
380 }
Chris Mason0b86a832008-03-24 15:01:56 -0400381 goto mapit;
382 }
Chris Mason065631f2008-02-20 12:07:25 -0500383
Chris Mason44b8bd72008-04-16 11:14:51 -0400384 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
385 inode, rw, bio, mirror_num,
386 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400387mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400388 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500389}
Chris Mason6885f302008-02-20 16:11:05 -0500390
Chris Masonba1da2f2008-07-17 12:54:15 -0400391static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400392 struct inode *inode, u64 file_offset,
393 struct list_head *list)
394{
395 struct list_head *cur;
396 struct btrfs_ordered_sum *sum;
397
398 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400399 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400400 sum = list_entry(cur, struct btrfs_ordered_sum, list);
401 mutex_lock(&BTRFS_I(inode)->csum_mutex);
402 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
403 inode, sum);
404 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400405 }
406 return 0;
407}
408
Chris Mason247e7432008-07-17 12:53:51 -0400409struct btrfs_writepage_fixup {
410 struct page *page;
411 struct btrfs_work work;
412};
413
414/* see btrfs_writepage_start_hook for details on why this is required */
415void btrfs_writepage_fixup_worker(struct btrfs_work *work)
416{
417 struct btrfs_writepage_fixup *fixup;
418 struct btrfs_ordered_extent *ordered;
419 struct page *page;
420 struct inode *inode;
421 u64 page_start;
422 u64 page_end;
423
424 fixup = container_of(work, struct btrfs_writepage_fixup, work);
425 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400426again:
Chris Mason247e7432008-07-17 12:53:51 -0400427 lock_page(page);
428 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
429 ClearPageChecked(page);
430 goto out_page;
431 }
432
433 inode = page->mapping->host;
434 page_start = page_offset(page);
435 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
436
437 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400438
439 /* already ordered? We're done */
440 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
441 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400442 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400443 }
444
445 ordered = btrfs_lookup_ordered_extent(inode, page_start);
446 if (ordered) {
447 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
448 page_end, GFP_NOFS);
449 unlock_page(page);
450 btrfs_start_ordered_extent(inode, ordered, 1);
451 goto again;
452 }
Chris Mason247e7432008-07-17 12:53:51 -0400453
454 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
455 GFP_NOFS);
456 ClearPageChecked(page);
457out:
458 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
459out_page:
460 unlock_page(page);
461 page_cache_release(page);
462}
463
464/*
465 * There are a few paths in the higher layers of the kernel that directly
466 * set the page dirty bit without asking the filesystem if it is a
467 * good idea. This causes problems because we want to make sure COW
468 * properly happens and the data=ordered rules are followed.
469 *
470 * In our case any range that doesn't have the EXTENT_ORDERED bit set
471 * hasn't been properly setup for IO. We kick off an async process
472 * to fix it up. The async helper will wait for ordered extents, set
473 * the delalloc bit and make it safe to write the page.
474 */
475int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
476{
477 struct inode *inode = page->mapping->host;
478 struct btrfs_writepage_fixup *fixup;
479 struct btrfs_root *root = BTRFS_I(inode)->root;
480 int ret;
481
482 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
483 EXTENT_ORDERED, 0);
484 if (ret)
485 return 0;
486
487 if (PageChecked(page))
488 return -EAGAIN;
489
490 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
491 if (!fixup)
492 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400493
Chris Mason247e7432008-07-17 12:53:51 -0400494 SetPageChecked(page);
495 page_cache_get(page);
496 fixup->work.func = btrfs_writepage_fixup_worker;
497 fixup->page = page;
498 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
499 return -EAGAIN;
500}
501
Chris Mason211f90e2008-07-18 11:56:15 -0400502static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400503{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400504 struct btrfs_root *root = BTRFS_I(inode)->root;
505 struct btrfs_trans_handle *trans;
506 struct btrfs_ordered_extent *ordered_extent;
507 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400508 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
509 struct extent_map *em;
Chris Masonf4219502008-07-22 11:18:09 -0400510 struct extent_map *em_orig;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400511 u64 alloc_hint = 0;
Chris Masone5a22172008-07-18 20:42:20 -0400512 u64 clear_start;
513 u64 clear_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400514 struct list_head list;
515 struct btrfs_key ins;
Chris Masonf4219502008-07-22 11:18:09 -0400516 struct rb_node *rb;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400517 int ret;
518
519 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400520 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400521 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400522
Chris Masonf9295742008-07-17 12:54:14 -0400523 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400524
525 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
526 BUG_ON(!ordered_extent);
527
528 lock_extent(io_tree, ordered_extent->file_offset,
529 ordered_extent->file_offset + ordered_extent->len - 1,
530 GFP_NOFS);
531
532 INIT_LIST_HEAD(&list);
533
534 ins.objectid = ordered_extent->start;
535 ins.offset = ordered_extent->len;
536 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400537
Chris Masone6dcd2d2008-07-17 12:53:50 -0400538 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
539 trans->transid, inode->i_ino,
540 ordered_extent->file_offset, &ins);
541 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400542
543 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400544
Chris Masonf4219502008-07-22 11:18:09 -0400545 spin_lock(&em_tree->lock);
546 clear_start = ordered_extent->file_offset;
547 clear_end = ordered_extent->file_offset + ordered_extent->len;
548 em = lookup_extent_mapping(em_tree, clear_start,
549 ordered_extent->len);
550 em_orig = em;
551 while(em && clear_start < extent_map_end(em) && clear_end > em->start) {
552 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
553 rb = rb_next(&em->rb_node);
554 if (!rb)
555 break;
556 em = rb_entry(rb, struct extent_map, rb_node);
557 }
558 free_extent_map(em_orig);
559 spin_unlock(&em_tree->lock);
560
Chris Masone6dcd2d2008-07-17 12:53:50 -0400561 ret = btrfs_drop_extents(trans, root, inode,
562 ordered_extent->file_offset,
563 ordered_extent->file_offset +
564 ordered_extent->len,
565 ordered_extent->file_offset, &alloc_hint);
566 BUG_ON(ret);
567 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
568 ordered_extent->file_offset,
569 ordered_extent->start,
570 ordered_extent->len,
571 ordered_extent->len, 0);
572 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400573
Chris Masone6dcd2d2008-07-17 12:53:50 -0400574 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
575 ordered_extent->file_offset +
576 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400577 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
578
Chris Masone6dcd2d2008-07-17 12:53:50 -0400579 inode->i_blocks += ordered_extent->len >> 9;
580 unlock_extent(io_tree, ordered_extent->file_offset,
581 ordered_extent->file_offset + ordered_extent->len - 1,
582 GFP_NOFS);
583 add_pending_csums(trans, inode, ordered_extent->file_offset,
584 &ordered_extent->list);
585
Chris Masondbe674a2008-07-17 12:54:05 -0400586 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400587 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400588
Chris Masone6dcd2d2008-07-17 12:53:50 -0400589 /* once for us */
590 btrfs_put_ordered_extent(ordered_extent);
591 /* once for the tree */
592 btrfs_put_ordered_extent(ordered_extent);
593
594 btrfs_update_inode(trans, root, inode);
595 btrfs_end_transaction(trans, root);
596 return 0;
597}
598
Chris Mason211f90e2008-07-18 11:56:15 -0400599int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
600 struct extent_state *state, int uptodate)
601{
602 return btrfs_finish_ordered_io(page->mapping->host, start, end);
603}
604
Chris Mason7e383262008-04-09 16:28:12 -0400605struct io_failure_record {
606 struct page *page;
607 u64 start;
608 u64 len;
609 u64 logical;
610 int last_mirror;
611};
612
Chris Mason1259ab72008-05-12 13:39:03 -0400613int btrfs_io_failed_hook(struct bio *failed_bio,
614 struct page *page, u64 start, u64 end,
615 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400616{
617 struct io_failure_record *failrec = NULL;
618 u64 private;
619 struct extent_map *em;
620 struct inode *inode = page->mapping->host;
621 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400622 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400623 struct bio *bio;
624 int num_copies;
625 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400626 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400627 u64 logical;
628
629 ret = get_state_private(failure_tree, start, &private);
630 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400631 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
632 if (!failrec)
633 return -ENOMEM;
634 failrec->start = start;
635 failrec->len = end - start + 1;
636 failrec->last_mirror = 0;
637
Chris Mason3b951512008-04-17 11:29:12 -0400638 spin_lock(&em_tree->lock);
639 em = lookup_extent_mapping(em_tree, start, failrec->len);
640 if (em->start > start || em->start + em->len < start) {
641 free_extent_map(em);
642 em = NULL;
643 }
644 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400645
646 if (!em || IS_ERR(em)) {
647 kfree(failrec);
648 return -EIO;
649 }
650 logical = start - em->start;
651 logical = em->block_start + logical;
652 failrec->logical = logical;
653 free_extent_map(em);
654 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
655 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400656 set_state_private(failure_tree, start,
657 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400658 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400659 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400660 }
661 num_copies = btrfs_num_copies(
662 &BTRFS_I(inode)->root->fs_info->mapping_tree,
663 failrec->logical, failrec->len);
664 failrec->last_mirror++;
665 if (!state) {
666 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
667 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
668 failrec->start,
669 EXTENT_LOCKED);
670 if (state && state->start != failrec->start)
671 state = NULL;
672 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
673 }
674 if (!state || failrec->last_mirror > num_copies) {
675 set_state_private(failure_tree, failrec->start, 0);
676 clear_extent_bits(failure_tree, failrec->start,
677 failrec->start + failrec->len - 1,
678 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
679 kfree(failrec);
680 return -EIO;
681 }
682 bio = bio_alloc(GFP_NOFS, 1);
683 bio->bi_private = state;
684 bio->bi_end_io = failed_bio->bi_end_io;
685 bio->bi_sector = failrec->logical >> 9;
686 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400687 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400688 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400689 if (failed_bio->bi_rw & (1 << BIO_RW))
690 rw = WRITE;
691 else
692 rw = READ;
693
694 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
695 failrec->last_mirror);
696 return 0;
697}
698
699int btrfs_clean_io_failures(struct inode *inode, u64 start)
700{
701 u64 private;
702 u64 private_failure;
703 struct io_failure_record *failure;
704 int ret;
705
706 private = 0;
707 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
708 (u64)-1, 1, EXTENT_DIRTY)) {
709 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
710 start, &private_failure);
711 if (ret == 0) {
712 failure = (struct io_failure_record *)(unsigned long)
713 private_failure;
714 set_state_private(&BTRFS_I(inode)->io_failure_tree,
715 failure->start, 0);
716 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
717 failure->start,
718 failure->start + failure->len - 1,
719 EXTENT_DIRTY | EXTENT_LOCKED,
720 GFP_NOFS);
721 kfree(failure);
722 }
723 }
Chris Mason7e383262008-04-09 16:28:12 -0400724 return 0;
725}
726
Chris Mason70dec802008-01-29 09:59:12 -0500727int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
728 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400729{
Chris Mason35ebb932007-10-30 16:56:53 -0400730 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400731 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500732 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400733 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500734 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400735 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400736 struct btrfs_root *root = BTRFS_I(inode)->root;
737 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400738 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500739
Yanb98b6762008-01-08 15:54:37 -0500740 if (btrfs_test_opt(root, NODATASUM) ||
741 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500742 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500743 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500744 private = state->private;
745 ret = 0;
746 } else {
747 ret = get_state_private(io_tree, start, &private);
748 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400749 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400750 kaddr = kmap_atomic(page, KM_IRQ0);
751 if (ret) {
752 goto zeroit;
753 }
Chris Masonff79f812007-10-15 16:22:25 -0400754 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
755 btrfs_csum_final(csum, (char *)&csum);
756 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400757 goto zeroit;
758 }
759 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400760 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400761
762 /* if the io failure tree for this inode is non-empty,
763 * check to see if we've recovered from a failed IO
764 */
Chris Mason1259ab72008-05-12 13:39:03 -0400765 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400766 return 0;
767
768zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500769 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
770 page->mapping->host->i_ino, (unsigned long long)start, csum,
771 private);
Chris Masondb945352007-10-15 16:15:53 -0400772 memset(kaddr + offset, 1, end - start + 1);
773 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400774 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400775 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400776 if (private == 0)
777 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400778 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400779}
Chris Masonb888db22007-08-27 16:49:44 -0400780
Josef Bacik7b128762008-07-24 12:17:14 -0400781/*
782 * This creates an orphan entry for the given inode in case something goes
783 * wrong in the middle of an unlink/truncate.
784 */
785int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
786{
787 struct btrfs_root *root = BTRFS_I(inode)->root;
788 int ret = 0;
789
Yanbcc63ab2008-07-30 16:29:20 -0400790 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400791
792 /* already on the orphan list, we're good */
793 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400794 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400795 return 0;
796 }
797
798 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
799
Yanbcc63ab2008-07-30 16:29:20 -0400800 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400801
802 /*
803 * insert an orphan item to track this unlinked/truncated file
804 */
805 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
806
807 return ret;
808}
809
810/*
811 * We have done the truncate/delete so we can go ahead and remove the orphan
812 * item for this particular inode.
813 */
814int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
815{
816 struct btrfs_root *root = BTRFS_I(inode)->root;
817 int ret = 0;
818
Yanbcc63ab2008-07-30 16:29:20 -0400819 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400820
821 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400822 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400823 return 0;
824 }
825
826 list_del_init(&BTRFS_I(inode)->i_orphan);
827 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400828 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400829 return 0;
830 }
831
Yanbcc63ab2008-07-30 16:29:20 -0400832 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400833
834 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
835
836 return ret;
837}
838
839/*
840 * this cleans up any orphans that may be left on the list from the last use
841 * of this root.
842 */
843void btrfs_orphan_cleanup(struct btrfs_root *root)
844{
845 struct btrfs_path *path;
846 struct extent_buffer *leaf;
847 struct btrfs_item *item;
848 struct btrfs_key key, found_key;
849 struct btrfs_trans_handle *trans;
850 struct inode *inode;
851 int ret = 0, nr_unlink = 0, nr_truncate = 0;
852
853 /* don't do orphan cleanup if the fs is readonly. */
854 if (root->inode->i_sb->s_flags & MS_RDONLY)
855 return;
856
857 path = btrfs_alloc_path();
858 if (!path)
859 return;
860 path->reada = -1;
861
862 key.objectid = BTRFS_ORPHAN_OBJECTID;
863 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
864 key.offset = (u64)-1;
865
866 trans = btrfs_start_transaction(root, 1);
867 btrfs_set_trans_block_group(trans, root->inode);
868
869 while (1) {
870 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
871 if (ret < 0) {
872 printk(KERN_ERR "Error searching slot for orphan: %d"
873 "\n", ret);
874 break;
875 }
876
877 /*
878 * if ret == 0 means we found what we were searching for, which
879 * is weird, but possible, so only screw with path if we didnt
880 * find the key and see if we have stuff that matches
881 */
882 if (ret > 0) {
883 if (path->slots[0] == 0)
884 break;
885 path->slots[0]--;
886 }
887
888 /* pull out the item */
889 leaf = path->nodes[0];
890 item = btrfs_item_nr(leaf, path->slots[0]);
891 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
892
893 /* make sure the item matches what we want */
894 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
895 break;
896 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
897 break;
898
899 /* release the path since we're done with it */
900 btrfs_release_path(root, path);
901
902 /*
903 * this is where we are basically btrfs_lookup, without the
904 * crossing root thing. we store the inode number in the
905 * offset of the orphan item.
906 */
907 inode = btrfs_iget_locked(root->inode->i_sb,
908 found_key.offset, root);
909 if (!inode)
910 break;
911
912 if (inode->i_state & I_NEW) {
913 BTRFS_I(inode)->root = root;
914
915 /* have to set the location manually */
916 BTRFS_I(inode)->location.objectid = inode->i_ino;
917 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
918 BTRFS_I(inode)->location.offset = 0;
919
920 btrfs_read_locked_inode(inode);
921 unlock_new_inode(inode);
922 }
923
924 /*
925 * add this inode to the orphan list so btrfs_orphan_del does
926 * the proper thing when we hit it
927 */
Yanbcc63ab2008-07-30 16:29:20 -0400928 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400929 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -0400930 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400931
932 /*
933 * if this is a bad inode, means we actually succeeded in
934 * removing the inode, but not the orphan record, which means
935 * we need to manually delete the orphan since iput will just
936 * do a destroy_inode
937 */
938 if (is_bad_inode(inode)) {
939 btrfs_orphan_del(trans, inode);
940 iput(inode);
941 continue;
942 }
943
944 /* if we have links, this was a truncate, lets do that */
945 if (inode->i_nlink) {
946 nr_truncate++;
947 btrfs_truncate(inode);
948 } else {
949 nr_unlink++;
950 }
951
952 /* this will do delete_inode and everything for us */
953 iput(inode);
954 }
955
956 if (nr_unlink)
957 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
958 if (nr_truncate)
959 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
960
961 btrfs_free_path(path);
962 btrfs_end_transaction(trans, root);
963}
964
Chris Mason39279cc2007-06-12 06:35:45 -0400965void btrfs_read_locked_inode(struct inode *inode)
966{
967 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400968 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400969 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400970 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400971 struct btrfs_root *root = BTRFS_I(inode)->root;
972 struct btrfs_key location;
973 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400974 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400975 int ret;
976
977 path = btrfs_alloc_path();
978 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400979 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500980
Chris Mason39279cc2007-06-12 06:35:45 -0400981 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400982 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400983 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400984
Chris Mason5f39d392007-10-15 16:14:19 -0400985 leaf = path->nodes[0];
986 inode_item = btrfs_item_ptr(leaf, path->slots[0],
987 struct btrfs_inode_item);
988
989 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
990 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
991 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
992 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -0400993 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400994
995 tspec = btrfs_inode_atime(inode_item);
996 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
997 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
998
999 tspec = btrfs_inode_mtime(inode_item);
1000 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1001 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1002
1003 tspec = btrfs_inode_ctime(inode_item);
1004 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1005 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1006
1007 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1008 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -04001009 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001010 rdev = btrfs_inode_rdev(leaf, inode_item);
1011
Josef Bacikaec74772008-07-24 12:12:38 -04001012 BTRFS_I(inode)->index_cnt = (u64)-1;
1013
Chris Mason5f39d392007-10-15 16:14:19 -04001014 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001015 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1016 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001017 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001018 if (!BTRFS_I(inode)->block_group) {
1019 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001020 NULL, 0,
1021 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001022 }
Chris Mason39279cc2007-06-12 06:35:45 -04001023 btrfs_free_path(path);
1024 inode_item = NULL;
1025
Chris Mason39279cc2007-06-12 06:35:45 -04001026 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001027 case S_IFREG:
1028 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001029 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001030 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001031 inode->i_fop = &btrfs_file_operations;
1032 inode->i_op = &btrfs_file_inode_operations;
1033 break;
1034 case S_IFDIR:
1035 inode->i_fop = &btrfs_dir_file_operations;
1036 if (root == root->fs_info->tree_root)
1037 inode->i_op = &btrfs_dir_ro_inode_operations;
1038 else
1039 inode->i_op = &btrfs_dir_inode_operations;
1040 break;
1041 case S_IFLNK:
1042 inode->i_op = &btrfs_symlink_inode_operations;
1043 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001044 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001045 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001046 default:
1047 init_special_inode(inode, inode->i_mode, rdev);
1048 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001049 }
1050 return;
1051
1052make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001053 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001054 make_bad_inode(inode);
1055}
1056
Chris Mason5f39d392007-10-15 16:14:19 -04001057static void fill_inode_item(struct extent_buffer *leaf,
1058 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001059 struct inode *inode)
1060{
Chris Mason5f39d392007-10-15 16:14:19 -04001061 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1062 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001063 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001064 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1065 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1066
1067 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1068 inode->i_atime.tv_sec);
1069 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1070 inode->i_atime.tv_nsec);
1071
1072 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1073 inode->i_mtime.tv_sec);
1074 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1075 inode->i_mtime.tv_nsec);
1076
1077 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1078 inode->i_ctime.tv_sec);
1079 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1080 inode->i_ctime.tv_nsec);
1081
1082 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1083 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1084 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001085 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001086 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001087 BTRFS_I(inode)->block_group->key.objectid);
1088}
1089
Chris Masonba1da2f2008-07-17 12:54:15 -04001090int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001091 struct btrfs_root *root,
1092 struct inode *inode)
1093{
1094 struct btrfs_inode_item *inode_item;
1095 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001096 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001097 int ret;
1098
1099 path = btrfs_alloc_path();
1100 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001101 ret = btrfs_lookup_inode(trans, root, path,
1102 &BTRFS_I(inode)->location, 1);
1103 if (ret) {
1104 if (ret > 0)
1105 ret = -ENOENT;
1106 goto failed;
1107 }
1108
Chris Mason5f39d392007-10-15 16:14:19 -04001109 leaf = path->nodes[0];
1110 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001111 struct btrfs_inode_item);
1112
Chris Mason5f39d392007-10-15 16:14:19 -04001113 fill_inode_item(leaf, inode_item, inode);
1114 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001115 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001116 ret = 0;
1117failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001118 btrfs_free_path(path);
1119 return ret;
1120}
1121
1122
1123static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1124 struct btrfs_root *root,
1125 struct inode *dir,
1126 struct dentry *dentry)
1127{
1128 struct btrfs_path *path;
1129 const char *name = dentry->d_name.name;
1130 int name_len = dentry->d_name.len;
1131 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001132 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001133 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001134 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001135 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001136
1137 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001138 if (!path) {
1139 ret = -ENOMEM;
1140 goto err;
1141 }
1142
Chris Mason39279cc2007-06-12 06:35:45 -04001143 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1144 name, name_len, -1);
1145 if (IS_ERR(di)) {
1146 ret = PTR_ERR(di);
1147 goto err;
1148 }
1149 if (!di) {
1150 ret = -ENOENT;
1151 goto err;
1152 }
Chris Mason5f39d392007-10-15 16:14:19 -04001153 leaf = path->nodes[0];
1154 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001155 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001156 if (ret)
1157 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001158 btrfs_release_path(root, path);
1159
Josef Bacikaec74772008-07-24 12:12:38 -04001160 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1161 dentry->d_inode->i_ino,
1162 dentry->d_parent->d_inode->i_ino, &index);
1163 if (ret) {
1164 printk("failed to delete reference to %.*s, "
1165 "inode %lu parent %lu\n", name_len, name,
1166 dentry->d_inode->i_ino,
1167 dentry->d_parent->d_inode->i_ino);
1168 goto err;
1169 }
1170
Chris Mason39279cc2007-06-12 06:35:45 -04001171 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001172 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001173 if (IS_ERR(di)) {
1174 ret = PTR_ERR(di);
1175 goto err;
1176 }
1177 if (!di) {
1178 ret = -ENOENT;
1179 goto err;
1180 }
1181 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001182 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001183
1184 dentry->d_inode->i_ctime = dir->i_ctime;
1185err:
1186 btrfs_free_path(path);
1187 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001188 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001189 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001190 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001191#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1192 dentry->d_inode->i_nlink--;
1193#else
Chris Mason39279cc2007-06-12 06:35:45 -04001194 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001195#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001196 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001197 dir->i_sb->s_dirt = 1;
1198 }
1199 return ret;
1200}
1201
1202static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1203{
1204 struct btrfs_root *root;
1205 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001206 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001207 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001208 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001209
1210 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001211
1212 ret = btrfs_check_free_space(root, 1, 1);
1213 if (ret)
1214 goto fail;
1215
Chris Mason39279cc2007-06-12 06:35:45 -04001216 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001217
Chris Mason39279cc2007-06-12 06:35:45 -04001218 btrfs_set_trans_block_group(trans, dir);
1219 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Josef Bacik7b128762008-07-24 12:17:14 -04001220
1221 if (inode->i_nlink == 0)
1222 ret = btrfs_orphan_add(trans, inode);
1223
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001224 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001225
Chris Mason89ce8a62008-06-25 16:01:31 -04001226 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001227fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001228 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001229 return ret;
1230}
1231
1232static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1233{
1234 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001235 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001236 int ret;
1237 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001238 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001239 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001240
Chris Mason925baed2008-06-25 16:01:30 -04001241 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001242 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001243 }
Yan134d4512007-10-25 15:49:25 -04001244
Chris Mason1832a6d2007-12-21 16:27:21 -05001245 ret = btrfs_check_free_space(root, 1, 1);
1246 if (ret)
1247 goto fail;
1248
Chris Mason39279cc2007-06-12 06:35:45 -04001249 trans = btrfs_start_transaction(root, 1);
1250 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001251
Josef Bacik7b128762008-07-24 12:17:14 -04001252 err = btrfs_orphan_add(trans, inode);
1253 if (err)
1254 goto fail_trans;
1255
Chris Mason39279cc2007-06-12 06:35:45 -04001256 /* now the directory is empty */
1257 err = btrfs_unlink_trans(trans, root, dir, dentry);
1258 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001259 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001260 }
Chris Mason39544012007-12-12 14:38:19 -05001261
Josef Bacik7b128762008-07-24 12:17:14 -04001262fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001263 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001264 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001265fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001266 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001267
Chris Mason39279cc2007-06-12 06:35:45 -04001268 if (ret && !err)
1269 err = ret;
1270 return err;
1271}
1272
Chris Mason39279cc2007-06-12 06:35:45 -04001273/*
Chris Mason39279cc2007-06-12 06:35:45 -04001274 * this can truncate away extent items, csum items and directory items.
1275 * It starts at a high offset and removes keys until it can't find
1276 * any higher than i_size.
1277 *
1278 * csum items that cross the new i_size are truncated to the new size
1279 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001280 *
1281 * min_type is the minimum key type to truncate down to. If set to 0, this
1282 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001283 */
1284static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1285 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001286 struct inode *inode,
1287 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001288{
1289 int ret;
1290 struct btrfs_path *path;
1291 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001292 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001293 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001294 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001295 struct btrfs_file_extent_item *fi;
1296 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001297 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001298 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001299 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001300 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001301 int found_extent;
1302 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001303 int pending_del_nr = 0;
1304 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001305 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001306 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001307
Chris Mason3b951512008-04-17 11:29:12 -04001308 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001309 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001310 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001311 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001312
Chris Mason39279cc2007-06-12 06:35:45 -04001313 /* FIXME, add redo link to tree so we don't leak on crash */
1314 key.objectid = inode->i_ino;
1315 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001316 key.type = (u8)-1;
1317
Chris Mason85e21ba2008-01-29 15:11:36 -05001318 btrfs_init_path(path);
1319search_again:
1320 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1321 if (ret < 0) {
1322 goto error;
1323 }
1324 if (ret > 0) {
1325 BUG_ON(path->slots[0] == 0);
1326 path->slots[0]--;
1327 }
1328
Chris Mason39279cc2007-06-12 06:35:45 -04001329 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001330 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001331 leaf = path->nodes[0];
1332 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1333 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001334
Chris Mason5f39d392007-10-15 16:14:19 -04001335 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001336 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001337
Chris Mason85e21ba2008-01-29 15:11:36 -05001338 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001339 break;
1340
Chris Mason5f39d392007-10-15 16:14:19 -04001341 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001342 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001343 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001344 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001345 extent_type = btrfs_file_extent_type(leaf, fi);
1346 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001347 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001348 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001349 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1350 struct btrfs_item *item = btrfs_item_nr(leaf,
1351 path->slots[0]);
1352 item_end += btrfs_file_extent_inline_len(leaf,
1353 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001354 }
Yan008630c2007-11-07 13:31:09 -05001355 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001356 }
1357 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1358 ret = btrfs_csum_truncate(trans, root, path,
1359 inode->i_size);
1360 BUG_ON(ret);
1361 }
Yan008630c2007-11-07 13:31:09 -05001362 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001363 if (found_type == BTRFS_DIR_ITEM_KEY) {
1364 found_type = BTRFS_INODE_ITEM_KEY;
1365 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1366 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001367 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1368 found_type = BTRFS_XATTR_ITEM_KEY;
1369 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1370 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001371 } else if (found_type) {
1372 found_type--;
1373 } else {
1374 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001375 }
Yana61721d2007-09-17 11:08:38 -04001376 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001377 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001378 }
Chris Mason5f39d392007-10-15 16:14:19 -04001379 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001380 del_item = 1;
1381 else
1382 del_item = 0;
1383 found_extent = 0;
1384
1385 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001386 if (found_type != BTRFS_EXTENT_DATA_KEY)
1387 goto delete;
1388
1389 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001390 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001391 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001392 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001393 u64 orig_num_bytes =
1394 btrfs_file_extent_num_bytes(leaf, fi);
1395 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001396 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001397 extent_num_bytes = extent_num_bytes &
1398 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001399 btrfs_set_file_extent_num_bytes(leaf, fi,
1400 extent_num_bytes);
1401 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001402 extent_num_bytes);
1403 if (extent_start != 0)
1404 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001405 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001406 } else {
Chris Masondb945352007-10-15 16:15:53 -04001407 extent_num_bytes =
1408 btrfs_file_extent_disk_num_bytes(leaf,
1409 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001410 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001411 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001412 if (extent_start != 0) {
1413 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001414 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001415 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001416 root_gen = btrfs_header_generation(leaf);
1417 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001418 }
Chris Mason90692182008-02-08 13:49:28 -05001419 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1420 if (!del_item) {
1421 u32 newsize = inode->i_size - found_key.offset;
1422 dec_i_blocks(inode, item_end + 1 -
1423 found_key.offset - newsize);
1424 newsize =
1425 btrfs_file_extent_calc_inline_size(newsize);
1426 ret = btrfs_truncate_item(trans, root, path,
1427 newsize, 1);
1428 BUG_ON(ret);
1429 } else {
1430 dec_i_blocks(inode, item_end + 1 -
1431 found_key.offset);
1432 }
Chris Mason39279cc2007-06-12 06:35:45 -04001433 }
Chris Mason179e29e2007-11-01 11:28:41 -04001434delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001435 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001436 if (!pending_del_nr) {
1437 /* no pending yet, add ourselves */
1438 pending_del_slot = path->slots[0];
1439 pending_del_nr = 1;
1440 } else if (pending_del_nr &&
1441 path->slots[0] + 1 == pending_del_slot) {
1442 /* hop on the pending chunk */
1443 pending_del_nr++;
1444 pending_del_slot = path->slots[0];
1445 } else {
1446 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1447 }
Chris Mason39279cc2007-06-12 06:35:45 -04001448 } else {
1449 break;
1450 }
Chris Mason39279cc2007-06-12 06:35:45 -04001451 if (found_extent) {
1452 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001453 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001454 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001455 root_gen, inode->i_ino,
1456 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001457 BUG_ON(ret);
1458 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001459next:
1460 if (path->slots[0] == 0) {
1461 if (pending_del_nr)
1462 goto del_pending;
1463 btrfs_release_path(root, path);
1464 goto search_again;
1465 }
1466
1467 path->slots[0]--;
1468 if (pending_del_nr &&
1469 path->slots[0] + 1 != pending_del_slot) {
1470 struct btrfs_key debug;
1471del_pending:
1472 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1473 pending_del_slot);
1474 ret = btrfs_del_items(trans, root, path,
1475 pending_del_slot,
1476 pending_del_nr);
1477 BUG_ON(ret);
1478 pending_del_nr = 0;
1479 btrfs_release_path(root, path);
1480 goto search_again;
1481 }
Chris Mason39279cc2007-06-12 06:35:45 -04001482 }
1483 ret = 0;
1484error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001485 if (pending_del_nr) {
1486 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1487 pending_del_nr);
1488 }
Chris Mason39279cc2007-06-12 06:35:45 -04001489 btrfs_free_path(path);
1490 inode->i_sb->s_dirt = 1;
1491 return ret;
1492}
1493
Chris Masona52d9a82007-08-27 16:49:44 -04001494/*
1495 * taken from block_truncate_page, but does cow as it zeros out
1496 * any bytes left in the last page in the file.
1497 */
1498static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1499{
1500 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001501 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001502 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1503 struct btrfs_ordered_extent *ordered;
1504 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001505 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001506 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1507 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1508 struct page *page;
1509 int ret = 0;
1510 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001511 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001512
1513 if ((offset & (blocksize - 1)) == 0)
1514 goto out;
1515
1516 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001517again:
Chris Masona52d9a82007-08-27 16:49:44 -04001518 page = grab_cache_page(mapping, index);
1519 if (!page)
1520 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001521
1522 page_start = page_offset(page);
1523 page_end = page_start + PAGE_CACHE_SIZE - 1;
1524
Chris Masona52d9a82007-08-27 16:49:44 -04001525 if (!PageUptodate(page)) {
1526 ret = btrfs_readpage(NULL, page);
1527 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001528 if (page->mapping != mapping) {
1529 unlock_page(page);
1530 page_cache_release(page);
1531 goto again;
1532 }
Chris Masona52d9a82007-08-27 16:49:44 -04001533 if (!PageUptodate(page)) {
1534 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001535 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001536 }
1537 }
Chris Mason211c17f2008-05-15 09:13:45 -04001538 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001539
1540 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1541 set_page_extent_mapped(page);
1542
1543 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1544 if (ordered) {
1545 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1546 unlock_page(page);
1547 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001548 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001549 btrfs_put_ordered_extent(ordered);
1550 goto again;
1551 }
1552
1553 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1554 page_end, GFP_NOFS);
1555 ret = 0;
1556 if (offset != PAGE_CACHE_SIZE) {
1557 kaddr = kmap(page);
1558 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1559 flush_dcache_page(page);
1560 kunmap(page);
1561 }
Chris Mason247e7432008-07-17 12:53:51 -04001562 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001563 set_page_dirty(page);
1564 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001565
Chris Mason89642222008-07-24 09:41:53 -04001566out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001567 unlock_page(page);
1568 page_cache_release(page);
1569out:
1570 return ret;
1571}
1572
1573static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1574{
1575 struct inode *inode = dentry->d_inode;
1576 int err;
1577
1578 err = inode_change_ok(inode, attr);
1579 if (err)
1580 return err;
1581
1582 if (S_ISREG(inode->i_mode) &&
1583 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1584 struct btrfs_trans_handle *trans;
1585 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001586 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001587
Chris Mason5f39d392007-10-15 16:14:19 -04001588 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001589 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001590 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001591 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001592 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001593
Chris Mason1b0f7c22008-01-30 14:33:02 -05001594 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001595 goto out;
1596
Chris Mason1832a6d2007-12-21 16:27:21 -05001597 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001598 if (err)
1599 goto fail;
1600
Chris Mason39279cc2007-06-12 06:35:45 -04001601 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1602
Chris Mason5f564062008-01-22 16:47:59 -05001603 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001604 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1605 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001606
Chris Mason39279cc2007-06-12 06:35:45 -04001607 trans = btrfs_start_transaction(root, 1);
1608 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001609 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001610 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001611 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001612 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001613
Chris Mason179e29e2007-11-01 11:28:41 -04001614 if (alloc_hint != EXTENT_MAP_INLINE) {
1615 err = btrfs_insert_file_extent(trans, root,
1616 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001617 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001618 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001619 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001620 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001621 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001622 }
Chris Masonee6e6502008-07-17 12:54:40 -04001623 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001624 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001625 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001626 if (err)
1627 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001628 }
1629out:
1630 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001631
1632 if (!err && ((attr->ia_valid & ATTR_MODE)))
1633 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001634fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001635 return err;
1636}
Chris Mason61295eb2008-01-14 16:24:38 -05001637
Chris Mason39279cc2007-06-12 06:35:45 -04001638void btrfs_delete_inode(struct inode *inode)
1639{
1640 struct btrfs_trans_handle *trans;
1641 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001642 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001643 int ret;
1644
1645 truncate_inode_pages(&inode->i_data, 0);
1646 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001647 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001648 goto no_delete;
1649 }
Chris Mason4a096752008-07-21 10:29:44 -04001650 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001651
Chris Masondbe674a2008-07-17 12:54:05 -04001652 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001653 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001654
Chris Mason39279cc2007-06-12 06:35:45 -04001655 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001656 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001657 if (ret) {
1658 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001659 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001660 }
1661
1662 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001663
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001664 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001665 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001666
Chris Mason39279cc2007-06-12 06:35:45 -04001667 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001668 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001669 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001670
1671no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001672 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001673 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001674 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001675no_delete:
1676 clear_inode(inode);
1677}
1678
1679/*
1680 * this returns the key found in the dir entry in the location pointer.
1681 * If no dir entries were found, location->objectid is 0.
1682 */
1683static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1684 struct btrfs_key *location)
1685{
1686 const char *name = dentry->d_name.name;
1687 int namelen = dentry->d_name.len;
1688 struct btrfs_dir_item *di;
1689 struct btrfs_path *path;
1690 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001691 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001692
Chris Mason39544012007-12-12 14:38:19 -05001693 if (namelen == 1 && strcmp(name, ".") == 0) {
1694 location->objectid = dir->i_ino;
1695 location->type = BTRFS_INODE_ITEM_KEY;
1696 location->offset = 0;
1697 return 0;
1698 }
Chris Mason39279cc2007-06-12 06:35:45 -04001699 path = btrfs_alloc_path();
1700 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001701
Chris Mason7a720532007-12-13 09:06:59 -05001702 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001703 struct btrfs_key key;
1704 struct extent_buffer *leaf;
Chris Mason39544012007-12-12 14:38:19 -05001705 int slot;
1706
1707 key.objectid = dir->i_ino;
Yan445dceb2008-07-24 12:19:32 -04001708 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001709 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001710 if (ret < 0 || path->slots[0] == 0)
1711 goto out_err;
Chris Mason39544012007-12-12 14:38:19 -05001712 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1713 BUG_ON(ret == 0);
1714 ret = 0;
Chris Mason39544012007-12-12 14:38:19 -05001715 leaf = path->nodes[0];
Yan445dceb2008-07-24 12:19:32 -04001716 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001717
1718 btrfs_item_key_to_cpu(leaf, &key, slot);
1719 if (key.objectid != dir->i_ino ||
1720 key.type != BTRFS_INODE_REF_KEY) {
1721 goto out_err;
1722 }
1723 location->objectid = key.offset;
1724 location->type = BTRFS_INODE_ITEM_KEY;
1725 location->offset = 0;
1726 goto out;
1727 }
1728
Chris Mason39279cc2007-06-12 06:35:45 -04001729 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1730 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001731 if (IS_ERR(di))
1732 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001733 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001734 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001735 }
Chris Mason5f39d392007-10-15 16:14:19 -04001736 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001737out:
Chris Mason39279cc2007-06-12 06:35:45 -04001738 btrfs_free_path(path);
1739 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001740out_err:
1741 location->objectid = 0;
1742 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001743}
1744
1745/*
1746 * when we hit a tree root in a directory, the btrfs part of the inode
1747 * needs to be changed to reflect the root directory of the tree root. This
1748 * is kind of like crossing a mount point.
1749 */
1750static int fixup_tree_root_location(struct btrfs_root *root,
1751 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001752 struct btrfs_root **sub_root,
1753 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001754{
Chris Mason39279cc2007-06-12 06:35:45 -04001755 struct btrfs_root_item *ri;
1756
1757 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1758 return 0;
1759 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1760 return 0;
1761
Josef Bacik58176a92007-08-29 15:47:34 -04001762 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1763 dentry->d_name.name,
1764 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001765 if (IS_ERR(*sub_root))
1766 return PTR_ERR(*sub_root);
1767
1768 ri = &(*sub_root)->root_item;
1769 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001770 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1771 location->offset = 0;
1772
Chris Mason39279cc2007-06-12 06:35:45 -04001773 return 0;
1774}
1775
1776static int btrfs_init_locked_inode(struct inode *inode, void *p)
1777{
1778 struct btrfs_iget_args *args = p;
1779 inode->i_ino = args->ino;
1780 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001781 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001782 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001783 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001784 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1785 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001786 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001787 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1788 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001789 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001790 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001791 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001792 return 0;
1793}
1794
1795static int btrfs_find_actor(struct inode *inode, void *opaque)
1796{
1797 struct btrfs_iget_args *args = opaque;
1798 return (args->ino == inode->i_ino &&
1799 args->root == BTRFS_I(inode)->root);
1800}
1801
Chris Masondc17ff82008-01-08 15:46:30 -05001802struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1803 u64 root_objectid)
1804{
1805 struct btrfs_iget_args args;
1806 args.ino = objectid;
1807 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1808
1809 if (!args.root)
1810 return NULL;
1811
1812 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1813}
1814
Chris Mason39279cc2007-06-12 06:35:45 -04001815struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1816 struct btrfs_root *root)
1817{
1818 struct inode *inode;
1819 struct btrfs_iget_args args;
1820 args.ino = objectid;
1821 args.root = root;
1822
1823 inode = iget5_locked(s, objectid, btrfs_find_actor,
1824 btrfs_init_locked_inode,
1825 (void *)&args);
1826 return inode;
1827}
1828
1829static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1830 struct nameidata *nd)
1831{
1832 struct inode * inode;
1833 struct btrfs_inode *bi = BTRFS_I(dir);
1834 struct btrfs_root *root = bi->root;
1835 struct btrfs_root *sub_root = root;
1836 struct btrfs_key location;
Josef Bacik7b128762008-07-24 12:17:14 -04001837 int ret, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001838
1839 if (dentry->d_name.len > BTRFS_NAME_LEN)
1840 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001841
Chris Mason39279cc2007-06-12 06:35:45 -04001842 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001843
Chris Mason39279cc2007-06-12 06:35:45 -04001844 if (ret < 0)
1845 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001846
Chris Mason39279cc2007-06-12 06:35:45 -04001847 inode = NULL;
1848 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001849 ret = fixup_tree_root_location(root, &location, &sub_root,
1850 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001851 if (ret < 0)
1852 return ERR_PTR(ret);
1853 if (ret > 0)
1854 return ERR_PTR(-ENOENT);
Josef Bacik7b128762008-07-24 12:17:14 -04001855
Chris Mason39279cc2007-06-12 06:35:45 -04001856 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1857 sub_root);
1858 if (!inode)
1859 return ERR_PTR(-EACCES);
1860 if (inode->i_state & I_NEW) {
1861 /* the inode and parent dir are two different roots */
1862 if (sub_root != root) {
1863 igrab(inode);
1864 sub_root->inode = inode;
Josef Bacik7b128762008-07-24 12:17:14 -04001865 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001866 }
1867 BTRFS_I(inode)->root = sub_root;
1868 memcpy(&BTRFS_I(inode)->location, &location,
1869 sizeof(location));
1870 btrfs_read_locked_inode(inode);
1871 unlock_new_inode(inode);
1872 }
1873 }
Josef Bacik7b128762008-07-24 12:17:14 -04001874
1875 if (unlikely(do_orphan))
1876 btrfs_orphan_cleanup(sub_root);
1877
Chris Mason39279cc2007-06-12 06:35:45 -04001878 return d_splice_alias(inode, dentry);
1879}
1880
Chris Mason39279cc2007-06-12 06:35:45 -04001881static unsigned char btrfs_filetype_table[] = {
1882 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1883};
1884
1885static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1886{
Chris Mason6da6aba2007-12-18 16:15:09 -05001887 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001888 struct btrfs_root *root = BTRFS_I(inode)->root;
1889 struct btrfs_item *item;
1890 struct btrfs_dir_item *di;
1891 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001892 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001893 struct btrfs_path *path;
1894 int ret;
1895 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001896 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001897 int slot;
1898 int advance;
1899 unsigned char d_type;
1900 int over = 0;
1901 u32 di_cur;
1902 u32 di_total;
1903 u32 di_len;
1904 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001905 char tmp_name[32];
1906 char *name_ptr;
1907 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001908
1909 /* FIXME, use a real flag for deciding about the key type */
1910 if (root->fs_info->tree_root == root)
1911 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001912
Chris Mason39544012007-12-12 14:38:19 -05001913 /* special case for "." */
1914 if (filp->f_pos == 0) {
1915 over = filldir(dirent, ".", 1,
1916 1, inode->i_ino,
1917 DT_DIR);
1918 if (over)
1919 return 0;
1920 filp->f_pos = 1;
1921 }
1922
Chris Mason39279cc2007-06-12 06:35:45 -04001923 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001924 path = btrfs_alloc_path();
1925 path->reada = 2;
1926
1927 /* special case for .., just use the back ref */
1928 if (filp->f_pos == 1) {
1929 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001930 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001931 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan445dceb2008-07-24 12:19:32 -04001932 if (ret < 0 || path->slots[0] == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001933 btrfs_release_path(root, path);
1934 goto read_dir_items;
1935 }
Yan445dceb2008-07-24 12:19:32 -04001936 BUG_ON(ret == 0);
1937 leaf = path->nodes[0];
1938 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001939 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1940 btrfs_release_path(root, path);
1941 if (found_key.objectid != key.objectid ||
1942 found_key.type != BTRFS_INODE_REF_KEY)
1943 goto read_dir_items;
1944 over = filldir(dirent, "..", 2,
1945 2, found_key.offset, DT_DIR);
1946 if (over)
1947 goto nopos;
1948 filp->f_pos = 2;
1949 }
1950
1951read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001952 btrfs_set_key_type(&key, key_type);
1953 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001954
Chris Mason39279cc2007-06-12 06:35:45 -04001955 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1956 if (ret < 0)
1957 goto err;
1958 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001959 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001960 leaf = path->nodes[0];
1961 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001962 slot = path->slots[0];
1963 if (advance || slot >= nritems) {
1964 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001965 ret = btrfs_next_leaf(root, path);
1966 if (ret)
1967 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001968 leaf = path->nodes[0];
1969 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001970 slot = path->slots[0];
1971 } else {
1972 slot++;
1973 path->slots[0]++;
1974 }
1975 }
1976 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001977 item = btrfs_item_nr(leaf, slot);
1978 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1979
1980 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001981 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001982 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001983 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001984 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001985 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001986
1987 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001988 advance = 1;
1989 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1990 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001991 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001992 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001993 struct btrfs_key location;
1994
1995 name_len = btrfs_dir_name_len(leaf, di);
1996 if (name_len < 32) {
1997 name_ptr = tmp_name;
1998 } else {
1999 name_ptr = kmalloc(name_len, GFP_NOFS);
2000 BUG_ON(!name_ptr);
2001 }
2002 read_extent_buffer(leaf, name_ptr,
2003 (unsigned long)(di + 1), name_len);
2004
2005 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2006 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002007 over = filldir(dirent, name_ptr, name_len,
2008 found_key.offset,
2009 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002010 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002011
2012 if (name_ptr != tmp_name)
2013 kfree(name_ptr);
2014
Chris Mason39279cc2007-06-12 06:35:45 -04002015 if (over)
2016 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05002017 di_len = btrfs_dir_name_len(leaf, di) +
2018 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002019 di_cur += di_len;
2020 di = (struct btrfs_dir_item *)((char *)di + di_len);
2021 }
2022 }
Yan Zheng5e591a02008-02-19 11:41:02 -05002023 if (key_type == BTRFS_DIR_INDEX_KEY)
2024 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2025 else
2026 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002027nopos:
2028 ret = 0;
2029err:
Chris Mason39279cc2007-06-12 06:35:45 -04002030 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002031 return ret;
2032}
2033
2034int btrfs_write_inode(struct inode *inode, int wait)
2035{
2036 struct btrfs_root *root = BTRFS_I(inode)->root;
2037 struct btrfs_trans_handle *trans;
2038 int ret = 0;
2039
2040 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002041 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002042 btrfs_set_trans_block_group(trans, inode);
2043 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002044 }
2045 return ret;
2046}
2047
2048/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002049 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002050 * inode changes. But, it is most likely to find the inode in cache.
2051 * FIXME, needs more benchmarking...there are no reasons other than performance
2052 * to keep or drop this code.
2053 */
2054void btrfs_dirty_inode(struct inode *inode)
2055{
2056 struct btrfs_root *root = BTRFS_I(inode)->root;
2057 struct btrfs_trans_handle *trans;
2058
Chris Masonf9295742008-07-17 12:54:14 -04002059 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002060 btrfs_set_trans_block_group(trans, inode);
2061 btrfs_update_inode(trans, root, inode);
2062 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002063}
2064
Josef Bacikaec74772008-07-24 12:12:38 -04002065static int btrfs_set_inode_index_count(struct inode *inode)
2066{
2067 struct btrfs_root *root = BTRFS_I(inode)->root;
2068 struct btrfs_key key, found_key;
2069 struct btrfs_path *path;
2070 struct extent_buffer *leaf;
2071 int ret;
2072
2073 key.objectid = inode->i_ino;
2074 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2075 key.offset = (u64)-1;
2076
2077 path = btrfs_alloc_path();
2078 if (!path)
2079 return -ENOMEM;
2080
2081 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2082 if (ret < 0)
2083 goto out;
2084 /* FIXME: we should be able to handle this */
2085 if (ret == 0)
2086 goto out;
2087 ret = 0;
2088
2089 /*
2090 * MAGIC NUMBER EXPLANATION:
2091 * since we search a directory based on f_pos we have to start at 2
2092 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2093 * else has to start at 2
2094 */
2095 if (path->slots[0] == 0) {
2096 BTRFS_I(inode)->index_cnt = 2;
2097 goto out;
2098 }
2099
2100 path->slots[0]--;
2101
2102 leaf = path->nodes[0];
2103 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2104
2105 if (found_key.objectid != inode->i_ino ||
2106 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2107 BTRFS_I(inode)->index_cnt = 2;
2108 goto out;
2109 }
2110
2111 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2112out:
2113 btrfs_free_path(path);
2114 return ret;
2115}
2116
2117static int btrfs_set_inode_index(struct inode *dir, struct inode *inode)
2118{
2119 int ret = 0;
2120
2121 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2122 ret = btrfs_set_inode_index_count(dir);
2123 if (ret)
2124 return ret;
2125 }
2126
2127 BTRFS_I(inode)->index = BTRFS_I(dir)->index_cnt;
2128 BTRFS_I(dir)->index_cnt++;
2129
2130 return ret;
2131}
2132
Chris Mason39279cc2007-06-12 06:35:45 -04002133static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2134 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002135 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002136 const char *name, int name_len,
2137 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002138 u64 objectid,
2139 struct btrfs_block_group_cache *group,
2140 int mode)
2141{
2142 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002143 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002144 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002145 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002146 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002147 struct btrfs_inode_ref *ref;
2148 struct btrfs_key key[2];
2149 u32 sizes[2];
2150 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002151 int ret;
2152 int owner;
2153
Chris Mason5f39d392007-10-15 16:14:19 -04002154 path = btrfs_alloc_path();
2155 BUG_ON(!path);
2156
Chris Mason39279cc2007-06-12 06:35:45 -04002157 inode = new_inode(root->fs_info->sb);
2158 if (!inode)
2159 return ERR_PTR(-ENOMEM);
2160
Josef Bacikaec74772008-07-24 12:12:38 -04002161 if (dir) {
2162 ret = btrfs_set_inode_index(dir, inode);
2163 if (ret)
2164 return ERR_PTR(ret);
2165 } else {
2166 BTRFS_I(inode)->index = 0;
2167 }
2168 /*
2169 * index_cnt is ignored for everything but a dir,
2170 * btrfs_get_inode_index_count has an explanation for the magic
2171 * number
2172 */
2173 BTRFS_I(inode)->index_cnt = 2;
2174
Chris Masond1310b22008-01-24 16:13:08 -05002175 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2176 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002177 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002178 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2179 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002180 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04002181 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002182 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002183 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002184 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002185 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002186
Chris Mason39279cc2007-06-12 06:35:45 -04002187 if (mode & S_IFDIR)
2188 owner = 0;
2189 else
2190 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002191 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002192 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002193 if (!new_inode_group) {
2194 printk("find_block group failed\n");
2195 new_inode_group = group;
2196 }
2197 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002198 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002199
2200 key[0].objectid = objectid;
2201 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2202 key[0].offset = 0;
2203
2204 key[1].objectid = objectid;
2205 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2206 key[1].offset = ref_objectid;
2207
2208 sizes[0] = sizeof(struct btrfs_inode_item);
2209 sizes[1] = name_len + sizeof(*ref);
2210
2211 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2212 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002213 goto fail;
2214
Chris Mason9c583092008-01-29 15:15:18 -05002215 if (objectid > root->highest_inode)
2216 root->highest_inode = objectid;
2217
Chris Mason39279cc2007-06-12 06:35:45 -04002218 inode->i_uid = current->fsuid;
2219 inode->i_gid = current->fsgid;
2220 inode->i_mode = mode;
2221 inode->i_ino = objectid;
2222 inode->i_blocks = 0;
2223 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002224 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2225 struct btrfs_inode_item);
2226 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002227
2228 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2229 struct btrfs_inode_ref);
2230 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Josef Bacikaec74772008-07-24 12:12:38 -04002231 btrfs_set_inode_ref_index(path->nodes[0], ref, BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002232 ptr = (unsigned long)(ref + 1);
2233 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2234
Chris Mason5f39d392007-10-15 16:14:19 -04002235 btrfs_mark_buffer_dirty(path->nodes[0]);
2236 btrfs_free_path(path);
2237
Chris Mason39279cc2007-06-12 06:35:45 -04002238 location = &BTRFS_I(inode)->location;
2239 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002240 location->offset = 0;
2241 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2242
Chris Mason39279cc2007-06-12 06:35:45 -04002243 insert_inode_hash(inode);
2244 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002245fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002246 if (dir)
2247 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002248 btrfs_free_path(path);
2249 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002250}
2251
2252static inline u8 btrfs_inode_type(struct inode *inode)
2253{
2254 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2255}
2256
2257static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002258 struct dentry *dentry, struct inode *inode,
2259 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002260{
2261 int ret;
2262 struct btrfs_key key;
2263 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002264 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002265
Chris Mason39279cc2007-06-12 06:35:45 -04002266 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002267 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2268 key.offset = 0;
2269
2270 ret = btrfs_insert_dir_item(trans, root,
2271 dentry->d_name.name, dentry->d_name.len,
2272 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002273 &key, btrfs_inode_type(inode),
2274 BTRFS_I(inode)->index);
Chris Mason39279cc2007-06-12 06:35:45 -04002275 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002276 if (add_backref) {
2277 ret = btrfs_insert_inode_ref(trans, root,
2278 dentry->d_name.name,
2279 dentry->d_name.len,
2280 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002281 parent_inode->i_ino,
2282 BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002283 }
Chris Masondbe674a2008-07-17 12:54:05 -04002284 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2285 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002286 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002287 ret = btrfs_update_inode(trans, root,
2288 dentry->d_parent->d_inode);
2289 }
2290 return ret;
2291}
2292
2293static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002294 struct dentry *dentry, struct inode *inode,
2295 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002296{
Chris Mason9c583092008-01-29 15:15:18 -05002297 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04002298 if (!err) {
2299 d_instantiate(dentry, inode);
2300 return 0;
2301 }
2302 if (err > 0)
2303 err = -EEXIST;
2304 return err;
2305}
2306
Josef Bacik618e21d2007-07-11 10:18:17 -04002307static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2308 int mode, dev_t rdev)
2309{
2310 struct btrfs_trans_handle *trans;
2311 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002312 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002313 int err;
2314 int drop_inode = 0;
2315 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002316 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002317
2318 if (!new_valid_dev(rdev))
2319 return -EINVAL;
2320
Chris Mason1832a6d2007-12-21 16:27:21 -05002321 err = btrfs_check_free_space(root, 1, 0);
2322 if (err)
2323 goto fail;
2324
Josef Bacik618e21d2007-07-11 10:18:17 -04002325 trans = btrfs_start_transaction(root, 1);
2326 btrfs_set_trans_block_group(trans, dir);
2327
2328 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2329 if (err) {
2330 err = -ENOSPC;
2331 goto out_unlock;
2332 }
2333
Josef Bacikaec74772008-07-24 12:12:38 -04002334 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002335 dentry->d_name.len,
2336 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002337 BTRFS_I(dir)->block_group, mode);
2338 err = PTR_ERR(inode);
2339 if (IS_ERR(inode))
2340 goto out_unlock;
2341
Josef Bacik33268ea2008-07-24 12:16:36 -04002342 err = btrfs_init_acl(inode, dir);
2343 if (err) {
2344 drop_inode = 1;
2345 goto out_unlock;
2346 }
2347
Josef Bacik618e21d2007-07-11 10:18:17 -04002348 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002349 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002350 if (err)
2351 drop_inode = 1;
2352 else {
2353 inode->i_op = &btrfs_special_inode_operations;
2354 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002355 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002356 }
2357 dir->i_sb->s_dirt = 1;
2358 btrfs_update_inode_block_group(trans, inode);
2359 btrfs_update_inode_block_group(trans, dir);
2360out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002361 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002362 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002363fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002364 if (drop_inode) {
2365 inode_dec_link_count(inode);
2366 iput(inode);
2367 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002368 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002369 return err;
2370}
2371
Chris Mason39279cc2007-06-12 06:35:45 -04002372static int btrfs_create(struct inode *dir, struct dentry *dentry,
2373 int mode, struct nameidata *nd)
2374{
2375 struct btrfs_trans_handle *trans;
2376 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002377 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002378 int err;
2379 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002380 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002381 u64 objectid;
2382
Chris Mason1832a6d2007-12-21 16:27:21 -05002383 err = btrfs_check_free_space(root, 1, 0);
2384 if (err)
2385 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002386 trans = btrfs_start_transaction(root, 1);
2387 btrfs_set_trans_block_group(trans, dir);
2388
2389 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2390 if (err) {
2391 err = -ENOSPC;
2392 goto out_unlock;
2393 }
2394
Josef Bacikaec74772008-07-24 12:12:38 -04002395 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002396 dentry->d_name.len,
2397 dentry->d_parent->d_inode->i_ino,
2398 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002399 err = PTR_ERR(inode);
2400 if (IS_ERR(inode))
2401 goto out_unlock;
2402
Josef Bacik33268ea2008-07-24 12:16:36 -04002403 err = btrfs_init_acl(inode, dir);
2404 if (err) {
2405 drop_inode = 1;
2406 goto out_unlock;
2407 }
2408
Chris Mason39279cc2007-06-12 06:35:45 -04002409 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002410 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002411 if (err)
2412 drop_inode = 1;
2413 else {
2414 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002415 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002416 inode->i_fop = &btrfs_file_operations;
2417 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002418 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2419 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002420 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002421 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2422 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002423 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002424 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002425 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002426 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002427 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002428 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002429 }
2430 dir->i_sb->s_dirt = 1;
2431 btrfs_update_inode_block_group(trans, inode);
2432 btrfs_update_inode_block_group(trans, dir);
2433out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002434 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002435 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002436fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002437 if (drop_inode) {
2438 inode_dec_link_count(inode);
2439 iput(inode);
2440 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002441 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002442 return err;
2443}
2444
2445static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2446 struct dentry *dentry)
2447{
2448 struct btrfs_trans_handle *trans;
2449 struct btrfs_root *root = BTRFS_I(dir)->root;
2450 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002451 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002452 int err;
2453 int drop_inode = 0;
2454
2455 if (inode->i_nlink == 0)
2456 return -ENOENT;
2457
Chris Mason6da6aba2007-12-18 16:15:09 -05002458#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2459 inode->i_nlink++;
2460#else
Chris Mason39279cc2007-06-12 06:35:45 -04002461 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002462#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002463 err = btrfs_check_free_space(root, 1, 0);
2464 if (err)
2465 goto fail;
Josef Bacikaec74772008-07-24 12:12:38 -04002466 err = btrfs_set_inode_index(dir, inode);
2467 if (err)
2468 goto fail;
2469
Chris Mason39279cc2007-06-12 06:35:45 -04002470 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002471
Chris Mason39279cc2007-06-12 06:35:45 -04002472 btrfs_set_trans_block_group(trans, dir);
2473 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002474
Chris Mason9c583092008-01-29 15:15:18 -05002475 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002476
Chris Mason39279cc2007-06-12 06:35:45 -04002477 if (err)
2478 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002479
Chris Mason39279cc2007-06-12 06:35:45 -04002480 dir->i_sb->s_dirt = 1;
2481 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002482 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002483
Chris Mason54aa1f42007-06-22 14:16:25 -04002484 if (err)
2485 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002486
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002487 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002488 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002489fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002490 if (drop_inode) {
2491 inode_dec_link_count(inode);
2492 iput(inode);
2493 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002494 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002495 return err;
2496}
2497
Chris Mason39279cc2007-06-12 06:35:45 -04002498static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2499{
Chris Masonb9d86662008-05-02 16:13:49 -04002500 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002501 struct btrfs_trans_handle *trans;
2502 struct btrfs_root *root = BTRFS_I(dir)->root;
2503 int err = 0;
2504 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002505 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002506 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002507
Chris Mason1832a6d2007-12-21 16:27:21 -05002508 err = btrfs_check_free_space(root, 1, 0);
2509 if (err)
2510 goto out_unlock;
2511
Chris Mason39279cc2007-06-12 06:35:45 -04002512 trans = btrfs_start_transaction(root, 1);
2513 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002514
Chris Mason39279cc2007-06-12 06:35:45 -04002515 if (IS_ERR(trans)) {
2516 err = PTR_ERR(trans);
2517 goto out_unlock;
2518 }
2519
2520 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2521 if (err) {
2522 err = -ENOSPC;
2523 goto out_unlock;
2524 }
2525
Josef Bacikaec74772008-07-24 12:12:38 -04002526 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002527 dentry->d_name.len,
2528 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002529 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2530 if (IS_ERR(inode)) {
2531 err = PTR_ERR(inode);
2532 goto out_fail;
2533 }
Chris Mason5f39d392007-10-15 16:14:19 -04002534
Chris Mason39279cc2007-06-12 06:35:45 -04002535 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002536
2537 err = btrfs_init_acl(inode, dir);
2538 if (err)
2539 goto out_fail;
2540
Chris Mason39279cc2007-06-12 06:35:45 -04002541 inode->i_op = &btrfs_dir_inode_operations;
2542 inode->i_fop = &btrfs_dir_file_operations;
2543 btrfs_set_trans_block_group(trans, inode);
2544
Chris Masondbe674a2008-07-17 12:54:05 -04002545 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002546 err = btrfs_update_inode(trans, root, inode);
2547 if (err)
2548 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002549
Chris Mason9c583092008-01-29 15:15:18 -05002550 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002551 if (err)
2552 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002553
Chris Mason39279cc2007-06-12 06:35:45 -04002554 d_instantiate(dentry, inode);
2555 drop_on_err = 0;
2556 dir->i_sb->s_dirt = 1;
2557 btrfs_update_inode_block_group(trans, inode);
2558 btrfs_update_inode_block_group(trans, dir);
2559
2560out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002561 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002562 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002563
Chris Mason39279cc2007-06-12 06:35:45 -04002564out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002565 if (drop_on_err)
2566 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002567 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002568 return err;
2569}
2570
Chris Mason3b951512008-04-17 11:29:12 -04002571static int merge_extent_mapping(struct extent_map_tree *em_tree,
2572 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002573 struct extent_map *em,
2574 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002575{
2576 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002577
Chris Masone6dcd2d2008-07-17 12:53:50 -04002578 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2579 start_diff = map_start - em->start;
2580 em->start = map_start;
2581 em->len = map_len;
2582 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2583 em->block_start += start_diff;
2584 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002585}
2586
Chris Masona52d9a82007-08-27 16:49:44 -04002587struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002588 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002589 int create)
2590{
2591 int ret;
2592 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002593 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002594 u64 extent_start = 0;
2595 u64 extent_end = 0;
2596 u64 objectid = inode->i_ino;
2597 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002598 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002599 struct btrfs_root *root = BTRFS_I(inode)->root;
2600 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002601 struct extent_buffer *leaf;
2602 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002603 struct extent_map *em = NULL;
2604 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002605 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002606 struct btrfs_trans_handle *trans = NULL;
2607
Chris Masona52d9a82007-08-27 16:49:44 -04002608again:
Chris Masond1310b22008-01-24 16:13:08 -05002609 spin_lock(&em_tree->lock);
2610 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002611 if (em)
2612 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002613 spin_unlock(&em_tree->lock);
2614
Chris Masona52d9a82007-08-27 16:49:44 -04002615 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002616 if (em->start > start || em->start + em->len <= start)
2617 free_extent_map(em);
2618 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002619 free_extent_map(em);
2620 else
2621 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002622 }
Chris Masond1310b22008-01-24 16:13:08 -05002623 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002624 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002625 err = -ENOMEM;
2626 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002627 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002628 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002629 em->start = EXTENT_MAP_HOLE;
2630 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002631
2632 if (!path) {
2633 path = btrfs_alloc_path();
2634 BUG_ON(!path);
2635 }
2636
Chris Mason179e29e2007-11-01 11:28:41 -04002637 ret = btrfs_lookup_file_extent(trans, root, path,
2638 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002639 if (ret < 0) {
2640 err = ret;
2641 goto out;
2642 }
2643
2644 if (ret != 0) {
2645 if (path->slots[0] == 0)
2646 goto not_found;
2647 path->slots[0]--;
2648 }
2649
Chris Mason5f39d392007-10-15 16:14:19 -04002650 leaf = path->nodes[0];
2651 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002652 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002653 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002654 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2655 found_type = btrfs_key_type(&found_key);
2656 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002657 found_type != BTRFS_EXTENT_DATA_KEY) {
2658 goto not_found;
2659 }
2660
Chris Mason5f39d392007-10-15 16:14:19 -04002661 found_type = btrfs_file_extent_type(leaf, item);
2662 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002663 if (found_type == BTRFS_FILE_EXTENT_REG) {
2664 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002665 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002666 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002667 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002668 em->start = start;
2669 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002670 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002671 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002672 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002673 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002674 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002675 }
2676 goto not_found_em;
2677 }
Chris Masondb945352007-10-15 16:15:53 -04002678 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2679 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002680 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002681 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002682 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002683 goto insert;
2684 }
Chris Masondb945352007-10-15 16:15:53 -04002685 bytenr += btrfs_file_extent_offset(leaf, item);
2686 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002687 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002688 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002689 goto insert;
2690 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002691 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002692 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002693 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002694 size_t size;
2695 size_t extent_offset;
2696 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002697
Chris Mason5f39d392007-10-15 16:14:19 -04002698 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2699 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002700 extent_end = (extent_start + size + root->sectorsize - 1) &
2701 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002702 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002703 em->start = start;
2704 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002705 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002706 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002707 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002708 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002709 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002710 }
2711 goto not_found_em;
2712 }
2713 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002714
2715 if (!page) {
2716 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002717 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002718 goto out;
2719 }
2720
Chris Mason70dec802008-01-29 09:59:12 -05002721 page_start = page_offset(page) + pg_offset;
2722 extent_offset = page_start - extent_start;
2723 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002724 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002725 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002726 em->len = (copy_size + root->sectorsize - 1) &
2727 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002728 map = kmap(page);
2729 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002730 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002731 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002732 copy_size);
2733 flush_dcache_page(page);
2734 } else if (create && PageUptodate(page)) {
2735 if (!trans) {
2736 kunmap(page);
2737 free_extent_map(em);
2738 em = NULL;
2739 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002740 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002741 goto again;
2742 }
Chris Mason70dec802008-01-29 09:59:12 -05002743 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002744 copy_size);
2745 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002746 }
Chris Masona52d9a82007-08-27 16:49:44 -04002747 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002748 set_extent_uptodate(io_tree, em->start,
2749 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002750 goto insert;
2751 } else {
2752 printk("unkknown found_type %d\n", found_type);
2753 WARN_ON(1);
2754 }
2755not_found:
2756 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002757 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002758not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002759 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002760insert:
2761 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002762 if (em->start > start || extent_map_end(em) <= start) {
2763 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002764 err = -EIO;
2765 goto out;
2766 }
Chris Masond1310b22008-01-24 16:13:08 -05002767
2768 err = 0;
2769 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002770 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002771 /* it is possible that someone inserted the extent into the tree
2772 * while we had the lock dropped. It is also possible that
2773 * an overlapping map exists in the tree
2774 */
Chris Masona52d9a82007-08-27 16:49:44 -04002775 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002776 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002777
2778 ret = 0;
2779
Chris Mason3b951512008-04-17 11:29:12 -04002780 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002781 if (existing && (existing->start > start ||
2782 existing->start + existing->len <= start)) {
2783 free_extent_map(existing);
2784 existing = NULL;
2785 }
Chris Mason3b951512008-04-17 11:29:12 -04002786 if (!existing) {
2787 existing = lookup_extent_mapping(em_tree, em->start,
2788 em->len);
2789 if (existing) {
2790 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002791 em, start,
2792 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002793 free_extent_map(existing);
2794 if (err) {
2795 free_extent_map(em);
2796 em = NULL;
2797 }
2798 } else {
2799 err = -EIO;
2800 printk("failing to insert %Lu %Lu\n",
2801 start, len);
2802 free_extent_map(em);
2803 em = NULL;
2804 }
2805 } else {
2806 free_extent_map(em);
2807 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002808 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002809 }
Chris Masona52d9a82007-08-27 16:49:44 -04002810 }
Chris Masond1310b22008-01-24 16:13:08 -05002811 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002812out:
Chris Masonf4219502008-07-22 11:18:09 -04002813 if (path)
2814 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002815 if (trans) {
2816 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002817 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002818 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002819 }
Chris Masona52d9a82007-08-27 16:49:44 -04002820 }
Chris Masona52d9a82007-08-27 16:49:44 -04002821 if (err) {
2822 free_extent_map(em);
2823 WARN_ON(1);
2824 return ERR_PTR(err);
2825 }
2826 return em;
2827}
2828
Chris Masone1c4b742008-04-22 13:26:46 -04002829#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002830static int btrfs_get_block(struct inode *inode, sector_t iblock,
2831 struct buffer_head *bh_result, int create)
2832{
2833 struct extent_map *em;
2834 u64 start = (u64)iblock << inode->i_blkbits;
2835 struct btrfs_multi_bio *multi = NULL;
2836 struct btrfs_root *root = BTRFS_I(inode)->root;
2837 u64 len;
2838 u64 logical;
2839 u64 map_length;
2840 int ret = 0;
2841
2842 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2843
2844 if (!em || IS_ERR(em))
2845 goto out;
2846
Chris Masone1c4b742008-04-22 13:26:46 -04002847 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002848 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002849 }
Chris Mason16432982008-04-10 10:23:21 -04002850
2851 if (em->block_start == EXTENT_MAP_INLINE) {
2852 ret = -EINVAL;
2853 goto out;
2854 }
2855
Chris Mason16432982008-04-10 10:23:21 -04002856 len = em->start + em->len - start;
2857 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2858
Chris Masone1c4b742008-04-22 13:26:46 -04002859 if (em->block_start == EXTENT_MAP_HOLE ||
2860 em->block_start == EXTENT_MAP_DELALLOC) {
2861 bh_result->b_size = len;
2862 goto out;
2863 }
2864
Chris Mason16432982008-04-10 10:23:21 -04002865 logical = start - em->start;
2866 logical = em->block_start + logical;
2867
2868 map_length = len;
2869 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2870 logical, &map_length, &multi, 0);
2871 BUG_ON(ret);
2872 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2873 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002874
Chris Mason16432982008-04-10 10:23:21 -04002875 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2876 set_buffer_mapped(bh_result);
2877 kfree(multi);
2878out:
2879 free_extent_map(em);
2880 return ret;
2881}
Chris Masone1c4b742008-04-22 13:26:46 -04002882#endif
Chris Mason16432982008-04-10 10:23:21 -04002883
2884static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2885 const struct iovec *iov, loff_t offset,
2886 unsigned long nr_segs)
2887{
Chris Masone1c4b742008-04-22 13:26:46 -04002888 return -EINVAL;
2889#if 0
Chris Mason16432982008-04-10 10:23:21 -04002890 struct file *file = iocb->ki_filp;
2891 struct inode *inode = file->f_mapping->host;
2892
2893 if (rw == WRITE)
2894 return -EINVAL;
2895
2896 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2897 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002898#endif
Chris Mason16432982008-04-10 10:23:21 -04002899}
2900
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002901static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002902{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002903 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002904}
2905
Chris Mason9ebefb182007-06-15 13:50:00 -04002906int btrfs_readpage(struct file *file, struct page *page)
2907{
Chris Masond1310b22008-01-24 16:13:08 -05002908 struct extent_io_tree *tree;
2909 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002910 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002911}
Chris Mason1832a6d2007-12-21 16:27:21 -05002912
Chris Mason39279cc2007-06-12 06:35:45 -04002913static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2914{
Chris Masond1310b22008-01-24 16:13:08 -05002915 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002916
2917
2918 if (current->flags & PF_MEMALLOC) {
2919 redirty_page_for_writepage(wbc, page);
2920 unlock_page(page);
2921 return 0;
2922 }
Chris Masond1310b22008-01-24 16:13:08 -05002923 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002924 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2925}
Chris Mason39279cc2007-06-12 06:35:45 -04002926
Chris Masonf4219502008-07-22 11:18:09 -04002927int btrfs_writepages(struct address_space *mapping,
2928 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04002929{
Chris Masond1310b22008-01-24 16:13:08 -05002930 struct extent_io_tree *tree;
2931 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002932 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2933}
2934
Chris Mason3ab2fb52007-11-08 10:59:22 -05002935static int
2936btrfs_readpages(struct file *file, struct address_space *mapping,
2937 struct list_head *pages, unsigned nr_pages)
2938{
Chris Masond1310b22008-01-24 16:13:08 -05002939 struct extent_io_tree *tree;
2940 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002941 return extent_readpages(tree, mapping, pages, nr_pages,
2942 btrfs_get_extent);
2943}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002944static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002945{
Chris Masond1310b22008-01-24 16:13:08 -05002946 struct extent_io_tree *tree;
2947 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002948 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002949
Chris Masond1310b22008-01-24 16:13:08 -05002950 tree = &BTRFS_I(page->mapping->host)->io_tree;
2951 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002952 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002953 if (ret == 1) {
2954 ClearPagePrivate(page);
2955 set_page_private(page, 0);
2956 page_cache_release(page);
2957 }
2958 return ret;
2959}
Chris Mason39279cc2007-06-12 06:35:45 -04002960
Chris Masone6dcd2d2008-07-17 12:53:50 -04002961static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2962{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002963 return __btrfs_releasepage(page, gfp_flags);
2964}
2965
Chris Masona52d9a82007-08-27 16:49:44 -04002966static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2967{
Chris Masond1310b22008-01-24 16:13:08 -05002968 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002969 struct btrfs_ordered_extent *ordered;
2970 u64 page_start = page_offset(page);
2971 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002972
Chris Masone6dcd2d2008-07-17 12:53:50 -04002973 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002974 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002975 if (offset) {
2976 btrfs_releasepage(page, GFP_NOFS);
2977 return;
2978 }
2979
2980 lock_extent(tree, page_start, page_end, GFP_NOFS);
2981 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2982 page_offset(page));
2983 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04002984 /*
2985 * IO on this page will never be started, so we need
2986 * to account for any ordered extents now
2987 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002988 clear_extent_bit(tree, page_start, page_end,
2989 EXTENT_DIRTY | EXTENT_DELALLOC |
2990 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04002991 btrfs_finish_ordered_io(page->mapping->host,
2992 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002993 btrfs_put_ordered_extent(ordered);
2994 lock_extent(tree, page_start, page_end, GFP_NOFS);
2995 }
2996 clear_extent_bit(tree, page_start, page_end,
2997 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2998 EXTENT_ORDERED,
2999 1, 1, GFP_NOFS);
3000 __btrfs_releasepage(page, GFP_NOFS);
3001
Chris Mason4a096752008-07-21 10:29:44 -04003002 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003003 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003004 ClearPagePrivate(page);
3005 set_page_private(page, 0);
3006 page_cache_release(page);
3007 }
Chris Mason39279cc2007-06-12 06:35:45 -04003008}
3009
Chris Mason9ebefb182007-06-15 13:50:00 -04003010/*
3011 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3012 * called from a page fault handler when a page is first dirtied. Hence we must
3013 * be careful to check for EOF conditions here. We set the page up correctly
3014 * for a written page which means we get ENOSPC checking when writing into
3015 * holes and correct delalloc and unwritten extent mapping on filesystems that
3016 * support these features.
3017 *
3018 * We are not allowed to take the i_mutex here so we have to play games to
3019 * protect against truncate races as the page could now be beyond EOF. Because
3020 * vmtruncate() writes the inode size before removing pages, once we have the
3021 * page lock we can determine safely if the page is beyond EOF. If it is not
3022 * beyond EOF, then the page is guaranteed safe against truncation until we
3023 * unlock the page.
3024 */
3025int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3026{
Chris Mason6da6aba2007-12-18 16:15:09 -05003027 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003028 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003029 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3030 struct btrfs_ordered_extent *ordered;
3031 char *kaddr;
3032 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003033 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003034 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003035 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003036 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003037
Chris Mason1832a6d2007-12-21 16:27:21 -05003038 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003039 if (ret)
3040 goto out;
3041
3042 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003043again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003044 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003045 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003046 page_start = page_offset(page);
3047 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003048
Chris Mason9ebefb182007-06-15 13:50:00 -04003049 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003050 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003051 /* page got truncated out from underneath us */
3052 goto out_unlock;
3053 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003054 wait_on_page_writeback(page);
3055
3056 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3057 set_page_extent_mapped(page);
3058
Chris Masoneb84ae02008-07-17 13:53:27 -04003059 /*
3060 * we can't set the delalloc bits if there are pending ordered
3061 * extents. Drop our locks and wait for them to finish
3062 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003063 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3064 if (ordered) {
3065 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3066 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003067 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003068 btrfs_put_ordered_extent(ordered);
3069 goto again;
3070 }
3071
3072 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
3073 page_end, GFP_NOFS);
3074 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003075
3076 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003077 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003078 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003079 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003080 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003081
Chris Masone6dcd2d2008-07-17 12:53:50 -04003082 if (zero_start != PAGE_CACHE_SIZE) {
3083 kaddr = kmap(page);
3084 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3085 flush_dcache_page(page);
3086 kunmap(page);
3087 }
Chris Mason247e7432008-07-17 12:53:51 -04003088 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003089 set_page_dirty(page);
3090 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003091
3092out_unlock:
3093 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003094out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003095 return ret;
3096}
3097
Chris Mason39279cc2007-06-12 06:35:45 -04003098static void btrfs_truncate(struct inode *inode)
3099{
3100 struct btrfs_root *root = BTRFS_I(inode)->root;
3101 int ret;
3102 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003103 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003104 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003105
3106 if (!S_ISREG(inode->i_mode))
3107 return;
3108 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3109 return;
3110
3111 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003112 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003113
Chris Mason39279cc2007-06-12 06:35:45 -04003114 trans = btrfs_start_transaction(root, 1);
3115 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003116 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003117
Josef Bacik7b128762008-07-24 12:17:14 -04003118 ret = btrfs_orphan_add(trans, inode);
3119 if (ret)
3120 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003121 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05003122 ret = btrfs_truncate_in_trans(trans, root, inode,
3123 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003124 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003125
Josef Bacik7b128762008-07-24 12:17:14 -04003126 ret = btrfs_orphan_del(trans, inode);
3127 BUG_ON(ret);
3128
3129out:
3130 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003131 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003132 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003133 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003134}
3135
Sven Wegener3b963622008-06-09 21:57:42 -04003136/*
3137 * Invalidate a single dcache entry at the root of the filesystem.
3138 * Needed after creation of snapshot or subvolume.
3139 */
3140void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3141 int namelen)
3142{
3143 struct dentry *alias, *entry;
3144 struct qstr qstr;
3145
3146 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3147 if (alias) {
3148 qstr.name = name;
3149 qstr.len = namelen;
3150 /* change me if btrfs ever gets a d_hash operation */
3151 qstr.hash = full_name_hash(qstr.name, qstr.len);
3152 entry = d_lookup(alias, &qstr);
3153 dput(alias);
3154 if (entry) {
3155 d_invalidate(entry);
3156 dput(entry);
3157 }
3158 }
3159}
3160
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003161int btrfs_create_subvol_root(struct btrfs_root *new_root,
3162 struct btrfs_trans_handle *trans, u64 new_dirid,
3163 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003164{
Chris Mason39279cc2007-06-12 06:35:45 -04003165 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003166
Josef Bacikaec74772008-07-24 12:12:38 -04003167 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003168 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04003169 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003170 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003171 inode->i_op = &btrfs_dir_inode_operations;
3172 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003173 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003174
Chris Mason39279cc2007-06-12 06:35:45 -04003175 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003176 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003177
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003178 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003179}
3180
Chris Masonedbd8d42007-12-21 16:27:24 -05003181unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003182 struct file_ra_state *ra, struct file *file,
3183 pgoff_t offset, pgoff_t last_index)
3184{
Chris Mason8e7bf942008-04-28 09:02:36 -04003185 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003186
3187#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003188 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3189 return offset;
3190#else
Chris Mason86479a02007-09-10 19:58:16 -04003191 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3192 return offset + req_size;
3193#endif
3194}
3195
Chris Mason39279cc2007-06-12 06:35:45 -04003196struct inode *btrfs_alloc_inode(struct super_block *sb)
3197{
3198 struct btrfs_inode *ei;
3199
3200 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3201 if (!ei)
3202 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003203 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003204 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003205 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3206 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003207 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003208 return &ei->vfs_inode;
3209}
3210
3211void btrfs_destroy_inode(struct inode *inode)
3212{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003213 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003214 WARN_ON(!list_empty(&inode->i_dentry));
3215 WARN_ON(inode->i_data.nrpages);
3216
Josef Bacik33268ea2008-07-24 12:16:36 -04003217 if (BTRFS_I(inode)->i_acl &&
3218 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3219 posix_acl_release(BTRFS_I(inode)->i_acl);
3220 if (BTRFS_I(inode)->i_default_acl &&
3221 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3222 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3223
Yanbcc63ab2008-07-30 16:29:20 -04003224 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003225 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3226 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3227 " list\n", inode->i_ino);
3228 dump_stack();
3229 }
Yanbcc63ab2008-07-30 16:29:20 -04003230 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003231
Chris Masone6dcd2d2008-07-17 12:53:50 -04003232 while(1) {
3233 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3234 if (!ordered)
3235 break;
3236 else {
3237 printk("found ordered extent %Lu %Lu\n",
3238 ordered->file_offset, ordered->len);
3239 btrfs_remove_ordered_extent(inode, ordered);
3240 btrfs_put_ordered_extent(ordered);
3241 btrfs_put_ordered_extent(ordered);
3242 }
3243 }
Chris Mason8c416c92008-01-14 15:10:26 -05003244 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003245 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3246}
3247
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003248#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3249static void init_once(void *foo)
3250#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003251static void init_once(struct kmem_cache * cachep, void *foo)
3252#else
Chris Mason39279cc2007-06-12 06:35:45 -04003253static void init_once(void * foo, struct kmem_cache * cachep,
3254 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003255#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003256{
3257 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3258
3259 inode_init_once(&ei->vfs_inode);
3260}
3261
3262void btrfs_destroy_cachep(void)
3263{
3264 if (btrfs_inode_cachep)
3265 kmem_cache_destroy(btrfs_inode_cachep);
3266 if (btrfs_trans_handle_cachep)
3267 kmem_cache_destroy(btrfs_trans_handle_cachep);
3268 if (btrfs_transaction_cachep)
3269 kmem_cache_destroy(btrfs_transaction_cachep);
3270 if (btrfs_bit_radix_cachep)
3271 kmem_cache_destroy(btrfs_bit_radix_cachep);
3272 if (btrfs_path_cachep)
3273 kmem_cache_destroy(btrfs_path_cachep);
3274}
3275
Chris Mason86479a02007-09-10 19:58:16 -04003276struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003277 unsigned long extra_flags,
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003278#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3279 void (*ctor)(void *)
3280#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003281 void (*ctor)(struct kmem_cache *, void *)
3282#else
Chris Mason92fee662007-07-25 12:31:35 -04003283 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003284 unsigned long)
3285#endif
3286 )
Chris Mason92fee662007-07-25 12:31:35 -04003287{
3288 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3289 SLAB_MEM_SPREAD | extra_flags), ctor
3290#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3291 ,NULL
3292#endif
3293 );
3294}
3295
Chris Mason39279cc2007-06-12 06:35:45 -04003296int btrfs_init_cachep(void)
3297{
Chris Mason86479a02007-09-10 19:58:16 -04003298 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003299 sizeof(struct btrfs_inode),
3300 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003301 if (!btrfs_inode_cachep)
3302 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003303 btrfs_trans_handle_cachep =
3304 btrfs_cache_create("btrfs_trans_handle_cache",
3305 sizeof(struct btrfs_trans_handle),
3306 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003307 if (!btrfs_trans_handle_cachep)
3308 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003309 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003310 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003311 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003312 if (!btrfs_transaction_cachep)
3313 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003314 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003315 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003316 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003317 if (!btrfs_path_cachep)
3318 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003319 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003320 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003321 if (!btrfs_bit_radix_cachep)
3322 goto fail;
3323 return 0;
3324fail:
3325 btrfs_destroy_cachep();
3326 return -ENOMEM;
3327}
3328
3329static int btrfs_getattr(struct vfsmount *mnt,
3330 struct dentry *dentry, struct kstat *stat)
3331{
3332 struct inode *inode = dentry->d_inode;
3333 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003334 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003335 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003336 return 0;
3337}
3338
3339static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3340 struct inode * new_dir,struct dentry *new_dentry)
3341{
3342 struct btrfs_trans_handle *trans;
3343 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3344 struct inode *new_inode = new_dentry->d_inode;
3345 struct inode *old_inode = old_dentry->d_inode;
3346 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04003347 int ret;
3348
3349 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3350 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3351 return -ENOTEMPTY;
3352 }
Chris Mason5f39d392007-10-15 16:14:19 -04003353
Chris Mason1832a6d2007-12-21 16:27:21 -05003354 ret = btrfs_check_free_space(root, 1, 0);
3355 if (ret)
3356 goto out_unlock;
3357
Chris Mason39279cc2007-06-12 06:35:45 -04003358 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003359
Chris Mason39279cc2007-06-12 06:35:45 -04003360 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003361
3362 old_dentry->d_inode->i_nlink++;
3363 old_dir->i_ctime = old_dir->i_mtime = ctime;
3364 new_dir->i_ctime = new_dir->i_mtime = ctime;
3365 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003366
Chris Mason39279cc2007-06-12 06:35:45 -04003367 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3368 if (ret)
3369 goto out_fail;
3370
3371 if (new_inode) {
3372 new_inode->i_ctime = CURRENT_TIME;
3373 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3374 if (ret)
3375 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003376 if (new_inode->i_nlink == 0) {
3377 ret = btrfs_orphan_add(trans, new_inode);
3378 if (ret)
3379 goto out_fail;
3380 }
Chris Mason39279cc2007-06-12 06:35:45 -04003381 }
Josef Bacikaec74772008-07-24 12:12:38 -04003382 ret = btrfs_set_inode_index(new_dir, old_inode);
3383 if (ret)
3384 goto out_fail;
3385
Chris Mason9c583092008-01-29 15:15:18 -05003386 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003387 if (ret)
3388 goto out_fail;
3389
3390out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003391 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003392out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003393 return ret;
3394}
3395
3396static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3397 const char *symname)
3398{
3399 struct btrfs_trans_handle *trans;
3400 struct btrfs_root *root = BTRFS_I(dir)->root;
3401 struct btrfs_path *path;
3402 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003403 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003404 int err;
3405 int drop_inode = 0;
3406 u64 objectid;
3407 int name_len;
3408 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003409 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003410 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003411 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003412 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003413
3414 name_len = strlen(symname) + 1;
3415 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3416 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003417
Chris Mason1832a6d2007-12-21 16:27:21 -05003418 err = btrfs_check_free_space(root, 1, 0);
3419 if (err)
3420 goto out_fail;
3421
Chris Mason39279cc2007-06-12 06:35:45 -04003422 trans = btrfs_start_transaction(root, 1);
3423 btrfs_set_trans_block_group(trans, dir);
3424
3425 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3426 if (err) {
3427 err = -ENOSPC;
3428 goto out_unlock;
3429 }
3430
Josef Bacikaec74772008-07-24 12:12:38 -04003431 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003432 dentry->d_name.len,
3433 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003434 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3435 err = PTR_ERR(inode);
3436 if (IS_ERR(inode))
3437 goto out_unlock;
3438
Josef Bacik33268ea2008-07-24 12:16:36 -04003439 err = btrfs_init_acl(inode, dir);
3440 if (err) {
3441 drop_inode = 1;
3442 goto out_unlock;
3443 }
3444
Chris Mason39279cc2007-06-12 06:35:45 -04003445 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003446 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003447 if (err)
3448 drop_inode = 1;
3449 else {
3450 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003451 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003452 inode->i_fop = &btrfs_file_operations;
3453 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003454 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3455 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003456 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003457 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3458 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04003459 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003460 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003461 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003462 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003463 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003464 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003465 }
3466 dir->i_sb->s_dirt = 1;
3467 btrfs_update_inode_block_group(trans, inode);
3468 btrfs_update_inode_block_group(trans, dir);
3469 if (drop_inode)
3470 goto out_unlock;
3471
3472 path = btrfs_alloc_path();
3473 BUG_ON(!path);
3474 key.objectid = inode->i_ino;
3475 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003476 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3477 datasize = btrfs_file_extent_calc_inline_size(name_len);
3478 err = btrfs_insert_empty_item(trans, root, path, &key,
3479 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003480 if (err) {
3481 drop_inode = 1;
3482 goto out_unlock;
3483 }
Chris Mason5f39d392007-10-15 16:14:19 -04003484 leaf = path->nodes[0];
3485 ei = btrfs_item_ptr(leaf, path->slots[0],
3486 struct btrfs_file_extent_item);
3487 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3488 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003489 BTRFS_FILE_EXTENT_INLINE);
3490 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003491 write_extent_buffer(leaf, symname, ptr, name_len);
3492 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003493 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003494
Chris Mason39279cc2007-06-12 06:35:45 -04003495 inode->i_op = &btrfs_symlink_inode_operations;
3496 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003497 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003498 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003499 err = btrfs_update_inode(trans, root, inode);
3500 if (err)
3501 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003502
3503out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003504 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003505 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003506out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003507 if (drop_inode) {
3508 inode_dec_link_count(inode);
3509 iput(inode);
3510 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003511 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003512 return err;
3513}
Chris Mason16432982008-04-10 10:23:21 -04003514
Chris Masone6dcd2d2008-07-17 12:53:50 -04003515static int btrfs_set_page_dirty(struct page *page)
3516{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003517 return __set_page_dirty_nobuffers(page);
3518}
3519
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003520#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3521static int btrfs_permission(struct inode *inode, int mask)
3522#else
Yanfdebe2b2008-01-14 13:26:08 -05003523static int btrfs_permission(struct inode *inode, int mask,
3524 struct nameidata *nd)
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003525#endif
Yanfdebe2b2008-01-14 13:26:08 -05003526{
3527 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3528 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003529 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003530}
Chris Mason39279cc2007-06-12 06:35:45 -04003531
3532static struct inode_operations btrfs_dir_inode_operations = {
3533 .lookup = btrfs_lookup,
3534 .create = btrfs_create,
3535 .unlink = btrfs_unlink,
3536 .link = btrfs_link,
3537 .mkdir = btrfs_mkdir,
3538 .rmdir = btrfs_rmdir,
3539 .rename = btrfs_rename,
3540 .symlink = btrfs_symlink,
3541 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003542 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003543 .setxattr = generic_setxattr,
3544 .getxattr = generic_getxattr,
3545 .listxattr = btrfs_listxattr,
3546 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003547 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003548};
Chris Mason39279cc2007-06-12 06:35:45 -04003549static struct inode_operations btrfs_dir_ro_inode_operations = {
3550 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003551 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003552};
Chris Mason39279cc2007-06-12 06:35:45 -04003553static struct file_operations btrfs_dir_file_operations = {
3554 .llseek = generic_file_llseek,
3555 .read = generic_read_dir,
3556 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003557 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003558#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003559 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003560#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003561 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003562};
3563
Chris Masond1310b22008-01-24 16:13:08 -05003564static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003565 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003566 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003567 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003568 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003569 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003570 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003571 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003572 .set_bit_hook = btrfs_set_bit_hook,
3573 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003574};
3575
Chris Mason39279cc2007-06-12 06:35:45 -04003576static struct address_space_operations btrfs_aops = {
3577 .readpage = btrfs_readpage,
3578 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003579 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003580 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003581 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003582 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003583 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003584 .invalidatepage = btrfs_invalidatepage,
3585 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003586 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003587};
3588
3589static struct address_space_operations btrfs_symlink_aops = {
3590 .readpage = btrfs_readpage,
3591 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003592 .invalidatepage = btrfs_invalidatepage,
3593 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003594};
3595
3596static struct inode_operations btrfs_file_inode_operations = {
3597 .truncate = btrfs_truncate,
3598 .getattr = btrfs_getattr,
3599 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003600 .setxattr = generic_setxattr,
3601 .getxattr = generic_getxattr,
3602 .listxattr = btrfs_listxattr,
3603 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003604 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003605};
Josef Bacik618e21d2007-07-11 10:18:17 -04003606static struct inode_operations btrfs_special_inode_operations = {
3607 .getattr = btrfs_getattr,
3608 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003609 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -04003610 .setxattr = generic_setxattr,
3611 .getxattr = generic_getxattr,
3612 .listxattr = btrfs_listxattr,
3613 .removexattr = generic_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003614};
Chris Mason39279cc2007-06-12 06:35:45 -04003615static struct inode_operations btrfs_symlink_inode_operations = {
3616 .readlink = generic_readlink,
3617 .follow_link = page_follow_link_light,
3618 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003619 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003620};