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