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