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