Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
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 | ||
8f18cf13 | 19 | #include <linux/kernel.h> |
065631f6 | 20 | #include <linux/bio.h> |
39279cc3 | 21 | #include <linux/buffer_head.h> |
f2eb0a24 | 22 | #include <linux/file.h> |
39279cc3 CM |
23 | #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> | |
9ebefb18 | 36 | #include <linux/bit_spinlock.h> |
92fee66d | 37 | #include <linux/version.h> |
5103e947 | 38 | #include <linux/xattr.h> |
33268eaf | 39 | #include <linux/posix_acl.h> |
39279cc3 CM |
40 | #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" | |
0b86a832 | 46 | #include "volumes.h" |
e6dcd2dc | 47 | #include "ordered-data.h" |
95819c05 | 48 | #include "xattr.h" |
39279cc3 CM |
49 | |
50 | struct btrfs_iget_args { | |
51 | u64 ino; | |
52 | struct btrfs_root *root; | |
53 | }; | |
54 | ||
55 | static struct inode_operations btrfs_dir_inode_operations; | |
56 | static struct inode_operations btrfs_symlink_inode_operations; | |
57 | static struct inode_operations btrfs_dir_ro_inode_operations; | |
618e21d5 | 58 | static struct inode_operations btrfs_special_inode_operations; |
39279cc3 CM |
59 | static struct inode_operations btrfs_file_inode_operations; |
60 | static struct address_space_operations btrfs_aops; | |
61 | static struct address_space_operations btrfs_symlink_aops; | |
62 | static struct file_operations btrfs_dir_file_operations; | |
d1310b2e | 63 | static struct extent_io_ops btrfs_extent_io_ops; |
39279cc3 CM |
64 | |
65 | static struct kmem_cache *btrfs_inode_cachep; | |
66 | struct kmem_cache *btrfs_trans_handle_cachep; | |
67 | struct kmem_cache *btrfs_transaction_cachep; | |
68 | struct kmem_cache *btrfs_bit_radix_cachep; | |
69 | struct kmem_cache *btrfs_path_cachep; | |
70 | ||
71 | #define S_SHIFT 12 | |
72 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { | |
73 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, | |
74 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, | |
75 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, | |
76 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, | |
77 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, | |
78 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, | |
79 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, | |
80 | }; | |
81 | ||
7b128766 JB |
82 | static void btrfs_truncate(struct inode *inode); |
83 | ||
1832a6d5 CM |
84 | int btrfs_check_free_space(struct btrfs_root *root, u64 num_required, |
85 | int for_del) | |
86 | { | |
a2135011 CM |
87 | u64 total; |
88 | u64 used; | |
1832a6d5 | 89 | u64 thresh; |
bcbfce8a | 90 | unsigned long flags; |
1832a6d5 CM |
91 | int ret = 0; |
92 | ||
a2135011 CM |
93 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); |
94 | total = btrfs_super_total_bytes(&root->fs_info->super_copy); | |
95 | used = btrfs_super_bytes_used(&root->fs_info->super_copy); | |
1832a6d5 | 96 | if (for_del) |
f9ef6604 | 97 | thresh = total * 90; |
1832a6d5 | 98 | else |
f9ef6604 CM |
99 | thresh = total * 85; |
100 | ||
101 | do_div(thresh, 100); | |
1832a6d5 | 102 | |
1832a6d5 CM |
103 | if (used + root->fs_info->delalloc_bytes + num_required > thresh) |
104 | ret = -ENOSPC; | |
bcbfce8a | 105 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); |
1832a6d5 CM |
106 | return ret; |
107 | } | |
108 | ||
be20aa9d | 109 | static int cow_file_range(struct inode *inode, u64 start, u64 end) |
b888db2b CM |
110 | { |
111 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
112 | struct btrfs_trans_handle *trans; | |
b888db2b | 113 | u64 alloc_hint = 0; |
db94535d | 114 | u64 num_bytes; |
c59f8951 | 115 | u64 cur_alloc_size; |
db94535d | 116 | u64 blocksize = root->sectorsize; |
d1310b2e | 117 | u64 orig_num_bytes; |
be20aa9d | 118 | struct btrfs_key ins; |
e6dcd2dc CM |
119 | struct extent_map *em; |
120 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
121 | int ret = 0; | |
b888db2b | 122 | |
f9295749 | 123 | trans = btrfs_join_transaction(root, 1); |
b888db2b | 124 | BUG_ON(!trans); |
be20aa9d CM |
125 | btrfs_set_trans_block_group(trans, inode); |
126 | ||
db94535d | 127 | num_bytes = (end - start + blocksize) & ~(blocksize - 1); |
be20aa9d | 128 | num_bytes = max(blocksize, num_bytes); |
d1310b2e | 129 | orig_num_bytes = num_bytes; |
db94535d | 130 | |
179e29e4 CM |
131 | if (alloc_hint == EXTENT_MAP_INLINE) |
132 | goto out; | |
133 | ||
3b951516 | 134 | BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy)); |
e5a2217e | 135 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
e6dcd2dc | 136 | btrfs_drop_extent_cache(inode, start, start + num_bytes - 1); |
e5a2217e | 137 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
3b951516 | 138 | |
c59f8951 CM |
139 | while(num_bytes > 0) { |
140 | cur_alloc_size = min(num_bytes, root->fs_info->max_extent); | |
e6dcd2dc CM |
141 | ret = btrfs_reserve_extent(trans, root, cur_alloc_size, |
142 | root->sectorsize, 0, 0, | |
143 | (u64)-1, &ins, 1); | |
c59f8951 CM |
144 | if (ret) { |
145 | WARN_ON(1); | |
146 | goto out; | |
147 | } | |
e6dcd2dc CM |
148 | em = alloc_extent_map(GFP_NOFS); |
149 | em->start = start; | |
150 | em->len = ins.offset; | |
151 | em->block_start = ins.objectid; | |
152 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
e5a2217e | 153 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
7f3c74fb | 154 | set_bit(EXTENT_FLAG_PINNED, &em->flags); |
e6dcd2dc CM |
155 | while(1) { |
156 | spin_lock(&em_tree->lock); | |
157 | ret = add_extent_mapping(em_tree, em); | |
158 | spin_unlock(&em_tree->lock); | |
159 | if (ret != -EEXIST) { | |
160 | free_extent_map(em); | |
161 | break; | |
162 | } | |
163 | btrfs_drop_extent_cache(inode, start, | |
164 | start + ins.offset - 1); | |
165 | } | |
e5a2217e | 166 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
e6dcd2dc | 167 | |
98d20f67 | 168 | cur_alloc_size = ins.offset; |
e6dcd2dc | 169 | ret = btrfs_add_ordered_extent(inode, start, ins.objectid, |
7ea394f1 | 170 | ins.offset, 0); |
e6dcd2dc | 171 | BUG_ON(ret); |
3b951516 CM |
172 | if (num_bytes < cur_alloc_size) { |
173 | printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes, | |
174 | cur_alloc_size); | |
175 | break; | |
176 | } | |
c59f8951 CM |
177 | num_bytes -= cur_alloc_size; |
178 | alloc_hint = ins.objectid + ins.offset; | |
179 | start += cur_alloc_size; | |
b888db2b | 180 | } |
b888db2b CM |
181 | out: |
182 | btrfs_end_transaction(trans, root); | |
be20aa9d CM |
183 | return ret; |
184 | } | |
185 | ||
186 | static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end) | |
187 | { | |
188 | u64 extent_start; | |
189 | u64 extent_end; | |
190 | u64 bytenr; | |
1832a6d5 | 191 | u64 loops = 0; |
c31f8830 | 192 | u64 total_fs_bytes; |
be20aa9d | 193 | struct btrfs_root *root = BTRFS_I(inode)->root; |
a68d5933 | 194 | struct btrfs_block_group_cache *block_group; |
7ea394f1 | 195 | struct btrfs_trans_handle *trans; |
be20aa9d CM |
196 | struct extent_buffer *leaf; |
197 | int found_type; | |
198 | struct btrfs_path *path; | |
199 | struct btrfs_file_extent_item *item; | |
200 | int ret; | |
7ea394f1 | 201 | int err = 0; |
be20aa9d CM |
202 | struct btrfs_key found_key; |
203 | ||
c31f8830 | 204 | total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
be20aa9d CM |
205 | path = btrfs_alloc_path(); |
206 | BUG_ON(!path); | |
7ea394f1 YZ |
207 | trans = btrfs_join_transaction(root, 1); |
208 | BUG_ON(!trans); | |
be20aa9d CM |
209 | again: |
210 | ret = btrfs_lookup_file_extent(NULL, root, path, | |
211 | inode->i_ino, start, 0); | |
212 | if (ret < 0) { | |
7ea394f1 YZ |
213 | err = ret; |
214 | goto out; | |
be20aa9d CM |
215 | } |
216 | ||
be20aa9d CM |
217 | if (ret != 0) { |
218 | if (path->slots[0] == 0) | |
219 | goto not_found; | |
220 | path->slots[0]--; | |
221 | } | |
222 | ||
223 | leaf = path->nodes[0]; | |
224 | item = btrfs_item_ptr(leaf, path->slots[0], | |
225 | struct btrfs_file_extent_item); | |
226 | ||
227 | /* are we inside the extent that was found? */ | |
228 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
229 | found_type = btrfs_key_type(&found_key); | |
230 | if (found_key.objectid != inode->i_ino || | |
bbaf549e | 231 | found_type != BTRFS_EXTENT_DATA_KEY) |
be20aa9d | 232 | goto not_found; |
be20aa9d CM |
233 | |
234 | found_type = btrfs_file_extent_type(leaf, item); | |
235 | extent_start = found_key.offset; | |
236 | if (found_type == BTRFS_FILE_EXTENT_REG) { | |
c31f8830 CM |
237 | u64 extent_num_bytes; |
238 | ||
239 | extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item); | |
240 | extent_end = extent_start + extent_num_bytes; | |
be20aa9d CM |
241 | err = 0; |
242 | ||
1832a6d5 CM |
243 | if (loops && start != extent_start) |
244 | goto not_found; | |
245 | ||
be20aa9d CM |
246 | if (start < extent_start || start >= extent_end) |
247 | goto not_found; | |
248 | ||
be20aa9d CM |
249 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
250 | if (bytenr == 0) | |
251 | goto not_found; | |
252 | ||
7ea394f1 | 253 | if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr)) |
a68d5933 | 254 | goto not_found; |
c31f8830 CM |
255 | /* |
256 | * we may be called by the resizer, make sure we're inside | |
257 | * the limits of the FS | |
258 | */ | |
a68d5933 CM |
259 | block_group = btrfs_lookup_block_group(root->fs_info, |
260 | bytenr); | |
261 | if (!block_group || block_group->ro) | |
c31f8830 CM |
262 | goto not_found; |
263 | ||
7ea394f1 YZ |
264 | bytenr += btrfs_file_extent_offset(leaf, item); |
265 | extent_num_bytes = min(end + 1, extent_end) - start; | |
266 | ret = btrfs_add_ordered_extent(inode, start, bytenr, | |
267 | extent_num_bytes, 1); | |
268 | if (ret) { | |
269 | err = ret; | |
270 | goto out; | |
271 | } | |
272 | ||
273 | btrfs_release_path(root, path); | |
be20aa9d | 274 | start = extent_end; |
7ea394f1 YZ |
275 | if (start <= end) { |
276 | loops++; | |
277 | goto again; | |
278 | } | |
bd09835d | 279 | } else { |
7ea394f1 YZ |
280 | not_found: |
281 | btrfs_end_transaction(trans, root); | |
be20aa9d | 282 | btrfs_free_path(path); |
7ea394f1 | 283 | return cow_file_range(inode, start, end); |
be20aa9d | 284 | } |
7ea394f1 YZ |
285 | out: |
286 | WARN_ON(err); | |
287 | btrfs_end_transaction(trans, root); | |
288 | btrfs_free_path(path); | |
289 | return err; | |
be20aa9d CM |
290 | } |
291 | ||
292 | static int run_delalloc_range(struct inode *inode, u64 start, u64 end) | |
293 | { | |
294 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
295 | int ret; | |
a2135011 | 296 | |
b98b6767 Y |
297 | if (btrfs_test_opt(root, NODATACOW) || |
298 | btrfs_test_flag(inode, NODATACOW)) | |
be20aa9d CM |
299 | ret = run_delalloc_nocow(inode, start, end); |
300 | else | |
301 | ret = cow_file_range(inode, start, end); | |
1832a6d5 | 302 | |
b888db2b CM |
303 | return ret; |
304 | } | |
305 | ||
291d673e | 306 | int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end, |
b0c68f8b | 307 | unsigned long old, unsigned long bits) |
291d673e | 308 | { |
bcbfce8a | 309 | unsigned long flags; |
b0c68f8b | 310 | if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
291d673e | 311 | struct btrfs_root *root = BTRFS_I(inode)->root; |
bcbfce8a | 312 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); |
9069218d | 313 | BTRFS_I(inode)->delalloc_bytes += end - start + 1; |
291d673e | 314 | root->fs_info->delalloc_bytes += end - start + 1; |
ea8c2819 CM |
315 | if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) { |
316 | list_add_tail(&BTRFS_I(inode)->delalloc_inodes, | |
317 | &root->fs_info->delalloc_inodes); | |
318 | } | |
bcbfce8a | 319 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); |
291d673e CM |
320 | } |
321 | return 0; | |
322 | } | |
323 | ||
324 | int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end, | |
b0c68f8b | 325 | unsigned long old, unsigned long bits) |
291d673e | 326 | { |
b0c68f8b | 327 | if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) { |
291d673e | 328 | struct btrfs_root *root = BTRFS_I(inode)->root; |
bcbfce8a CM |
329 | unsigned long flags; |
330 | ||
331 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); | |
b0c68f8b CM |
332 | if (end - start + 1 > root->fs_info->delalloc_bytes) { |
333 | printk("warning: delalloc account %Lu %Lu\n", | |
334 | end - start + 1, root->fs_info->delalloc_bytes); | |
335 | root->fs_info->delalloc_bytes = 0; | |
9069218d | 336 | BTRFS_I(inode)->delalloc_bytes = 0; |
b0c68f8b CM |
337 | } else { |
338 | root->fs_info->delalloc_bytes -= end - start + 1; | |
9069218d | 339 | BTRFS_I(inode)->delalloc_bytes -= end - start + 1; |
b0c68f8b | 340 | } |
ea8c2819 CM |
341 | if (BTRFS_I(inode)->delalloc_bytes == 0 && |
342 | !list_empty(&BTRFS_I(inode)->delalloc_inodes)) { | |
343 | list_del_init(&BTRFS_I(inode)->delalloc_inodes); | |
344 | } | |
bcbfce8a | 345 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); |
291d673e CM |
346 | } |
347 | return 0; | |
348 | } | |
349 | ||
239b14b3 CM |
350 | int btrfs_merge_bio_hook(struct page *page, unsigned long offset, |
351 | size_t size, struct bio *bio) | |
352 | { | |
353 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; | |
354 | struct btrfs_mapping_tree *map_tree; | |
239b14b3 | 355 | u64 logical = bio->bi_sector << 9; |
239b14b3 CM |
356 | u64 length = 0; |
357 | u64 map_length; | |
239b14b3 CM |
358 | int ret; |
359 | ||
f2d8d74d | 360 | length = bio->bi_size; |
239b14b3 CM |
361 | map_tree = &root->fs_info->mapping_tree; |
362 | map_length = length; | |
cea9e445 | 363 | ret = btrfs_map_block(map_tree, READ, logical, |
f188591e | 364 | &map_length, NULL, 0); |
cea9e445 | 365 | |
239b14b3 | 366 | if (map_length < length + size) { |
239b14b3 CM |
367 | return 1; |
368 | } | |
369 | return 0; | |
370 | } | |
371 | ||
44b8bd7e | 372 | int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, |
f188591e | 373 | int mirror_num) |
065631f6 | 374 | { |
065631f6 | 375 | struct btrfs_root *root = BTRFS_I(inode)->root; |
065631f6 | 376 | int ret = 0; |
e015640f | 377 | |
3edf7d33 | 378 | ret = btrfs_csum_one_bio(root, inode, bio); |
44b8bd7e | 379 | BUG_ON(ret); |
e015640f | 380 | |
8b712842 | 381 | return btrfs_map_bio(root, rw, bio, mirror_num, 1); |
44b8bd7e CM |
382 | } |
383 | ||
384 | int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio, | |
385 | int mirror_num) | |
386 | { | |
387 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
388 | int ret = 0; | |
389 | ||
e6dcd2dc CM |
390 | ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0); |
391 | BUG_ON(ret); | |
065631f6 | 392 | |
7ea394f1 YZ |
393 | if (btrfs_test_opt(root, NODATASUM) || |
394 | btrfs_test_flag(inode, NODATASUM)) { | |
395 | goto mapit; | |
396 | } | |
397 | ||
4d1b5fb4 CM |
398 | if (!(rw & (1 << BIO_RW))) { |
399 | btrfs_lookup_bio_sums(root, inode, bio); | |
400 | goto mapit; | |
401 | } | |
44b8bd7e CM |
402 | return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info, |
403 | inode, rw, bio, mirror_num, | |
404 | __btrfs_submit_bio_hook); | |
0b86a832 | 405 | mapit: |
8b712842 | 406 | return btrfs_map_bio(root, rw, bio, mirror_num, 0); |
065631f6 | 407 | } |
6885f308 | 408 | |
ba1da2f4 | 409 | static noinline int add_pending_csums(struct btrfs_trans_handle *trans, |
e6dcd2dc CM |
410 | struct inode *inode, u64 file_offset, |
411 | struct list_head *list) | |
412 | { | |
413 | struct list_head *cur; | |
414 | struct btrfs_ordered_sum *sum; | |
415 | ||
416 | btrfs_set_trans_block_group(trans, inode); | |
ba1da2f4 | 417 | list_for_each(cur, list) { |
e6dcd2dc | 418 | sum = list_entry(cur, struct btrfs_ordered_sum, list); |
e6dcd2dc CM |
419 | btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root, |
420 | inode, sum); | |
e6dcd2dc CM |
421 | } |
422 | return 0; | |
423 | } | |
424 | ||
ea8c2819 CM |
425 | int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end) |
426 | { | |
427 | return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end, | |
428 | GFP_NOFS); | |
429 | } | |
430 | ||
247e743c CM |
431 | struct btrfs_writepage_fixup { |
432 | struct page *page; | |
433 | struct btrfs_work work; | |
434 | }; | |
435 | ||
436 | /* see btrfs_writepage_start_hook for details on why this is required */ | |
437 | void btrfs_writepage_fixup_worker(struct btrfs_work *work) | |
438 | { | |
439 | struct btrfs_writepage_fixup *fixup; | |
440 | struct btrfs_ordered_extent *ordered; | |
441 | struct page *page; | |
442 | struct inode *inode; | |
443 | u64 page_start; | |
444 | u64 page_end; | |
445 | ||
446 | fixup = container_of(work, struct btrfs_writepage_fixup, work); | |
447 | page = fixup->page; | |
4a096752 | 448 | again: |
247e743c CM |
449 | lock_page(page); |
450 | if (!page->mapping || !PageDirty(page) || !PageChecked(page)) { | |
451 | ClearPageChecked(page); | |
452 | goto out_page; | |
453 | } | |
454 | ||
455 | inode = page->mapping->host; | |
456 | page_start = page_offset(page); | |
457 | page_end = page_offset(page) + PAGE_CACHE_SIZE - 1; | |
458 | ||
459 | lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); | |
4a096752 CM |
460 | |
461 | /* already ordered? We're done */ | |
462 | if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end, | |
463 | EXTENT_ORDERED, 0)) { | |
247e743c | 464 | goto out; |
4a096752 CM |
465 | } |
466 | ||
467 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
468 | if (ordered) { | |
469 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, | |
470 | page_end, GFP_NOFS); | |
471 | unlock_page(page); | |
472 | btrfs_start_ordered_extent(inode, ordered, 1); | |
473 | goto again; | |
474 | } | |
247e743c | 475 | |
ea8c2819 | 476 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
247e743c CM |
477 | ClearPageChecked(page); |
478 | out: | |
479 | unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS); | |
480 | out_page: | |
481 | unlock_page(page); | |
482 | page_cache_release(page); | |
483 | } | |
484 | ||
485 | /* | |
486 | * There are a few paths in the higher layers of the kernel that directly | |
487 | * set the page dirty bit without asking the filesystem if it is a | |
488 | * good idea. This causes problems because we want to make sure COW | |
489 | * properly happens and the data=ordered rules are followed. | |
490 | * | |
491 | * In our case any range that doesn't have the EXTENT_ORDERED bit set | |
492 | * hasn't been properly setup for IO. We kick off an async process | |
493 | * to fix it up. The async helper will wait for ordered extents, set | |
494 | * the delalloc bit and make it safe to write the page. | |
495 | */ | |
496 | int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end) | |
497 | { | |
498 | struct inode *inode = page->mapping->host; | |
499 | struct btrfs_writepage_fixup *fixup; | |
500 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
501 | int ret; | |
502 | ||
503 | ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end, | |
504 | EXTENT_ORDERED, 0); | |
505 | if (ret) | |
506 | return 0; | |
507 | ||
508 | if (PageChecked(page)) | |
509 | return -EAGAIN; | |
510 | ||
511 | fixup = kzalloc(sizeof(*fixup), GFP_NOFS); | |
512 | if (!fixup) | |
513 | return -EAGAIN; | |
f421950f | 514 | |
247e743c CM |
515 | SetPageChecked(page); |
516 | page_cache_get(page); | |
517 | fixup->work.func = btrfs_writepage_fixup_worker; | |
518 | fixup->page = page; | |
519 | btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work); | |
520 | return -EAGAIN; | |
521 | } | |
522 | ||
211f90e6 | 523 | static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end) |
e6dcd2dc | 524 | { |
e6dcd2dc CM |
525 | struct btrfs_root *root = BTRFS_I(inode)->root; |
526 | struct btrfs_trans_handle *trans; | |
527 | struct btrfs_ordered_extent *ordered_extent; | |
528 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; | |
529 | u64 alloc_hint = 0; | |
530 | struct list_head list; | |
531 | struct btrfs_key ins; | |
532 | int ret; | |
533 | ||
534 | ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1); | |
ba1da2f4 | 535 | if (!ret) |
e6dcd2dc | 536 | return 0; |
e6dcd2dc | 537 | |
f9295749 | 538 | trans = btrfs_join_transaction(root, 1); |
e6dcd2dc CM |
539 | |
540 | ordered_extent = btrfs_lookup_ordered_extent(inode, start); | |
541 | BUG_ON(!ordered_extent); | |
7ea394f1 YZ |
542 | if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) |
543 | goto nocow; | |
e6dcd2dc CM |
544 | |
545 | lock_extent(io_tree, ordered_extent->file_offset, | |
546 | ordered_extent->file_offset + ordered_extent->len - 1, | |
547 | GFP_NOFS); | |
548 | ||
549 | INIT_LIST_HEAD(&list); | |
550 | ||
551 | ins.objectid = ordered_extent->start; | |
552 | ins.offset = ordered_extent->len; | |
553 | ins.type = BTRFS_EXTENT_ITEM_KEY; | |
e5a2217e | 554 | |
e6dcd2dc CM |
555 | ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid, |
556 | trans->transid, inode->i_ino, | |
557 | ordered_extent->file_offset, &ins); | |
558 | BUG_ON(ret); | |
ee6e6504 CM |
559 | |
560 | mutex_lock(&BTRFS_I(inode)->extent_mutex); | |
e5a2217e | 561 | |
e6dcd2dc CM |
562 | ret = btrfs_drop_extents(trans, root, inode, |
563 | ordered_extent->file_offset, | |
564 | ordered_extent->file_offset + | |
565 | ordered_extent->len, | |
566 | ordered_extent->file_offset, &alloc_hint); | |
567 | BUG_ON(ret); | |
568 | ret = btrfs_insert_file_extent(trans, root, inode->i_ino, | |
569 | ordered_extent->file_offset, | |
570 | ordered_extent->start, | |
571 | ordered_extent->len, | |
572 | ordered_extent->len, 0); | |
573 | BUG_ON(ret); | |
7f3c74fb | 574 | |
e6dcd2dc CM |
575 | btrfs_drop_extent_cache(inode, ordered_extent->file_offset, |
576 | ordered_extent->file_offset + | |
577 | ordered_extent->len - 1); | |
ee6e6504 CM |
578 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
579 | ||
e6dcd2dc CM |
580 | inode->i_blocks += ordered_extent->len >> 9; |
581 | unlock_extent(io_tree, ordered_extent->file_offset, | |
582 | ordered_extent->file_offset + ordered_extent->len - 1, | |
583 | GFP_NOFS); | |
7ea394f1 | 584 | nocow: |
e6dcd2dc CM |
585 | add_pending_csums(trans, inode, ordered_extent->file_offset, |
586 | &ordered_extent->list); | |
587 | ||
dbe674a9 | 588 | btrfs_ordered_update_i_size(inode, ordered_extent); |
e6dcd2dc | 589 | btrfs_remove_ordered_extent(inode, ordered_extent); |
7f3c74fb | 590 | |
e6dcd2dc CM |
591 | /* once for us */ |
592 | btrfs_put_ordered_extent(ordered_extent); | |
593 | /* once for the tree */ | |
594 | btrfs_put_ordered_extent(ordered_extent); | |
595 | ||
596 | btrfs_update_inode(trans, root, inode); | |
597 | btrfs_end_transaction(trans, root); | |
598 | return 0; | |
599 | } | |
600 | ||
211f90e6 CM |
601 | int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end, |
602 | struct extent_state *state, int uptodate) | |
603 | { | |
604 | return btrfs_finish_ordered_io(page->mapping->host, start, end); | |
605 | } | |
606 | ||
7e38326f CM |
607 | struct io_failure_record { |
608 | struct page *page; | |
609 | u64 start; | |
610 | u64 len; | |
611 | u64 logical; | |
612 | int last_mirror; | |
613 | }; | |
614 | ||
1259ab75 CM |
615 | int btrfs_io_failed_hook(struct bio *failed_bio, |
616 | struct page *page, u64 start, u64 end, | |
617 | struct extent_state *state) | |
7e38326f CM |
618 | { |
619 | struct io_failure_record *failrec = NULL; | |
620 | u64 private; | |
621 | struct extent_map *em; | |
622 | struct inode *inode = page->mapping->host; | |
623 | struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree; | |
3b951516 | 624 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; |
7e38326f CM |
625 | struct bio *bio; |
626 | int num_copies; | |
627 | int ret; | |
1259ab75 | 628 | int rw; |
7e38326f CM |
629 | u64 logical; |
630 | ||
631 | ret = get_state_private(failure_tree, start, &private); | |
632 | if (ret) { | |
7e38326f CM |
633 | failrec = kmalloc(sizeof(*failrec), GFP_NOFS); |
634 | if (!failrec) | |
635 | return -ENOMEM; | |
636 | failrec->start = start; | |
637 | failrec->len = end - start + 1; | |
638 | failrec->last_mirror = 0; | |
639 | ||
3b951516 CM |
640 | spin_lock(&em_tree->lock); |
641 | em = lookup_extent_mapping(em_tree, start, failrec->len); | |
642 | if (em->start > start || em->start + em->len < start) { | |
643 | free_extent_map(em); | |
644 | em = NULL; | |
645 | } | |
646 | spin_unlock(&em_tree->lock); | |
7e38326f CM |
647 | |
648 | if (!em || IS_ERR(em)) { | |
649 | kfree(failrec); | |
650 | return -EIO; | |
651 | } | |
652 | logical = start - em->start; | |
653 | logical = em->block_start + logical; | |
654 | failrec->logical = logical; | |
655 | free_extent_map(em); | |
656 | set_extent_bits(failure_tree, start, end, EXTENT_LOCKED | | |
657 | EXTENT_DIRTY, GFP_NOFS); | |
587f7704 CM |
658 | set_state_private(failure_tree, start, |
659 | (u64)(unsigned long)failrec); | |
7e38326f | 660 | } else { |
587f7704 | 661 | failrec = (struct io_failure_record *)(unsigned long)private; |
7e38326f CM |
662 | } |
663 | num_copies = btrfs_num_copies( | |
664 | &BTRFS_I(inode)->root->fs_info->mapping_tree, | |
665 | failrec->logical, failrec->len); | |
666 | failrec->last_mirror++; | |
667 | if (!state) { | |
668 | spin_lock_irq(&BTRFS_I(inode)->io_tree.lock); | |
669 | state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree, | |
670 | failrec->start, | |
671 | EXTENT_LOCKED); | |
672 | if (state && state->start != failrec->start) | |
673 | state = NULL; | |
674 | spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock); | |
675 | } | |
676 | if (!state || failrec->last_mirror > num_copies) { | |
677 | set_state_private(failure_tree, failrec->start, 0); | |
678 | clear_extent_bits(failure_tree, failrec->start, | |
679 | failrec->start + failrec->len - 1, | |
680 | EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS); | |
681 | kfree(failrec); | |
682 | return -EIO; | |
683 | } | |
684 | bio = bio_alloc(GFP_NOFS, 1); | |
685 | bio->bi_private = state; | |
686 | bio->bi_end_io = failed_bio->bi_end_io; | |
687 | bio->bi_sector = failrec->logical >> 9; | |
688 | bio->bi_bdev = failed_bio->bi_bdev; | |
e1c4b745 | 689 | bio->bi_size = 0; |
7e38326f | 690 | bio_add_page(bio, page, failrec->len, start - page_offset(page)); |
1259ab75 CM |
691 | if (failed_bio->bi_rw & (1 << BIO_RW)) |
692 | rw = WRITE; | |
693 | else | |
694 | rw = READ; | |
695 | ||
696 | BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio, | |
697 | failrec->last_mirror); | |
698 | return 0; | |
699 | } | |
700 | ||
701 | int btrfs_clean_io_failures(struct inode *inode, u64 start) | |
702 | { | |
703 | u64 private; | |
704 | u64 private_failure; | |
705 | struct io_failure_record *failure; | |
706 | int ret; | |
707 | ||
708 | private = 0; | |
709 | if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private, | |
710 | (u64)-1, 1, EXTENT_DIRTY)) { | |
711 | ret = get_state_private(&BTRFS_I(inode)->io_failure_tree, | |
712 | start, &private_failure); | |
713 | if (ret == 0) { | |
714 | failure = (struct io_failure_record *)(unsigned long) | |
715 | private_failure; | |
716 | set_state_private(&BTRFS_I(inode)->io_failure_tree, | |
717 | failure->start, 0); | |
718 | clear_extent_bits(&BTRFS_I(inode)->io_failure_tree, | |
719 | failure->start, | |
720 | failure->start + failure->len - 1, | |
721 | EXTENT_DIRTY | EXTENT_LOCKED, | |
722 | GFP_NOFS); | |
723 | kfree(failure); | |
724 | } | |
725 | } | |
7e38326f CM |
726 | return 0; |
727 | } | |
728 | ||
70dec807 CM |
729 | int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end, |
730 | struct extent_state *state) | |
07157aac | 731 | { |
35ebb934 | 732 | size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT); |
07157aac | 733 | struct inode *inode = page->mapping->host; |
d1310b2e | 734 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
07157aac | 735 | char *kaddr; |
aadfeb6e | 736 | u64 private = ~(u32)0; |
07157aac | 737 | int ret; |
ff79f819 CM |
738 | struct btrfs_root *root = BTRFS_I(inode)->root; |
739 | u32 csum = ~(u32)0; | |
bbf0d006 | 740 | unsigned long flags; |
d1310b2e | 741 | |
b98b6767 Y |
742 | if (btrfs_test_opt(root, NODATASUM) || |
743 | btrfs_test_flag(inode, NODATASUM)) | |
b6cda9bc | 744 | return 0; |
c2e639f0 | 745 | if (state && state->start == start) { |
70dec807 CM |
746 | private = state->private; |
747 | ret = 0; | |
748 | } else { | |
749 | ret = get_state_private(io_tree, start, &private); | |
750 | } | |
bbf0d006 | 751 | local_irq_save(flags); |
07157aac CM |
752 | kaddr = kmap_atomic(page, KM_IRQ0); |
753 | if (ret) { | |
754 | goto zeroit; | |
755 | } | |
ff79f819 CM |
756 | csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1); |
757 | btrfs_csum_final(csum, (char *)&csum); | |
758 | if (csum != private) { | |
07157aac CM |
759 | goto zeroit; |
760 | } | |
761 | kunmap_atomic(kaddr, KM_IRQ0); | |
bbf0d006 | 762 | local_irq_restore(flags); |
7e38326f CM |
763 | |
764 | /* if the io failure tree for this inode is non-empty, | |
765 | * check to see if we've recovered from a failed IO | |
766 | */ | |
1259ab75 | 767 | btrfs_clean_io_failures(inode, start); |
07157aac CM |
768 | return 0; |
769 | ||
770 | zeroit: | |
aadfeb6e CM |
771 | printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n", |
772 | page->mapping->host->i_ino, (unsigned long long)start, csum, | |
773 | private); | |
db94535d CM |
774 | memset(kaddr + offset, 1, end - start + 1); |
775 | flush_dcache_page(page); | |
07157aac | 776 | kunmap_atomic(kaddr, KM_IRQ0); |
bbf0d006 | 777 | local_irq_restore(flags); |
3b951516 CM |
778 | if (private == 0) |
779 | return 0; | |
7e38326f | 780 | return -EIO; |
07157aac | 781 | } |
b888db2b | 782 | |
7b128766 JB |
783 | /* |
784 | * This creates an orphan entry for the given inode in case something goes | |
785 | * wrong in the middle of an unlink/truncate. | |
786 | */ | |
787 | int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode) | |
788 | { | |
789 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
790 | int ret = 0; | |
791 | ||
bcc63abb | 792 | spin_lock(&root->list_lock); |
7b128766 JB |
793 | |
794 | /* already on the orphan list, we're good */ | |
795 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { | |
bcc63abb | 796 | spin_unlock(&root->list_lock); |
7b128766 JB |
797 | return 0; |
798 | } | |
799 | ||
800 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); | |
801 | ||
bcc63abb | 802 | spin_unlock(&root->list_lock); |
7b128766 JB |
803 | |
804 | /* | |
805 | * insert an orphan item to track this unlinked/truncated file | |
806 | */ | |
807 | ret = btrfs_insert_orphan_item(trans, root, inode->i_ino); | |
808 | ||
809 | return ret; | |
810 | } | |
811 | ||
812 | /* | |
813 | * We have done the truncate/delete so we can go ahead and remove the orphan | |
814 | * item for this particular inode. | |
815 | */ | |
816 | int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode) | |
817 | { | |
818 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
819 | int ret = 0; | |
820 | ||
bcc63abb | 821 | spin_lock(&root->list_lock); |
7b128766 JB |
822 | |
823 | if (list_empty(&BTRFS_I(inode)->i_orphan)) { | |
bcc63abb | 824 | spin_unlock(&root->list_lock); |
7b128766 JB |
825 | return 0; |
826 | } | |
827 | ||
828 | list_del_init(&BTRFS_I(inode)->i_orphan); | |
829 | if (!trans) { | |
bcc63abb | 830 | spin_unlock(&root->list_lock); |
7b128766 JB |
831 | return 0; |
832 | } | |
833 | ||
bcc63abb | 834 | spin_unlock(&root->list_lock); |
7b128766 JB |
835 | |
836 | ret = btrfs_del_orphan_item(trans, root, inode->i_ino); | |
837 | ||
838 | return ret; | |
839 | } | |
840 | ||
841 | /* | |
842 | * this cleans up any orphans that may be left on the list from the last use | |
843 | * of this root. | |
844 | */ | |
845 | void btrfs_orphan_cleanup(struct btrfs_root *root) | |
846 | { | |
847 | struct btrfs_path *path; | |
848 | struct extent_buffer *leaf; | |
849 | struct btrfs_item *item; | |
850 | struct btrfs_key key, found_key; | |
851 | struct btrfs_trans_handle *trans; | |
852 | struct inode *inode; | |
853 | int ret = 0, nr_unlink = 0, nr_truncate = 0; | |
854 | ||
855 | /* don't do orphan cleanup if the fs is readonly. */ | |
856 | if (root->inode->i_sb->s_flags & MS_RDONLY) | |
857 | return; | |
858 | ||
859 | path = btrfs_alloc_path(); | |
860 | if (!path) | |
861 | return; | |
862 | path->reada = -1; | |
863 | ||
864 | key.objectid = BTRFS_ORPHAN_OBJECTID; | |
865 | btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY); | |
866 | key.offset = (u64)-1; | |
867 | ||
868 | trans = btrfs_start_transaction(root, 1); | |
869 | btrfs_set_trans_block_group(trans, root->inode); | |
870 | ||
871 | while (1) { | |
872 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
873 | if (ret < 0) { | |
874 | printk(KERN_ERR "Error searching slot for orphan: %d" | |
875 | "\n", ret); | |
876 | break; | |
877 | } | |
878 | ||
879 | /* | |
880 | * if ret == 0 means we found what we were searching for, which | |
881 | * is weird, but possible, so only screw with path if we didnt | |
882 | * find the key and see if we have stuff that matches | |
883 | */ | |
884 | if (ret > 0) { | |
885 | if (path->slots[0] == 0) | |
886 | break; | |
887 | path->slots[0]--; | |
888 | } | |
889 | ||
890 | /* pull out the item */ | |
891 | leaf = path->nodes[0]; | |
892 | item = btrfs_item_nr(leaf, path->slots[0]); | |
893 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
894 | ||
895 | /* make sure the item matches what we want */ | |
896 | if (found_key.objectid != BTRFS_ORPHAN_OBJECTID) | |
897 | break; | |
898 | if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY) | |
899 | break; | |
900 | ||
901 | /* release the path since we're done with it */ | |
902 | btrfs_release_path(root, path); | |
903 | ||
904 | /* | |
905 | * this is where we are basically btrfs_lookup, without the | |
906 | * crossing root thing. we store the inode number in the | |
907 | * offset of the orphan item. | |
908 | */ | |
909 | inode = btrfs_iget_locked(root->inode->i_sb, | |
910 | found_key.offset, root); | |
911 | if (!inode) | |
912 | break; | |
913 | ||
914 | if (inode->i_state & I_NEW) { | |
915 | BTRFS_I(inode)->root = root; | |
916 | ||
917 | /* have to set the location manually */ | |
918 | BTRFS_I(inode)->location.objectid = inode->i_ino; | |
919 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
920 | BTRFS_I(inode)->location.offset = 0; | |
921 | ||
922 | btrfs_read_locked_inode(inode); | |
923 | unlock_new_inode(inode); | |
924 | } | |
925 | ||
926 | /* | |
927 | * add this inode to the orphan list so btrfs_orphan_del does | |
928 | * the proper thing when we hit it | |
929 | */ | |
bcc63abb | 930 | spin_lock(&root->list_lock); |
7b128766 | 931 | list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list); |
bcc63abb | 932 | spin_unlock(&root->list_lock); |
7b128766 JB |
933 | |
934 | /* | |
935 | * if this is a bad inode, means we actually succeeded in | |
936 | * removing the inode, but not the orphan record, which means | |
937 | * we need to manually delete the orphan since iput will just | |
938 | * do a destroy_inode | |
939 | */ | |
940 | if (is_bad_inode(inode)) { | |
941 | btrfs_orphan_del(trans, inode); | |
942 | iput(inode); | |
943 | continue; | |
944 | } | |
945 | ||
946 | /* if we have links, this was a truncate, lets do that */ | |
947 | if (inode->i_nlink) { | |
948 | nr_truncate++; | |
949 | btrfs_truncate(inode); | |
950 | } else { | |
951 | nr_unlink++; | |
952 | } | |
953 | ||
954 | /* this will do delete_inode and everything for us */ | |
955 | iput(inode); | |
956 | } | |
957 | ||
958 | if (nr_unlink) | |
959 | printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink); | |
960 | if (nr_truncate) | |
961 | printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate); | |
962 | ||
963 | btrfs_free_path(path); | |
964 | btrfs_end_transaction(trans, root); | |
965 | } | |
966 | ||
39279cc3 CM |
967 | void btrfs_read_locked_inode(struct inode *inode) |
968 | { | |
969 | struct btrfs_path *path; | |
5f39d397 | 970 | struct extent_buffer *leaf; |
39279cc3 | 971 | struct btrfs_inode_item *inode_item; |
0b86a832 | 972 | struct btrfs_timespec *tspec; |
39279cc3 CM |
973 | struct btrfs_root *root = BTRFS_I(inode)->root; |
974 | struct btrfs_key location; | |
975 | u64 alloc_group_block; | |
618e21d5 | 976 | u32 rdev; |
39279cc3 CM |
977 | int ret; |
978 | ||
979 | path = btrfs_alloc_path(); | |
980 | BUG_ON(!path); | |
39279cc3 | 981 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
dc17ff8f | 982 | |
39279cc3 | 983 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
5f39d397 | 984 | if (ret) |
39279cc3 | 985 | goto make_bad; |
39279cc3 | 986 | |
5f39d397 CM |
987 | leaf = path->nodes[0]; |
988 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
989 | struct btrfs_inode_item); | |
990 | ||
991 | inode->i_mode = btrfs_inode_mode(leaf, inode_item); | |
992 | inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); | |
993 | inode->i_uid = btrfs_inode_uid(leaf, inode_item); | |
994 | inode->i_gid = btrfs_inode_gid(leaf, inode_item); | |
dbe674a9 | 995 | btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item)); |
5f39d397 CM |
996 | |
997 | tspec = btrfs_inode_atime(inode_item); | |
998 | inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
999 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
1000 | ||
1001 | tspec = btrfs_inode_mtime(inode_item); | |
1002 | inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
1003 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
1004 | ||
1005 | tspec = btrfs_inode_ctime(inode_item); | |
1006 | inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec); | |
1007 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec); | |
1008 | ||
1009 | inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item); | |
1010 | inode->i_generation = btrfs_inode_generation(leaf, inode_item); | |
618e21d5 | 1011 | inode->i_rdev = 0; |
5f39d397 CM |
1012 | rdev = btrfs_inode_rdev(leaf, inode_item); |
1013 | ||
aec7477b JB |
1014 | BTRFS_I(inode)->index_cnt = (u64)-1; |
1015 | ||
5f39d397 | 1016 | alloc_group_block = btrfs_inode_block_group(leaf, inode_item); |
39279cc3 CM |
1017 | BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info, |
1018 | alloc_group_block); | |
b98b6767 | 1019 | BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); |
e52ec0eb CM |
1020 | if (!BTRFS_I(inode)->block_group) { |
1021 | BTRFS_I(inode)->block_group = btrfs_find_block_group(root, | |
0b86a832 CM |
1022 | NULL, 0, |
1023 | BTRFS_BLOCK_GROUP_METADATA, 0); | |
e52ec0eb | 1024 | } |
39279cc3 CM |
1025 | btrfs_free_path(path); |
1026 | inode_item = NULL; | |
1027 | ||
39279cc3 | 1028 | switch (inode->i_mode & S_IFMT) { |
39279cc3 CM |
1029 | case S_IFREG: |
1030 | inode->i_mapping->a_ops = &btrfs_aops; | |
04160088 | 1031 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
d1310b2e | 1032 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
39279cc3 CM |
1033 | inode->i_fop = &btrfs_file_operations; |
1034 | inode->i_op = &btrfs_file_inode_operations; | |
1035 | break; | |
1036 | case S_IFDIR: | |
1037 | inode->i_fop = &btrfs_dir_file_operations; | |
1038 | if (root == root->fs_info->tree_root) | |
1039 | inode->i_op = &btrfs_dir_ro_inode_operations; | |
1040 | else | |
1041 | inode->i_op = &btrfs_dir_inode_operations; | |
1042 | break; | |
1043 | case S_IFLNK: | |
1044 | inode->i_op = &btrfs_symlink_inode_operations; | |
1045 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
04160088 | 1046 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
39279cc3 | 1047 | break; |
618e21d5 JB |
1048 | default: |
1049 | init_special_inode(inode, inode->i_mode, rdev); | |
1050 | break; | |
39279cc3 CM |
1051 | } |
1052 | return; | |
1053 | ||
1054 | make_bad: | |
39279cc3 | 1055 | btrfs_free_path(path); |
39279cc3 CM |
1056 | make_bad_inode(inode); |
1057 | } | |
1058 | ||
5f39d397 CM |
1059 | static void fill_inode_item(struct extent_buffer *leaf, |
1060 | struct btrfs_inode_item *item, | |
39279cc3 CM |
1061 | struct inode *inode) |
1062 | { | |
5f39d397 CM |
1063 | btrfs_set_inode_uid(leaf, item, inode->i_uid); |
1064 | btrfs_set_inode_gid(leaf, item, inode->i_gid); | |
dbe674a9 | 1065 | btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size); |
5f39d397 CM |
1066 | btrfs_set_inode_mode(leaf, item, inode->i_mode); |
1067 | btrfs_set_inode_nlink(leaf, item, inode->i_nlink); | |
1068 | ||
1069 | btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item), | |
1070 | inode->i_atime.tv_sec); | |
1071 | btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item), | |
1072 | inode->i_atime.tv_nsec); | |
1073 | ||
1074 | btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item), | |
1075 | inode->i_mtime.tv_sec); | |
1076 | btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item), | |
1077 | inode->i_mtime.tv_nsec); | |
1078 | ||
1079 | btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item), | |
1080 | inode->i_ctime.tv_sec); | |
1081 | btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item), | |
1082 | inode->i_ctime.tv_nsec); | |
1083 | ||
1084 | btrfs_set_inode_nblocks(leaf, item, inode->i_blocks); | |
1085 | btrfs_set_inode_generation(leaf, item, inode->i_generation); | |
1086 | btrfs_set_inode_rdev(leaf, item, inode->i_rdev); | |
b98b6767 | 1087 | btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); |
5f39d397 | 1088 | btrfs_set_inode_block_group(leaf, item, |
39279cc3 CM |
1089 | BTRFS_I(inode)->block_group->key.objectid); |
1090 | } | |
1091 | ||
ba1da2f4 | 1092 | int noinline btrfs_update_inode(struct btrfs_trans_handle *trans, |
39279cc3 CM |
1093 | struct btrfs_root *root, |
1094 | struct inode *inode) | |
1095 | { | |
1096 | struct btrfs_inode_item *inode_item; | |
1097 | struct btrfs_path *path; | |
5f39d397 | 1098 | struct extent_buffer *leaf; |
39279cc3 CM |
1099 | int ret; |
1100 | ||
1101 | path = btrfs_alloc_path(); | |
1102 | BUG_ON(!path); | |
39279cc3 CM |
1103 | ret = btrfs_lookup_inode(trans, root, path, |
1104 | &BTRFS_I(inode)->location, 1); | |
1105 | if (ret) { | |
1106 | if (ret > 0) | |
1107 | ret = -ENOENT; | |
1108 | goto failed; | |
1109 | } | |
1110 | ||
5f39d397 CM |
1111 | leaf = path->nodes[0]; |
1112 | inode_item = btrfs_item_ptr(leaf, path->slots[0], | |
39279cc3 CM |
1113 | struct btrfs_inode_item); |
1114 | ||
5f39d397 CM |
1115 | fill_inode_item(leaf, inode_item, inode); |
1116 | btrfs_mark_buffer_dirty(leaf); | |
15ee9bc7 | 1117 | btrfs_set_inode_last_trans(trans, inode); |
39279cc3 CM |
1118 | ret = 0; |
1119 | failed: | |
39279cc3 CM |
1120 | btrfs_free_path(path); |
1121 | return ret; | |
1122 | } | |
1123 | ||
1124 | ||
1125 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, | |
1126 | struct btrfs_root *root, | |
1127 | struct inode *dir, | |
1128 | struct dentry *dentry) | |
1129 | { | |
1130 | struct btrfs_path *path; | |
1131 | const char *name = dentry->d_name.name; | |
1132 | int name_len = dentry->d_name.len; | |
1133 | int ret = 0; | |
5f39d397 | 1134 | struct extent_buffer *leaf; |
39279cc3 | 1135 | struct btrfs_dir_item *di; |
5f39d397 | 1136 | struct btrfs_key key; |
aec7477b | 1137 | u64 index; |
39279cc3 CM |
1138 | |
1139 | path = btrfs_alloc_path(); | |
54aa1f4d CM |
1140 | if (!path) { |
1141 | ret = -ENOMEM; | |
1142 | goto err; | |
1143 | } | |
1144 | ||
39279cc3 CM |
1145 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
1146 | name, name_len, -1); | |
1147 | if (IS_ERR(di)) { | |
1148 | ret = PTR_ERR(di); | |
1149 | goto err; | |
1150 | } | |
1151 | if (!di) { | |
1152 | ret = -ENOENT; | |
1153 | goto err; | |
1154 | } | |
5f39d397 CM |
1155 | leaf = path->nodes[0]; |
1156 | btrfs_dir_item_key_to_cpu(leaf, di, &key); | |
39279cc3 | 1157 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
54aa1f4d CM |
1158 | if (ret) |
1159 | goto err; | |
39279cc3 CM |
1160 | btrfs_release_path(root, path); |
1161 | ||
aec7477b JB |
1162 | ret = btrfs_del_inode_ref(trans, root, name, name_len, |
1163 | dentry->d_inode->i_ino, | |
1164 | dentry->d_parent->d_inode->i_ino, &index); | |
1165 | if (ret) { | |
1166 | printk("failed to delete reference to %.*s, " | |
1167 | "inode %lu parent %lu\n", name_len, name, | |
1168 | dentry->d_inode->i_ino, | |
1169 | dentry->d_parent->d_inode->i_ino); | |
1170 | goto err; | |
1171 | } | |
1172 | ||
39279cc3 | 1173 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
aec7477b | 1174 | index, name, name_len, -1); |
39279cc3 CM |
1175 | if (IS_ERR(di)) { |
1176 | ret = PTR_ERR(di); | |
1177 | goto err; | |
1178 | } | |
1179 | if (!di) { | |
1180 | ret = -ENOENT; | |
1181 | goto err; | |
1182 | } | |
1183 | ret = btrfs_delete_one_dir_name(trans, root, path, di); | |
925baedd | 1184 | btrfs_release_path(root, path); |
39279cc3 CM |
1185 | |
1186 | dentry->d_inode->i_ctime = dir->i_ctime; | |
1187 | err: | |
1188 | btrfs_free_path(path); | |
1189 | if (!ret) { | |
dbe674a9 | 1190 | btrfs_i_size_write(dir, dir->i_size - name_len * 2); |
79c44584 | 1191 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
39279cc3 | 1192 | btrfs_update_inode(trans, root, dir); |
6da6abae CM |
1193 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
1194 | dentry->d_inode->i_nlink--; | |
1195 | #else | |
39279cc3 | 1196 | drop_nlink(dentry->d_inode); |
6da6abae | 1197 | #endif |
54aa1f4d | 1198 | ret = btrfs_update_inode(trans, root, dentry->d_inode); |
39279cc3 CM |
1199 | dir->i_sb->s_dirt = 1; |
1200 | } | |
1201 | return ret; | |
1202 | } | |
1203 | ||
1204 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) | |
1205 | { | |
1206 | struct btrfs_root *root; | |
1207 | struct btrfs_trans_handle *trans; | |
7b128766 | 1208 | struct inode *inode = dentry->d_inode; |
39279cc3 | 1209 | int ret; |
1832a6d5 | 1210 | unsigned long nr = 0; |
39279cc3 CM |
1211 | |
1212 | root = BTRFS_I(dir)->root; | |
1832a6d5 CM |
1213 | |
1214 | ret = btrfs_check_free_space(root, 1, 1); | |
1215 | if (ret) | |
1216 | goto fail; | |
1217 | ||
39279cc3 | 1218 | trans = btrfs_start_transaction(root, 1); |
5f39d397 | 1219 | |
39279cc3 CM |
1220 | btrfs_set_trans_block_group(trans, dir); |
1221 | ret = btrfs_unlink_trans(trans, root, dir, dentry); | |
7b128766 JB |
1222 | |
1223 | if (inode->i_nlink == 0) | |
1224 | ret = btrfs_orphan_add(trans, inode); | |
1225 | ||
d3c2fdcf | 1226 | nr = trans->blocks_used; |
5f39d397 | 1227 | |
89ce8a63 | 1228 | btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 1229 | fail: |
d3c2fdcf | 1230 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
1231 | return ret; |
1232 | } | |
1233 | ||
1234 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) | |
1235 | { | |
1236 | struct inode *inode = dentry->d_inode; | |
1832a6d5 | 1237 | int err = 0; |
39279cc3 CM |
1238 | int ret; |
1239 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
39279cc3 | 1240 | struct btrfs_trans_handle *trans; |
1832a6d5 | 1241 | unsigned long nr = 0; |
39279cc3 | 1242 | |
925baedd | 1243 | if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) { |
134d4512 | 1244 | return -ENOTEMPTY; |
925baedd | 1245 | } |
134d4512 | 1246 | |
1832a6d5 CM |
1247 | ret = btrfs_check_free_space(root, 1, 1); |
1248 | if (ret) | |
1249 | goto fail; | |
1250 | ||
39279cc3 CM |
1251 | trans = btrfs_start_transaction(root, 1); |
1252 | btrfs_set_trans_block_group(trans, dir); | |
39279cc3 | 1253 | |
7b128766 JB |
1254 | err = btrfs_orphan_add(trans, inode); |
1255 | if (err) | |
1256 | goto fail_trans; | |
1257 | ||
39279cc3 CM |
1258 | /* now the directory is empty */ |
1259 | err = btrfs_unlink_trans(trans, root, dir, dentry); | |
1260 | if (!err) { | |
dbe674a9 | 1261 | btrfs_i_size_write(inode, 0); |
39279cc3 | 1262 | } |
3954401f | 1263 | |
7b128766 | 1264 | fail_trans: |
d3c2fdcf | 1265 | nr = trans->blocks_used; |
89ce8a63 | 1266 | ret = btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 1267 | fail: |
d3c2fdcf | 1268 | btrfs_btree_balance_dirty(root, nr); |
3954401f | 1269 | |
39279cc3 CM |
1270 | if (ret && !err) |
1271 | err = ret; | |
1272 | return err; | |
1273 | } | |
1274 | ||
39279cc3 CM |
1275 | /* |
1276 | * this can truncate away extent items, csum items and directory items. | |
1277 | * It starts at a high offset and removes keys until it can't find | |
1278 | * any higher than i_size. | |
1279 | * | |
1280 | * csum items that cross the new i_size are truncated to the new size | |
1281 | * as well. | |
7b128766 JB |
1282 | * |
1283 | * min_type is the minimum key type to truncate down to. If set to 0, this | |
1284 | * will kill all the items on this inode, including the INODE_ITEM_KEY. | |
39279cc3 CM |
1285 | */ |
1286 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, | |
1287 | struct btrfs_root *root, | |
85e21bac CM |
1288 | struct inode *inode, |
1289 | u32 min_type) | |
39279cc3 CM |
1290 | { |
1291 | int ret; | |
1292 | struct btrfs_path *path; | |
1293 | struct btrfs_key key; | |
5f39d397 | 1294 | struct btrfs_key found_key; |
39279cc3 | 1295 | u32 found_type; |
5f39d397 | 1296 | struct extent_buffer *leaf; |
39279cc3 CM |
1297 | struct btrfs_file_extent_item *fi; |
1298 | u64 extent_start = 0; | |
db94535d | 1299 | u64 extent_num_bytes = 0; |
39279cc3 | 1300 | u64 item_end = 0; |
7bb86316 | 1301 | u64 root_gen = 0; |
d8d5f3e1 | 1302 | u64 root_owner = 0; |
39279cc3 CM |
1303 | int found_extent; |
1304 | int del_item; | |
85e21bac CM |
1305 | int pending_del_nr = 0; |
1306 | int pending_del_slot = 0; | |
179e29e4 | 1307 | int extent_type = -1; |
3b951516 | 1308 | u64 mask = root->sectorsize - 1; |
39279cc3 | 1309 | |
3b951516 | 1310 | btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1); |
39279cc3 | 1311 | path = btrfs_alloc_path(); |
3c69faec | 1312 | path->reada = -1; |
39279cc3 | 1313 | BUG_ON(!path); |
5f39d397 | 1314 | |
39279cc3 CM |
1315 | /* FIXME, add redo link to tree so we don't leak on crash */ |
1316 | key.objectid = inode->i_ino; | |
1317 | key.offset = (u64)-1; | |
5f39d397 CM |
1318 | key.type = (u8)-1; |
1319 | ||
85e21bac CM |
1320 | btrfs_init_path(path); |
1321 | search_again: | |
1322 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); | |
1323 | if (ret < 0) { | |
1324 | goto error; | |
1325 | } | |
1326 | if (ret > 0) { | |
1327 | BUG_ON(path->slots[0] == 0); | |
1328 | path->slots[0]--; | |
1329 | } | |
1330 | ||
39279cc3 | 1331 | while(1) { |
39279cc3 | 1332 | fi = NULL; |
5f39d397 CM |
1333 | leaf = path->nodes[0]; |
1334 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
1335 | found_type = btrfs_key_type(&found_key); | |
39279cc3 | 1336 | |
5f39d397 | 1337 | if (found_key.objectid != inode->i_ino) |
39279cc3 | 1338 | break; |
5f39d397 | 1339 | |
85e21bac | 1340 | if (found_type < min_type) |
39279cc3 CM |
1341 | break; |
1342 | ||
5f39d397 | 1343 | item_end = found_key.offset; |
39279cc3 | 1344 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
5f39d397 | 1345 | fi = btrfs_item_ptr(leaf, path->slots[0], |
39279cc3 | 1346 | struct btrfs_file_extent_item); |
179e29e4 CM |
1347 | extent_type = btrfs_file_extent_type(leaf, fi); |
1348 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
5f39d397 | 1349 | item_end += |
db94535d | 1350 | btrfs_file_extent_num_bytes(leaf, fi); |
179e29e4 CM |
1351 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
1352 | struct btrfs_item *item = btrfs_item_nr(leaf, | |
1353 | path->slots[0]); | |
1354 | item_end += btrfs_file_extent_inline_len(leaf, | |
1355 | item); | |
39279cc3 | 1356 | } |
008630c1 | 1357 | item_end--; |
39279cc3 CM |
1358 | } |
1359 | if (found_type == BTRFS_CSUM_ITEM_KEY) { | |
1360 | ret = btrfs_csum_truncate(trans, root, path, | |
1361 | inode->i_size); | |
1362 | BUG_ON(ret); | |
1363 | } | |
008630c1 | 1364 | if (item_end < inode->i_size) { |
b888db2b CM |
1365 | if (found_type == BTRFS_DIR_ITEM_KEY) { |
1366 | found_type = BTRFS_INODE_ITEM_KEY; | |
1367 | } else if (found_type == BTRFS_EXTENT_ITEM_KEY) { | |
1368 | found_type = BTRFS_CSUM_ITEM_KEY; | |
85e21bac CM |
1369 | } else if (found_type == BTRFS_EXTENT_DATA_KEY) { |
1370 | found_type = BTRFS_XATTR_ITEM_KEY; | |
1371 | } else if (found_type == BTRFS_XATTR_ITEM_KEY) { | |
1372 | found_type = BTRFS_INODE_REF_KEY; | |
b888db2b CM |
1373 | } else if (found_type) { |
1374 | found_type--; | |
1375 | } else { | |
1376 | break; | |
39279cc3 | 1377 | } |
a61721d5 | 1378 | btrfs_set_key_type(&key, found_type); |
85e21bac | 1379 | goto next; |
39279cc3 | 1380 | } |
5f39d397 | 1381 | if (found_key.offset >= inode->i_size) |
39279cc3 CM |
1382 | del_item = 1; |
1383 | else | |
1384 | del_item = 0; | |
1385 | found_extent = 0; | |
1386 | ||
1387 | /* FIXME, shrink the extent if the ref count is only 1 */ | |
179e29e4 CM |
1388 | if (found_type != BTRFS_EXTENT_DATA_KEY) |
1389 | goto delete; | |
1390 | ||
1391 | if (extent_type != BTRFS_FILE_EXTENT_INLINE) { | |
39279cc3 | 1392 | u64 num_dec; |
db94535d | 1393 | extent_start = btrfs_file_extent_disk_bytenr(leaf, fi); |
39279cc3 | 1394 | if (!del_item) { |
db94535d CM |
1395 | u64 orig_num_bytes = |
1396 | btrfs_file_extent_num_bytes(leaf, fi); | |
1397 | extent_num_bytes = inode->i_size - | |
5f39d397 | 1398 | found_key.offset + root->sectorsize - 1; |
b1632b10 Y |
1399 | extent_num_bytes = extent_num_bytes & |
1400 | ~((u64)root->sectorsize - 1); | |
db94535d CM |
1401 | btrfs_set_file_extent_num_bytes(leaf, fi, |
1402 | extent_num_bytes); | |
1403 | num_dec = (orig_num_bytes - | |
9069218d CM |
1404 | extent_num_bytes); |
1405 | if (extent_start != 0) | |
1406 | dec_i_blocks(inode, num_dec); | |
5f39d397 | 1407 | btrfs_mark_buffer_dirty(leaf); |
39279cc3 | 1408 | } else { |
db94535d CM |
1409 | extent_num_bytes = |
1410 | btrfs_file_extent_disk_num_bytes(leaf, | |
1411 | fi); | |
39279cc3 | 1412 | /* FIXME blocksize != 4096 */ |
9069218d | 1413 | num_dec = btrfs_file_extent_num_bytes(leaf, fi); |
39279cc3 CM |
1414 | if (extent_start != 0) { |
1415 | found_extent = 1; | |
9069218d | 1416 | dec_i_blocks(inode, num_dec); |
39279cc3 | 1417 | } |
d8d5f3e1 CM |
1418 | root_gen = btrfs_header_generation(leaf); |
1419 | root_owner = btrfs_header_owner(leaf); | |
39279cc3 | 1420 | } |
9069218d CM |
1421 | } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) { |
1422 | if (!del_item) { | |
1423 | u32 newsize = inode->i_size - found_key.offset; | |
1424 | dec_i_blocks(inode, item_end + 1 - | |
1425 | found_key.offset - newsize); | |
1426 | newsize = | |
1427 | btrfs_file_extent_calc_inline_size(newsize); | |
1428 | ret = btrfs_truncate_item(trans, root, path, | |
1429 | newsize, 1); | |
1430 | BUG_ON(ret); | |
1431 | } else { | |
1432 | dec_i_blocks(inode, item_end + 1 - | |
1433 | found_key.offset); | |
1434 | } | |
39279cc3 | 1435 | } |
179e29e4 | 1436 | delete: |
39279cc3 | 1437 | if (del_item) { |
85e21bac CM |
1438 | if (!pending_del_nr) { |
1439 | /* no pending yet, add ourselves */ | |
1440 | pending_del_slot = path->slots[0]; | |
1441 | pending_del_nr = 1; | |
1442 | } else if (pending_del_nr && | |
1443 | path->slots[0] + 1 == pending_del_slot) { | |
1444 | /* hop on the pending chunk */ | |
1445 | pending_del_nr++; | |
1446 | pending_del_slot = path->slots[0]; | |
1447 | } else { | |
1448 | printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot); | |
1449 | } | |
39279cc3 CM |
1450 | } else { |
1451 | break; | |
1452 | } | |
39279cc3 CM |
1453 | if (found_extent) { |
1454 | ret = btrfs_free_extent(trans, root, extent_start, | |
7bb86316 | 1455 | extent_num_bytes, |
d8d5f3e1 | 1456 | root_owner, |
7bb86316 CM |
1457 | root_gen, inode->i_ino, |
1458 | found_key.offset, 0); | |
39279cc3 CM |
1459 | BUG_ON(ret); |
1460 | } | |
85e21bac CM |
1461 | next: |
1462 | if (path->slots[0] == 0) { | |
1463 | if (pending_del_nr) | |
1464 | goto del_pending; | |
1465 | btrfs_release_path(root, path); | |
1466 | goto search_again; | |
1467 | } | |
1468 | ||
1469 | path->slots[0]--; | |
1470 | if (pending_del_nr && | |
1471 | path->slots[0] + 1 != pending_del_slot) { | |
1472 | struct btrfs_key debug; | |
1473 | del_pending: | |
1474 | btrfs_item_key_to_cpu(path->nodes[0], &debug, | |
1475 | pending_del_slot); | |
1476 | ret = btrfs_del_items(trans, root, path, | |
1477 | pending_del_slot, | |
1478 | pending_del_nr); | |
1479 | BUG_ON(ret); | |
1480 | pending_del_nr = 0; | |
1481 | btrfs_release_path(root, path); | |
1482 | goto search_again; | |
1483 | } | |
39279cc3 CM |
1484 | } |
1485 | ret = 0; | |
1486 | error: | |
85e21bac CM |
1487 | if (pending_del_nr) { |
1488 | ret = btrfs_del_items(trans, root, path, pending_del_slot, | |
1489 | pending_del_nr); | |
1490 | } | |
39279cc3 CM |
1491 | btrfs_free_path(path); |
1492 | inode->i_sb->s_dirt = 1; | |
1493 | return ret; | |
1494 | } | |
1495 | ||
1496 | /* | |
1497 | * taken from block_truncate_page, but does cow as it zeros out | |
1498 | * any bytes left in the last page in the file. | |
1499 | */ | |
1500 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) | |
1501 | { | |
1502 | struct inode *inode = mapping->host; | |
db94535d | 1503 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
1504 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
1505 | struct btrfs_ordered_extent *ordered; | |
1506 | char *kaddr; | |
db94535d | 1507 | u32 blocksize = root->sectorsize; |
39279cc3 CM |
1508 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
1509 | unsigned offset = from & (PAGE_CACHE_SIZE-1); | |
1510 | struct page *page; | |
39279cc3 | 1511 | int ret = 0; |
a52d9a80 | 1512 | u64 page_start; |
e6dcd2dc | 1513 | u64 page_end; |
39279cc3 CM |
1514 | |
1515 | if ((offset & (blocksize - 1)) == 0) | |
1516 | goto out; | |
1517 | ||
1518 | ret = -ENOMEM; | |
211c17f5 | 1519 | again: |
39279cc3 CM |
1520 | page = grab_cache_page(mapping, index); |
1521 | if (!page) | |
1522 | goto out; | |
e6dcd2dc CM |
1523 | |
1524 | page_start = page_offset(page); | |
1525 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
1526 | ||
39279cc3 | 1527 | if (!PageUptodate(page)) { |
9ebefb18 | 1528 | ret = btrfs_readpage(NULL, page); |
39279cc3 | 1529 | lock_page(page); |
211c17f5 CM |
1530 | if (page->mapping != mapping) { |
1531 | unlock_page(page); | |
1532 | page_cache_release(page); | |
1533 | goto again; | |
1534 | } | |
39279cc3 CM |
1535 | if (!PageUptodate(page)) { |
1536 | ret = -EIO; | |
89642229 | 1537 | goto out_unlock; |
39279cc3 CM |
1538 | } |
1539 | } | |
211c17f5 | 1540 | wait_on_page_writeback(page); |
e6dcd2dc CM |
1541 | |
1542 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
1543 | set_page_extent_mapped(page); | |
1544 | ||
1545 | ordered = btrfs_lookup_ordered_extent(inode, page_start); | |
1546 | if (ordered) { | |
1547 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
1548 | unlock_page(page); | |
1549 | page_cache_release(page); | |
eb84ae03 | 1550 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
1551 | btrfs_put_ordered_extent(ordered); |
1552 | goto again; | |
1553 | } | |
1554 | ||
ea8c2819 | 1555 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
e6dcd2dc CM |
1556 | ret = 0; |
1557 | if (offset != PAGE_CACHE_SIZE) { | |
1558 | kaddr = kmap(page); | |
1559 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); | |
1560 | flush_dcache_page(page); | |
1561 | kunmap(page); | |
1562 | } | |
247e743c | 1563 | ClearPageChecked(page); |
e6dcd2dc CM |
1564 | set_page_dirty(page); |
1565 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
39279cc3 | 1566 | |
89642229 | 1567 | out_unlock: |
39279cc3 CM |
1568 | unlock_page(page); |
1569 | page_cache_release(page); | |
1570 | out: | |
1571 | return ret; | |
1572 | } | |
1573 | ||
1574 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) | |
1575 | { | |
1576 | struct inode *inode = dentry->d_inode; | |
1577 | int err; | |
1578 | ||
1579 | err = inode_change_ok(inode, attr); | |
1580 | if (err) | |
1581 | return err; | |
1582 | ||
1583 | if (S_ISREG(inode->i_mode) && | |
1584 | attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) { | |
1585 | struct btrfs_trans_handle *trans; | |
1586 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d1310b2e | 1587 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
2bf5a725 | 1588 | |
5f39d397 | 1589 | u64 mask = root->sectorsize - 1; |
1b0f7c29 | 1590 | u64 hole_start = (inode->i_size + mask) & ~mask; |
f392a938 | 1591 | u64 block_end = (attr->ia_size + mask) & ~mask; |
39279cc3 | 1592 | u64 hole_size; |
179e29e4 | 1593 | u64 alloc_hint = 0; |
39279cc3 | 1594 | |
1b0f7c29 | 1595 | if (attr->ia_size <= hole_start) |
39279cc3 CM |
1596 | goto out; |
1597 | ||
1832a6d5 | 1598 | err = btrfs_check_free_space(root, 1, 0); |
1832a6d5 CM |
1599 | if (err) |
1600 | goto fail; | |
1601 | ||
39279cc3 CM |
1602 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
1603 | ||
5f56406a | 1604 | hole_size = block_end - hole_start; |
7c2fe32a CM |
1605 | while(1) { |
1606 | struct btrfs_ordered_extent *ordered; | |
1607 | btrfs_wait_ordered_range(inode, hole_start, hole_size); | |
1608 | ||
1609 | lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); | |
1610 | ordered = btrfs_lookup_ordered_extent(inode, hole_start); | |
1611 | if (ordered) { | |
1612 | unlock_extent(io_tree, hole_start, | |
1613 | block_end - 1, GFP_NOFS); | |
1614 | btrfs_put_ordered_extent(ordered); | |
1615 | } else { | |
1616 | break; | |
1617 | } | |
1618 | } | |
39279cc3 | 1619 | |
39279cc3 CM |
1620 | trans = btrfs_start_transaction(root, 1); |
1621 | btrfs_set_trans_block_group(trans, inode); | |
ee6e6504 | 1622 | mutex_lock(&BTRFS_I(inode)->extent_mutex); |
2bf5a725 | 1623 | err = btrfs_drop_extents(trans, root, inode, |
1b0f7c29 | 1624 | hole_start, block_end, hole_start, |
3326d1b0 | 1625 | &alloc_hint); |
2bf5a725 | 1626 | |
179e29e4 CM |
1627 | if (alloc_hint != EXTENT_MAP_INLINE) { |
1628 | err = btrfs_insert_file_extent(trans, root, | |
1629 | inode->i_ino, | |
5f56406a | 1630 | hole_start, 0, 0, |
f2eb0a24 | 1631 | hole_size, 0); |
d1310b2e | 1632 | btrfs_drop_extent_cache(inode, hole_start, |
3b951516 | 1633 | (u64)-1); |
5f56406a | 1634 | btrfs_check_file(root, inode); |
179e29e4 | 1635 | } |
ee6e6504 | 1636 | mutex_unlock(&BTRFS_I(inode)->extent_mutex); |
39279cc3 | 1637 | btrfs_end_transaction(trans, root); |
1b0f7c29 | 1638 | unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); |
54aa1f4d CM |
1639 | if (err) |
1640 | return err; | |
39279cc3 CM |
1641 | } |
1642 | out: | |
1643 | err = inode_setattr(inode, attr); | |
33268eaf JB |
1644 | |
1645 | if (!err && ((attr->ia_valid & ATTR_MODE))) | |
1646 | err = btrfs_acl_chmod(inode); | |
1832a6d5 | 1647 | fail: |
39279cc3 CM |
1648 | return err; |
1649 | } | |
61295eb8 | 1650 | |
39279cc3 CM |
1651 | void btrfs_delete_inode(struct inode *inode) |
1652 | { | |
1653 | struct btrfs_trans_handle *trans; | |
1654 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
d3c2fdcf | 1655 | unsigned long nr; |
39279cc3 CM |
1656 | int ret; |
1657 | ||
1658 | truncate_inode_pages(&inode->i_data, 0); | |
1659 | if (is_bad_inode(inode)) { | |
7b128766 | 1660 | btrfs_orphan_del(NULL, inode); |
39279cc3 CM |
1661 | goto no_delete; |
1662 | } | |
4a096752 | 1663 | btrfs_wait_ordered_range(inode, 0, (u64)-1); |
5f39d397 | 1664 | |
dbe674a9 | 1665 | btrfs_i_size_write(inode, 0); |
39279cc3 | 1666 | trans = btrfs_start_transaction(root, 1); |
5f39d397 | 1667 | |
39279cc3 | 1668 | btrfs_set_trans_block_group(trans, inode); |
85e21bac | 1669 | ret = btrfs_truncate_in_trans(trans, root, inode, 0); |
7b128766 JB |
1670 | if (ret) { |
1671 | btrfs_orphan_del(NULL, inode); | |
54aa1f4d | 1672 | goto no_delete_lock; |
7b128766 JB |
1673 | } |
1674 | ||
1675 | btrfs_orphan_del(trans, inode); | |
85e21bac | 1676 | |
d3c2fdcf | 1677 | nr = trans->blocks_used; |
85e21bac | 1678 | clear_inode(inode); |
5f39d397 | 1679 | |
39279cc3 | 1680 | btrfs_end_transaction(trans, root); |
d3c2fdcf | 1681 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 | 1682 | return; |
54aa1f4d CM |
1683 | |
1684 | no_delete_lock: | |
d3c2fdcf | 1685 | nr = trans->blocks_used; |
54aa1f4d | 1686 | btrfs_end_transaction(trans, root); |
d3c2fdcf | 1687 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
1688 | no_delete: |
1689 | clear_inode(inode); | |
1690 | } | |
1691 | ||
1692 | /* | |
1693 | * this returns the key found in the dir entry in the location pointer. | |
1694 | * If no dir entries were found, location->objectid is 0. | |
1695 | */ | |
1696 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, | |
1697 | struct btrfs_key *location) | |
1698 | { | |
1699 | const char *name = dentry->d_name.name; | |
1700 | int namelen = dentry->d_name.len; | |
1701 | struct btrfs_dir_item *di; | |
1702 | struct btrfs_path *path; | |
1703 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
0d9f7f3e | 1704 | int ret = 0; |
39279cc3 CM |
1705 | |
1706 | path = btrfs_alloc_path(); | |
1707 | BUG_ON(!path); | |
3954401f | 1708 | |
39279cc3 CM |
1709 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
1710 | namelen, 0); | |
0d9f7f3e Y |
1711 | if (IS_ERR(di)) |
1712 | ret = PTR_ERR(di); | |
39279cc3 | 1713 | if (!di || IS_ERR(di)) { |
3954401f | 1714 | goto out_err; |
39279cc3 | 1715 | } |
5f39d397 | 1716 | btrfs_dir_item_key_to_cpu(path->nodes[0], di, location); |
39279cc3 | 1717 | out: |
39279cc3 CM |
1718 | btrfs_free_path(path); |
1719 | return ret; | |
3954401f CM |
1720 | out_err: |
1721 | location->objectid = 0; | |
1722 | goto out; | |
39279cc3 CM |
1723 | } |
1724 | ||
1725 | /* | |
1726 | * when we hit a tree root in a directory, the btrfs part of the inode | |
1727 | * needs to be changed to reflect the root directory of the tree root. This | |
1728 | * is kind of like crossing a mount point. | |
1729 | */ | |
1730 | static int fixup_tree_root_location(struct btrfs_root *root, | |
1731 | struct btrfs_key *location, | |
58176a96 JB |
1732 | struct btrfs_root **sub_root, |
1733 | struct dentry *dentry) | |
39279cc3 | 1734 | { |
39279cc3 CM |
1735 | struct btrfs_root_item *ri; |
1736 | ||
1737 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) | |
1738 | return 0; | |
1739 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) | |
1740 | return 0; | |
1741 | ||
58176a96 JB |
1742 | *sub_root = btrfs_read_fs_root(root->fs_info, location, |
1743 | dentry->d_name.name, | |
1744 | dentry->d_name.len); | |
39279cc3 CM |
1745 | if (IS_ERR(*sub_root)) |
1746 | return PTR_ERR(*sub_root); | |
1747 | ||
1748 | ri = &(*sub_root)->root_item; | |
1749 | location->objectid = btrfs_root_dirid(ri); | |
39279cc3 CM |
1750 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
1751 | location->offset = 0; | |
1752 | ||
39279cc3 CM |
1753 | return 0; |
1754 | } | |
1755 | ||
1756 | static int btrfs_init_locked_inode(struct inode *inode, void *p) | |
1757 | { | |
1758 | struct btrfs_iget_args *args = p; | |
1759 | inode->i_ino = args->ino; | |
1760 | BTRFS_I(inode)->root = args->root; | |
9069218d | 1761 | BTRFS_I(inode)->delalloc_bytes = 0; |
db69e0eb | 1762 | inode->i_mapping->writeback_index = 0; |
dbe674a9 | 1763 | BTRFS_I(inode)->disk_i_size = 0; |
aec7477b | 1764 | BTRFS_I(inode)->index_cnt = (u64)-1; |
d1310b2e CM |
1765 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
1766 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | |
b888db2b | 1767 | inode->i_mapping, GFP_NOFS); |
7e38326f CM |
1768 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
1769 | inode->i_mapping, GFP_NOFS); | |
ea8c2819 | 1770 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); |
ba1da2f4 | 1771 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); |
1b1e2135 | 1772 | mutex_init(&BTRFS_I(inode)->csum_mutex); |
ee6e6504 | 1773 | mutex_init(&BTRFS_I(inode)->extent_mutex); |
39279cc3 CM |
1774 | return 0; |
1775 | } | |
1776 | ||
1777 | static int btrfs_find_actor(struct inode *inode, void *opaque) | |
1778 | { | |
1779 | struct btrfs_iget_args *args = opaque; | |
1780 | return (args->ino == inode->i_ino && | |
1781 | args->root == BTRFS_I(inode)->root); | |
1782 | } | |
1783 | ||
dc17ff8f CM |
1784 | struct inode *btrfs_ilookup(struct super_block *s, u64 objectid, |
1785 | u64 root_objectid) | |
1786 | { | |
1787 | struct btrfs_iget_args args; | |
1788 | args.ino = objectid; | |
1789 | args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid); | |
1790 | ||
1791 | if (!args.root) | |
1792 | return NULL; | |
1793 | ||
1794 | return ilookup5(s, objectid, btrfs_find_actor, (void *)&args); | |
1795 | } | |
1796 | ||
39279cc3 CM |
1797 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
1798 | struct btrfs_root *root) | |
1799 | { | |
1800 | struct inode *inode; | |
1801 | struct btrfs_iget_args args; | |
1802 | args.ino = objectid; | |
1803 | args.root = root; | |
1804 | ||
1805 | inode = iget5_locked(s, objectid, btrfs_find_actor, | |
1806 | btrfs_init_locked_inode, | |
1807 | (void *)&args); | |
1808 | return inode; | |
1809 | } | |
1810 | ||
1a54ef8c BR |
1811 | /* Get an inode object given its location and corresponding root. |
1812 | * Returns in *is_new if the inode was read from disk | |
1813 | */ | |
1814 | struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location, | |
1815 | struct btrfs_root *root, int *is_new) | |
1816 | { | |
1817 | struct inode *inode; | |
1818 | ||
1819 | inode = btrfs_iget_locked(s, location->objectid, root); | |
1820 | if (!inode) | |
1821 | return ERR_PTR(-EACCES); | |
1822 | ||
1823 | if (inode->i_state & I_NEW) { | |
1824 | BTRFS_I(inode)->root = root; | |
1825 | memcpy(&BTRFS_I(inode)->location, location, sizeof(*location)); | |
1826 | btrfs_read_locked_inode(inode); | |
1827 | unlock_new_inode(inode); | |
1828 | if (is_new) | |
1829 | *is_new = 1; | |
1830 | } else { | |
1831 | if (is_new) | |
1832 | *is_new = 0; | |
1833 | } | |
1834 | ||
1835 | return inode; | |
1836 | } | |
1837 | ||
39279cc3 CM |
1838 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
1839 | struct nameidata *nd) | |
1840 | { | |
1841 | struct inode * inode; | |
1842 | struct btrfs_inode *bi = BTRFS_I(dir); | |
1843 | struct btrfs_root *root = bi->root; | |
1844 | struct btrfs_root *sub_root = root; | |
1845 | struct btrfs_key location; | |
1a54ef8c | 1846 | int ret, new, do_orphan = 0; |
39279cc3 CM |
1847 | |
1848 | if (dentry->d_name.len > BTRFS_NAME_LEN) | |
1849 | return ERR_PTR(-ENAMETOOLONG); | |
5f39d397 | 1850 | |
39279cc3 | 1851 | ret = btrfs_inode_by_name(dir, dentry, &location); |
5f39d397 | 1852 | |
39279cc3 CM |
1853 | if (ret < 0) |
1854 | return ERR_PTR(ret); | |
5f39d397 | 1855 | |
39279cc3 CM |
1856 | inode = NULL; |
1857 | if (location.objectid) { | |
58176a96 JB |
1858 | ret = fixup_tree_root_location(root, &location, &sub_root, |
1859 | dentry); | |
39279cc3 CM |
1860 | if (ret < 0) |
1861 | return ERR_PTR(ret); | |
1862 | if (ret > 0) | |
1863 | return ERR_PTR(-ENOENT); | |
1a54ef8c BR |
1864 | inode = btrfs_iget(dir->i_sb, &location, sub_root, &new); |
1865 | if (IS_ERR(inode)) | |
1866 | return ERR_CAST(inode); | |
1867 | ||
1868 | /* the inode and parent dir are two different roots */ | |
1869 | if (new && root != sub_root) { | |
1870 | igrab(inode); | |
1871 | sub_root->inode = inode; | |
1872 | do_orphan = 1; | |
39279cc3 CM |
1873 | } |
1874 | } | |
7b128766 JB |
1875 | |
1876 | if (unlikely(do_orphan)) | |
1877 | btrfs_orphan_cleanup(sub_root); | |
1878 | ||
39279cc3 CM |
1879 | return d_splice_alias(inode, dentry); |
1880 | } | |
1881 | ||
39279cc3 CM |
1882 | static unsigned char btrfs_filetype_table[] = { |
1883 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK | |
1884 | }; | |
1885 | ||
cbdf5a24 DW |
1886 | static int btrfs_real_readdir(struct file *filp, void *dirent, |
1887 | filldir_t filldir) | |
39279cc3 | 1888 | { |
6da6abae | 1889 | struct inode *inode = filp->f_dentry->d_inode; |
39279cc3 CM |
1890 | struct btrfs_root *root = BTRFS_I(inode)->root; |
1891 | struct btrfs_item *item; | |
1892 | struct btrfs_dir_item *di; | |
1893 | struct btrfs_key key; | |
5f39d397 | 1894 | struct btrfs_key found_key; |
39279cc3 CM |
1895 | struct btrfs_path *path; |
1896 | int ret; | |
1897 | u32 nritems; | |
5f39d397 | 1898 | struct extent_buffer *leaf; |
39279cc3 CM |
1899 | int slot; |
1900 | int advance; | |
1901 | unsigned char d_type; | |
1902 | int over = 0; | |
1903 | u32 di_cur; | |
1904 | u32 di_total; | |
1905 | u32 di_len; | |
1906 | int key_type = BTRFS_DIR_INDEX_KEY; | |
5f39d397 CM |
1907 | char tmp_name[32]; |
1908 | char *name_ptr; | |
1909 | int name_len; | |
39279cc3 CM |
1910 | |
1911 | /* FIXME, use a real flag for deciding about the key type */ | |
1912 | if (root->fs_info->tree_root == root) | |
1913 | key_type = BTRFS_DIR_ITEM_KEY; | |
5f39d397 | 1914 | |
3954401f CM |
1915 | /* special case for "." */ |
1916 | if (filp->f_pos == 0) { | |
1917 | over = filldir(dirent, ".", 1, | |
1918 | 1, inode->i_ino, | |
1919 | DT_DIR); | |
1920 | if (over) | |
1921 | return 0; | |
1922 | filp->f_pos = 1; | |
1923 | } | |
3954401f CM |
1924 | /* special case for .., just use the back ref */ |
1925 | if (filp->f_pos == 1) { | |
5ecc7e5d | 1926 | u64 pino = parent_ino(filp->f_path.dentry); |
3954401f | 1927 | over = filldir(dirent, "..", 2, |
5ecc7e5d | 1928 | 2, pino, DT_DIR); |
3954401f | 1929 | if (over) |
49593bfa | 1930 | return 0; |
3954401f CM |
1931 | filp->f_pos = 2; |
1932 | } | |
1933 | ||
49593bfa DW |
1934 | path = btrfs_alloc_path(); |
1935 | path->reada = 2; | |
1936 | ||
39279cc3 CM |
1937 | btrfs_set_key_type(&key, key_type); |
1938 | key.offset = filp->f_pos; | |
49593bfa | 1939 | key.objectid = inode->i_ino; |
5f39d397 | 1940 | |
39279cc3 CM |
1941 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
1942 | if (ret < 0) | |
1943 | goto err; | |
1944 | advance = 0; | |
49593bfa DW |
1945 | |
1946 | while (1) { | |
5f39d397 CM |
1947 | leaf = path->nodes[0]; |
1948 | nritems = btrfs_header_nritems(leaf); | |
39279cc3 CM |
1949 | slot = path->slots[0]; |
1950 | if (advance || slot >= nritems) { | |
49593bfa | 1951 | if (slot >= nritems - 1) { |
39279cc3 CM |
1952 | ret = btrfs_next_leaf(root, path); |
1953 | if (ret) | |
1954 | break; | |
5f39d397 CM |
1955 | leaf = path->nodes[0]; |
1956 | nritems = btrfs_header_nritems(leaf); | |
39279cc3 CM |
1957 | slot = path->slots[0]; |
1958 | } else { | |
1959 | slot++; | |
1960 | path->slots[0]++; | |
1961 | } | |
1962 | } | |
1963 | advance = 1; | |
5f39d397 CM |
1964 | item = btrfs_item_nr(leaf, slot); |
1965 | btrfs_item_key_to_cpu(leaf, &found_key, slot); | |
1966 | ||
1967 | if (found_key.objectid != key.objectid) | |
39279cc3 | 1968 | break; |
5f39d397 | 1969 | if (btrfs_key_type(&found_key) != key_type) |
39279cc3 | 1970 | break; |
5f39d397 | 1971 | if (found_key.offset < filp->f_pos) |
39279cc3 | 1972 | continue; |
5f39d397 CM |
1973 | |
1974 | filp->f_pos = found_key.offset; | |
49593bfa | 1975 | |
39279cc3 CM |
1976 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
1977 | di_cur = 0; | |
5f39d397 | 1978 | di_total = btrfs_item_size(leaf, item); |
49593bfa DW |
1979 | |
1980 | while (di_cur < di_total) { | |
5f39d397 CM |
1981 | struct btrfs_key location; |
1982 | ||
1983 | name_len = btrfs_dir_name_len(leaf, di); | |
49593bfa | 1984 | if (name_len <= sizeof(tmp_name)) { |
5f39d397 CM |
1985 | name_ptr = tmp_name; |
1986 | } else { | |
1987 | name_ptr = kmalloc(name_len, GFP_NOFS); | |
49593bfa DW |
1988 | if (!name_ptr) { |
1989 | ret = -ENOMEM; | |
1990 | goto err; | |
1991 | } | |
5f39d397 CM |
1992 | } |
1993 | read_extent_buffer(leaf, name_ptr, | |
1994 | (unsigned long)(di + 1), name_len); | |
1995 | ||
1996 | d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)]; | |
1997 | btrfs_dir_item_key_to_cpu(leaf, di, &location); | |
5f39d397 | 1998 | over = filldir(dirent, name_ptr, name_len, |
49593bfa | 1999 | found_key.offset, location.objectid, |
39279cc3 | 2000 | d_type); |
5f39d397 CM |
2001 | |
2002 | if (name_ptr != tmp_name) | |
2003 | kfree(name_ptr); | |
2004 | ||
39279cc3 CM |
2005 | if (over) |
2006 | goto nopos; | |
49593bfa | 2007 | |
5103e947 | 2008 | di_len = btrfs_dir_name_len(leaf, di) + |
49593bfa | 2009 | btrfs_dir_data_len(leaf, di) + sizeof(*di); |
39279cc3 CM |
2010 | di_cur += di_len; |
2011 | di = (struct btrfs_dir_item *)((char *)di + di_len); | |
2012 | } | |
2013 | } | |
49593bfa DW |
2014 | |
2015 | /* Reached end of directory/root. Bump pos past the last item. */ | |
5e591a07 YZ |
2016 | if (key_type == BTRFS_DIR_INDEX_KEY) |
2017 | filp->f_pos = INT_LIMIT(typeof(filp->f_pos)); | |
2018 | else | |
2019 | filp->f_pos++; | |
39279cc3 CM |
2020 | nopos: |
2021 | ret = 0; | |
2022 | err: | |
39279cc3 | 2023 | btrfs_free_path(path); |
39279cc3 CM |
2024 | return ret; |
2025 | } | |
2026 | ||
cbdf5a24 DW |
2027 | /* Kernels earlier than 2.6.28 still have the NFS deadlock where nfsd |
2028 | will call the file system's ->lookup() method from within its | |
2029 | filldir callback, which in turn was called from the file system's | |
2030 | ->readdir() method. And will deadlock for many file systems. */ | |
2031 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28) | |
2032 | ||
2033 | struct nfshack_dirent { | |
2034 | u64 ino; | |
2035 | loff_t offset; | |
2036 | int namlen; | |
2037 | unsigned int d_type; | |
2038 | char name[]; | |
2039 | }; | |
2040 | ||
2041 | struct nfshack_readdir { | |
2042 | char *dirent; | |
2043 | size_t used; | |
f2322b1c | 2044 | int full; |
cbdf5a24 DW |
2045 | }; |
2046 | ||
2047 | ||
2048 | ||
2049 | static int btrfs_nfshack_filldir(void *__buf, const char *name, int namlen, | |
2050 | loff_t offset, u64 ino, unsigned int d_type) | |
2051 | { | |
2052 | struct nfshack_readdir *buf = __buf; | |
2053 | struct nfshack_dirent *de = (void *)(buf->dirent + buf->used); | |
2054 | unsigned int reclen; | |
2055 | ||
2056 | reclen = ALIGN(sizeof(struct nfshack_dirent) + namlen, sizeof(u64)); | |
f2322b1c DW |
2057 | if (buf->used + reclen > PAGE_SIZE) { |
2058 | buf->full = 1; | |
cbdf5a24 | 2059 | return -EINVAL; |
f2322b1c | 2060 | } |
cbdf5a24 DW |
2061 | |
2062 | de->namlen = namlen; | |
2063 | de->offset = offset; | |
2064 | de->ino = ino; | |
2065 | de->d_type = d_type; | |
2066 | memcpy(de->name, name, namlen); | |
2067 | buf->used += reclen; | |
2068 | ||
2069 | return 0; | |
2070 | } | |
2071 | ||
2072 | static int btrfs_nfshack_readdir(struct file *file, void *dirent, | |
2073 | filldir_t filldir) | |
2074 | { | |
2075 | struct nfshack_readdir buf; | |
2076 | struct nfshack_dirent *de; | |
2077 | int err; | |
2078 | int size; | |
2079 | loff_t offset; | |
2080 | ||
2081 | buf.dirent = (void *)__get_free_page(GFP_KERNEL); | |
2082 | if (!buf.dirent) | |
2083 | return -ENOMEM; | |
2084 | ||
2085 | offset = file->f_pos; | |
2086 | ||
f2322b1c | 2087 | do { |
cbdf5a24 DW |
2088 | unsigned int reclen; |
2089 | ||
2090 | buf.used = 0; | |
f2322b1c | 2091 | buf.full = 0; |
cbdf5a24 DW |
2092 | err = btrfs_real_readdir(file, &buf, btrfs_nfshack_filldir); |
2093 | if (err) | |
2094 | break; | |
2095 | ||
2096 | size = buf.used; | |
2097 | ||
2098 | if (!size) | |
2099 | break; | |
2100 | ||
2101 | de = (struct nfshack_dirent *)buf.dirent; | |
2102 | while (size > 0) { | |
2103 | offset = de->offset; | |
2104 | ||
2105 | if (filldir(dirent, de->name, de->namlen, de->offset, | |
2106 | de->ino, de->d_type)) | |
2107 | goto done; | |
2108 | offset = file->f_pos; | |
2109 | ||
2110 | reclen = ALIGN(sizeof(*de) + de->namlen, | |
2111 | sizeof(u64)); | |
2112 | size -= reclen; | |
2113 | de = (struct nfshack_dirent *)((char *)de + reclen); | |
2114 | } | |
f2322b1c | 2115 | } while (buf.full); |
cbdf5a24 DW |
2116 | |
2117 | done: | |
2118 | free_page((unsigned long)buf.dirent); | |
2119 | file->f_pos = offset; | |
2120 | ||
2121 | return err; | |
2122 | } | |
2123 | #endif | |
2124 | ||
39279cc3 CM |
2125 | int btrfs_write_inode(struct inode *inode, int wait) |
2126 | { | |
2127 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2128 | struct btrfs_trans_handle *trans; | |
2129 | int ret = 0; | |
2130 | ||
4ca8b41e CM |
2131 | if (root->fs_info->closing > 1) |
2132 | return 0; | |
2133 | ||
39279cc3 | 2134 | if (wait) { |
f9295749 | 2135 | trans = btrfs_join_transaction(root, 1); |
39279cc3 CM |
2136 | btrfs_set_trans_block_group(trans, inode); |
2137 | ret = btrfs_commit_transaction(trans, root); | |
39279cc3 CM |
2138 | } |
2139 | return ret; | |
2140 | } | |
2141 | ||
2142 | /* | |
54aa1f4d | 2143 | * This is somewhat expensive, updating the tree every time the |
39279cc3 CM |
2144 | * inode changes. But, it is most likely to find the inode in cache. |
2145 | * FIXME, needs more benchmarking...there are no reasons other than performance | |
2146 | * to keep or drop this code. | |
2147 | */ | |
2148 | void btrfs_dirty_inode(struct inode *inode) | |
2149 | { | |
2150 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2151 | struct btrfs_trans_handle *trans; | |
2152 | ||
f9295749 | 2153 | trans = btrfs_join_transaction(root, 1); |
39279cc3 CM |
2154 | btrfs_set_trans_block_group(trans, inode); |
2155 | btrfs_update_inode(trans, root, inode); | |
2156 | btrfs_end_transaction(trans, root); | |
39279cc3 CM |
2157 | } |
2158 | ||
aec7477b JB |
2159 | static int btrfs_set_inode_index_count(struct inode *inode) |
2160 | { | |
2161 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2162 | struct btrfs_key key, found_key; | |
2163 | struct btrfs_path *path; | |
2164 | struct extent_buffer *leaf; | |
2165 | int ret; | |
2166 | ||
2167 | key.objectid = inode->i_ino; | |
2168 | btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY); | |
2169 | key.offset = (u64)-1; | |
2170 | ||
2171 | path = btrfs_alloc_path(); | |
2172 | if (!path) | |
2173 | return -ENOMEM; | |
2174 | ||
2175 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2176 | if (ret < 0) | |
2177 | goto out; | |
2178 | /* FIXME: we should be able to handle this */ | |
2179 | if (ret == 0) | |
2180 | goto out; | |
2181 | ret = 0; | |
2182 | ||
2183 | /* | |
2184 | * MAGIC NUMBER EXPLANATION: | |
2185 | * since we search a directory based on f_pos we have to start at 2 | |
2186 | * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody | |
2187 | * else has to start at 2 | |
2188 | */ | |
2189 | if (path->slots[0] == 0) { | |
2190 | BTRFS_I(inode)->index_cnt = 2; | |
2191 | goto out; | |
2192 | } | |
2193 | ||
2194 | path->slots[0]--; | |
2195 | ||
2196 | leaf = path->nodes[0]; | |
2197 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2198 | ||
2199 | if (found_key.objectid != inode->i_ino || | |
2200 | btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) { | |
2201 | BTRFS_I(inode)->index_cnt = 2; | |
2202 | goto out; | |
2203 | } | |
2204 | ||
2205 | BTRFS_I(inode)->index_cnt = found_key.offset + 1; | |
2206 | out: | |
2207 | btrfs_free_path(path); | |
2208 | return ret; | |
2209 | } | |
2210 | ||
00e4e6b3 CM |
2211 | static int btrfs_set_inode_index(struct inode *dir, struct inode *inode, |
2212 | u64 *index) | |
aec7477b JB |
2213 | { |
2214 | int ret = 0; | |
2215 | ||
2216 | if (BTRFS_I(dir)->index_cnt == (u64)-1) { | |
2217 | ret = btrfs_set_inode_index_count(dir); | |
2218 | if (ret) | |
2219 | return ret; | |
2220 | } | |
2221 | ||
00e4e6b3 | 2222 | *index = BTRFS_I(dir)->index_cnt; |
aec7477b JB |
2223 | BTRFS_I(dir)->index_cnt++; |
2224 | ||
2225 | return ret; | |
2226 | } | |
2227 | ||
39279cc3 CM |
2228 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
2229 | struct btrfs_root *root, | |
aec7477b | 2230 | struct inode *dir, |
9c58309d CM |
2231 | const char *name, int name_len, |
2232 | u64 ref_objectid, | |
39279cc3 CM |
2233 | u64 objectid, |
2234 | struct btrfs_block_group_cache *group, | |
00e4e6b3 | 2235 | int mode, u64 *index) |
39279cc3 CM |
2236 | { |
2237 | struct inode *inode; | |
5f39d397 | 2238 | struct btrfs_inode_item *inode_item; |
6324fbf3 | 2239 | struct btrfs_block_group_cache *new_inode_group; |
39279cc3 | 2240 | struct btrfs_key *location; |
5f39d397 | 2241 | struct btrfs_path *path; |
9c58309d CM |
2242 | struct btrfs_inode_ref *ref; |
2243 | struct btrfs_key key[2]; | |
2244 | u32 sizes[2]; | |
2245 | unsigned long ptr; | |
39279cc3 CM |
2246 | int ret; |
2247 | int owner; | |
2248 | ||
5f39d397 CM |
2249 | path = btrfs_alloc_path(); |
2250 | BUG_ON(!path); | |
2251 | ||
39279cc3 CM |
2252 | inode = new_inode(root->fs_info->sb); |
2253 | if (!inode) | |
2254 | return ERR_PTR(-ENOMEM); | |
2255 | ||
aec7477b | 2256 | if (dir) { |
00e4e6b3 | 2257 | ret = btrfs_set_inode_index(dir, inode, index); |
aec7477b JB |
2258 | if (ret) |
2259 | return ERR_PTR(ret); | |
aec7477b JB |
2260 | } |
2261 | /* | |
2262 | * index_cnt is ignored for everything but a dir, | |
2263 | * btrfs_get_inode_index_count has an explanation for the magic | |
2264 | * number | |
2265 | */ | |
2266 | BTRFS_I(inode)->index_cnt = 2; | |
2267 | ||
d1310b2e CM |
2268 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
2269 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | |
b888db2b | 2270 | inode->i_mapping, GFP_NOFS); |
7e38326f CM |
2271 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
2272 | inode->i_mapping, GFP_NOFS); | |
ba1da2f4 | 2273 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); |
ea8c2819 | 2274 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); |
1b1e2135 | 2275 | mutex_init(&BTRFS_I(inode)->csum_mutex); |
ee6e6504 | 2276 | mutex_init(&BTRFS_I(inode)->extent_mutex); |
9069218d | 2277 | BTRFS_I(inode)->delalloc_bytes = 0; |
db69e0eb | 2278 | inode->i_mapping->writeback_index = 0; |
dbe674a9 | 2279 | BTRFS_I(inode)->disk_i_size = 0; |
39279cc3 | 2280 | BTRFS_I(inode)->root = root; |
b888db2b | 2281 | |
39279cc3 CM |
2282 | if (mode & S_IFDIR) |
2283 | owner = 0; | |
2284 | else | |
2285 | owner = 1; | |
6324fbf3 | 2286 | new_inode_group = btrfs_find_block_group(root, group, 0, |
0b86a832 | 2287 | BTRFS_BLOCK_GROUP_METADATA, owner); |
6324fbf3 CM |
2288 | if (!new_inode_group) { |
2289 | printk("find_block group failed\n"); | |
2290 | new_inode_group = group; | |
2291 | } | |
2292 | BTRFS_I(inode)->block_group = new_inode_group; | |
b98b6767 | 2293 | BTRFS_I(inode)->flags = 0; |
9c58309d CM |
2294 | |
2295 | key[0].objectid = objectid; | |
2296 | btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); | |
2297 | key[0].offset = 0; | |
2298 | ||
2299 | key[1].objectid = objectid; | |
2300 | btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY); | |
2301 | key[1].offset = ref_objectid; | |
2302 | ||
2303 | sizes[0] = sizeof(struct btrfs_inode_item); | |
2304 | sizes[1] = name_len + sizeof(*ref); | |
2305 | ||
2306 | ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2); | |
2307 | if (ret != 0) | |
5f39d397 CM |
2308 | goto fail; |
2309 | ||
9c58309d CM |
2310 | if (objectid > root->highest_inode) |
2311 | root->highest_inode = objectid; | |
2312 | ||
39279cc3 CM |
2313 | inode->i_uid = current->fsuid; |
2314 | inode->i_gid = current->fsgid; | |
2315 | inode->i_mode = mode; | |
2316 | inode->i_ino = objectid; | |
2317 | inode->i_blocks = 0; | |
2318 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | |
5f39d397 CM |
2319 | inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
2320 | struct btrfs_inode_item); | |
2321 | fill_inode_item(path->nodes[0], inode_item, inode); | |
9c58309d CM |
2322 | |
2323 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
2324 | struct btrfs_inode_ref); | |
2325 | btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len); | |
00e4e6b3 | 2326 | btrfs_set_inode_ref_index(path->nodes[0], ref, *index); |
9c58309d CM |
2327 | ptr = (unsigned long)(ref + 1); |
2328 | write_extent_buffer(path->nodes[0], name, ptr, name_len); | |
2329 | ||
5f39d397 CM |
2330 | btrfs_mark_buffer_dirty(path->nodes[0]); |
2331 | btrfs_free_path(path); | |
2332 | ||
39279cc3 CM |
2333 | location = &BTRFS_I(inode)->location; |
2334 | location->objectid = objectid; | |
39279cc3 CM |
2335 | location->offset = 0; |
2336 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); | |
2337 | ||
39279cc3 CM |
2338 | insert_inode_hash(inode); |
2339 | return inode; | |
5f39d397 | 2340 | fail: |
aec7477b JB |
2341 | if (dir) |
2342 | BTRFS_I(dir)->index_cnt--; | |
5f39d397 CM |
2343 | btrfs_free_path(path); |
2344 | return ERR_PTR(ret); | |
39279cc3 CM |
2345 | } |
2346 | ||
2347 | static inline u8 btrfs_inode_type(struct inode *inode) | |
2348 | { | |
2349 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; | |
2350 | } | |
2351 | ||
2352 | static int btrfs_add_link(struct btrfs_trans_handle *trans, | |
9c58309d | 2353 | struct dentry *dentry, struct inode *inode, |
00e4e6b3 | 2354 | int add_backref, u64 index) |
39279cc3 CM |
2355 | { |
2356 | int ret; | |
2357 | struct btrfs_key key; | |
2358 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; | |
aec7477b | 2359 | struct inode *parent_inode = dentry->d_parent->d_inode; |
5f39d397 | 2360 | |
39279cc3 | 2361 | key.objectid = inode->i_ino; |
39279cc3 CM |
2362 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
2363 | key.offset = 0; | |
2364 | ||
2365 | ret = btrfs_insert_dir_item(trans, root, | |
2366 | dentry->d_name.name, dentry->d_name.len, | |
2367 | dentry->d_parent->d_inode->i_ino, | |
aec7477b | 2368 | &key, btrfs_inode_type(inode), |
00e4e6b3 | 2369 | index); |
39279cc3 | 2370 | if (ret == 0) { |
9c58309d CM |
2371 | if (add_backref) { |
2372 | ret = btrfs_insert_inode_ref(trans, root, | |
2373 | dentry->d_name.name, | |
2374 | dentry->d_name.len, | |
2375 | inode->i_ino, | |
aec7477b | 2376 | parent_inode->i_ino, |
00e4e6b3 | 2377 | index); |
9c58309d | 2378 | } |
dbe674a9 CM |
2379 | btrfs_i_size_write(parent_inode, parent_inode->i_size + |
2380 | dentry->d_name.len * 2); | |
79c44584 | 2381 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
39279cc3 CM |
2382 | ret = btrfs_update_inode(trans, root, |
2383 | dentry->d_parent->d_inode); | |
2384 | } | |
2385 | return ret; | |
2386 | } | |
2387 | ||
2388 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, | |
9c58309d | 2389 | struct dentry *dentry, struct inode *inode, |
00e4e6b3 | 2390 | int backref, u64 index) |
39279cc3 | 2391 | { |
00e4e6b3 | 2392 | int err = btrfs_add_link(trans, dentry, inode, backref, index); |
39279cc3 CM |
2393 | if (!err) { |
2394 | d_instantiate(dentry, inode); | |
2395 | return 0; | |
2396 | } | |
2397 | if (err > 0) | |
2398 | err = -EEXIST; | |
2399 | return err; | |
2400 | } | |
2401 | ||
618e21d5 JB |
2402 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
2403 | int mode, dev_t rdev) | |
2404 | { | |
2405 | struct btrfs_trans_handle *trans; | |
2406 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 2407 | struct inode *inode = NULL; |
618e21d5 JB |
2408 | int err; |
2409 | int drop_inode = 0; | |
2410 | u64 objectid; | |
1832a6d5 | 2411 | unsigned long nr = 0; |
00e4e6b3 | 2412 | u64 index = 0; |
618e21d5 JB |
2413 | |
2414 | if (!new_valid_dev(rdev)) | |
2415 | return -EINVAL; | |
2416 | ||
1832a6d5 CM |
2417 | err = btrfs_check_free_space(root, 1, 0); |
2418 | if (err) | |
2419 | goto fail; | |
2420 | ||
618e21d5 JB |
2421 | trans = btrfs_start_transaction(root, 1); |
2422 | btrfs_set_trans_block_group(trans, dir); | |
2423 | ||
2424 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
2425 | if (err) { | |
2426 | err = -ENOSPC; | |
2427 | goto out_unlock; | |
2428 | } | |
2429 | ||
aec7477b | 2430 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
9c58309d CM |
2431 | dentry->d_name.len, |
2432 | dentry->d_parent->d_inode->i_ino, objectid, | |
00e4e6b3 | 2433 | BTRFS_I(dir)->block_group, mode, &index); |
618e21d5 JB |
2434 | err = PTR_ERR(inode); |
2435 | if (IS_ERR(inode)) | |
2436 | goto out_unlock; | |
2437 | ||
33268eaf JB |
2438 | err = btrfs_init_acl(inode, dir); |
2439 | if (err) { | |
2440 | drop_inode = 1; | |
2441 | goto out_unlock; | |
2442 | } | |
2443 | ||
618e21d5 | 2444 | btrfs_set_trans_block_group(trans, inode); |
00e4e6b3 | 2445 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
618e21d5 JB |
2446 | if (err) |
2447 | drop_inode = 1; | |
2448 | else { | |
2449 | inode->i_op = &btrfs_special_inode_operations; | |
2450 | init_special_inode(inode, inode->i_mode, rdev); | |
1b4ab1bb | 2451 | btrfs_update_inode(trans, root, inode); |
618e21d5 JB |
2452 | } |
2453 | dir->i_sb->s_dirt = 1; | |
2454 | btrfs_update_inode_block_group(trans, inode); | |
2455 | btrfs_update_inode_block_group(trans, dir); | |
2456 | out_unlock: | |
d3c2fdcf | 2457 | nr = trans->blocks_used; |
89ce8a63 | 2458 | btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 2459 | fail: |
618e21d5 JB |
2460 | if (drop_inode) { |
2461 | inode_dec_link_count(inode); | |
2462 | iput(inode); | |
2463 | } | |
d3c2fdcf | 2464 | btrfs_btree_balance_dirty(root, nr); |
618e21d5 JB |
2465 | return err; |
2466 | } | |
2467 | ||
39279cc3 CM |
2468 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
2469 | int mode, struct nameidata *nd) | |
2470 | { | |
2471 | struct btrfs_trans_handle *trans; | |
2472 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
1832a6d5 | 2473 | struct inode *inode = NULL; |
39279cc3 CM |
2474 | int err; |
2475 | int drop_inode = 0; | |
1832a6d5 | 2476 | unsigned long nr = 0; |
39279cc3 | 2477 | u64 objectid; |
00e4e6b3 | 2478 | u64 index = 0; |
39279cc3 | 2479 | |
1832a6d5 CM |
2480 | err = btrfs_check_free_space(root, 1, 0); |
2481 | if (err) | |
2482 | goto fail; | |
39279cc3 CM |
2483 | trans = btrfs_start_transaction(root, 1); |
2484 | btrfs_set_trans_block_group(trans, dir); | |
2485 | ||
2486 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
2487 | if (err) { | |
2488 | err = -ENOSPC; | |
2489 | goto out_unlock; | |
2490 | } | |
2491 | ||
aec7477b | 2492 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
9c58309d CM |
2493 | dentry->d_name.len, |
2494 | dentry->d_parent->d_inode->i_ino, | |
00e4e6b3 CM |
2495 | objectid, BTRFS_I(dir)->block_group, mode, |
2496 | &index); | |
39279cc3 CM |
2497 | err = PTR_ERR(inode); |
2498 | if (IS_ERR(inode)) | |
2499 | goto out_unlock; | |
2500 | ||
33268eaf JB |
2501 | err = btrfs_init_acl(inode, dir); |
2502 | if (err) { | |
2503 | drop_inode = 1; | |
2504 | goto out_unlock; | |
2505 | } | |
2506 | ||
39279cc3 | 2507 | btrfs_set_trans_block_group(trans, inode); |
00e4e6b3 | 2508 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
39279cc3 CM |
2509 | if (err) |
2510 | drop_inode = 1; | |
2511 | else { | |
2512 | inode->i_mapping->a_ops = &btrfs_aops; | |
04160088 | 2513 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
39279cc3 CM |
2514 | inode->i_fop = &btrfs_file_operations; |
2515 | inode->i_op = &btrfs_file_inode_operations; | |
d1310b2e CM |
2516 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
2517 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | |
a52d9a80 | 2518 | inode->i_mapping, GFP_NOFS); |
7e38326f CM |
2519 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
2520 | inode->i_mapping, GFP_NOFS); | |
ea8c2819 | 2521 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); |
1b1e2135 | 2522 | mutex_init(&BTRFS_I(inode)->csum_mutex); |
ee6e6504 | 2523 | mutex_init(&BTRFS_I(inode)->extent_mutex); |
9069218d | 2524 | BTRFS_I(inode)->delalloc_bytes = 0; |
dbe674a9 | 2525 | BTRFS_I(inode)->disk_i_size = 0; |
db69e0eb | 2526 | inode->i_mapping->writeback_index = 0; |
d1310b2e | 2527 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
ba1da2f4 | 2528 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); |
39279cc3 CM |
2529 | } |
2530 | dir->i_sb->s_dirt = 1; | |
2531 | btrfs_update_inode_block_group(trans, inode); | |
2532 | btrfs_update_inode_block_group(trans, dir); | |
2533 | out_unlock: | |
d3c2fdcf | 2534 | nr = trans->blocks_used; |
ab78c84d | 2535 | btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 2536 | fail: |
39279cc3 CM |
2537 | if (drop_inode) { |
2538 | inode_dec_link_count(inode); | |
2539 | iput(inode); | |
2540 | } | |
d3c2fdcf | 2541 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
2542 | return err; |
2543 | } | |
2544 | ||
2545 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, | |
2546 | struct dentry *dentry) | |
2547 | { | |
2548 | struct btrfs_trans_handle *trans; | |
2549 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
2550 | struct inode *inode = old_dentry->d_inode; | |
00e4e6b3 | 2551 | u64 index; |
1832a6d5 | 2552 | unsigned long nr = 0; |
39279cc3 CM |
2553 | int err; |
2554 | int drop_inode = 0; | |
2555 | ||
2556 | if (inode->i_nlink == 0) | |
2557 | return -ENOENT; | |
2558 | ||
6da6abae CM |
2559 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
2560 | inode->i_nlink++; | |
2561 | #else | |
39279cc3 | 2562 | inc_nlink(inode); |
6da6abae | 2563 | #endif |
1832a6d5 CM |
2564 | err = btrfs_check_free_space(root, 1, 0); |
2565 | if (err) | |
2566 | goto fail; | |
00e4e6b3 | 2567 | err = btrfs_set_inode_index(dir, inode, &index); |
aec7477b JB |
2568 | if (err) |
2569 | goto fail; | |
2570 | ||
39279cc3 | 2571 | trans = btrfs_start_transaction(root, 1); |
5f39d397 | 2572 | |
39279cc3 CM |
2573 | btrfs_set_trans_block_group(trans, dir); |
2574 | atomic_inc(&inode->i_count); | |
aec7477b | 2575 | |
00e4e6b3 | 2576 | err = btrfs_add_nondir(trans, dentry, inode, 1, index); |
5f39d397 | 2577 | |
39279cc3 CM |
2578 | if (err) |
2579 | drop_inode = 1; | |
5f39d397 | 2580 | |
39279cc3 CM |
2581 | dir->i_sb->s_dirt = 1; |
2582 | btrfs_update_inode_block_group(trans, dir); | |
54aa1f4d | 2583 | err = btrfs_update_inode(trans, root, inode); |
5f39d397 | 2584 | |
54aa1f4d CM |
2585 | if (err) |
2586 | drop_inode = 1; | |
39279cc3 | 2587 | |
d3c2fdcf | 2588 | nr = trans->blocks_used; |
ab78c84d | 2589 | btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 2590 | fail: |
39279cc3 CM |
2591 | if (drop_inode) { |
2592 | inode_dec_link_count(inode); | |
2593 | iput(inode); | |
2594 | } | |
d3c2fdcf | 2595 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
2596 | return err; |
2597 | } | |
2598 | ||
39279cc3 CM |
2599 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
2600 | { | |
b9d86667 | 2601 | struct inode *inode = NULL; |
39279cc3 CM |
2602 | struct btrfs_trans_handle *trans; |
2603 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
2604 | int err = 0; | |
2605 | int drop_on_err = 0; | |
b9d86667 | 2606 | u64 objectid = 0; |
00e4e6b3 | 2607 | u64 index = 0; |
d3c2fdcf | 2608 | unsigned long nr = 1; |
39279cc3 | 2609 | |
1832a6d5 CM |
2610 | err = btrfs_check_free_space(root, 1, 0); |
2611 | if (err) | |
2612 | goto out_unlock; | |
2613 | ||
39279cc3 CM |
2614 | trans = btrfs_start_transaction(root, 1); |
2615 | btrfs_set_trans_block_group(trans, dir); | |
5f39d397 | 2616 | |
39279cc3 CM |
2617 | if (IS_ERR(trans)) { |
2618 | err = PTR_ERR(trans); | |
2619 | goto out_unlock; | |
2620 | } | |
2621 | ||
2622 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
2623 | if (err) { | |
2624 | err = -ENOSPC; | |
2625 | goto out_unlock; | |
2626 | } | |
2627 | ||
aec7477b | 2628 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
9c58309d CM |
2629 | dentry->d_name.len, |
2630 | dentry->d_parent->d_inode->i_ino, objectid, | |
00e4e6b3 CM |
2631 | BTRFS_I(dir)->block_group, S_IFDIR | mode, |
2632 | &index); | |
39279cc3 CM |
2633 | if (IS_ERR(inode)) { |
2634 | err = PTR_ERR(inode); | |
2635 | goto out_fail; | |
2636 | } | |
5f39d397 | 2637 | |
39279cc3 | 2638 | drop_on_err = 1; |
33268eaf JB |
2639 | |
2640 | err = btrfs_init_acl(inode, dir); | |
2641 | if (err) | |
2642 | goto out_fail; | |
2643 | ||
39279cc3 CM |
2644 | inode->i_op = &btrfs_dir_inode_operations; |
2645 | inode->i_fop = &btrfs_dir_file_operations; | |
2646 | btrfs_set_trans_block_group(trans, inode); | |
2647 | ||
dbe674a9 | 2648 | btrfs_i_size_write(inode, 0); |
39279cc3 CM |
2649 | err = btrfs_update_inode(trans, root, inode); |
2650 | if (err) | |
2651 | goto out_fail; | |
5f39d397 | 2652 | |
00e4e6b3 | 2653 | err = btrfs_add_link(trans, dentry, inode, 0, index); |
39279cc3 CM |
2654 | if (err) |
2655 | goto out_fail; | |
5f39d397 | 2656 | |
39279cc3 CM |
2657 | d_instantiate(dentry, inode); |
2658 | drop_on_err = 0; | |
2659 | dir->i_sb->s_dirt = 1; | |
2660 | btrfs_update_inode_block_group(trans, inode); | |
2661 | btrfs_update_inode_block_group(trans, dir); | |
2662 | ||
2663 | out_fail: | |
d3c2fdcf | 2664 | nr = trans->blocks_used; |
ab78c84d | 2665 | btrfs_end_transaction_throttle(trans, root); |
5f39d397 | 2666 | |
39279cc3 | 2667 | out_unlock: |
39279cc3 CM |
2668 | if (drop_on_err) |
2669 | iput(inode); | |
d3c2fdcf | 2670 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
2671 | return err; |
2672 | } | |
2673 | ||
3b951516 CM |
2674 | static int merge_extent_mapping(struct extent_map_tree *em_tree, |
2675 | struct extent_map *existing, | |
e6dcd2dc CM |
2676 | struct extent_map *em, |
2677 | u64 map_start, u64 map_len) | |
3b951516 CM |
2678 | { |
2679 | u64 start_diff; | |
3b951516 | 2680 | |
e6dcd2dc CM |
2681 | BUG_ON(map_start < em->start || map_start >= extent_map_end(em)); |
2682 | start_diff = map_start - em->start; | |
2683 | em->start = map_start; | |
2684 | em->len = map_len; | |
2685 | if (em->block_start < EXTENT_MAP_LAST_BYTE) | |
2686 | em->block_start += start_diff; | |
2687 | return add_extent_mapping(em_tree, em); | |
3b951516 CM |
2688 | } |
2689 | ||
a52d9a80 | 2690 | struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page, |
70dec807 | 2691 | size_t pg_offset, u64 start, u64 len, |
a52d9a80 CM |
2692 | int create) |
2693 | { | |
2694 | int ret; | |
2695 | int err = 0; | |
db94535d | 2696 | u64 bytenr; |
a52d9a80 CM |
2697 | u64 extent_start = 0; |
2698 | u64 extent_end = 0; | |
2699 | u64 objectid = inode->i_ino; | |
2700 | u32 found_type; | |
f421950f | 2701 | struct btrfs_path *path = NULL; |
a52d9a80 CM |
2702 | struct btrfs_root *root = BTRFS_I(inode)->root; |
2703 | struct btrfs_file_extent_item *item; | |
5f39d397 CM |
2704 | struct extent_buffer *leaf; |
2705 | struct btrfs_key found_key; | |
a52d9a80 CM |
2706 | struct extent_map *em = NULL; |
2707 | struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree; | |
d1310b2e | 2708 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
a52d9a80 CM |
2709 | struct btrfs_trans_handle *trans = NULL; |
2710 | ||
a52d9a80 | 2711 | again: |
d1310b2e CM |
2712 | spin_lock(&em_tree->lock); |
2713 | em = lookup_extent_mapping(em_tree, start, len); | |
a061fc8d CM |
2714 | if (em) |
2715 | em->bdev = root->fs_info->fs_devices->latest_bdev; | |
d1310b2e CM |
2716 | spin_unlock(&em_tree->lock); |
2717 | ||
a52d9a80 | 2718 | if (em) { |
e1c4b745 CM |
2719 | if (em->start > start || em->start + em->len <= start) |
2720 | free_extent_map(em); | |
2721 | else if (em->block_start == EXTENT_MAP_INLINE && page) | |
70dec807 CM |
2722 | free_extent_map(em); |
2723 | else | |
2724 | goto out; | |
a52d9a80 | 2725 | } |
d1310b2e | 2726 | em = alloc_extent_map(GFP_NOFS); |
a52d9a80 | 2727 | if (!em) { |
d1310b2e CM |
2728 | err = -ENOMEM; |
2729 | goto out; | |
a52d9a80 | 2730 | } |
e6dcd2dc | 2731 | em->bdev = root->fs_info->fs_devices->latest_bdev; |
d1310b2e CM |
2732 | em->start = EXTENT_MAP_HOLE; |
2733 | em->len = (u64)-1; | |
f421950f CM |
2734 | |
2735 | if (!path) { | |
2736 | path = btrfs_alloc_path(); | |
2737 | BUG_ON(!path); | |
2738 | } | |
2739 | ||
179e29e4 CM |
2740 | ret = btrfs_lookup_file_extent(trans, root, path, |
2741 | objectid, start, trans != NULL); | |
a52d9a80 CM |
2742 | if (ret < 0) { |
2743 | err = ret; | |
2744 | goto out; | |
2745 | } | |
2746 | ||
2747 | if (ret != 0) { | |
2748 | if (path->slots[0] == 0) | |
2749 | goto not_found; | |
2750 | path->slots[0]--; | |
2751 | } | |
2752 | ||
5f39d397 CM |
2753 | leaf = path->nodes[0]; |
2754 | item = btrfs_item_ptr(leaf, path->slots[0], | |
a52d9a80 | 2755 | struct btrfs_file_extent_item); |
a52d9a80 | 2756 | /* are we inside the extent that was found? */ |
5f39d397 CM |
2757 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
2758 | found_type = btrfs_key_type(&found_key); | |
2759 | if (found_key.objectid != objectid || | |
a52d9a80 CM |
2760 | found_type != BTRFS_EXTENT_DATA_KEY) { |
2761 | goto not_found; | |
2762 | } | |
2763 | ||
5f39d397 CM |
2764 | found_type = btrfs_file_extent_type(leaf, item); |
2765 | extent_start = found_key.offset; | |
a52d9a80 CM |
2766 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
2767 | extent_end = extent_start + | |
db94535d | 2768 | btrfs_file_extent_num_bytes(leaf, item); |
a52d9a80 | 2769 | err = 0; |
b888db2b | 2770 | if (start < extent_start || start >= extent_end) { |
a52d9a80 CM |
2771 | em->start = start; |
2772 | if (start < extent_start) { | |
d1310b2e | 2773 | if (start + len <= extent_start) |
b888db2b | 2774 | goto not_found; |
d1310b2e | 2775 | em->len = extent_end - extent_start; |
a52d9a80 | 2776 | } else { |
d1310b2e | 2777 | em->len = len; |
a52d9a80 CM |
2778 | } |
2779 | goto not_found_em; | |
2780 | } | |
db94535d CM |
2781 | bytenr = btrfs_file_extent_disk_bytenr(leaf, item); |
2782 | if (bytenr == 0) { | |
a52d9a80 | 2783 | em->start = extent_start; |
d1310b2e | 2784 | em->len = extent_end - extent_start; |
5f39d397 | 2785 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 CM |
2786 | goto insert; |
2787 | } | |
db94535d CM |
2788 | bytenr += btrfs_file_extent_offset(leaf, item); |
2789 | em->block_start = bytenr; | |
a52d9a80 | 2790 | em->start = extent_start; |
d1310b2e | 2791 | em->len = extent_end - extent_start; |
a52d9a80 CM |
2792 | goto insert; |
2793 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { | |
70dec807 | 2794 | u64 page_start; |
5f39d397 | 2795 | unsigned long ptr; |
a52d9a80 | 2796 | char *map; |
3326d1b0 CM |
2797 | size_t size; |
2798 | size_t extent_offset; | |
2799 | size_t copy_size; | |
a52d9a80 | 2800 | |
5f39d397 CM |
2801 | size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf, |
2802 | path->slots[0])); | |
d1310b2e CM |
2803 | extent_end = (extent_start + size + root->sectorsize - 1) & |
2804 | ~((u64)root->sectorsize - 1); | |
b888db2b | 2805 | if (start < extent_start || start >= extent_end) { |
a52d9a80 CM |
2806 | em->start = start; |
2807 | if (start < extent_start) { | |
d1310b2e | 2808 | if (start + len <= extent_start) |
b888db2b | 2809 | goto not_found; |
d1310b2e | 2810 | em->len = extent_end - extent_start; |
a52d9a80 | 2811 | } else { |
d1310b2e | 2812 | em->len = len; |
a52d9a80 CM |
2813 | } |
2814 | goto not_found_em; | |
2815 | } | |
689f9346 | 2816 | em->block_start = EXTENT_MAP_INLINE; |
689f9346 Y |
2817 | |
2818 | if (!page) { | |
2819 | em->start = extent_start; | |
d1310b2e | 2820 | em->len = size; |
689f9346 Y |
2821 | goto out; |
2822 | } | |
5f39d397 | 2823 | |
70dec807 CM |
2824 | page_start = page_offset(page) + pg_offset; |
2825 | extent_offset = page_start - extent_start; | |
2826 | copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset, | |
3326d1b0 | 2827 | size - extent_offset); |
3326d1b0 | 2828 | em->start = extent_start + extent_offset; |
70dec807 CM |
2829 | em->len = (copy_size + root->sectorsize - 1) & |
2830 | ~((u64)root->sectorsize - 1); | |
689f9346 Y |
2831 | map = kmap(page); |
2832 | ptr = btrfs_file_extent_inline_start(item) + extent_offset; | |
179e29e4 | 2833 | if (create == 0 && !PageUptodate(page)) { |
70dec807 | 2834 | read_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 CM |
2835 | copy_size); |
2836 | flush_dcache_page(page); | |
2837 | } else if (create && PageUptodate(page)) { | |
2838 | if (!trans) { | |
2839 | kunmap(page); | |
2840 | free_extent_map(em); | |
2841 | em = NULL; | |
2842 | btrfs_release_path(root, path); | |
f9295749 | 2843 | trans = btrfs_join_transaction(root, 1); |
179e29e4 CM |
2844 | goto again; |
2845 | } | |
70dec807 | 2846 | write_extent_buffer(leaf, map + pg_offset, ptr, |
179e29e4 CM |
2847 | copy_size); |
2848 | btrfs_mark_buffer_dirty(leaf); | |
a52d9a80 | 2849 | } |
a52d9a80 | 2850 | kunmap(page); |
d1310b2e CM |
2851 | set_extent_uptodate(io_tree, em->start, |
2852 | extent_map_end(em) - 1, GFP_NOFS); | |
a52d9a80 CM |
2853 | goto insert; |
2854 | } else { | |
2855 | printk("unkknown found_type %d\n", found_type); | |
2856 | WARN_ON(1); | |
2857 | } | |
2858 | not_found: | |
2859 | em->start = start; | |
d1310b2e | 2860 | em->len = len; |
a52d9a80 | 2861 | not_found_em: |
5f39d397 | 2862 | em->block_start = EXTENT_MAP_HOLE; |
a52d9a80 CM |
2863 | insert: |
2864 | btrfs_release_path(root, path); | |
d1310b2e CM |
2865 | if (em->start > start || extent_map_end(em) <= start) { |
2866 | printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len); | |
a52d9a80 CM |
2867 | err = -EIO; |
2868 | goto out; | |
2869 | } | |
d1310b2e CM |
2870 | |
2871 | err = 0; | |
2872 | spin_lock(&em_tree->lock); | |
a52d9a80 | 2873 | ret = add_extent_mapping(em_tree, em); |
3b951516 CM |
2874 | /* it is possible that someone inserted the extent into the tree |
2875 | * while we had the lock dropped. It is also possible that | |
2876 | * an overlapping map exists in the tree | |
2877 | */ | |
a52d9a80 | 2878 | if (ret == -EEXIST) { |
3b951516 | 2879 | struct extent_map *existing; |
e6dcd2dc CM |
2880 | |
2881 | ret = 0; | |
2882 | ||
3b951516 | 2883 | existing = lookup_extent_mapping(em_tree, start, len); |
e1c4b745 CM |
2884 | if (existing && (existing->start > start || |
2885 | existing->start + existing->len <= start)) { | |
2886 | free_extent_map(existing); | |
2887 | existing = NULL; | |
2888 | } | |
3b951516 CM |
2889 | if (!existing) { |
2890 | existing = lookup_extent_mapping(em_tree, em->start, | |
2891 | em->len); | |
2892 | if (existing) { | |
2893 | err = merge_extent_mapping(em_tree, existing, | |
e6dcd2dc CM |
2894 | em, start, |
2895 | root->sectorsize); | |
3b951516 CM |
2896 | free_extent_map(existing); |
2897 | if (err) { | |
2898 | free_extent_map(em); | |
2899 | em = NULL; | |
2900 | } | |
2901 | } else { | |
2902 | err = -EIO; | |
2903 | printk("failing to insert %Lu %Lu\n", | |
2904 | start, len); | |
2905 | free_extent_map(em); | |
2906 | em = NULL; | |
2907 | } | |
2908 | } else { | |
2909 | free_extent_map(em); | |
2910 | em = existing; | |
e6dcd2dc | 2911 | err = 0; |
a52d9a80 | 2912 | } |
a52d9a80 | 2913 | } |
d1310b2e | 2914 | spin_unlock(&em_tree->lock); |
a52d9a80 | 2915 | out: |
f421950f CM |
2916 | if (path) |
2917 | btrfs_free_path(path); | |
a52d9a80 CM |
2918 | if (trans) { |
2919 | ret = btrfs_end_transaction(trans, root); | |
e6dcd2dc | 2920 | if (!err) { |
a52d9a80 | 2921 | err = ret; |
e6dcd2dc | 2922 | } |
a52d9a80 | 2923 | } |
a52d9a80 CM |
2924 | if (err) { |
2925 | free_extent_map(em); | |
2926 | WARN_ON(1); | |
2927 | return ERR_PTR(err); | |
2928 | } | |
2929 | return em; | |
2930 | } | |
2931 | ||
e1c4b745 | 2932 | #if 0 /* waiting for O_DIRECT reads */ |
16432985 CM |
2933 | static int btrfs_get_block(struct inode *inode, sector_t iblock, |
2934 | struct buffer_head *bh_result, int create) | |
2935 | { | |
2936 | struct extent_map *em; | |
2937 | u64 start = (u64)iblock << inode->i_blkbits; | |
2938 | struct btrfs_multi_bio *multi = NULL; | |
2939 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
2940 | u64 len; | |
2941 | u64 logical; | |
2942 | u64 map_length; | |
2943 | int ret = 0; | |
2944 | ||
2945 | em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0); | |
2946 | ||
2947 | if (!em || IS_ERR(em)) | |
2948 | goto out; | |
2949 | ||
e1c4b745 | 2950 | if (em->start > start || em->start + em->len <= start) { |
16432985 | 2951 | goto out; |
e1c4b745 | 2952 | } |
16432985 CM |
2953 | |
2954 | if (em->block_start == EXTENT_MAP_INLINE) { | |
2955 | ret = -EINVAL; | |
2956 | goto out; | |
2957 | } | |
2958 | ||
e1c4b745 CM |
2959 | len = em->start + em->len - start; |
2960 | len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size))); | |
2961 | ||
16432985 CM |
2962 | if (em->block_start == EXTENT_MAP_HOLE || |
2963 | em->block_start == EXTENT_MAP_DELALLOC) { | |
e1c4b745 | 2964 | bh_result->b_size = len; |
16432985 CM |
2965 | goto out; |
2966 | } | |
2967 | ||
16432985 CM |
2968 | logical = start - em->start; |
2969 | logical = em->block_start + logical; | |
2970 | ||
2971 | map_length = len; | |
2972 | ret = btrfs_map_block(&root->fs_info->mapping_tree, READ, | |
2973 | logical, &map_length, &multi, 0); | |
2974 | BUG_ON(ret); | |
2975 | bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits; | |
2976 | bh_result->b_size = min(map_length, len); | |
e1c4b745 | 2977 | |
16432985 CM |
2978 | bh_result->b_bdev = multi->stripes[0].dev->bdev; |
2979 | set_buffer_mapped(bh_result); | |
2980 | kfree(multi); | |
2981 | out: | |
2982 | free_extent_map(em); | |
2983 | return ret; | |
2984 | } | |
e1c4b745 | 2985 | #endif |
16432985 CM |
2986 | |
2987 | static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb, | |
2988 | const struct iovec *iov, loff_t offset, | |
2989 | unsigned long nr_segs) | |
2990 | { | |
e1c4b745 CM |
2991 | return -EINVAL; |
2992 | #if 0 | |
16432985 CM |
2993 | struct file *file = iocb->ki_filp; |
2994 | struct inode *inode = file->f_mapping->host; | |
2995 | ||
2996 | if (rw == WRITE) | |
2997 | return -EINVAL; | |
2998 | ||
2999 | return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov, | |
3000 | offset, nr_segs, btrfs_get_block, NULL); | |
e1c4b745 | 3001 | #endif |
16432985 CM |
3002 | } |
3003 | ||
d396c6f5 | 3004 | static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock) |
39279cc3 | 3005 | { |
d396c6f5 | 3006 | return extent_bmap(mapping, iblock, btrfs_get_extent); |
39279cc3 CM |
3007 | } |
3008 | ||
a52d9a80 | 3009 | int btrfs_readpage(struct file *file, struct page *page) |
9ebefb18 | 3010 | { |
d1310b2e CM |
3011 | struct extent_io_tree *tree; |
3012 | tree = &BTRFS_I(page->mapping->host)->io_tree; | |
a52d9a80 | 3013 | return extent_read_full_page(tree, page, btrfs_get_extent); |
9ebefb18 | 3014 | } |
1832a6d5 | 3015 | |
a52d9a80 | 3016 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
39279cc3 | 3017 | { |
d1310b2e | 3018 | struct extent_io_tree *tree; |
b888db2b CM |
3019 | |
3020 | ||
3021 | if (current->flags & PF_MEMALLOC) { | |
3022 | redirty_page_for_writepage(wbc, page); | |
3023 | unlock_page(page); | |
3024 | return 0; | |
3025 | } | |
d1310b2e | 3026 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
a52d9a80 | 3027 | return extent_write_full_page(tree, page, btrfs_get_extent, wbc); |
9ebefb18 CM |
3028 | } |
3029 | ||
f421950f CM |
3030 | int btrfs_writepages(struct address_space *mapping, |
3031 | struct writeback_control *wbc) | |
b293f02e | 3032 | { |
d1310b2e CM |
3033 | struct extent_io_tree *tree; |
3034 | tree = &BTRFS_I(mapping->host)->io_tree; | |
b293f02e CM |
3035 | return extent_writepages(tree, mapping, btrfs_get_extent, wbc); |
3036 | } | |
3037 | ||
3ab2fb5a CM |
3038 | static int |
3039 | btrfs_readpages(struct file *file, struct address_space *mapping, | |
3040 | struct list_head *pages, unsigned nr_pages) | |
3041 | { | |
d1310b2e CM |
3042 | struct extent_io_tree *tree; |
3043 | tree = &BTRFS_I(mapping->host)->io_tree; | |
3ab2fb5a CM |
3044 | return extent_readpages(tree, mapping, pages, nr_pages, |
3045 | btrfs_get_extent); | |
3046 | } | |
e6dcd2dc | 3047 | static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
9ebefb18 | 3048 | { |
d1310b2e CM |
3049 | struct extent_io_tree *tree; |
3050 | struct extent_map_tree *map; | |
a52d9a80 | 3051 | int ret; |
8c2383c3 | 3052 | |
d1310b2e CM |
3053 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
3054 | map = &BTRFS_I(page->mapping->host)->extent_tree; | |
70dec807 | 3055 | ret = try_release_extent_mapping(map, tree, page, gfp_flags); |
a52d9a80 CM |
3056 | if (ret == 1) { |
3057 | ClearPagePrivate(page); | |
3058 | set_page_private(page, 0); | |
3059 | page_cache_release(page); | |
39279cc3 | 3060 | } |
a52d9a80 | 3061 | return ret; |
39279cc3 CM |
3062 | } |
3063 | ||
e6dcd2dc CM |
3064 | static int btrfs_releasepage(struct page *page, gfp_t gfp_flags) |
3065 | { | |
e6dcd2dc CM |
3066 | return __btrfs_releasepage(page, gfp_flags); |
3067 | } | |
3068 | ||
a52d9a80 | 3069 | static void btrfs_invalidatepage(struct page *page, unsigned long offset) |
39279cc3 | 3070 | { |
d1310b2e | 3071 | struct extent_io_tree *tree; |
e6dcd2dc CM |
3072 | struct btrfs_ordered_extent *ordered; |
3073 | u64 page_start = page_offset(page); | |
3074 | u64 page_end = page_start + PAGE_CACHE_SIZE - 1; | |
39279cc3 | 3075 | |
e6dcd2dc | 3076 | wait_on_page_writeback(page); |
d1310b2e | 3077 | tree = &BTRFS_I(page->mapping->host)->io_tree; |
e6dcd2dc CM |
3078 | if (offset) { |
3079 | btrfs_releasepage(page, GFP_NOFS); | |
3080 | return; | |
3081 | } | |
3082 | ||
3083 | lock_extent(tree, page_start, page_end, GFP_NOFS); | |
3084 | ordered = btrfs_lookup_ordered_extent(page->mapping->host, | |
3085 | page_offset(page)); | |
3086 | if (ordered) { | |
eb84ae03 CM |
3087 | /* |
3088 | * IO on this page will never be started, so we need | |
3089 | * to account for any ordered extents now | |
3090 | */ | |
e6dcd2dc CM |
3091 | clear_extent_bit(tree, page_start, page_end, |
3092 | EXTENT_DIRTY | EXTENT_DELALLOC | | |
3093 | EXTENT_LOCKED, 1, 0, GFP_NOFS); | |
211f90e6 CM |
3094 | btrfs_finish_ordered_io(page->mapping->host, |
3095 | page_start, page_end); | |
e6dcd2dc CM |
3096 | btrfs_put_ordered_extent(ordered); |
3097 | lock_extent(tree, page_start, page_end, GFP_NOFS); | |
3098 | } | |
3099 | clear_extent_bit(tree, page_start, page_end, | |
3100 | EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC | | |
3101 | EXTENT_ORDERED, | |
3102 | 1, 1, GFP_NOFS); | |
3103 | __btrfs_releasepage(page, GFP_NOFS); | |
3104 | ||
4a096752 | 3105 | ClearPageChecked(page); |
9ad6b7bc | 3106 | if (PagePrivate(page)) { |
9ad6b7bc CM |
3107 | ClearPagePrivate(page); |
3108 | set_page_private(page, 0); | |
3109 | page_cache_release(page); | |
3110 | } | |
39279cc3 CM |
3111 | } |
3112 | ||
9ebefb18 CM |
3113 | /* |
3114 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets | |
3115 | * called from a page fault handler when a page is first dirtied. Hence we must | |
3116 | * be careful to check for EOF conditions here. We set the page up correctly | |
3117 | * for a written page which means we get ENOSPC checking when writing into | |
3118 | * holes and correct delalloc and unwritten extent mapping on filesystems that | |
3119 | * support these features. | |
3120 | * | |
3121 | * We are not allowed to take the i_mutex here so we have to play games to | |
3122 | * protect against truncate races as the page could now be beyond EOF. Because | |
3123 | * vmtruncate() writes the inode size before removing pages, once we have the | |
3124 | * page lock we can determine safely if the page is beyond EOF. If it is not | |
3125 | * beyond EOF, then the page is guaranteed safe against truncation until we | |
3126 | * unlock the page. | |
3127 | */ | |
3128 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page) | |
3129 | { | |
6da6abae | 3130 | struct inode *inode = fdentry(vma->vm_file)->d_inode; |
1832a6d5 | 3131 | struct btrfs_root *root = BTRFS_I(inode)->root; |
e6dcd2dc CM |
3132 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
3133 | struct btrfs_ordered_extent *ordered; | |
3134 | char *kaddr; | |
3135 | unsigned long zero_start; | |
9ebefb18 | 3136 | loff_t size; |
1832a6d5 | 3137 | int ret; |
a52d9a80 | 3138 | u64 page_start; |
e6dcd2dc | 3139 | u64 page_end; |
9ebefb18 | 3140 | |
1832a6d5 | 3141 | ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0); |
1832a6d5 CM |
3142 | if (ret) |
3143 | goto out; | |
3144 | ||
3145 | ret = -EINVAL; | |
e6dcd2dc | 3146 | again: |
9ebefb18 | 3147 | lock_page(page); |
9ebefb18 | 3148 | size = i_size_read(inode); |
e6dcd2dc CM |
3149 | page_start = page_offset(page); |
3150 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
a52d9a80 | 3151 | |
9ebefb18 | 3152 | if ((page->mapping != inode->i_mapping) || |
e6dcd2dc | 3153 | (page_start >= size)) { |
9ebefb18 CM |
3154 | /* page got truncated out from underneath us */ |
3155 | goto out_unlock; | |
3156 | } | |
e6dcd2dc CM |
3157 | wait_on_page_writeback(page); |
3158 | ||
3159 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
3160 | set_page_extent_mapped(page); | |
3161 | ||
eb84ae03 CM |
3162 | /* |
3163 | * we can't set the delalloc bits if there are pending ordered | |
3164 | * extents. Drop our locks and wait for them to finish | |
3165 | */ | |
e6dcd2dc CM |
3166 | ordered = btrfs_lookup_ordered_extent(inode, page_start); |
3167 | if (ordered) { | |
3168 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
3169 | unlock_page(page); | |
eb84ae03 | 3170 | btrfs_start_ordered_extent(inode, ordered, 1); |
e6dcd2dc CM |
3171 | btrfs_put_ordered_extent(ordered); |
3172 | goto again; | |
3173 | } | |
3174 | ||
ea8c2819 | 3175 | btrfs_set_extent_delalloc(inode, page_start, page_end); |
e6dcd2dc | 3176 | ret = 0; |
9ebefb18 CM |
3177 | |
3178 | /* page is wholly or partially inside EOF */ | |
a52d9a80 | 3179 | if (page_start + PAGE_CACHE_SIZE > size) |
e6dcd2dc | 3180 | zero_start = size & ~PAGE_CACHE_MASK; |
9ebefb18 | 3181 | else |
e6dcd2dc | 3182 | zero_start = PAGE_CACHE_SIZE; |
9ebefb18 | 3183 | |
e6dcd2dc CM |
3184 | if (zero_start != PAGE_CACHE_SIZE) { |
3185 | kaddr = kmap(page); | |
3186 | memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start); | |
3187 | flush_dcache_page(page); | |
3188 | kunmap(page); | |
3189 | } | |
247e743c | 3190 | ClearPageChecked(page); |
e6dcd2dc CM |
3191 | set_page_dirty(page); |
3192 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); | |
9ebefb18 CM |
3193 | |
3194 | out_unlock: | |
3195 | unlock_page(page); | |
1832a6d5 | 3196 | out: |
9ebefb18 CM |
3197 | return ret; |
3198 | } | |
3199 | ||
39279cc3 CM |
3200 | static void btrfs_truncate(struct inode *inode) |
3201 | { | |
3202 | struct btrfs_root *root = BTRFS_I(inode)->root; | |
3203 | int ret; | |
3204 | struct btrfs_trans_handle *trans; | |
d3c2fdcf | 3205 | unsigned long nr; |
dbe674a9 | 3206 | u64 mask = root->sectorsize - 1; |
39279cc3 CM |
3207 | |
3208 | if (!S_ISREG(inode->i_mode)) | |
3209 | return; | |
3210 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
3211 | return; | |
3212 | ||
3213 | btrfs_truncate_page(inode->i_mapping, inode->i_size); | |
4a096752 | 3214 | btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); |
39279cc3 | 3215 | |
39279cc3 CM |
3216 | trans = btrfs_start_transaction(root, 1); |
3217 | btrfs_set_trans_block_group(trans, inode); | |
dbe674a9 | 3218 | btrfs_i_size_write(inode, inode->i_size); |
39279cc3 | 3219 | |
7b128766 JB |
3220 | ret = btrfs_orphan_add(trans, inode); |
3221 | if (ret) | |
3222 | goto out; | |
39279cc3 | 3223 | /* FIXME, add redo link to tree so we don't leak on crash */ |
85e21bac CM |
3224 | ret = btrfs_truncate_in_trans(trans, root, inode, |
3225 | BTRFS_EXTENT_DATA_KEY); | |
39279cc3 | 3226 | btrfs_update_inode(trans, root, inode); |
5f39d397 | 3227 | |
7b128766 JB |
3228 | ret = btrfs_orphan_del(trans, inode); |
3229 | BUG_ON(ret); | |
3230 | ||
3231 | out: | |
3232 | nr = trans->blocks_used; | |
89ce8a63 | 3233 | ret = btrfs_end_transaction_throttle(trans, root); |
39279cc3 | 3234 | BUG_ON(ret); |
d3c2fdcf | 3235 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
3236 | } |
3237 | ||
3b96362c SW |
3238 | /* |
3239 | * Invalidate a single dcache entry at the root of the filesystem. | |
3240 | * Needed after creation of snapshot or subvolume. | |
3241 | */ | |
3242 | void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name, | |
3243 | int namelen) | |
3244 | { | |
3245 | struct dentry *alias, *entry; | |
3246 | struct qstr qstr; | |
3247 | ||
3248 | alias = d_find_alias(root->fs_info->sb->s_root->d_inode); | |
3249 | if (alias) { | |
3250 | qstr.name = name; | |
3251 | qstr.len = namelen; | |
3252 | /* change me if btrfs ever gets a d_hash operation */ | |
3253 | qstr.hash = full_name_hash(qstr.name, qstr.len); | |
3254 | entry = d_lookup(alias, &qstr); | |
3255 | dput(alias); | |
3256 | if (entry) { | |
3257 | d_invalidate(entry); | |
3258 | dput(entry); | |
3259 | } | |
3260 | } | |
3261 | } | |
3262 | ||
f46b5a66 CH |
3263 | int btrfs_create_subvol_root(struct btrfs_root *new_root, |
3264 | struct btrfs_trans_handle *trans, u64 new_dirid, | |
3265 | struct btrfs_block_group_cache *block_group) | |
39279cc3 | 3266 | { |
39279cc3 | 3267 | struct inode *inode; |
00e4e6b3 | 3268 | u64 index = 0; |
39279cc3 | 3269 | |
aec7477b | 3270 | inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, |
00e4e6b3 | 3271 | new_dirid, block_group, S_IFDIR | 0700, &index); |
54aa1f4d | 3272 | if (IS_ERR(inode)) |
f46b5a66 | 3273 | return PTR_ERR(inode); |
39279cc3 CM |
3274 | inode->i_op = &btrfs_dir_inode_operations; |
3275 | inode->i_fop = &btrfs_dir_file_operations; | |
34088780 | 3276 | new_root->inode = inode; |
39279cc3 | 3277 | |
39279cc3 | 3278 | inode->i_nlink = 1; |
dbe674a9 | 3279 | btrfs_i_size_write(inode, 0); |
3b96362c | 3280 | |
f46b5a66 | 3281 | return btrfs_update_inode(trans, new_root, inode); |
39279cc3 CM |
3282 | } |
3283 | ||
edbd8d4e | 3284 | unsigned long btrfs_force_ra(struct address_space *mapping, |
86479a04 CM |
3285 | struct file_ra_state *ra, struct file *file, |
3286 | pgoff_t offset, pgoff_t last_index) | |
3287 | { | |
8e7bf94f | 3288 | pgoff_t req_size = last_index - offset + 1; |
86479a04 CM |
3289 | |
3290 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) | |
86479a04 CM |
3291 | offset = page_cache_readahead(mapping, ra, file, offset, req_size); |
3292 | return offset; | |
3293 | #else | |
86479a04 CM |
3294 | page_cache_sync_readahead(mapping, ra, file, offset, req_size); |
3295 | return offset + req_size; | |
3296 | #endif | |
3297 | } | |
3298 | ||
39279cc3 CM |
3299 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
3300 | { | |
3301 | struct btrfs_inode *ei; | |
3302 | ||
3303 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); | |
3304 | if (!ei) | |
3305 | return NULL; | |
15ee9bc7 | 3306 | ei->last_trans = 0; |
e6dcd2dc | 3307 | btrfs_ordered_inode_tree_init(&ei->ordered_tree); |
33268eaf JB |
3308 | ei->i_acl = BTRFS_ACL_NOT_CACHED; |
3309 | ei->i_default_acl = BTRFS_ACL_NOT_CACHED; | |
7b128766 | 3310 | INIT_LIST_HEAD(&ei->i_orphan); |
39279cc3 CM |
3311 | return &ei->vfs_inode; |
3312 | } | |
3313 | ||
3314 | void btrfs_destroy_inode(struct inode *inode) | |
3315 | { | |
e6dcd2dc | 3316 | struct btrfs_ordered_extent *ordered; |
39279cc3 CM |
3317 | WARN_ON(!list_empty(&inode->i_dentry)); |
3318 | WARN_ON(inode->i_data.nrpages); | |
3319 | ||
33268eaf JB |
3320 | if (BTRFS_I(inode)->i_acl && |
3321 | BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED) | |
3322 | posix_acl_release(BTRFS_I(inode)->i_acl); | |
3323 | if (BTRFS_I(inode)->i_default_acl && | |
3324 | BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED) | |
3325 | posix_acl_release(BTRFS_I(inode)->i_default_acl); | |
3326 | ||
bcc63abb | 3327 | spin_lock(&BTRFS_I(inode)->root->list_lock); |
7b128766 JB |
3328 | if (!list_empty(&BTRFS_I(inode)->i_orphan)) { |
3329 | printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan" | |
3330 | " list\n", inode->i_ino); | |
3331 | dump_stack(); | |
3332 | } | |
bcc63abb | 3333 | spin_unlock(&BTRFS_I(inode)->root->list_lock); |
7b128766 | 3334 | |
e6dcd2dc CM |
3335 | while(1) { |
3336 | ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1); | |
3337 | if (!ordered) | |
3338 | break; | |
3339 | else { | |
3340 | printk("found ordered extent %Lu %Lu\n", | |
3341 | ordered->file_offset, ordered->len); | |
3342 | btrfs_remove_ordered_extent(inode, ordered); | |
3343 | btrfs_put_ordered_extent(ordered); | |
3344 | btrfs_put_ordered_extent(ordered); | |
3345 | } | |
3346 | } | |
8c416c9e | 3347 | btrfs_drop_extent_cache(inode, 0, (u64)-1); |
39279cc3 CM |
3348 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
3349 | } | |
3350 | ||
0ee0fda0 SW |
3351 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26) |
3352 | static void init_once(void *foo) | |
3353 | #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) | |
44ec0b71 CM |
3354 | static void init_once(struct kmem_cache * cachep, void *foo) |
3355 | #else | |
39279cc3 CM |
3356 | static void init_once(void * foo, struct kmem_cache * cachep, |
3357 | unsigned long flags) | |
44ec0b71 | 3358 | #endif |
39279cc3 CM |
3359 | { |
3360 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; | |
3361 | ||
3362 | inode_init_once(&ei->vfs_inode); | |
3363 | } | |
3364 | ||
3365 | void btrfs_destroy_cachep(void) | |
3366 | { | |
3367 | if (btrfs_inode_cachep) | |
3368 | kmem_cache_destroy(btrfs_inode_cachep); | |
3369 | if (btrfs_trans_handle_cachep) | |
3370 | kmem_cache_destroy(btrfs_trans_handle_cachep); | |
3371 | if (btrfs_transaction_cachep) | |
3372 | kmem_cache_destroy(btrfs_transaction_cachep); | |
3373 | if (btrfs_bit_radix_cachep) | |
3374 | kmem_cache_destroy(btrfs_bit_radix_cachep); | |
3375 | if (btrfs_path_cachep) | |
3376 | kmem_cache_destroy(btrfs_path_cachep); | |
3377 | } | |
3378 | ||
86479a04 | 3379 | struct kmem_cache *btrfs_cache_create(const char *name, size_t size, |
92fee66d | 3380 | unsigned long extra_flags, |
0ee0fda0 SW |
3381 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26) |
3382 | void (*ctor)(void *) | |
3383 | #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23) | |
44ec0b71 CM |
3384 | void (*ctor)(struct kmem_cache *, void *) |
3385 | #else | |
92fee66d | 3386 | void (*ctor)(void *, struct kmem_cache *, |
44ec0b71 CM |
3387 | unsigned long) |
3388 | #endif | |
3389 | ) | |
92fee66d CM |
3390 | { |
3391 | return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT | | |
3392 | SLAB_MEM_SPREAD | extra_flags), ctor | |
3393 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) | |
3394 | ,NULL | |
3395 | #endif | |
3396 | ); | |
3397 | } | |
3398 | ||
39279cc3 CM |
3399 | int btrfs_init_cachep(void) |
3400 | { | |
86479a04 | 3401 | btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache", |
92fee66d CM |
3402 | sizeof(struct btrfs_inode), |
3403 | 0, init_once); | |
39279cc3 CM |
3404 | if (!btrfs_inode_cachep) |
3405 | goto fail; | |
86479a04 CM |
3406 | btrfs_trans_handle_cachep = |
3407 | btrfs_cache_create("btrfs_trans_handle_cache", | |
3408 | sizeof(struct btrfs_trans_handle), | |
3409 | 0, NULL); | |
39279cc3 CM |
3410 | if (!btrfs_trans_handle_cachep) |
3411 | goto fail; | |
86479a04 | 3412 | btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache", |
39279cc3 | 3413 | sizeof(struct btrfs_transaction), |
92fee66d | 3414 | 0, NULL); |
39279cc3 CM |
3415 | if (!btrfs_transaction_cachep) |
3416 | goto fail; | |
86479a04 | 3417 | btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache", |
23223584 | 3418 | sizeof(struct btrfs_path), |
92fee66d | 3419 | 0, NULL); |
39279cc3 CM |
3420 | if (!btrfs_path_cachep) |
3421 | goto fail; | |
86479a04 | 3422 | btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256, |
92fee66d | 3423 | SLAB_DESTROY_BY_RCU, NULL); |
39279cc3 CM |
3424 | if (!btrfs_bit_radix_cachep) |
3425 | goto fail; | |
3426 | return 0; | |
3427 | fail: | |
3428 | btrfs_destroy_cachep(); | |
3429 | return -ENOMEM; | |
3430 | } | |
3431 | ||
3432 | static int btrfs_getattr(struct vfsmount *mnt, | |
3433 | struct dentry *dentry, struct kstat *stat) | |
3434 | { | |
3435 | struct inode *inode = dentry->d_inode; | |
3436 | generic_fillattr(inode, stat); | |
d6667462 | 3437 | stat->blksize = PAGE_CACHE_SIZE; |
9069218d | 3438 | stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9); |
39279cc3 CM |
3439 | return 0; |
3440 | } | |
3441 | ||
3442 | static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, | |
3443 | struct inode * new_dir,struct dentry *new_dentry) | |
3444 | { | |
3445 | struct btrfs_trans_handle *trans; | |
3446 | struct btrfs_root *root = BTRFS_I(old_dir)->root; | |
3447 | struct inode *new_inode = new_dentry->d_inode; | |
3448 | struct inode *old_inode = old_dentry->d_inode; | |
3449 | struct timespec ctime = CURRENT_TIME; | |
00e4e6b3 | 3450 | u64 index = 0; |
39279cc3 CM |
3451 | int ret; |
3452 | ||
3453 | if (S_ISDIR(old_inode->i_mode) && new_inode && | |
3454 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { | |
3455 | return -ENOTEMPTY; | |
3456 | } | |
5f39d397 | 3457 | |
1832a6d5 CM |
3458 | ret = btrfs_check_free_space(root, 1, 0); |
3459 | if (ret) | |
3460 | goto out_unlock; | |
3461 | ||
39279cc3 | 3462 | trans = btrfs_start_transaction(root, 1); |
5f39d397 | 3463 | |
39279cc3 | 3464 | btrfs_set_trans_block_group(trans, new_dir); |
39279cc3 CM |
3465 | |
3466 | old_dentry->d_inode->i_nlink++; | |
3467 | old_dir->i_ctime = old_dir->i_mtime = ctime; | |
3468 | new_dir->i_ctime = new_dir->i_mtime = ctime; | |
3469 | old_inode->i_ctime = ctime; | |
5f39d397 | 3470 | |
39279cc3 CM |
3471 | ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry); |
3472 | if (ret) | |
3473 | goto out_fail; | |
3474 | ||
3475 | if (new_inode) { | |
3476 | new_inode->i_ctime = CURRENT_TIME; | |
3477 | ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry); | |
3478 | if (ret) | |
3479 | goto out_fail; | |
7b128766 JB |
3480 | if (new_inode->i_nlink == 0) { |
3481 | ret = btrfs_orphan_add(trans, new_inode); | |
3482 | if (ret) | |
3483 | goto out_fail; | |
3484 | } | |
39279cc3 | 3485 | } |
00e4e6b3 | 3486 | ret = btrfs_set_inode_index(new_dir, old_inode, &index); |
aec7477b JB |
3487 | if (ret) |
3488 | goto out_fail; | |
3489 | ||
00e4e6b3 | 3490 | ret = btrfs_add_link(trans, new_dentry, old_inode, 1, index); |
39279cc3 CM |
3491 | if (ret) |
3492 | goto out_fail; | |
3493 | ||
3494 | out_fail: | |
ab78c84d | 3495 | btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 3496 | out_unlock: |
39279cc3 CM |
3497 | return ret; |
3498 | } | |
3499 | ||
ea8c2819 CM |
3500 | int btrfs_start_delalloc_inodes(struct btrfs_root *root) |
3501 | { | |
3502 | struct list_head *head = &root->fs_info->delalloc_inodes; | |
3503 | struct btrfs_inode *binode; | |
3504 | unsigned long flags; | |
3505 | ||
3506 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); | |
3507 | while(!list_empty(head)) { | |
3508 | binode = list_entry(head->next, struct btrfs_inode, | |
3509 | delalloc_inodes); | |
3510 | atomic_inc(&binode->vfs_inode.i_count); | |
3511 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); | |
3512 | filemap_write_and_wait(binode->vfs_inode.i_mapping); | |
3513 | iput(&binode->vfs_inode); | |
3514 | spin_lock_irqsave(&root->fs_info->delalloc_lock, flags); | |
3515 | } | |
3516 | spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); | |
3517 | return 0; | |
3518 | } | |
3519 | ||
39279cc3 CM |
3520 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
3521 | const char *symname) | |
3522 | { | |
3523 | struct btrfs_trans_handle *trans; | |
3524 | struct btrfs_root *root = BTRFS_I(dir)->root; | |
3525 | struct btrfs_path *path; | |
3526 | struct btrfs_key key; | |
1832a6d5 | 3527 | struct inode *inode = NULL; |
39279cc3 CM |
3528 | int err; |
3529 | int drop_inode = 0; | |
3530 | u64 objectid; | |
00e4e6b3 | 3531 | u64 index = 0 ; |
39279cc3 CM |
3532 | int name_len; |
3533 | int datasize; | |
5f39d397 | 3534 | unsigned long ptr; |
39279cc3 | 3535 | struct btrfs_file_extent_item *ei; |
5f39d397 | 3536 | struct extent_buffer *leaf; |
1832a6d5 | 3537 | unsigned long nr = 0; |
39279cc3 CM |
3538 | |
3539 | name_len = strlen(symname) + 1; | |
3540 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) | |
3541 | return -ENAMETOOLONG; | |
1832a6d5 | 3542 | |
1832a6d5 CM |
3543 | err = btrfs_check_free_space(root, 1, 0); |
3544 | if (err) | |
3545 | goto out_fail; | |
3546 | ||
39279cc3 CM |
3547 | trans = btrfs_start_transaction(root, 1); |
3548 | btrfs_set_trans_block_group(trans, dir); | |
3549 | ||
3550 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); | |
3551 | if (err) { | |
3552 | err = -ENOSPC; | |
3553 | goto out_unlock; | |
3554 | } | |
3555 | ||
aec7477b | 3556 | inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, |
9c58309d CM |
3557 | dentry->d_name.len, |
3558 | dentry->d_parent->d_inode->i_ino, objectid, | |
00e4e6b3 CM |
3559 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, |
3560 | &index); | |
39279cc3 CM |
3561 | err = PTR_ERR(inode); |
3562 | if (IS_ERR(inode)) | |
3563 | goto out_unlock; | |
3564 | ||
33268eaf JB |
3565 | err = btrfs_init_acl(inode, dir); |
3566 | if (err) { | |
3567 | drop_inode = 1; | |
3568 | goto out_unlock; | |
3569 | } | |
3570 | ||
39279cc3 | 3571 | btrfs_set_trans_block_group(trans, inode); |
00e4e6b3 | 3572 | err = btrfs_add_nondir(trans, dentry, inode, 0, index); |
39279cc3 CM |
3573 | if (err) |
3574 | drop_inode = 1; | |
3575 | else { | |
3576 | inode->i_mapping->a_ops = &btrfs_aops; | |
04160088 | 3577 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
39279cc3 CM |
3578 | inode->i_fop = &btrfs_file_operations; |
3579 | inode->i_op = &btrfs_file_inode_operations; | |
d1310b2e CM |
3580 | extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS); |
3581 | extent_io_tree_init(&BTRFS_I(inode)->io_tree, | |
a52d9a80 | 3582 | inode->i_mapping, GFP_NOFS); |
7e38326f CM |
3583 | extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree, |
3584 | inode->i_mapping, GFP_NOFS); | |
ea8c2819 | 3585 | INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes); |
1b1e2135 | 3586 | mutex_init(&BTRFS_I(inode)->csum_mutex); |
ee6e6504 | 3587 | mutex_init(&BTRFS_I(inode)->extent_mutex); |
9069218d | 3588 | BTRFS_I(inode)->delalloc_bytes = 0; |
dbe674a9 | 3589 | BTRFS_I(inode)->disk_i_size = 0; |
db69e0eb | 3590 | inode->i_mapping->writeback_index = 0; |
d1310b2e | 3591 | BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; |
ba1da2f4 | 3592 | btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree); |
39279cc3 CM |
3593 | } |
3594 | dir->i_sb->s_dirt = 1; | |
3595 | btrfs_update_inode_block_group(trans, inode); | |
3596 | btrfs_update_inode_block_group(trans, dir); | |
3597 | if (drop_inode) | |
3598 | goto out_unlock; | |
3599 | ||
3600 | path = btrfs_alloc_path(); | |
3601 | BUG_ON(!path); | |
3602 | key.objectid = inode->i_ino; | |
3603 | key.offset = 0; | |
39279cc3 CM |
3604 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
3605 | datasize = btrfs_file_extent_calc_inline_size(name_len); | |
3606 | err = btrfs_insert_empty_item(trans, root, path, &key, | |
3607 | datasize); | |
54aa1f4d CM |
3608 | if (err) { |
3609 | drop_inode = 1; | |
3610 | goto out_unlock; | |
3611 | } | |
5f39d397 CM |
3612 | leaf = path->nodes[0]; |
3613 | ei = btrfs_item_ptr(leaf, path->slots[0], | |
3614 | struct btrfs_file_extent_item); | |
3615 | btrfs_set_file_extent_generation(leaf, ei, trans->transid); | |
3616 | btrfs_set_file_extent_type(leaf, ei, | |
39279cc3 CM |
3617 | BTRFS_FILE_EXTENT_INLINE); |
3618 | ptr = btrfs_file_extent_inline_start(ei); | |
5f39d397 CM |
3619 | write_extent_buffer(leaf, symname, ptr, name_len); |
3620 | btrfs_mark_buffer_dirty(leaf); | |
39279cc3 | 3621 | btrfs_free_path(path); |
5f39d397 | 3622 | |
39279cc3 CM |
3623 | inode->i_op = &btrfs_symlink_inode_operations; |
3624 | inode->i_mapping->a_ops = &btrfs_symlink_aops; | |
04160088 | 3625 | inode->i_mapping->backing_dev_info = &root->fs_info->bdi; |
dbe674a9 | 3626 | btrfs_i_size_write(inode, name_len - 1); |
54aa1f4d CM |
3627 | err = btrfs_update_inode(trans, root, inode); |
3628 | if (err) | |
3629 | drop_inode = 1; | |
39279cc3 CM |
3630 | |
3631 | out_unlock: | |
d3c2fdcf | 3632 | nr = trans->blocks_used; |
ab78c84d | 3633 | btrfs_end_transaction_throttle(trans, root); |
1832a6d5 | 3634 | out_fail: |
39279cc3 CM |
3635 | if (drop_inode) { |
3636 | inode_dec_link_count(inode); | |
3637 | iput(inode); | |
3638 | } | |
d3c2fdcf | 3639 | btrfs_btree_balance_dirty(root, nr); |
39279cc3 CM |
3640 | return err; |
3641 | } | |
16432985 | 3642 | |
e6dcd2dc CM |
3643 | static int btrfs_set_page_dirty(struct page *page) |
3644 | { | |
e6dcd2dc CM |
3645 | return __set_page_dirty_nobuffers(page); |
3646 | } | |
3647 | ||
0ee0fda0 SW |
3648 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26) |
3649 | static int btrfs_permission(struct inode *inode, int mask) | |
3650 | #else | |
fdebe2bd Y |
3651 | static int btrfs_permission(struct inode *inode, int mask, |
3652 | struct nameidata *nd) | |
0ee0fda0 | 3653 | #endif |
fdebe2bd Y |
3654 | { |
3655 | if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE)) | |
3656 | return -EACCES; | |
33268eaf | 3657 | return generic_permission(inode, mask, btrfs_check_acl); |
fdebe2bd | 3658 | } |
39279cc3 CM |
3659 | |
3660 | static struct inode_operations btrfs_dir_inode_operations = { | |
3661 | .lookup = btrfs_lookup, | |
3662 | .create = btrfs_create, | |
3663 | .unlink = btrfs_unlink, | |
3664 | .link = btrfs_link, | |
3665 | .mkdir = btrfs_mkdir, | |
3666 | .rmdir = btrfs_rmdir, | |
3667 | .rename = btrfs_rename, | |
3668 | .symlink = btrfs_symlink, | |
3669 | .setattr = btrfs_setattr, | |
618e21d5 | 3670 | .mknod = btrfs_mknod, |
95819c05 CH |
3671 | .setxattr = btrfs_setxattr, |
3672 | .getxattr = btrfs_getxattr, | |
5103e947 | 3673 | .listxattr = btrfs_listxattr, |
95819c05 | 3674 | .removexattr = btrfs_removexattr, |
fdebe2bd | 3675 | .permission = btrfs_permission, |
39279cc3 | 3676 | }; |
39279cc3 CM |
3677 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
3678 | .lookup = btrfs_lookup, | |
fdebe2bd | 3679 | .permission = btrfs_permission, |
39279cc3 | 3680 | }; |
39279cc3 CM |
3681 | static struct file_operations btrfs_dir_file_operations = { |
3682 | .llseek = generic_file_llseek, | |
3683 | .read = generic_read_dir, | |
cbdf5a24 DW |
3684 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28) |
3685 | .readdir = btrfs_nfshack_readdir, | |
3686 | #else /* NFSd readdir/lookup deadlock is fixed */ | |
3687 | .readdir = btrfs_real_readdir, | |
3688 | #endif | |
34287aa3 | 3689 | .unlocked_ioctl = btrfs_ioctl, |
39279cc3 | 3690 | #ifdef CONFIG_COMPAT |
34287aa3 | 3691 | .compat_ioctl = btrfs_ioctl, |
39279cc3 | 3692 | #endif |
6bf13c0c | 3693 | .release = btrfs_release_file, |
39279cc3 CM |
3694 | }; |
3695 | ||
d1310b2e | 3696 | static struct extent_io_ops btrfs_extent_io_ops = { |
07157aac | 3697 | .fill_delalloc = run_delalloc_range, |
065631f6 | 3698 | .submit_bio_hook = btrfs_submit_bio_hook, |
239b14b3 | 3699 | .merge_bio_hook = btrfs_merge_bio_hook, |
07157aac | 3700 | .readpage_end_io_hook = btrfs_readpage_end_io_hook, |
e6dcd2dc | 3701 | .writepage_end_io_hook = btrfs_writepage_end_io_hook, |
247e743c | 3702 | .writepage_start_hook = btrfs_writepage_start_hook, |
1259ab75 | 3703 | .readpage_io_failed_hook = btrfs_io_failed_hook, |
b0c68f8b CM |
3704 | .set_bit_hook = btrfs_set_bit_hook, |
3705 | .clear_bit_hook = btrfs_clear_bit_hook, | |
07157aac CM |
3706 | }; |
3707 | ||
39279cc3 CM |
3708 | static struct address_space_operations btrfs_aops = { |
3709 | .readpage = btrfs_readpage, | |
3710 | .writepage = btrfs_writepage, | |
b293f02e | 3711 | .writepages = btrfs_writepages, |
3ab2fb5a | 3712 | .readpages = btrfs_readpages, |
39279cc3 | 3713 | .sync_page = block_sync_page, |
39279cc3 | 3714 | .bmap = btrfs_bmap, |
16432985 | 3715 | .direct_IO = btrfs_direct_IO, |
a52d9a80 CM |
3716 | .invalidatepage = btrfs_invalidatepage, |
3717 | .releasepage = btrfs_releasepage, | |
e6dcd2dc | 3718 | .set_page_dirty = btrfs_set_page_dirty, |
39279cc3 CM |
3719 | }; |
3720 | ||
3721 | static struct address_space_operations btrfs_symlink_aops = { | |
3722 | .readpage = btrfs_readpage, | |
3723 | .writepage = btrfs_writepage, | |
2bf5a725 CM |
3724 | .invalidatepage = btrfs_invalidatepage, |
3725 | .releasepage = btrfs_releasepage, | |
39279cc3 CM |
3726 | }; |
3727 | ||
3728 | static struct inode_operations btrfs_file_inode_operations = { | |
3729 | .truncate = btrfs_truncate, | |
3730 | .getattr = btrfs_getattr, | |
3731 | .setattr = btrfs_setattr, | |
95819c05 CH |
3732 | .setxattr = btrfs_setxattr, |
3733 | .getxattr = btrfs_getxattr, | |
5103e947 | 3734 | .listxattr = btrfs_listxattr, |
95819c05 | 3735 | .removexattr = btrfs_removexattr, |
fdebe2bd | 3736 | .permission = btrfs_permission, |
39279cc3 | 3737 | }; |
618e21d5 JB |
3738 | static struct inode_operations btrfs_special_inode_operations = { |
3739 | .getattr = btrfs_getattr, | |
3740 | .setattr = btrfs_setattr, | |
fdebe2bd | 3741 | .permission = btrfs_permission, |
95819c05 CH |
3742 | .setxattr = btrfs_setxattr, |
3743 | .getxattr = btrfs_getxattr, | |
33268eaf | 3744 | .listxattr = btrfs_listxattr, |
95819c05 | 3745 | .removexattr = btrfs_removexattr, |
618e21d5 | 3746 | }; |
39279cc3 CM |
3747 | static struct inode_operations btrfs_symlink_inode_operations = { |
3748 | .readlink = generic_readlink, | |
3749 | .follow_link = page_follow_link_light, | |
3750 | .put_link = page_put_link, | |
fdebe2bd | 3751 | .permission = btrfs_permission, |
39279cc3 | 3752 | }; |