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