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