Btrfs: log changed inodes based on the extent map tree
[deliverable/linux.git] / fs / btrfs / inode.c
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
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
23 #include <linux/fs.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include <linux/slab.h>
40 #include <linux/ratelimit.h>
41 #include <linux/mount.h>
42 #include "compat.h"
43 #include "ctree.h"
44 #include "disk-io.h"
45 #include "transaction.h"
46 #include "btrfs_inode.h"
47 #include "ioctl.h"
48 #include "print-tree.h"
49 #include "ordered-data.h"
50 #include "xattr.h"
51 #include "tree-log.h"
52 #include "volumes.h"
53 #include "compression.h"
54 #include "locking.h"
55 #include "free-space-cache.h"
56 #include "inode-map.h"
57
58 struct btrfs_iget_args {
59 u64 ino;
60 struct btrfs_root *root;
61 };
62
63 static const struct inode_operations btrfs_dir_inode_operations;
64 static const struct inode_operations btrfs_symlink_inode_operations;
65 static const struct inode_operations btrfs_dir_ro_inode_operations;
66 static const struct inode_operations btrfs_special_inode_operations;
67 static const struct inode_operations btrfs_file_inode_operations;
68 static const struct address_space_operations btrfs_aops;
69 static const struct address_space_operations btrfs_symlink_aops;
70 static const struct file_operations btrfs_dir_file_operations;
71 static struct extent_io_ops btrfs_extent_io_ops;
72
73 static struct kmem_cache *btrfs_inode_cachep;
74 static struct kmem_cache *btrfs_delalloc_work_cachep;
75 struct kmem_cache *btrfs_trans_handle_cachep;
76 struct kmem_cache *btrfs_transaction_cachep;
77 struct kmem_cache *btrfs_path_cachep;
78 struct kmem_cache *btrfs_free_space_cachep;
79
80 #define S_SHIFT 12
81 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
82 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
83 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
84 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
85 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
86 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
87 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
88 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
89 };
90
91 static int btrfs_setsize(struct inode *inode, loff_t newsize);
92 static int btrfs_truncate(struct inode *inode);
93 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
94 static noinline int cow_file_range(struct inode *inode,
95 struct page *locked_page,
96 u64 start, u64 end, int *page_started,
97 unsigned long *nr_written, int unlock);
98 static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
99 u64 len, u64 orig_start,
100 u64 block_start, u64 block_len,
101 u64 orig_block_len, int type);
102
103 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
104 struct inode *inode, struct inode *dir,
105 const struct qstr *qstr)
106 {
107 int err;
108
109 err = btrfs_init_acl(trans, inode, dir);
110 if (!err)
111 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
112 return err;
113 }
114
115 /*
116 * this does all the hard work for inserting an inline extent into
117 * the btree. The caller should have done a btrfs_drop_extents so that
118 * no overlapping inline items exist in the btree
119 */
120 static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
121 struct btrfs_root *root, struct inode *inode,
122 u64 start, size_t size, size_t compressed_size,
123 int compress_type,
124 struct page **compressed_pages)
125 {
126 struct btrfs_key key;
127 struct btrfs_path *path;
128 struct extent_buffer *leaf;
129 struct page *page = NULL;
130 char *kaddr;
131 unsigned long ptr;
132 struct btrfs_file_extent_item *ei;
133 int err = 0;
134 int ret;
135 size_t cur_size = size;
136 size_t datasize;
137 unsigned long offset;
138
139 if (compressed_size && compressed_pages)
140 cur_size = compressed_size;
141
142 path = btrfs_alloc_path();
143 if (!path)
144 return -ENOMEM;
145
146 path->leave_spinning = 1;
147
148 key.objectid = btrfs_ino(inode);
149 key.offset = start;
150 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
151 datasize = btrfs_file_extent_calc_inline_size(cur_size);
152
153 inode_add_bytes(inode, size);
154 ret = btrfs_insert_empty_item(trans, root, path, &key,
155 datasize);
156 if (ret) {
157 err = ret;
158 goto fail;
159 }
160 leaf = path->nodes[0];
161 ei = btrfs_item_ptr(leaf, path->slots[0],
162 struct btrfs_file_extent_item);
163 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
164 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
165 btrfs_set_file_extent_encryption(leaf, ei, 0);
166 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
167 btrfs_set_file_extent_ram_bytes(leaf, ei, size);
168 ptr = btrfs_file_extent_inline_start(ei);
169
170 if (compress_type != BTRFS_COMPRESS_NONE) {
171 struct page *cpage;
172 int i = 0;
173 while (compressed_size > 0) {
174 cpage = compressed_pages[i];
175 cur_size = min_t(unsigned long, compressed_size,
176 PAGE_CACHE_SIZE);
177
178 kaddr = kmap_atomic(cpage);
179 write_extent_buffer(leaf, kaddr, ptr, cur_size);
180 kunmap_atomic(kaddr);
181
182 i++;
183 ptr += cur_size;
184 compressed_size -= cur_size;
185 }
186 btrfs_set_file_extent_compression(leaf, ei,
187 compress_type);
188 } else {
189 page = find_get_page(inode->i_mapping,
190 start >> PAGE_CACHE_SHIFT);
191 btrfs_set_file_extent_compression(leaf, ei, 0);
192 kaddr = kmap_atomic(page);
193 offset = start & (PAGE_CACHE_SIZE - 1);
194 write_extent_buffer(leaf, kaddr + offset, ptr, size);
195 kunmap_atomic(kaddr);
196 page_cache_release(page);
197 }
198 btrfs_mark_buffer_dirty(leaf);
199 btrfs_free_path(path);
200
201 /*
202 * we're an inline extent, so nobody can
203 * extend the file past i_size without locking
204 * a page we already have locked.
205 *
206 * We must do any isize and inode updates
207 * before we unlock the pages. Otherwise we
208 * could end up racing with unlink.
209 */
210 BTRFS_I(inode)->disk_i_size = inode->i_size;
211 ret = btrfs_update_inode(trans, root, inode);
212
213 return ret;
214 fail:
215 btrfs_free_path(path);
216 return err;
217 }
218
219
220 /*
221 * conditionally insert an inline extent into the file. This
222 * does the checks required to make sure the data is small enough
223 * to fit as an inline extent.
224 */
225 static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
226 struct btrfs_root *root,
227 struct inode *inode, u64 start, u64 end,
228 size_t compressed_size, int compress_type,
229 struct page **compressed_pages)
230 {
231 u64 isize = i_size_read(inode);
232 u64 actual_end = min(end + 1, isize);
233 u64 inline_len = actual_end - start;
234 u64 aligned_end = (end + root->sectorsize - 1) &
235 ~((u64)root->sectorsize - 1);
236 u64 data_len = inline_len;
237 int ret;
238
239 if (compressed_size)
240 data_len = compressed_size;
241
242 if (start > 0 ||
243 actual_end >= PAGE_CACHE_SIZE ||
244 data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
245 (!compressed_size &&
246 (actual_end & (root->sectorsize - 1)) == 0) ||
247 end + 1 < isize ||
248 data_len > root->fs_info->max_inline) {
249 return 1;
250 }
251
252 ret = btrfs_drop_extents(trans, root, inode, start, aligned_end, 1);
253 if (ret)
254 return ret;
255
256 if (isize > actual_end)
257 inline_len = min_t(u64, isize, actual_end);
258 ret = insert_inline_extent(trans, root, inode, start,
259 inline_len, compressed_size,
260 compress_type, compressed_pages);
261 if (ret && ret != -ENOSPC) {
262 btrfs_abort_transaction(trans, root, ret);
263 return ret;
264 } else if (ret == -ENOSPC) {
265 return 1;
266 }
267
268 btrfs_delalloc_release_metadata(inode, end + 1 - start);
269 btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
270 return 0;
271 }
272
273 struct async_extent {
274 u64 start;
275 u64 ram_size;
276 u64 compressed_size;
277 struct page **pages;
278 unsigned long nr_pages;
279 int compress_type;
280 struct list_head list;
281 };
282
283 struct async_cow {
284 struct inode *inode;
285 struct btrfs_root *root;
286 struct page *locked_page;
287 u64 start;
288 u64 end;
289 struct list_head extents;
290 struct btrfs_work work;
291 };
292
293 static noinline int add_async_extent(struct async_cow *cow,
294 u64 start, u64 ram_size,
295 u64 compressed_size,
296 struct page **pages,
297 unsigned long nr_pages,
298 int compress_type)
299 {
300 struct async_extent *async_extent;
301
302 async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
303 BUG_ON(!async_extent); /* -ENOMEM */
304 async_extent->start = start;
305 async_extent->ram_size = ram_size;
306 async_extent->compressed_size = compressed_size;
307 async_extent->pages = pages;
308 async_extent->nr_pages = nr_pages;
309 async_extent->compress_type = compress_type;
310 list_add_tail(&async_extent->list, &cow->extents);
311 return 0;
312 }
313
314 /*
315 * we create compressed extents in two phases. The first
316 * phase compresses a range of pages that have already been
317 * locked (both pages and state bits are locked).
318 *
319 * This is done inside an ordered work queue, and the compression
320 * is spread across many cpus. The actual IO submission is step
321 * two, and the ordered work queue takes care of making sure that
322 * happens in the same order things were put onto the queue by
323 * writepages and friends.
324 *
325 * If this code finds it can't get good compression, it puts an
326 * entry onto the work queue to write the uncompressed bytes. This
327 * makes sure that both compressed inodes and uncompressed inodes
328 * are written in the same order that the flusher thread sent them
329 * down.
330 */
331 static noinline int compress_file_range(struct inode *inode,
332 struct page *locked_page,
333 u64 start, u64 end,
334 struct async_cow *async_cow,
335 int *num_added)
336 {
337 struct btrfs_root *root = BTRFS_I(inode)->root;
338 struct btrfs_trans_handle *trans;
339 u64 num_bytes;
340 u64 blocksize = root->sectorsize;
341 u64 actual_end;
342 u64 isize = i_size_read(inode);
343 int ret = 0;
344 struct page **pages = NULL;
345 unsigned long nr_pages;
346 unsigned long nr_pages_ret = 0;
347 unsigned long total_compressed = 0;
348 unsigned long total_in = 0;
349 unsigned long max_compressed = 128 * 1024;
350 unsigned long max_uncompressed = 128 * 1024;
351 int i;
352 int will_compress;
353 int compress_type = root->fs_info->compress_type;
354
355 /* if this is a small write inside eof, kick off a defrag */
356 if ((end - start + 1) < 16 * 1024 &&
357 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
358 btrfs_add_inode_defrag(NULL, inode);
359
360 actual_end = min_t(u64, isize, end + 1);
361 again:
362 will_compress = 0;
363 nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
364 nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
365
366 /*
367 * we don't want to send crud past the end of i_size through
368 * compression, that's just a waste of CPU time. So, if the
369 * end of the file is before the start of our current
370 * requested range of bytes, we bail out to the uncompressed
371 * cleanup code that can deal with all of this.
372 *
373 * It isn't really the fastest way to fix things, but this is a
374 * very uncommon corner.
375 */
376 if (actual_end <= start)
377 goto cleanup_and_bail_uncompressed;
378
379 total_compressed = actual_end - start;
380
381 /* we want to make sure that amount of ram required to uncompress
382 * an extent is reasonable, so we limit the total size in ram
383 * of a compressed extent to 128k. This is a crucial number
384 * because it also controls how easily we can spread reads across
385 * cpus for decompression.
386 *
387 * We also want to make sure the amount of IO required to do
388 * a random read is reasonably small, so we limit the size of
389 * a compressed extent to 128k.
390 */
391 total_compressed = min(total_compressed, max_uncompressed);
392 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
393 num_bytes = max(blocksize, num_bytes);
394 total_in = 0;
395 ret = 0;
396
397 /*
398 * we do compression for mount -o compress and when the
399 * inode has not been flagged as nocompress. This flag can
400 * change at any time if we discover bad compression ratios.
401 */
402 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
403 (btrfs_test_opt(root, COMPRESS) ||
404 (BTRFS_I(inode)->force_compress) ||
405 (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
406 WARN_ON(pages);
407 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
408 if (!pages) {
409 /* just bail out to the uncompressed code */
410 goto cont;
411 }
412
413 if (BTRFS_I(inode)->force_compress)
414 compress_type = BTRFS_I(inode)->force_compress;
415
416 ret = btrfs_compress_pages(compress_type,
417 inode->i_mapping, start,
418 total_compressed, pages,
419 nr_pages, &nr_pages_ret,
420 &total_in,
421 &total_compressed,
422 max_compressed);
423
424 if (!ret) {
425 unsigned long offset = total_compressed &
426 (PAGE_CACHE_SIZE - 1);
427 struct page *page = pages[nr_pages_ret - 1];
428 char *kaddr;
429
430 /* zero the tail end of the last page, we might be
431 * sending it down to disk
432 */
433 if (offset) {
434 kaddr = kmap_atomic(page);
435 memset(kaddr + offset, 0,
436 PAGE_CACHE_SIZE - offset);
437 kunmap_atomic(kaddr);
438 }
439 will_compress = 1;
440 }
441 }
442 cont:
443 if (start == 0) {
444 trans = btrfs_join_transaction(root);
445 if (IS_ERR(trans)) {
446 ret = PTR_ERR(trans);
447 trans = NULL;
448 goto cleanup_and_out;
449 }
450 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
451
452 /* lets try to make an inline extent */
453 if (ret || total_in < (actual_end - start)) {
454 /* we didn't compress the entire range, try
455 * to make an uncompressed inline extent.
456 */
457 ret = cow_file_range_inline(trans, root, inode,
458 start, end, 0, 0, NULL);
459 } else {
460 /* try making a compressed inline extent */
461 ret = cow_file_range_inline(trans, root, inode,
462 start, end,
463 total_compressed,
464 compress_type, pages);
465 }
466 if (ret <= 0) {
467 /*
468 * inline extent creation worked or returned error,
469 * we don't need to create any more async work items.
470 * Unlock and free up our temp pages.
471 */
472 extent_clear_unlock_delalloc(inode,
473 &BTRFS_I(inode)->io_tree,
474 start, end, NULL,
475 EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
476 EXTENT_CLEAR_DELALLOC |
477 EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
478
479 btrfs_end_transaction(trans, root);
480 goto free_pages_out;
481 }
482 btrfs_end_transaction(trans, root);
483 }
484
485 if (will_compress) {
486 /*
487 * we aren't doing an inline extent round the compressed size
488 * up to a block size boundary so the allocator does sane
489 * things
490 */
491 total_compressed = (total_compressed + blocksize - 1) &
492 ~(blocksize - 1);
493
494 /*
495 * one last check to make sure the compression is really a
496 * win, compare the page count read with the blocks on disk
497 */
498 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
499 ~(PAGE_CACHE_SIZE - 1);
500 if (total_compressed >= total_in) {
501 will_compress = 0;
502 } else {
503 num_bytes = total_in;
504 }
505 }
506 if (!will_compress && pages) {
507 /*
508 * the compression code ran but failed to make things smaller,
509 * free any pages it allocated and our page pointer array
510 */
511 for (i = 0; i < nr_pages_ret; i++) {
512 WARN_ON(pages[i]->mapping);
513 page_cache_release(pages[i]);
514 }
515 kfree(pages);
516 pages = NULL;
517 total_compressed = 0;
518 nr_pages_ret = 0;
519
520 /* flag the file so we don't compress in the future */
521 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
522 !(BTRFS_I(inode)->force_compress)) {
523 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
524 }
525 }
526 if (will_compress) {
527 *num_added += 1;
528
529 /* the async work queues will take care of doing actual
530 * allocation on disk for these compressed pages,
531 * and will submit them to the elevator.
532 */
533 add_async_extent(async_cow, start, num_bytes,
534 total_compressed, pages, nr_pages_ret,
535 compress_type);
536
537 if (start + num_bytes < end) {
538 start += num_bytes;
539 pages = NULL;
540 cond_resched();
541 goto again;
542 }
543 } else {
544 cleanup_and_bail_uncompressed:
545 /*
546 * No compression, but we still need to write the pages in
547 * the file we've been given so far. redirty the locked
548 * page if it corresponds to our extent and set things up
549 * for the async work queue to run cow_file_range to do
550 * the normal delalloc dance
551 */
552 if (page_offset(locked_page) >= start &&
553 page_offset(locked_page) <= end) {
554 __set_page_dirty_nobuffers(locked_page);
555 /* unlocked later on in the async handlers */
556 }
557 add_async_extent(async_cow, start, end - start + 1,
558 0, NULL, 0, BTRFS_COMPRESS_NONE);
559 *num_added += 1;
560 }
561
562 out:
563 return ret;
564
565 free_pages_out:
566 for (i = 0; i < nr_pages_ret; i++) {
567 WARN_ON(pages[i]->mapping);
568 page_cache_release(pages[i]);
569 }
570 kfree(pages);
571
572 goto out;
573
574 cleanup_and_out:
575 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
576 start, end, NULL,
577 EXTENT_CLEAR_UNLOCK_PAGE |
578 EXTENT_CLEAR_DIRTY |
579 EXTENT_CLEAR_DELALLOC |
580 EXTENT_SET_WRITEBACK |
581 EXTENT_END_WRITEBACK);
582 if (!trans || IS_ERR(trans))
583 btrfs_error(root->fs_info, ret, "Failed to join transaction");
584 else
585 btrfs_abort_transaction(trans, root, ret);
586 goto free_pages_out;
587 }
588
589 /*
590 * phase two of compressed writeback. This is the ordered portion
591 * of the code, which only gets called in the order the work was
592 * queued. We walk all the async extents created by compress_file_range
593 * and send them down to the disk.
594 */
595 static noinline int submit_compressed_extents(struct inode *inode,
596 struct async_cow *async_cow)
597 {
598 struct async_extent *async_extent;
599 u64 alloc_hint = 0;
600 struct btrfs_trans_handle *trans;
601 struct btrfs_key ins;
602 struct extent_map *em;
603 struct btrfs_root *root = BTRFS_I(inode)->root;
604 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
605 struct extent_io_tree *io_tree;
606 int ret = 0;
607
608 if (list_empty(&async_cow->extents))
609 return 0;
610
611
612 while (!list_empty(&async_cow->extents)) {
613 async_extent = list_entry(async_cow->extents.next,
614 struct async_extent, list);
615 list_del(&async_extent->list);
616
617 io_tree = &BTRFS_I(inode)->io_tree;
618
619 retry:
620 /* did the compression code fall back to uncompressed IO? */
621 if (!async_extent->pages) {
622 int page_started = 0;
623 unsigned long nr_written = 0;
624
625 lock_extent(io_tree, async_extent->start,
626 async_extent->start +
627 async_extent->ram_size - 1);
628
629 /* allocate blocks */
630 ret = cow_file_range(inode, async_cow->locked_page,
631 async_extent->start,
632 async_extent->start +
633 async_extent->ram_size - 1,
634 &page_started, &nr_written, 0);
635
636 /* JDM XXX */
637
638 /*
639 * if page_started, cow_file_range inserted an
640 * inline extent and took care of all the unlocking
641 * and IO for us. Otherwise, we need to submit
642 * all those pages down to the drive.
643 */
644 if (!page_started && !ret)
645 extent_write_locked_range(io_tree,
646 inode, async_extent->start,
647 async_extent->start +
648 async_extent->ram_size - 1,
649 btrfs_get_extent,
650 WB_SYNC_ALL);
651 kfree(async_extent);
652 cond_resched();
653 continue;
654 }
655
656 lock_extent(io_tree, async_extent->start,
657 async_extent->start + async_extent->ram_size - 1);
658
659 trans = btrfs_join_transaction(root);
660 if (IS_ERR(trans)) {
661 ret = PTR_ERR(trans);
662 } else {
663 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
664 ret = btrfs_reserve_extent(trans, root,
665 async_extent->compressed_size,
666 async_extent->compressed_size,
667 0, alloc_hint, &ins, 1);
668 if (ret && ret != -ENOSPC)
669 btrfs_abort_transaction(trans, root, ret);
670 btrfs_end_transaction(trans, root);
671 }
672
673 if (ret) {
674 int i;
675 for (i = 0; i < async_extent->nr_pages; i++) {
676 WARN_ON(async_extent->pages[i]->mapping);
677 page_cache_release(async_extent->pages[i]);
678 }
679 kfree(async_extent->pages);
680 async_extent->nr_pages = 0;
681 async_extent->pages = NULL;
682 unlock_extent(io_tree, async_extent->start,
683 async_extent->start +
684 async_extent->ram_size - 1);
685 if (ret == -ENOSPC)
686 goto retry;
687 goto out_free; /* JDM: Requeue? */
688 }
689
690 /*
691 * here we're doing allocation and writeback of the
692 * compressed pages
693 */
694 btrfs_drop_extent_cache(inode, async_extent->start,
695 async_extent->start +
696 async_extent->ram_size - 1, 0);
697
698 em = alloc_extent_map();
699 BUG_ON(!em); /* -ENOMEM */
700 em->start = async_extent->start;
701 em->len = async_extent->ram_size;
702 em->orig_start = em->start;
703
704 em->block_start = ins.objectid;
705 em->block_len = ins.offset;
706 em->orig_block_len = ins.offset;
707 em->bdev = root->fs_info->fs_devices->latest_bdev;
708 em->compress_type = async_extent->compress_type;
709 set_bit(EXTENT_FLAG_PINNED, &em->flags);
710 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
711 em->generation = -1;
712
713 while (1) {
714 write_lock(&em_tree->lock);
715 ret = add_extent_mapping(em_tree, em);
716 if (!ret)
717 list_move(&em->list,
718 &em_tree->modified_extents);
719 write_unlock(&em_tree->lock);
720 if (ret != -EEXIST) {
721 free_extent_map(em);
722 break;
723 }
724 btrfs_drop_extent_cache(inode, async_extent->start,
725 async_extent->start +
726 async_extent->ram_size - 1, 0);
727 }
728
729 ret = btrfs_add_ordered_extent_compress(inode,
730 async_extent->start,
731 ins.objectid,
732 async_extent->ram_size,
733 ins.offset,
734 BTRFS_ORDERED_COMPRESSED,
735 async_extent->compress_type);
736 BUG_ON(ret); /* -ENOMEM */
737
738 /*
739 * clear dirty, set writeback and unlock the pages.
740 */
741 extent_clear_unlock_delalloc(inode,
742 &BTRFS_I(inode)->io_tree,
743 async_extent->start,
744 async_extent->start +
745 async_extent->ram_size - 1,
746 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
747 EXTENT_CLEAR_UNLOCK |
748 EXTENT_CLEAR_DELALLOC |
749 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
750
751 ret = btrfs_submit_compressed_write(inode,
752 async_extent->start,
753 async_extent->ram_size,
754 ins.objectid,
755 ins.offset, async_extent->pages,
756 async_extent->nr_pages);
757
758 BUG_ON(ret); /* -ENOMEM */
759 alloc_hint = ins.objectid + ins.offset;
760 kfree(async_extent);
761 cond_resched();
762 }
763 ret = 0;
764 out:
765 return ret;
766 out_free:
767 kfree(async_extent);
768 goto out;
769 }
770
771 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
772 u64 num_bytes)
773 {
774 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
775 struct extent_map *em;
776 u64 alloc_hint = 0;
777
778 read_lock(&em_tree->lock);
779 em = search_extent_mapping(em_tree, start, num_bytes);
780 if (em) {
781 /*
782 * if block start isn't an actual block number then find the
783 * first block in this inode and use that as a hint. If that
784 * block is also bogus then just don't worry about it.
785 */
786 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
787 free_extent_map(em);
788 em = search_extent_mapping(em_tree, 0, 0);
789 if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
790 alloc_hint = em->block_start;
791 if (em)
792 free_extent_map(em);
793 } else {
794 alloc_hint = em->block_start;
795 free_extent_map(em);
796 }
797 }
798 read_unlock(&em_tree->lock);
799
800 return alloc_hint;
801 }
802
803 /*
804 * when extent_io.c finds a delayed allocation range in the file,
805 * the call backs end up in this code. The basic idea is to
806 * allocate extents on disk for the range, and create ordered data structs
807 * in ram to track those extents.
808 *
809 * locked_page is the page that writepage had locked already. We use
810 * it to make sure we don't do extra locks or unlocks.
811 *
812 * *page_started is set to one if we unlock locked_page and do everything
813 * required to start IO on it. It may be clean and already done with
814 * IO when we return.
815 */
816 static noinline int __cow_file_range(struct btrfs_trans_handle *trans,
817 struct inode *inode,
818 struct btrfs_root *root,
819 struct page *locked_page,
820 u64 start, u64 end, int *page_started,
821 unsigned long *nr_written,
822 int unlock)
823 {
824 u64 alloc_hint = 0;
825 u64 num_bytes;
826 unsigned long ram_size;
827 u64 disk_num_bytes;
828 u64 cur_alloc_size;
829 u64 blocksize = root->sectorsize;
830 struct btrfs_key ins;
831 struct extent_map *em;
832 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
833 int ret = 0;
834
835 BUG_ON(btrfs_is_free_space_inode(inode));
836
837 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
838 num_bytes = max(blocksize, num_bytes);
839 disk_num_bytes = num_bytes;
840
841 /* if this is a small write inside eof, kick off defrag */
842 if (num_bytes < 64 * 1024 &&
843 (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
844 btrfs_add_inode_defrag(trans, inode);
845
846 if (start == 0) {
847 /* lets try to make an inline extent */
848 ret = cow_file_range_inline(trans, root, inode,
849 start, end, 0, 0, NULL);
850 if (ret == 0) {
851 extent_clear_unlock_delalloc(inode,
852 &BTRFS_I(inode)->io_tree,
853 start, end, NULL,
854 EXTENT_CLEAR_UNLOCK_PAGE |
855 EXTENT_CLEAR_UNLOCK |
856 EXTENT_CLEAR_DELALLOC |
857 EXTENT_CLEAR_DIRTY |
858 EXTENT_SET_WRITEBACK |
859 EXTENT_END_WRITEBACK);
860
861 *nr_written = *nr_written +
862 (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
863 *page_started = 1;
864 goto out;
865 } else if (ret < 0) {
866 btrfs_abort_transaction(trans, root, ret);
867 goto out_unlock;
868 }
869 }
870
871 BUG_ON(disk_num_bytes >
872 btrfs_super_total_bytes(root->fs_info->super_copy));
873
874 alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
875 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
876
877 while (disk_num_bytes > 0) {
878 unsigned long op;
879
880 cur_alloc_size = disk_num_bytes;
881 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
882 root->sectorsize, 0, alloc_hint,
883 &ins, 1);
884 if (ret < 0) {
885 btrfs_abort_transaction(trans, root, ret);
886 goto out_unlock;
887 }
888
889 em = alloc_extent_map();
890 BUG_ON(!em); /* -ENOMEM */
891 em->start = start;
892 em->orig_start = em->start;
893 ram_size = ins.offset;
894 em->len = ins.offset;
895
896 em->block_start = ins.objectid;
897 em->block_len = ins.offset;
898 em->orig_block_len = ins.offset;
899 em->bdev = root->fs_info->fs_devices->latest_bdev;
900 set_bit(EXTENT_FLAG_PINNED, &em->flags);
901 em->generation = -1;
902
903 while (1) {
904 write_lock(&em_tree->lock);
905 ret = add_extent_mapping(em_tree, em);
906 if (!ret)
907 list_move(&em->list,
908 &em_tree->modified_extents);
909 write_unlock(&em_tree->lock);
910 if (ret != -EEXIST) {
911 free_extent_map(em);
912 break;
913 }
914 btrfs_drop_extent_cache(inode, start,
915 start + ram_size - 1, 0);
916 }
917
918 cur_alloc_size = ins.offset;
919 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
920 ram_size, cur_alloc_size, 0);
921 BUG_ON(ret); /* -ENOMEM */
922
923 if (root->root_key.objectid ==
924 BTRFS_DATA_RELOC_TREE_OBJECTID) {
925 ret = btrfs_reloc_clone_csums(inode, start,
926 cur_alloc_size);
927 if (ret) {
928 btrfs_abort_transaction(trans, root, ret);
929 goto out_unlock;
930 }
931 }
932
933 if (disk_num_bytes < cur_alloc_size)
934 break;
935
936 /* we're not doing compressed IO, don't unlock the first
937 * page (which the caller expects to stay locked), don't
938 * clear any dirty bits and don't set any writeback bits
939 *
940 * Do set the Private2 bit so we know this page was properly
941 * setup for writepage
942 */
943 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
944 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
945 EXTENT_SET_PRIVATE2;
946
947 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
948 start, start + ram_size - 1,
949 locked_page, op);
950 disk_num_bytes -= cur_alloc_size;
951 num_bytes -= cur_alloc_size;
952 alloc_hint = ins.objectid + ins.offset;
953 start += cur_alloc_size;
954 }
955 out:
956 return ret;
957
958 out_unlock:
959 extent_clear_unlock_delalloc(inode,
960 &BTRFS_I(inode)->io_tree,
961 start, end, locked_page,
962 EXTENT_CLEAR_UNLOCK_PAGE |
963 EXTENT_CLEAR_UNLOCK |
964 EXTENT_CLEAR_DELALLOC |
965 EXTENT_CLEAR_DIRTY |
966 EXTENT_SET_WRITEBACK |
967 EXTENT_END_WRITEBACK);
968
969 goto out;
970 }
971
972 static noinline int cow_file_range(struct inode *inode,
973 struct page *locked_page,
974 u64 start, u64 end, int *page_started,
975 unsigned long *nr_written,
976 int unlock)
977 {
978 struct btrfs_trans_handle *trans;
979 struct btrfs_root *root = BTRFS_I(inode)->root;
980 int ret;
981
982 trans = btrfs_join_transaction(root);
983 if (IS_ERR(trans)) {
984 extent_clear_unlock_delalloc(inode,
985 &BTRFS_I(inode)->io_tree,
986 start, end, locked_page,
987 EXTENT_CLEAR_UNLOCK_PAGE |
988 EXTENT_CLEAR_UNLOCK |
989 EXTENT_CLEAR_DELALLOC |
990 EXTENT_CLEAR_DIRTY |
991 EXTENT_SET_WRITEBACK |
992 EXTENT_END_WRITEBACK);
993 return PTR_ERR(trans);
994 }
995 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
996
997 ret = __cow_file_range(trans, inode, root, locked_page, start, end,
998 page_started, nr_written, unlock);
999
1000 btrfs_end_transaction(trans, root);
1001
1002 return ret;
1003 }
1004
1005 /*
1006 * work queue call back to started compression on a file and pages
1007 */
1008 static noinline void async_cow_start(struct btrfs_work *work)
1009 {
1010 struct async_cow *async_cow;
1011 int num_added = 0;
1012 async_cow = container_of(work, struct async_cow, work);
1013
1014 compress_file_range(async_cow->inode, async_cow->locked_page,
1015 async_cow->start, async_cow->end, async_cow,
1016 &num_added);
1017 if (num_added == 0) {
1018 btrfs_add_delayed_iput(async_cow->inode);
1019 async_cow->inode = NULL;
1020 }
1021 }
1022
1023 /*
1024 * work queue call back to submit previously compressed pages
1025 */
1026 static noinline void async_cow_submit(struct btrfs_work *work)
1027 {
1028 struct async_cow *async_cow;
1029 struct btrfs_root *root;
1030 unsigned long nr_pages;
1031
1032 async_cow = container_of(work, struct async_cow, work);
1033
1034 root = async_cow->root;
1035 nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
1036 PAGE_CACHE_SHIFT;
1037
1038 if (atomic_sub_return(nr_pages, &root->fs_info->async_delalloc_pages) <
1039 5 * 1024 * 1024 &&
1040 waitqueue_active(&root->fs_info->async_submit_wait))
1041 wake_up(&root->fs_info->async_submit_wait);
1042
1043 if (async_cow->inode)
1044 submit_compressed_extents(async_cow->inode, async_cow);
1045 }
1046
1047 static noinline void async_cow_free(struct btrfs_work *work)
1048 {
1049 struct async_cow *async_cow;
1050 async_cow = container_of(work, struct async_cow, work);
1051 if (async_cow->inode)
1052 btrfs_add_delayed_iput(async_cow->inode);
1053 kfree(async_cow);
1054 }
1055
1056 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1057 u64 start, u64 end, int *page_started,
1058 unsigned long *nr_written)
1059 {
1060 struct async_cow *async_cow;
1061 struct btrfs_root *root = BTRFS_I(inode)->root;
1062 unsigned long nr_pages;
1063 u64 cur_end;
1064 int limit = 10 * 1024 * 1024;
1065
1066 clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1067 1, 0, NULL, GFP_NOFS);
1068 while (start < end) {
1069 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
1070 BUG_ON(!async_cow); /* -ENOMEM */
1071 async_cow->inode = igrab(inode);
1072 async_cow->root = root;
1073 async_cow->locked_page = locked_page;
1074 async_cow->start = start;
1075
1076 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
1077 cur_end = end;
1078 else
1079 cur_end = min(end, start + 512 * 1024 - 1);
1080
1081 async_cow->end = cur_end;
1082 INIT_LIST_HEAD(&async_cow->extents);
1083
1084 async_cow->work.func = async_cow_start;
1085 async_cow->work.ordered_func = async_cow_submit;
1086 async_cow->work.ordered_free = async_cow_free;
1087 async_cow->work.flags = 0;
1088
1089 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
1090 PAGE_CACHE_SHIFT;
1091 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
1092
1093 btrfs_queue_worker(&root->fs_info->delalloc_workers,
1094 &async_cow->work);
1095
1096 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
1097 wait_event(root->fs_info->async_submit_wait,
1098 (atomic_read(&root->fs_info->async_delalloc_pages) <
1099 limit));
1100 }
1101
1102 while (atomic_read(&root->fs_info->async_submit_draining) &&
1103 atomic_read(&root->fs_info->async_delalloc_pages)) {
1104 wait_event(root->fs_info->async_submit_wait,
1105 (atomic_read(&root->fs_info->async_delalloc_pages) ==
1106 0));
1107 }
1108
1109 *nr_written += nr_pages;
1110 start = cur_end + 1;
1111 }
1112 *page_started = 1;
1113 return 0;
1114 }
1115
1116 static noinline int csum_exist_in_range(struct btrfs_root *root,
1117 u64 bytenr, u64 num_bytes)
1118 {
1119 int ret;
1120 struct btrfs_ordered_sum *sums;
1121 LIST_HEAD(list);
1122
1123 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1124 bytenr + num_bytes - 1, &list, 0);
1125 if (ret == 0 && list_empty(&list))
1126 return 0;
1127
1128 while (!list_empty(&list)) {
1129 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1130 list_del(&sums->list);
1131 kfree(sums);
1132 }
1133 return 1;
1134 }
1135
1136 /*
1137 * when nowcow writeback call back. This checks for snapshots or COW copies
1138 * of the extents that exist in the file, and COWs the file as required.
1139 *
1140 * If no cow copies or snapshots exist, we write directly to the existing
1141 * blocks on disk
1142 */
1143 static noinline int run_delalloc_nocow(struct inode *inode,
1144 struct page *locked_page,
1145 u64 start, u64 end, int *page_started, int force,
1146 unsigned long *nr_written)
1147 {
1148 struct btrfs_root *root = BTRFS_I(inode)->root;
1149 struct btrfs_trans_handle *trans;
1150 struct extent_buffer *leaf;
1151 struct btrfs_path *path;
1152 struct btrfs_file_extent_item *fi;
1153 struct btrfs_key found_key;
1154 u64 cow_start;
1155 u64 cur_offset;
1156 u64 extent_end;
1157 u64 extent_offset;
1158 u64 disk_bytenr;
1159 u64 num_bytes;
1160 u64 disk_num_bytes;
1161 int extent_type;
1162 int ret, err;
1163 int type;
1164 int nocow;
1165 int check_prev = 1;
1166 bool nolock;
1167 u64 ino = btrfs_ino(inode);
1168
1169 path = btrfs_alloc_path();
1170 if (!path) {
1171 extent_clear_unlock_delalloc(inode,
1172 &BTRFS_I(inode)->io_tree,
1173 start, end, locked_page,
1174 EXTENT_CLEAR_UNLOCK_PAGE |
1175 EXTENT_CLEAR_UNLOCK |
1176 EXTENT_CLEAR_DELALLOC |
1177 EXTENT_CLEAR_DIRTY |
1178 EXTENT_SET_WRITEBACK |
1179 EXTENT_END_WRITEBACK);
1180 return -ENOMEM;
1181 }
1182
1183 nolock = btrfs_is_free_space_inode(inode);
1184
1185 if (nolock)
1186 trans = btrfs_join_transaction_nolock(root);
1187 else
1188 trans = btrfs_join_transaction(root);
1189
1190 if (IS_ERR(trans)) {
1191 extent_clear_unlock_delalloc(inode,
1192 &BTRFS_I(inode)->io_tree,
1193 start, end, locked_page,
1194 EXTENT_CLEAR_UNLOCK_PAGE |
1195 EXTENT_CLEAR_UNLOCK |
1196 EXTENT_CLEAR_DELALLOC |
1197 EXTENT_CLEAR_DIRTY |
1198 EXTENT_SET_WRITEBACK |
1199 EXTENT_END_WRITEBACK);
1200 btrfs_free_path(path);
1201 return PTR_ERR(trans);
1202 }
1203
1204 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1205
1206 cow_start = (u64)-1;
1207 cur_offset = start;
1208 while (1) {
1209 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1210 cur_offset, 0);
1211 if (ret < 0) {
1212 btrfs_abort_transaction(trans, root, ret);
1213 goto error;
1214 }
1215 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1216 leaf = path->nodes[0];
1217 btrfs_item_key_to_cpu(leaf, &found_key,
1218 path->slots[0] - 1);
1219 if (found_key.objectid == ino &&
1220 found_key.type == BTRFS_EXTENT_DATA_KEY)
1221 path->slots[0]--;
1222 }
1223 check_prev = 0;
1224 next_slot:
1225 leaf = path->nodes[0];
1226 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1227 ret = btrfs_next_leaf(root, path);
1228 if (ret < 0) {
1229 btrfs_abort_transaction(trans, root, ret);
1230 goto error;
1231 }
1232 if (ret > 0)
1233 break;
1234 leaf = path->nodes[0];
1235 }
1236
1237 nocow = 0;
1238 disk_bytenr = 0;
1239 num_bytes = 0;
1240 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1241
1242 if (found_key.objectid > ino ||
1243 found_key.type > BTRFS_EXTENT_DATA_KEY ||
1244 found_key.offset > end)
1245 break;
1246
1247 if (found_key.offset > cur_offset) {
1248 extent_end = found_key.offset;
1249 extent_type = 0;
1250 goto out_check;
1251 }
1252
1253 fi = btrfs_item_ptr(leaf, path->slots[0],
1254 struct btrfs_file_extent_item);
1255 extent_type = btrfs_file_extent_type(leaf, fi);
1256
1257 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1258 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1259 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1260 extent_offset = btrfs_file_extent_offset(leaf, fi);
1261 extent_end = found_key.offset +
1262 btrfs_file_extent_num_bytes(leaf, fi);
1263 disk_num_bytes =
1264 btrfs_file_extent_disk_num_bytes(leaf, fi);
1265 if (extent_end <= start) {
1266 path->slots[0]++;
1267 goto next_slot;
1268 }
1269 if (disk_bytenr == 0)
1270 goto out_check;
1271 if (btrfs_file_extent_compression(leaf, fi) ||
1272 btrfs_file_extent_encryption(leaf, fi) ||
1273 btrfs_file_extent_other_encoding(leaf, fi))
1274 goto out_check;
1275 if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1276 goto out_check;
1277 if (btrfs_extent_readonly(root, disk_bytenr))
1278 goto out_check;
1279 if (btrfs_cross_ref_exist(trans, root, ino,
1280 found_key.offset -
1281 extent_offset, disk_bytenr))
1282 goto out_check;
1283 disk_bytenr += extent_offset;
1284 disk_bytenr += cur_offset - found_key.offset;
1285 num_bytes = min(end + 1, extent_end) - cur_offset;
1286 /*
1287 * force cow if csum exists in the range.
1288 * this ensure that csum for a given extent are
1289 * either valid or do not exist.
1290 */
1291 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1292 goto out_check;
1293 nocow = 1;
1294 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1295 extent_end = found_key.offset +
1296 btrfs_file_extent_inline_len(leaf, fi);
1297 extent_end = ALIGN(extent_end, root->sectorsize);
1298 } else {
1299 BUG_ON(1);
1300 }
1301 out_check:
1302 if (extent_end <= start) {
1303 path->slots[0]++;
1304 goto next_slot;
1305 }
1306 if (!nocow) {
1307 if (cow_start == (u64)-1)
1308 cow_start = cur_offset;
1309 cur_offset = extent_end;
1310 if (cur_offset > end)
1311 break;
1312 path->slots[0]++;
1313 goto next_slot;
1314 }
1315
1316 btrfs_release_path(path);
1317 if (cow_start != (u64)-1) {
1318 ret = __cow_file_range(trans, inode, root, locked_page,
1319 cow_start, found_key.offset - 1,
1320 page_started, nr_written, 1);
1321 if (ret) {
1322 btrfs_abort_transaction(trans, root, ret);
1323 goto error;
1324 }
1325 cow_start = (u64)-1;
1326 }
1327
1328 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1329 struct extent_map *em;
1330 struct extent_map_tree *em_tree;
1331 em_tree = &BTRFS_I(inode)->extent_tree;
1332 em = alloc_extent_map();
1333 BUG_ON(!em); /* -ENOMEM */
1334 em->start = cur_offset;
1335 em->orig_start = found_key.offset - extent_offset;
1336 em->len = num_bytes;
1337 em->block_len = num_bytes;
1338 em->block_start = disk_bytenr;
1339 em->orig_block_len = disk_num_bytes;
1340 em->bdev = root->fs_info->fs_devices->latest_bdev;
1341 set_bit(EXTENT_FLAG_PINNED, &em->flags);
1342 set_bit(EXTENT_FLAG_FILLING, &em->flags);
1343 em->generation = -1;
1344 while (1) {
1345 write_lock(&em_tree->lock);
1346 ret = add_extent_mapping(em_tree, em);
1347 if (!ret)
1348 list_move(&em->list,
1349 &em_tree->modified_extents);
1350 write_unlock(&em_tree->lock);
1351 if (ret != -EEXIST) {
1352 free_extent_map(em);
1353 break;
1354 }
1355 btrfs_drop_extent_cache(inode, em->start,
1356 em->start + em->len - 1, 0);
1357 }
1358 type = BTRFS_ORDERED_PREALLOC;
1359 } else {
1360 type = BTRFS_ORDERED_NOCOW;
1361 }
1362
1363 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1364 num_bytes, num_bytes, type);
1365 BUG_ON(ret); /* -ENOMEM */
1366
1367 if (root->root_key.objectid ==
1368 BTRFS_DATA_RELOC_TREE_OBJECTID) {
1369 ret = btrfs_reloc_clone_csums(inode, cur_offset,
1370 num_bytes);
1371 if (ret) {
1372 btrfs_abort_transaction(trans, root, ret);
1373 goto error;
1374 }
1375 }
1376
1377 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1378 cur_offset, cur_offset + num_bytes - 1,
1379 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1380 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1381 EXTENT_SET_PRIVATE2);
1382 cur_offset = extent_end;
1383 if (cur_offset > end)
1384 break;
1385 }
1386 btrfs_release_path(path);
1387
1388 if (cur_offset <= end && cow_start == (u64)-1) {
1389 cow_start = cur_offset;
1390 cur_offset = end;
1391 }
1392
1393 if (cow_start != (u64)-1) {
1394 ret = __cow_file_range(trans, inode, root, locked_page,
1395 cow_start, end,
1396 page_started, nr_written, 1);
1397 if (ret) {
1398 btrfs_abort_transaction(trans, root, ret);
1399 goto error;
1400 }
1401 }
1402
1403 error:
1404 err = btrfs_end_transaction(trans, root);
1405 if (!ret)
1406 ret = err;
1407
1408 if (ret && cur_offset < end)
1409 extent_clear_unlock_delalloc(inode,
1410 &BTRFS_I(inode)->io_tree,
1411 cur_offset, end, locked_page,
1412 EXTENT_CLEAR_UNLOCK_PAGE |
1413 EXTENT_CLEAR_UNLOCK |
1414 EXTENT_CLEAR_DELALLOC |
1415 EXTENT_CLEAR_DIRTY |
1416 EXTENT_SET_WRITEBACK |
1417 EXTENT_END_WRITEBACK);
1418
1419 btrfs_free_path(path);
1420 return ret;
1421 }
1422
1423 /*
1424 * extent_io.c call back to do delayed allocation processing
1425 */
1426 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1427 u64 start, u64 end, int *page_started,
1428 unsigned long *nr_written)
1429 {
1430 int ret;
1431 struct btrfs_root *root = BTRFS_I(inode)->root;
1432
1433 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) {
1434 ret = run_delalloc_nocow(inode, locked_page, start, end,
1435 page_started, 1, nr_written);
1436 } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC) {
1437 ret = run_delalloc_nocow(inode, locked_page, start, end,
1438 page_started, 0, nr_written);
1439 } else if (!btrfs_test_opt(root, COMPRESS) &&
1440 !(BTRFS_I(inode)->force_compress) &&
1441 !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS)) {
1442 ret = cow_file_range(inode, locked_page, start, end,
1443 page_started, nr_written, 1);
1444 } else {
1445 set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1446 &BTRFS_I(inode)->runtime_flags);
1447 ret = cow_file_range_async(inode, locked_page, start, end,
1448 page_started, nr_written);
1449 }
1450 return ret;
1451 }
1452
1453 static void btrfs_split_extent_hook(struct inode *inode,
1454 struct extent_state *orig, u64 split)
1455 {
1456 /* not delalloc, ignore it */
1457 if (!(orig->state & EXTENT_DELALLOC))
1458 return;
1459
1460 spin_lock(&BTRFS_I(inode)->lock);
1461 BTRFS_I(inode)->outstanding_extents++;
1462 spin_unlock(&BTRFS_I(inode)->lock);
1463 }
1464
1465 /*
1466 * extent_io.c merge_extent_hook, used to track merged delayed allocation
1467 * extents so we can keep track of new extents that are just merged onto old
1468 * extents, such as when we are doing sequential writes, so we can properly
1469 * account for the metadata space we'll need.
1470 */
1471 static void btrfs_merge_extent_hook(struct inode *inode,
1472 struct extent_state *new,
1473 struct extent_state *other)
1474 {
1475 /* not delalloc, ignore it */
1476 if (!(other->state & EXTENT_DELALLOC))
1477 return;
1478
1479 spin_lock(&BTRFS_I(inode)->lock);
1480 BTRFS_I(inode)->outstanding_extents--;
1481 spin_unlock(&BTRFS_I(inode)->lock);
1482 }
1483
1484 /*
1485 * extent_io.c set_bit_hook, used to track delayed allocation
1486 * bytes in this file, and to maintain the list of inodes that
1487 * have pending delalloc work to be done.
1488 */
1489 static void btrfs_set_bit_hook(struct inode *inode,
1490 struct extent_state *state, int *bits)
1491 {
1492
1493 /*
1494 * set_bit and clear bit hooks normally require _irqsave/restore
1495 * but in this case, we are only testing for the DELALLOC
1496 * bit, which is only set or cleared with irqs on
1497 */
1498 if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1499 struct btrfs_root *root = BTRFS_I(inode)->root;
1500 u64 len = state->end + 1 - state->start;
1501 bool do_list = !btrfs_is_free_space_inode(inode);
1502
1503 if (*bits & EXTENT_FIRST_DELALLOC) {
1504 *bits &= ~EXTENT_FIRST_DELALLOC;
1505 } else {
1506 spin_lock(&BTRFS_I(inode)->lock);
1507 BTRFS_I(inode)->outstanding_extents++;
1508 spin_unlock(&BTRFS_I(inode)->lock);
1509 }
1510
1511 spin_lock(&root->fs_info->delalloc_lock);
1512 BTRFS_I(inode)->delalloc_bytes += len;
1513 root->fs_info->delalloc_bytes += len;
1514 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1515 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1516 &root->fs_info->delalloc_inodes);
1517 }
1518 spin_unlock(&root->fs_info->delalloc_lock);
1519 }
1520 }
1521
1522 /*
1523 * extent_io.c clear_bit_hook, see set_bit_hook for why
1524 */
1525 static void btrfs_clear_bit_hook(struct inode *inode,
1526 struct extent_state *state, int *bits)
1527 {
1528 /*
1529 * set_bit and clear bit hooks normally require _irqsave/restore
1530 * but in this case, we are only testing for the DELALLOC
1531 * bit, which is only set or cleared with irqs on
1532 */
1533 if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1534 struct btrfs_root *root = BTRFS_I(inode)->root;
1535 u64 len = state->end + 1 - state->start;
1536 bool do_list = !btrfs_is_free_space_inode(inode);
1537
1538 if (*bits & EXTENT_FIRST_DELALLOC) {
1539 *bits &= ~EXTENT_FIRST_DELALLOC;
1540 } else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1541 spin_lock(&BTRFS_I(inode)->lock);
1542 BTRFS_I(inode)->outstanding_extents--;
1543 spin_unlock(&BTRFS_I(inode)->lock);
1544 }
1545
1546 if (*bits & EXTENT_DO_ACCOUNTING)
1547 btrfs_delalloc_release_metadata(inode, len);
1548
1549 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1550 && do_list)
1551 btrfs_free_reserved_data_space(inode, len);
1552
1553 spin_lock(&root->fs_info->delalloc_lock);
1554 root->fs_info->delalloc_bytes -= len;
1555 BTRFS_I(inode)->delalloc_bytes -= len;
1556
1557 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1558 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1559 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1560 }
1561 spin_unlock(&root->fs_info->delalloc_lock);
1562 }
1563 }
1564
1565 /*
1566 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1567 * we don't create bios that span stripes or chunks
1568 */
1569 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1570 size_t size, struct bio *bio,
1571 unsigned long bio_flags)
1572 {
1573 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1574 u64 logical = (u64)bio->bi_sector << 9;
1575 u64 length = 0;
1576 u64 map_length;
1577 int ret;
1578
1579 if (bio_flags & EXTENT_BIO_COMPRESSED)
1580 return 0;
1581
1582 length = bio->bi_size;
1583 map_length = length;
1584 ret = btrfs_map_block(root->fs_info, READ, logical,
1585 &map_length, NULL, 0);
1586 /* Will always return 0 with map_multi == NULL */
1587 BUG_ON(ret < 0);
1588 if (map_length < length + size)
1589 return 1;
1590 return 0;
1591 }
1592
1593 /*
1594 * in order to insert checksums into the metadata in large chunks,
1595 * we wait until bio submission time. All the pages in the bio are
1596 * checksummed and sums are attached onto the ordered extent record.
1597 *
1598 * At IO completion time the cums attached on the ordered extent record
1599 * are inserted into the btree
1600 */
1601 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1602 struct bio *bio, int mirror_num,
1603 unsigned long bio_flags,
1604 u64 bio_offset)
1605 {
1606 struct btrfs_root *root = BTRFS_I(inode)->root;
1607 int ret = 0;
1608
1609 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1610 BUG_ON(ret); /* -ENOMEM */
1611 return 0;
1612 }
1613
1614 /*
1615 * in order to insert checksums into the metadata in large chunks,
1616 * we wait until bio submission time. All the pages in the bio are
1617 * checksummed and sums are attached onto the ordered extent record.
1618 *
1619 * At IO completion time the cums attached on the ordered extent record
1620 * are inserted into the btree
1621 */
1622 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1623 int mirror_num, unsigned long bio_flags,
1624 u64 bio_offset)
1625 {
1626 struct btrfs_root *root = BTRFS_I(inode)->root;
1627 int ret;
1628
1629 ret = btrfs_map_bio(root, rw, bio, mirror_num, 1);
1630 if (ret)
1631 bio_endio(bio, ret);
1632 return ret;
1633 }
1634
1635 /*
1636 * extent_io.c submission hook. This does the right thing for csum calculation
1637 * on write, or reading the csums from the tree before a read
1638 */
1639 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1640 int mirror_num, unsigned long bio_flags,
1641 u64 bio_offset)
1642 {
1643 struct btrfs_root *root = BTRFS_I(inode)->root;
1644 int ret = 0;
1645 int skip_sum;
1646 int metadata = 0;
1647 int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
1648
1649 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1650
1651 if (btrfs_is_free_space_inode(inode))
1652 metadata = 2;
1653
1654 if (!(rw & REQ_WRITE)) {
1655 ret = btrfs_bio_wq_end_io(root->fs_info, bio, metadata);
1656 if (ret)
1657 goto out;
1658
1659 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1660 ret = btrfs_submit_compressed_read(inode, bio,
1661 mirror_num,
1662 bio_flags);
1663 goto out;
1664 } else if (!skip_sum) {
1665 ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1666 if (ret)
1667 goto out;
1668 }
1669 goto mapit;
1670 } else if (async && !skip_sum) {
1671 /* csum items have already been cloned */
1672 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1673 goto mapit;
1674 /* we're doing a write, do the async checksumming */
1675 ret = btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1676 inode, rw, bio, mirror_num,
1677 bio_flags, bio_offset,
1678 __btrfs_submit_bio_start,
1679 __btrfs_submit_bio_done);
1680 goto out;
1681 } else if (!skip_sum) {
1682 ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1683 if (ret)
1684 goto out;
1685 }
1686
1687 mapit:
1688 ret = btrfs_map_bio(root, rw, bio, mirror_num, 0);
1689
1690 out:
1691 if (ret < 0)
1692 bio_endio(bio, ret);
1693 return ret;
1694 }
1695
1696 /*
1697 * given a list of ordered sums record them in the inode. This happens
1698 * at IO completion time based on sums calculated at bio submission time.
1699 */
1700 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1701 struct inode *inode, u64 file_offset,
1702 struct list_head *list)
1703 {
1704 struct btrfs_ordered_sum *sum;
1705
1706 list_for_each_entry(sum, list, list) {
1707 btrfs_csum_file_blocks(trans,
1708 BTRFS_I(inode)->root->fs_info->csum_root, sum);
1709 }
1710 return 0;
1711 }
1712
1713 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1714 struct extent_state **cached_state)
1715 {
1716 WARN_ON((end & (PAGE_CACHE_SIZE - 1)) == 0);
1717 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1718 cached_state, GFP_NOFS);
1719 }
1720
1721 /* see btrfs_writepage_start_hook for details on why this is required */
1722 struct btrfs_writepage_fixup {
1723 struct page *page;
1724 struct btrfs_work work;
1725 };
1726
1727 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1728 {
1729 struct btrfs_writepage_fixup *fixup;
1730 struct btrfs_ordered_extent *ordered;
1731 struct extent_state *cached_state = NULL;
1732 struct page *page;
1733 struct inode *inode;
1734 u64 page_start;
1735 u64 page_end;
1736 int ret;
1737
1738 fixup = container_of(work, struct btrfs_writepage_fixup, work);
1739 page = fixup->page;
1740 again:
1741 lock_page(page);
1742 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1743 ClearPageChecked(page);
1744 goto out_page;
1745 }
1746
1747 inode = page->mapping->host;
1748 page_start = page_offset(page);
1749 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1750
1751 lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1752 &cached_state);
1753
1754 /* already ordered? We're done */
1755 if (PagePrivate2(page))
1756 goto out;
1757
1758 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1759 if (ordered) {
1760 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1761 page_end, &cached_state, GFP_NOFS);
1762 unlock_page(page);
1763 btrfs_start_ordered_extent(inode, ordered, 1);
1764 btrfs_put_ordered_extent(ordered);
1765 goto again;
1766 }
1767
1768 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
1769 if (ret) {
1770 mapping_set_error(page->mapping, ret);
1771 end_extent_writepage(page, ret, page_start, page_end);
1772 ClearPageChecked(page);
1773 goto out;
1774 }
1775
1776 btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
1777 ClearPageChecked(page);
1778 set_page_dirty(page);
1779 out:
1780 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1781 &cached_state, GFP_NOFS);
1782 out_page:
1783 unlock_page(page);
1784 page_cache_release(page);
1785 kfree(fixup);
1786 }
1787
1788 /*
1789 * There are a few paths in the higher layers of the kernel that directly
1790 * set the page dirty bit without asking the filesystem if it is a
1791 * good idea. This causes problems because we want to make sure COW
1792 * properly happens and the data=ordered rules are followed.
1793 *
1794 * In our case any range that doesn't have the ORDERED bit set
1795 * hasn't been properly setup for IO. We kick off an async process
1796 * to fix it up. The async helper will wait for ordered extents, set
1797 * the delalloc bit and make it safe to write the page.
1798 */
1799 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1800 {
1801 struct inode *inode = page->mapping->host;
1802 struct btrfs_writepage_fixup *fixup;
1803 struct btrfs_root *root = BTRFS_I(inode)->root;
1804
1805 /* this page is properly in the ordered list */
1806 if (TestClearPagePrivate2(page))
1807 return 0;
1808
1809 if (PageChecked(page))
1810 return -EAGAIN;
1811
1812 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1813 if (!fixup)
1814 return -EAGAIN;
1815
1816 SetPageChecked(page);
1817 page_cache_get(page);
1818 fixup->work.func = btrfs_writepage_fixup_worker;
1819 fixup->page = page;
1820 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1821 return -EBUSY;
1822 }
1823
1824 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1825 struct inode *inode, u64 file_pos,
1826 u64 disk_bytenr, u64 disk_num_bytes,
1827 u64 num_bytes, u64 ram_bytes,
1828 u8 compression, u8 encryption,
1829 u16 other_encoding, int extent_type)
1830 {
1831 struct btrfs_root *root = BTRFS_I(inode)->root;
1832 struct btrfs_file_extent_item *fi;
1833 struct btrfs_path *path;
1834 struct extent_buffer *leaf;
1835 struct btrfs_key ins;
1836 int ret;
1837
1838 path = btrfs_alloc_path();
1839 if (!path)
1840 return -ENOMEM;
1841
1842 path->leave_spinning = 1;
1843
1844 /*
1845 * we may be replacing one extent in the tree with another.
1846 * The new extent is pinned in the extent map, and we don't want
1847 * to drop it from the cache until it is completely in the btree.
1848 *
1849 * So, tell btrfs_drop_extents to leave this extent in the cache.
1850 * the caller is expected to unpin it and allow it to be merged
1851 * with the others.
1852 */
1853 ret = btrfs_drop_extents(trans, root, inode, file_pos,
1854 file_pos + num_bytes, 0);
1855 if (ret)
1856 goto out;
1857
1858 ins.objectid = btrfs_ino(inode);
1859 ins.offset = file_pos;
1860 ins.type = BTRFS_EXTENT_DATA_KEY;
1861 ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1862 if (ret)
1863 goto out;
1864 leaf = path->nodes[0];
1865 fi = btrfs_item_ptr(leaf, path->slots[0],
1866 struct btrfs_file_extent_item);
1867 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1868 btrfs_set_file_extent_type(leaf, fi, extent_type);
1869 btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1870 btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1871 btrfs_set_file_extent_offset(leaf, fi, 0);
1872 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1873 btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1874 btrfs_set_file_extent_compression(leaf, fi, compression);
1875 btrfs_set_file_extent_encryption(leaf, fi, encryption);
1876 btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1877
1878 btrfs_mark_buffer_dirty(leaf);
1879 btrfs_release_path(path);
1880
1881 inode_add_bytes(inode, num_bytes);
1882
1883 ins.objectid = disk_bytenr;
1884 ins.offset = disk_num_bytes;
1885 ins.type = BTRFS_EXTENT_ITEM_KEY;
1886 ret = btrfs_alloc_reserved_file_extent(trans, root,
1887 root->root_key.objectid,
1888 btrfs_ino(inode), file_pos, &ins);
1889 out:
1890 btrfs_free_path(path);
1891
1892 return ret;
1893 }
1894
1895 /*
1896 * helper function for btrfs_finish_ordered_io, this
1897 * just reads in some of the csum leaves to prime them into ram
1898 * before we start the transaction. It limits the amount of btree
1899 * reads required while inside the transaction.
1900 */
1901 /* as ordered data IO finishes, this gets called so we can finish
1902 * an ordered extent if the range of bytes in the file it covers are
1903 * fully written.
1904 */
1905 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
1906 {
1907 struct inode *inode = ordered_extent->inode;
1908 struct btrfs_root *root = BTRFS_I(inode)->root;
1909 struct btrfs_trans_handle *trans = NULL;
1910 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1911 struct extent_state *cached_state = NULL;
1912 int compress_type = 0;
1913 int ret;
1914 bool nolock;
1915
1916 nolock = btrfs_is_free_space_inode(inode);
1917
1918 if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
1919 ret = -EIO;
1920 goto out;
1921 }
1922
1923 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1924 BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
1925 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1926 if (!ret) {
1927 if (nolock)
1928 trans = btrfs_join_transaction_nolock(root);
1929 else
1930 trans = btrfs_join_transaction(root);
1931 if (IS_ERR(trans)) {
1932 ret = PTR_ERR(trans);
1933 trans = NULL;
1934 goto out;
1935 }
1936 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1937 ret = btrfs_update_inode_fallback(trans, root, inode);
1938 if (ret) /* -ENOMEM or corruption */
1939 btrfs_abort_transaction(trans, root, ret);
1940 }
1941 goto out;
1942 }
1943
1944 lock_extent_bits(io_tree, ordered_extent->file_offset,
1945 ordered_extent->file_offset + ordered_extent->len - 1,
1946 0, &cached_state);
1947
1948 if (nolock)
1949 trans = btrfs_join_transaction_nolock(root);
1950 else
1951 trans = btrfs_join_transaction(root);
1952 if (IS_ERR(trans)) {
1953 ret = PTR_ERR(trans);
1954 trans = NULL;
1955 goto out_unlock;
1956 }
1957 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1958
1959 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1960 compress_type = ordered_extent->compress_type;
1961 if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1962 BUG_ON(compress_type);
1963 ret = btrfs_mark_extent_written(trans, inode,
1964 ordered_extent->file_offset,
1965 ordered_extent->file_offset +
1966 ordered_extent->len);
1967 } else {
1968 BUG_ON(root == root->fs_info->tree_root);
1969 ret = insert_reserved_file_extent(trans, inode,
1970 ordered_extent->file_offset,
1971 ordered_extent->start,
1972 ordered_extent->disk_len,
1973 ordered_extent->len,
1974 ordered_extent->len,
1975 compress_type, 0, 0,
1976 BTRFS_FILE_EXTENT_REG);
1977 }
1978 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1979 ordered_extent->file_offset, ordered_extent->len,
1980 trans->transid);
1981 if (ret < 0) {
1982 btrfs_abort_transaction(trans, root, ret);
1983 goto out_unlock;
1984 }
1985
1986 add_pending_csums(trans, inode, ordered_extent->file_offset,
1987 &ordered_extent->list);
1988
1989 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1990 if (!ret || !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1991 ret = btrfs_update_inode_fallback(trans, root, inode);
1992 if (ret) { /* -ENOMEM or corruption */
1993 btrfs_abort_transaction(trans, root, ret);
1994 goto out_unlock;
1995 }
1996 } else {
1997 btrfs_set_inode_last_trans(trans, inode);
1998 }
1999 ret = 0;
2000 out_unlock:
2001 unlock_extent_cached(io_tree, ordered_extent->file_offset,
2002 ordered_extent->file_offset +
2003 ordered_extent->len - 1, &cached_state, GFP_NOFS);
2004 out:
2005 if (root != root->fs_info->tree_root)
2006 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
2007 if (trans)
2008 btrfs_end_transaction(trans, root);
2009
2010 if (ret)
2011 clear_extent_uptodate(io_tree, ordered_extent->file_offset,
2012 ordered_extent->file_offset +
2013 ordered_extent->len - 1, NULL, GFP_NOFS);
2014
2015 /*
2016 * This needs to be done to make sure anybody waiting knows we are done
2017 * updating everything for this ordered extent.
2018 */
2019 btrfs_remove_ordered_extent(inode, ordered_extent);
2020
2021 /* once for us */
2022 btrfs_put_ordered_extent(ordered_extent);
2023 /* once for the tree */
2024 btrfs_put_ordered_extent(ordered_extent);
2025
2026 return ret;
2027 }
2028
2029 static void finish_ordered_fn(struct btrfs_work *work)
2030 {
2031 struct btrfs_ordered_extent *ordered_extent;
2032 ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2033 btrfs_finish_ordered_io(ordered_extent);
2034 }
2035
2036 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
2037 struct extent_state *state, int uptodate)
2038 {
2039 struct inode *inode = page->mapping->host;
2040 struct btrfs_root *root = BTRFS_I(inode)->root;
2041 struct btrfs_ordered_extent *ordered_extent = NULL;
2042 struct btrfs_workers *workers;
2043
2044 trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2045
2046 ClearPagePrivate2(page);
2047 if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2048 end - start + 1, uptodate))
2049 return 0;
2050
2051 ordered_extent->work.func = finish_ordered_fn;
2052 ordered_extent->work.flags = 0;
2053
2054 if (btrfs_is_free_space_inode(inode))
2055 workers = &root->fs_info->endio_freespace_worker;
2056 else
2057 workers = &root->fs_info->endio_write_workers;
2058 btrfs_queue_worker(workers, &ordered_extent->work);
2059
2060 return 0;
2061 }
2062
2063 /*
2064 * when reads are done, we need to check csums to verify the data is correct
2065 * if there's a match, we allow the bio to finish. If not, the code in
2066 * extent_io.c will try to find good copies for us.
2067 */
2068 static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
2069 struct extent_state *state, int mirror)
2070 {
2071 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
2072 struct inode *inode = page->mapping->host;
2073 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2074 char *kaddr;
2075 u64 private = ~(u32)0;
2076 int ret;
2077 struct btrfs_root *root = BTRFS_I(inode)->root;
2078 u32 csum = ~(u32)0;
2079
2080 if (PageChecked(page)) {
2081 ClearPageChecked(page);
2082 goto good;
2083 }
2084
2085 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
2086 goto good;
2087
2088 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
2089 test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
2090 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
2091 GFP_NOFS);
2092 return 0;
2093 }
2094
2095 if (state && state->start == start) {
2096 private = state->private;
2097 ret = 0;
2098 } else {
2099 ret = get_state_private(io_tree, start, &private);
2100 }
2101 kaddr = kmap_atomic(page);
2102 if (ret)
2103 goto zeroit;
2104
2105 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
2106 btrfs_csum_final(csum, (char *)&csum);
2107 if (csum != private)
2108 goto zeroit;
2109
2110 kunmap_atomic(kaddr);
2111 good:
2112 return 0;
2113
2114 zeroit:
2115 printk_ratelimited(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
2116 "private %llu\n",
2117 (unsigned long long)btrfs_ino(page->mapping->host),
2118 (unsigned long long)start, csum,
2119 (unsigned long long)private);
2120 memset(kaddr + offset, 1, end - start + 1);
2121 flush_dcache_page(page);
2122 kunmap_atomic(kaddr);
2123 if (private == 0)
2124 return 0;
2125 return -EIO;
2126 }
2127
2128 struct delayed_iput {
2129 struct list_head list;
2130 struct inode *inode;
2131 };
2132
2133 /* JDM: If this is fs-wide, why can't we add a pointer to
2134 * btrfs_inode instead and avoid the allocation? */
2135 void btrfs_add_delayed_iput(struct inode *inode)
2136 {
2137 struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2138 struct delayed_iput *delayed;
2139
2140 if (atomic_add_unless(&inode->i_count, -1, 1))
2141 return;
2142
2143 delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2144 delayed->inode = inode;
2145
2146 spin_lock(&fs_info->delayed_iput_lock);
2147 list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2148 spin_unlock(&fs_info->delayed_iput_lock);
2149 }
2150
2151 void btrfs_run_delayed_iputs(struct btrfs_root *root)
2152 {
2153 LIST_HEAD(list);
2154 struct btrfs_fs_info *fs_info = root->fs_info;
2155 struct delayed_iput *delayed;
2156 int empty;
2157
2158 spin_lock(&fs_info->delayed_iput_lock);
2159 empty = list_empty(&fs_info->delayed_iputs);
2160 spin_unlock(&fs_info->delayed_iput_lock);
2161 if (empty)
2162 return;
2163
2164 spin_lock(&fs_info->delayed_iput_lock);
2165 list_splice_init(&fs_info->delayed_iputs, &list);
2166 spin_unlock(&fs_info->delayed_iput_lock);
2167
2168 while (!list_empty(&list)) {
2169 delayed = list_entry(list.next, struct delayed_iput, list);
2170 list_del(&delayed->list);
2171 iput(delayed->inode);
2172 kfree(delayed);
2173 }
2174 }
2175
2176 enum btrfs_orphan_cleanup_state {
2177 ORPHAN_CLEANUP_STARTED = 1,
2178 ORPHAN_CLEANUP_DONE = 2,
2179 };
2180
2181 /*
2182 * This is called in transaction commit time. If there are no orphan
2183 * files in the subvolume, it removes orphan item and frees block_rsv
2184 * structure.
2185 */
2186 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2187 struct btrfs_root *root)
2188 {
2189 struct btrfs_block_rsv *block_rsv;
2190 int ret;
2191
2192 if (atomic_read(&root->orphan_inodes) ||
2193 root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2194 return;
2195
2196 spin_lock(&root->orphan_lock);
2197 if (atomic_read(&root->orphan_inodes)) {
2198 spin_unlock(&root->orphan_lock);
2199 return;
2200 }
2201
2202 if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
2203 spin_unlock(&root->orphan_lock);
2204 return;
2205 }
2206
2207 block_rsv = root->orphan_block_rsv;
2208 root->orphan_block_rsv = NULL;
2209 spin_unlock(&root->orphan_lock);
2210
2211 if (root->orphan_item_inserted &&
2212 btrfs_root_refs(&root->root_item) > 0) {
2213 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2214 root->root_key.objectid);
2215 BUG_ON(ret);
2216 root->orphan_item_inserted = 0;
2217 }
2218
2219 if (block_rsv) {
2220 WARN_ON(block_rsv->size > 0);
2221 btrfs_free_block_rsv(root, block_rsv);
2222 }
2223 }
2224
2225 /*
2226 * This creates an orphan entry for the given inode in case something goes
2227 * wrong in the middle of an unlink/truncate.
2228 *
2229 * NOTE: caller of this function should reserve 5 units of metadata for
2230 * this function.
2231 */
2232 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2233 {
2234 struct btrfs_root *root = BTRFS_I(inode)->root;
2235 struct btrfs_block_rsv *block_rsv = NULL;
2236 int reserve = 0;
2237 int insert = 0;
2238 int ret;
2239
2240 if (!root->orphan_block_rsv) {
2241 block_rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
2242 if (!block_rsv)
2243 return -ENOMEM;
2244 }
2245
2246 spin_lock(&root->orphan_lock);
2247 if (!root->orphan_block_rsv) {
2248 root->orphan_block_rsv = block_rsv;
2249 } else if (block_rsv) {
2250 btrfs_free_block_rsv(root, block_rsv);
2251 block_rsv = NULL;
2252 }
2253
2254 if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2255 &BTRFS_I(inode)->runtime_flags)) {
2256 #if 0
2257 /*
2258 * For proper ENOSPC handling, we should do orphan
2259 * cleanup when mounting. But this introduces backward
2260 * compatibility issue.
2261 */
2262 if (!xchg(&root->orphan_item_inserted, 1))
2263 insert = 2;
2264 else
2265 insert = 1;
2266 #endif
2267 insert = 1;
2268 atomic_inc(&root->orphan_inodes);
2269 }
2270
2271 if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2272 &BTRFS_I(inode)->runtime_flags))
2273 reserve = 1;
2274 spin_unlock(&root->orphan_lock);
2275
2276 /* grab metadata reservation from transaction handle */
2277 if (reserve) {
2278 ret = btrfs_orphan_reserve_metadata(trans, inode);
2279 BUG_ON(ret); /* -ENOSPC in reservation; Logic error? JDM */
2280 }
2281
2282 /* insert an orphan item to track this unlinked/truncated file */
2283 if (insert >= 1) {
2284 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
2285 if (ret && ret != -EEXIST) {
2286 clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2287 &BTRFS_I(inode)->runtime_flags);
2288 btrfs_abort_transaction(trans, root, ret);
2289 return ret;
2290 }
2291 ret = 0;
2292 }
2293
2294 /* insert an orphan item to track subvolume contains orphan files */
2295 if (insert >= 2) {
2296 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2297 root->root_key.objectid);
2298 if (ret && ret != -EEXIST) {
2299 btrfs_abort_transaction(trans, root, ret);
2300 return ret;
2301 }
2302 }
2303 return 0;
2304 }
2305
2306 /*
2307 * We have done the truncate/delete so we can go ahead and remove the orphan
2308 * item for this particular inode.
2309 */
2310 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2311 {
2312 struct btrfs_root *root = BTRFS_I(inode)->root;
2313 int delete_item = 0;
2314 int release_rsv = 0;
2315 int ret = 0;
2316
2317 spin_lock(&root->orphan_lock);
2318 if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2319 &BTRFS_I(inode)->runtime_flags))
2320 delete_item = 1;
2321
2322 if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
2323 &BTRFS_I(inode)->runtime_flags))
2324 release_rsv = 1;
2325 spin_unlock(&root->orphan_lock);
2326
2327 if (trans && delete_item) {
2328 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
2329 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
2330 }
2331
2332 if (release_rsv) {
2333 btrfs_orphan_release_metadata(inode);
2334 atomic_dec(&root->orphan_inodes);
2335 }
2336
2337 return 0;
2338 }
2339
2340 /*
2341 * this cleans up any orphans that may be left on the list from the last use
2342 * of this root.
2343 */
2344 int btrfs_orphan_cleanup(struct btrfs_root *root)
2345 {
2346 struct btrfs_path *path;
2347 struct extent_buffer *leaf;
2348 struct btrfs_key key, found_key;
2349 struct btrfs_trans_handle *trans;
2350 struct inode *inode;
2351 u64 last_objectid = 0;
2352 int ret = 0, nr_unlink = 0, nr_truncate = 0;
2353
2354 if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
2355 return 0;
2356
2357 path = btrfs_alloc_path();
2358 if (!path) {
2359 ret = -ENOMEM;
2360 goto out;
2361 }
2362 path->reada = -1;
2363
2364 key.objectid = BTRFS_ORPHAN_OBJECTID;
2365 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2366 key.offset = (u64)-1;
2367
2368 while (1) {
2369 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2370 if (ret < 0)
2371 goto out;
2372
2373 /*
2374 * if ret == 0 means we found what we were searching for, which
2375 * is weird, but possible, so only screw with path if we didn't
2376 * find the key and see if we have stuff that matches
2377 */
2378 if (ret > 0) {
2379 ret = 0;
2380 if (path->slots[0] == 0)
2381 break;
2382 path->slots[0]--;
2383 }
2384
2385 /* pull out the item */
2386 leaf = path->nodes[0];
2387 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2388
2389 /* make sure the item matches what we want */
2390 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2391 break;
2392 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2393 break;
2394
2395 /* release the path since we're done with it */
2396 btrfs_release_path(path);
2397
2398 /*
2399 * this is where we are basically btrfs_lookup, without the
2400 * crossing root thing. we store the inode number in the
2401 * offset of the orphan item.
2402 */
2403
2404 if (found_key.offset == last_objectid) {
2405 printk(KERN_ERR "btrfs: Error removing orphan entry, "
2406 "stopping orphan cleanup\n");
2407 ret = -EINVAL;
2408 goto out;
2409 }
2410
2411 last_objectid = found_key.offset;
2412
2413 found_key.objectid = found_key.offset;
2414 found_key.type = BTRFS_INODE_ITEM_KEY;
2415 found_key.offset = 0;
2416 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
2417 ret = PTR_RET(inode);
2418 if (ret && ret != -ESTALE)
2419 goto out;
2420
2421 if (ret == -ESTALE && root == root->fs_info->tree_root) {
2422 struct btrfs_root *dead_root;
2423 struct btrfs_fs_info *fs_info = root->fs_info;
2424 int is_dead_root = 0;
2425
2426 /*
2427 * this is an orphan in the tree root. Currently these
2428 * could come from 2 sources:
2429 * a) a snapshot deletion in progress
2430 * b) a free space cache inode
2431 * We need to distinguish those two, as the snapshot
2432 * orphan must not get deleted.
2433 * find_dead_roots already ran before us, so if this
2434 * is a snapshot deletion, we should find the root
2435 * in the dead_roots list
2436 */
2437 spin_lock(&fs_info->trans_lock);
2438 list_for_each_entry(dead_root, &fs_info->dead_roots,
2439 root_list) {
2440 if (dead_root->root_key.objectid ==
2441 found_key.objectid) {
2442 is_dead_root = 1;
2443 break;
2444 }
2445 }
2446 spin_unlock(&fs_info->trans_lock);
2447 if (is_dead_root) {
2448 /* prevent this orphan from being found again */
2449 key.offset = found_key.objectid - 1;
2450 continue;
2451 }
2452 }
2453 /*
2454 * Inode is already gone but the orphan item is still there,
2455 * kill the orphan item.
2456 */
2457 if (ret == -ESTALE) {
2458 trans = btrfs_start_transaction(root, 1);
2459 if (IS_ERR(trans)) {
2460 ret = PTR_ERR(trans);
2461 goto out;
2462 }
2463 printk(KERN_ERR "auto deleting %Lu\n",
2464 found_key.objectid);
2465 ret = btrfs_del_orphan_item(trans, root,
2466 found_key.objectid);
2467 BUG_ON(ret); /* -ENOMEM or corruption (JDM: Recheck) */
2468 btrfs_end_transaction(trans, root);
2469 continue;
2470 }
2471
2472 /*
2473 * add this inode to the orphan list so btrfs_orphan_del does
2474 * the proper thing when we hit it
2475 */
2476 set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
2477 &BTRFS_I(inode)->runtime_flags);
2478
2479 /* if we have links, this was a truncate, lets do that */
2480 if (inode->i_nlink) {
2481 if (!S_ISREG(inode->i_mode)) {
2482 WARN_ON(1);
2483 iput(inode);
2484 continue;
2485 }
2486 nr_truncate++;
2487 ret = btrfs_truncate(inode);
2488 } else {
2489 nr_unlink++;
2490 }
2491
2492 /* this will do delete_inode and everything for us */
2493 iput(inode);
2494 if (ret)
2495 goto out;
2496 }
2497 /* release the path since we're done with it */
2498 btrfs_release_path(path);
2499
2500 root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2501
2502 if (root->orphan_block_rsv)
2503 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2504 (u64)-1);
2505
2506 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2507 trans = btrfs_join_transaction(root);
2508 if (!IS_ERR(trans))
2509 btrfs_end_transaction(trans, root);
2510 }
2511
2512 if (nr_unlink)
2513 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2514 if (nr_truncate)
2515 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2516
2517 out:
2518 if (ret)
2519 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2520 btrfs_free_path(path);
2521 return ret;
2522 }
2523
2524 /*
2525 * very simple check to peek ahead in the leaf looking for xattrs. If we
2526 * don't find any xattrs, we know there can't be any acls.
2527 *
2528 * slot is the slot the inode is in, objectid is the objectid of the inode
2529 */
2530 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2531 int slot, u64 objectid)
2532 {
2533 u32 nritems = btrfs_header_nritems(leaf);
2534 struct btrfs_key found_key;
2535 int scanned = 0;
2536
2537 slot++;
2538 while (slot < nritems) {
2539 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2540
2541 /* we found a different objectid, there must not be acls */
2542 if (found_key.objectid != objectid)
2543 return 0;
2544
2545 /* we found an xattr, assume we've got an acl */
2546 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2547 return 1;
2548
2549 /*
2550 * we found a key greater than an xattr key, there can't
2551 * be any acls later on
2552 */
2553 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2554 return 0;
2555
2556 slot++;
2557 scanned++;
2558
2559 /*
2560 * it goes inode, inode backrefs, xattrs, extents,
2561 * so if there are a ton of hard links to an inode there can
2562 * be a lot of backrefs. Don't waste time searching too hard,
2563 * this is just an optimization
2564 */
2565 if (scanned >= 8)
2566 break;
2567 }
2568 /* we hit the end of the leaf before we found an xattr or
2569 * something larger than an xattr. We have to assume the inode
2570 * has acls
2571 */
2572 return 1;
2573 }
2574
2575 /*
2576 * read an inode from the btree into the in-memory inode
2577 */
2578 static void btrfs_read_locked_inode(struct inode *inode)
2579 {
2580 struct btrfs_path *path;
2581 struct extent_buffer *leaf;
2582 struct btrfs_inode_item *inode_item;
2583 struct btrfs_timespec *tspec;
2584 struct btrfs_root *root = BTRFS_I(inode)->root;
2585 struct btrfs_key location;
2586 int maybe_acls;
2587 u32 rdev;
2588 int ret;
2589 bool filled = false;
2590
2591 ret = btrfs_fill_inode(inode, &rdev);
2592 if (!ret)
2593 filled = true;
2594
2595 path = btrfs_alloc_path();
2596 if (!path)
2597 goto make_bad;
2598
2599 path->leave_spinning = 1;
2600 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
2601
2602 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
2603 if (ret)
2604 goto make_bad;
2605
2606 leaf = path->nodes[0];
2607
2608 if (filled)
2609 goto cache_acl;
2610
2611 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2612 struct btrfs_inode_item);
2613 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2614 set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
2615 i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
2616 i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
2617 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
2618
2619 tspec = btrfs_inode_atime(inode_item);
2620 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2621 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2622
2623 tspec = btrfs_inode_mtime(inode_item);
2624 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2625 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2626
2627 tspec = btrfs_inode_ctime(inode_item);
2628 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2629 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2630
2631 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
2632 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
2633 BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
2634
2635 /*
2636 * If we were modified in the current generation and evicted from memory
2637 * and then re-read we need to do a full sync since we don't have any
2638 * idea about which extents were modified before we were evicted from
2639 * cache.
2640 */
2641 if (BTRFS_I(inode)->last_trans == root->fs_info->generation)
2642 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
2643 &BTRFS_I(inode)->runtime_flags);
2644
2645 inode->i_version = btrfs_inode_sequence(leaf, inode_item);
2646 inode->i_generation = BTRFS_I(inode)->generation;
2647 inode->i_rdev = 0;
2648 rdev = btrfs_inode_rdev(leaf, inode_item);
2649
2650 BTRFS_I(inode)->index_cnt = (u64)-1;
2651 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2652 cache_acl:
2653 /*
2654 * try to precache a NULL acl entry for files that don't have
2655 * any xattrs or acls
2656 */
2657 maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2658 btrfs_ino(inode));
2659 if (!maybe_acls)
2660 cache_no_acl(inode);
2661
2662 btrfs_free_path(path);
2663
2664 switch (inode->i_mode & S_IFMT) {
2665 case S_IFREG:
2666 inode->i_mapping->a_ops = &btrfs_aops;
2667 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2668 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2669 inode->i_fop = &btrfs_file_operations;
2670 inode->i_op = &btrfs_file_inode_operations;
2671 break;
2672 case S_IFDIR:
2673 inode->i_fop = &btrfs_dir_file_operations;
2674 if (root == root->fs_info->tree_root)
2675 inode->i_op = &btrfs_dir_ro_inode_operations;
2676 else
2677 inode->i_op = &btrfs_dir_inode_operations;
2678 break;
2679 case S_IFLNK:
2680 inode->i_op = &btrfs_symlink_inode_operations;
2681 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2682 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2683 break;
2684 default:
2685 inode->i_op = &btrfs_special_inode_operations;
2686 init_special_inode(inode, inode->i_mode, rdev);
2687 break;
2688 }
2689
2690 btrfs_update_iflags(inode);
2691 return;
2692
2693 make_bad:
2694 btrfs_free_path(path);
2695 make_bad_inode(inode);
2696 }
2697
2698 /*
2699 * given a leaf and an inode, copy the inode fields into the leaf
2700 */
2701 static void fill_inode_item(struct btrfs_trans_handle *trans,
2702 struct extent_buffer *leaf,
2703 struct btrfs_inode_item *item,
2704 struct inode *inode)
2705 {
2706 btrfs_set_inode_uid(leaf, item, i_uid_read(inode));
2707 btrfs_set_inode_gid(leaf, item, i_gid_read(inode));
2708 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
2709 btrfs_set_inode_mode(leaf, item, inode->i_mode);
2710 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2711
2712 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2713 inode->i_atime.tv_sec);
2714 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2715 inode->i_atime.tv_nsec);
2716
2717 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2718 inode->i_mtime.tv_sec);
2719 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2720 inode->i_mtime.tv_nsec);
2721
2722 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2723 inode->i_ctime.tv_sec);
2724 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2725 inode->i_ctime.tv_nsec);
2726
2727 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
2728 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
2729 btrfs_set_inode_sequence(leaf, item, inode->i_version);
2730 btrfs_set_inode_transid(leaf, item, trans->transid);
2731 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
2732 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
2733 btrfs_set_inode_block_group(leaf, item, 0);
2734 }
2735
2736 /*
2737 * copy everything in the in-memory inode into the btree.
2738 */
2739 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
2740 struct btrfs_root *root, struct inode *inode)
2741 {
2742 struct btrfs_inode_item *inode_item;
2743 struct btrfs_path *path;
2744 struct extent_buffer *leaf;
2745 int ret;
2746
2747 path = btrfs_alloc_path();
2748 if (!path)
2749 return -ENOMEM;
2750
2751 path->leave_spinning = 1;
2752 ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2753 1);
2754 if (ret) {
2755 if (ret > 0)
2756 ret = -ENOENT;
2757 goto failed;
2758 }
2759
2760 btrfs_unlock_up_safe(path, 1);
2761 leaf = path->nodes[0];
2762 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2763 struct btrfs_inode_item);
2764
2765 fill_inode_item(trans, leaf, inode_item, inode);
2766 btrfs_mark_buffer_dirty(leaf);
2767 btrfs_set_inode_last_trans(trans, inode);
2768 ret = 0;
2769 failed:
2770 btrfs_free_path(path);
2771 return ret;
2772 }
2773
2774 /*
2775 * copy everything in the in-memory inode into the btree.
2776 */
2777 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2778 struct btrfs_root *root, struct inode *inode)
2779 {
2780 int ret;
2781
2782 /*
2783 * If the inode is a free space inode, we can deadlock during commit
2784 * if we put it into the delayed code.
2785 *
2786 * The data relocation inode should also be directly updated
2787 * without delay
2788 */
2789 if (!btrfs_is_free_space_inode(inode)
2790 && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID) {
2791 btrfs_update_root_times(trans, root);
2792
2793 ret = btrfs_delayed_update_inode(trans, root, inode);
2794 if (!ret)
2795 btrfs_set_inode_last_trans(trans, inode);
2796 return ret;
2797 }
2798
2799 return btrfs_update_inode_item(trans, root, inode);
2800 }
2801
2802 noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
2803 struct btrfs_root *root,
2804 struct inode *inode)
2805 {
2806 int ret;
2807
2808 ret = btrfs_update_inode(trans, root, inode);
2809 if (ret == -ENOSPC)
2810 return btrfs_update_inode_item(trans, root, inode);
2811 return ret;
2812 }
2813
2814 /*
2815 * unlink helper that gets used here in inode.c and in the tree logging
2816 * recovery code. It remove a link in a directory with a given name, and
2817 * also drops the back refs in the inode to the directory
2818 */
2819 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2820 struct btrfs_root *root,
2821 struct inode *dir, struct inode *inode,
2822 const char *name, int name_len)
2823 {
2824 struct btrfs_path *path;
2825 int ret = 0;
2826 struct extent_buffer *leaf;
2827 struct btrfs_dir_item *di;
2828 struct btrfs_key key;
2829 u64 index;
2830 u64 ino = btrfs_ino(inode);
2831 u64 dir_ino = btrfs_ino(dir);
2832
2833 path = btrfs_alloc_path();
2834 if (!path) {
2835 ret = -ENOMEM;
2836 goto out;
2837 }
2838
2839 path->leave_spinning = 1;
2840 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2841 name, name_len, -1);
2842 if (IS_ERR(di)) {
2843 ret = PTR_ERR(di);
2844 goto err;
2845 }
2846 if (!di) {
2847 ret = -ENOENT;
2848 goto err;
2849 }
2850 leaf = path->nodes[0];
2851 btrfs_dir_item_key_to_cpu(leaf, di, &key);
2852 ret = btrfs_delete_one_dir_name(trans, root, path, di);
2853 if (ret)
2854 goto err;
2855 btrfs_release_path(path);
2856
2857 ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2858 dir_ino, &index);
2859 if (ret) {
2860 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2861 "inode %llu parent %llu\n", name_len, name,
2862 (unsigned long long)ino, (unsigned long long)dir_ino);
2863 btrfs_abort_transaction(trans, root, ret);
2864 goto err;
2865 }
2866
2867 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2868 if (ret) {
2869 btrfs_abort_transaction(trans, root, ret);
2870 goto err;
2871 }
2872
2873 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2874 inode, dir_ino);
2875 if (ret != 0 && ret != -ENOENT) {
2876 btrfs_abort_transaction(trans, root, ret);
2877 goto err;
2878 }
2879
2880 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2881 dir, index);
2882 if (ret == -ENOENT)
2883 ret = 0;
2884 err:
2885 btrfs_free_path(path);
2886 if (ret)
2887 goto out;
2888
2889 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2890 inode_inc_iversion(inode);
2891 inode_inc_iversion(dir);
2892 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2893 ret = btrfs_update_inode(trans, root, dir);
2894 out:
2895 return ret;
2896 }
2897
2898 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2899 struct btrfs_root *root,
2900 struct inode *dir, struct inode *inode,
2901 const char *name, int name_len)
2902 {
2903 int ret;
2904 ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2905 if (!ret) {
2906 btrfs_drop_nlink(inode);
2907 ret = btrfs_update_inode(trans, root, inode);
2908 }
2909 return ret;
2910 }
2911
2912
2913 /* helper to check if there is any shared block in the path */
2914 static int check_path_shared(struct btrfs_root *root,
2915 struct btrfs_path *path)
2916 {
2917 struct extent_buffer *eb;
2918 int level;
2919 u64 refs = 1;
2920
2921 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2922 int ret;
2923
2924 if (!path->nodes[level])
2925 break;
2926 eb = path->nodes[level];
2927 if (!btrfs_block_can_be_shared(root, eb))
2928 continue;
2929 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2930 &refs, NULL);
2931 if (refs > 1)
2932 return 1;
2933 }
2934 return 0;
2935 }
2936
2937 /*
2938 * helper to start transaction for unlink and rmdir.
2939 *
2940 * unlink and rmdir are special in btrfs, they do not always free space.
2941 * so in enospc case, we should make sure they will free space before
2942 * allowing them to use the global metadata reservation.
2943 */
2944 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2945 struct dentry *dentry)
2946 {
2947 struct btrfs_trans_handle *trans;
2948 struct btrfs_root *root = BTRFS_I(dir)->root;
2949 struct btrfs_path *path;
2950 struct btrfs_dir_item *di;
2951 struct inode *inode = dentry->d_inode;
2952 u64 index;
2953 int check_link = 1;
2954 int err = -ENOSPC;
2955 int ret;
2956 u64 ino = btrfs_ino(inode);
2957 u64 dir_ino = btrfs_ino(dir);
2958
2959 /*
2960 * 1 for the possible orphan item
2961 * 1 for the dir item
2962 * 1 for the dir index
2963 * 1 for the inode ref
2964 * 1 for the inode ref in the tree log
2965 * 2 for the dir entries in the log
2966 * 1 for the inode
2967 */
2968 trans = btrfs_start_transaction(root, 8);
2969 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2970 return trans;
2971
2972 if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2973 return ERR_PTR(-ENOSPC);
2974
2975 /* check if there is someone else holds reference */
2976 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2977 return ERR_PTR(-ENOSPC);
2978
2979 if (atomic_read(&inode->i_count) > 2)
2980 return ERR_PTR(-ENOSPC);
2981
2982 if (xchg(&root->fs_info->enospc_unlink, 1))
2983 return ERR_PTR(-ENOSPC);
2984
2985 path = btrfs_alloc_path();
2986 if (!path) {
2987 root->fs_info->enospc_unlink = 0;
2988 return ERR_PTR(-ENOMEM);
2989 }
2990
2991 /* 1 for the orphan item */
2992 trans = btrfs_start_transaction(root, 1);
2993 if (IS_ERR(trans)) {
2994 btrfs_free_path(path);
2995 root->fs_info->enospc_unlink = 0;
2996 return trans;
2997 }
2998
2999 path->skip_locking = 1;
3000 path->search_commit_root = 1;
3001
3002 ret = btrfs_lookup_inode(trans, root, path,
3003 &BTRFS_I(dir)->location, 0);
3004 if (ret < 0) {
3005 err = ret;
3006 goto out;
3007 }
3008 if (ret == 0) {
3009 if (check_path_shared(root, path))
3010 goto out;
3011 } else {
3012 check_link = 0;
3013 }
3014 btrfs_release_path(path);
3015
3016 ret = btrfs_lookup_inode(trans, root, path,
3017 &BTRFS_I(inode)->location, 0);
3018 if (ret < 0) {
3019 err = ret;
3020 goto out;
3021 }
3022 if (ret == 0) {
3023 if (check_path_shared(root, path))
3024 goto out;
3025 } else {
3026 check_link = 0;
3027 }
3028 btrfs_release_path(path);
3029
3030 if (ret == 0 && S_ISREG(inode->i_mode)) {
3031 ret = btrfs_lookup_file_extent(trans, root, path,
3032 ino, (u64)-1, 0);
3033 if (ret < 0) {
3034 err = ret;
3035 goto out;
3036 }
3037 BUG_ON(ret == 0); /* Corruption */
3038 if (check_path_shared(root, path))
3039 goto out;
3040 btrfs_release_path(path);
3041 }
3042
3043 if (!check_link) {
3044 err = 0;
3045 goto out;
3046 }
3047
3048 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3049 dentry->d_name.name, dentry->d_name.len, 0);
3050 if (IS_ERR(di)) {
3051 err = PTR_ERR(di);
3052 goto out;
3053 }
3054 if (di) {
3055 if (check_path_shared(root, path))
3056 goto out;
3057 } else {
3058 err = 0;
3059 goto out;
3060 }
3061 btrfs_release_path(path);
3062
3063 ret = btrfs_get_inode_ref_index(trans, root, path, dentry->d_name.name,
3064 dentry->d_name.len, ino, dir_ino, 0,
3065 &index);
3066 if (ret) {
3067 err = ret;
3068 goto out;
3069 }
3070
3071 if (check_path_shared(root, path))
3072 goto out;
3073
3074 btrfs_release_path(path);
3075
3076 /*
3077 * This is a commit root search, if we can lookup inode item and other
3078 * relative items in the commit root, it means the transaction of
3079 * dir/file creation has been committed, and the dir index item that we
3080 * delay to insert has also been inserted into the commit root. So
3081 * we needn't worry about the delayed insertion of the dir index item
3082 * here.
3083 */
3084 di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
3085 dentry->d_name.name, dentry->d_name.len, 0);
3086 if (IS_ERR(di)) {
3087 err = PTR_ERR(di);
3088 goto out;
3089 }
3090 BUG_ON(ret == -ENOENT);
3091 if (check_path_shared(root, path))
3092 goto out;
3093
3094 err = 0;
3095 out:
3096 btrfs_free_path(path);
3097 /* Migrate the orphan reservation over */
3098 if (!err)
3099 err = btrfs_block_rsv_migrate(trans->block_rsv,
3100 &root->fs_info->global_block_rsv,
3101 trans->bytes_reserved);
3102
3103 if (err) {
3104 btrfs_end_transaction(trans, root);
3105 root->fs_info->enospc_unlink = 0;
3106 return ERR_PTR(err);
3107 }
3108
3109 trans->block_rsv = &root->fs_info->global_block_rsv;
3110 return trans;
3111 }
3112
3113 static void __unlink_end_trans(struct btrfs_trans_handle *trans,
3114 struct btrfs_root *root)
3115 {
3116 if (trans->block_rsv->type == BTRFS_BLOCK_RSV_GLOBAL) {
3117 btrfs_block_rsv_release(root, trans->block_rsv,
3118 trans->bytes_reserved);
3119 trans->block_rsv = &root->fs_info->trans_block_rsv;
3120 BUG_ON(!root->fs_info->enospc_unlink);
3121 root->fs_info->enospc_unlink = 0;
3122 }
3123 btrfs_end_transaction(trans, root);
3124 }
3125
3126 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3127 {
3128 struct btrfs_root *root = BTRFS_I(dir)->root;
3129 struct btrfs_trans_handle *trans;
3130 struct inode *inode = dentry->d_inode;
3131 int ret;
3132
3133 trans = __unlink_start_trans(dir, dentry);
3134 if (IS_ERR(trans))
3135 return PTR_ERR(trans);
3136
3137 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3138
3139 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3140 dentry->d_name.name, dentry->d_name.len);
3141 if (ret)
3142 goto out;
3143
3144 if (inode->i_nlink == 0) {
3145 ret = btrfs_orphan_add(trans, inode);
3146 if (ret)
3147 goto out;
3148 }
3149
3150 out:
3151 __unlink_end_trans(trans, root);
3152 btrfs_btree_balance_dirty(root);
3153 return ret;
3154 }
3155
3156 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3157 struct btrfs_root *root,
3158 struct inode *dir, u64 objectid,
3159 const char *name, int name_len)
3160 {
3161 struct btrfs_path *path;
3162 struct extent_buffer *leaf;
3163 struct btrfs_dir_item *di;
3164 struct btrfs_key key;
3165 u64 index;
3166 int ret;
3167 u64 dir_ino = btrfs_ino(dir);
3168
3169 path = btrfs_alloc_path();
3170 if (!path)
3171 return -ENOMEM;
3172
3173 di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3174 name, name_len, -1);
3175 if (IS_ERR_OR_NULL(di)) {
3176 if (!di)
3177 ret = -ENOENT;
3178 else
3179 ret = PTR_ERR(di);
3180 goto out;
3181 }
3182
3183 leaf = path->nodes[0];
3184 btrfs_dir_item_key_to_cpu(leaf, di, &key);
3185 WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3186 ret = btrfs_delete_one_dir_name(trans, root, path, di);
3187 if (ret) {
3188 btrfs_abort_transaction(trans, root, ret);
3189 goto out;
3190 }
3191 btrfs_release_path(path);
3192
3193 ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3194 objectid, root->root_key.objectid,
3195 dir_ino, &index, name, name_len);
3196 if (ret < 0) {
3197 if (ret != -ENOENT) {
3198 btrfs_abort_transaction(trans, root, ret);
3199 goto out;
3200 }
3201 di = btrfs_search_dir_index_item(root, path, dir_ino,
3202 name, name_len);
3203 if (IS_ERR_OR_NULL(di)) {
3204 if (!di)
3205 ret = -ENOENT;
3206 else
3207 ret = PTR_ERR(di);
3208 btrfs_abort_transaction(trans, root, ret);
3209 goto out;
3210 }
3211
3212 leaf = path->nodes[0];
3213 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3214 btrfs_release_path(path);
3215 index = key.offset;
3216 }
3217 btrfs_release_path(path);
3218
3219 ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3220 if (ret) {
3221 btrfs_abort_transaction(trans, root, ret);
3222 goto out;
3223 }
3224
3225 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3226 inode_inc_iversion(dir);
3227 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3228 ret = btrfs_update_inode_fallback(trans, root, dir);
3229 if (ret)
3230 btrfs_abort_transaction(trans, root, ret);
3231 out:
3232 btrfs_free_path(path);
3233 return ret;
3234 }
3235
3236 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3237 {
3238 struct inode *inode = dentry->d_inode;
3239 int err = 0;
3240 struct btrfs_root *root = BTRFS_I(dir)->root;
3241 struct btrfs_trans_handle *trans;
3242
3243 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
3244 return -ENOTEMPTY;
3245 if (btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3246 return -EPERM;
3247
3248 trans = __unlink_start_trans(dir, dentry);
3249 if (IS_ERR(trans))
3250 return PTR_ERR(trans);
3251
3252 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3253 err = btrfs_unlink_subvol(trans, root, dir,
3254 BTRFS_I(inode)->location.objectid,
3255 dentry->d_name.name,
3256 dentry->d_name.len);
3257 goto out;
3258 }
3259
3260 err = btrfs_orphan_add(trans, inode);
3261 if (err)
3262 goto out;
3263
3264 /* now the directory is empty */
3265 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3266 dentry->d_name.name, dentry->d_name.len);
3267 if (!err)
3268 btrfs_i_size_write(inode, 0);
3269 out:
3270 __unlink_end_trans(trans, root);
3271 btrfs_btree_balance_dirty(root);
3272
3273 return err;
3274 }
3275
3276 /*
3277 * this can truncate away extent items, csum items and directory items.
3278 * It starts at a high offset and removes keys until it can't find
3279 * any higher than new_size
3280 *
3281 * csum items that cross the new i_size are truncated to the new size
3282 * as well.
3283 *
3284 * min_type is the minimum key type to truncate down to. If set to 0, this
3285 * will kill all the items on this inode, including the INODE_ITEM_KEY.
3286 */
3287 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3288 struct btrfs_root *root,
3289 struct inode *inode,
3290 u64 new_size, u32 min_type)
3291 {
3292 struct btrfs_path *path;
3293 struct extent_buffer *leaf;
3294 struct btrfs_file_extent_item *fi;
3295 struct btrfs_key key;
3296 struct btrfs_key found_key;
3297 u64 extent_start = 0;
3298 u64 extent_num_bytes = 0;
3299 u64 extent_offset = 0;
3300 u64 item_end = 0;
3301 u64 mask = root->sectorsize - 1;
3302 u32 found_type = (u8)-1;
3303 int found_extent;
3304 int del_item;
3305 int pending_del_nr = 0;
3306 int pending_del_slot = 0;
3307 int extent_type = -1;
3308 int ret;
3309 int err = 0;
3310 u64 ino = btrfs_ino(inode);
3311
3312 BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3313
3314 path = btrfs_alloc_path();
3315 if (!path)
3316 return -ENOMEM;
3317 path->reada = -1;
3318
3319 /*
3320 * We want to drop from the next block forward in case this new size is
3321 * not block aligned since we will be keeping the last block of the
3322 * extent just the way it is.
3323 */
3324 if (root->ref_cows || root == root->fs_info->tree_root)
3325 btrfs_drop_extent_cache(inode, (new_size + mask) & (~mask), (u64)-1, 0);
3326
3327 /*
3328 * This function is also used to drop the items in the log tree before
3329 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3330 * it is used to drop the loged items. So we shouldn't kill the delayed
3331 * items.
3332 */
3333 if (min_type == 0 && root == BTRFS_I(inode)->root)
3334 btrfs_kill_delayed_inode_items(inode);
3335
3336 key.objectid = ino;
3337 key.offset = (u64)-1;
3338 key.type = (u8)-1;
3339
3340 search_again:
3341 path->leave_spinning = 1;
3342 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3343 if (ret < 0) {
3344 err = ret;
3345 goto out;
3346 }
3347
3348 if (ret > 0) {
3349 /* there are no items in the tree for us to truncate, we're
3350 * done
3351 */
3352 if (path->slots[0] == 0)
3353 goto out;
3354 path->slots[0]--;
3355 }
3356
3357 while (1) {
3358 fi = NULL;
3359 leaf = path->nodes[0];
3360 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3361 found_type = btrfs_key_type(&found_key);
3362
3363 if (found_key.objectid != ino)
3364 break;
3365
3366 if (found_type < min_type)
3367 break;
3368
3369 item_end = found_key.offset;
3370 if (found_type == BTRFS_EXTENT_DATA_KEY) {
3371 fi = btrfs_item_ptr(leaf, path->slots[0],
3372 struct btrfs_file_extent_item);
3373 extent_type = btrfs_file_extent_type(leaf, fi);
3374 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3375 item_end +=
3376 btrfs_file_extent_num_bytes(leaf, fi);
3377 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3378 item_end += btrfs_file_extent_inline_len(leaf,
3379 fi);
3380 }
3381 item_end--;
3382 }
3383 if (found_type > min_type) {
3384 del_item = 1;
3385 } else {
3386 if (item_end < new_size)
3387 break;
3388 if (found_key.offset >= new_size)
3389 del_item = 1;
3390 else
3391 del_item = 0;
3392 }
3393 found_extent = 0;
3394 /* FIXME, shrink the extent if the ref count is only 1 */
3395 if (found_type != BTRFS_EXTENT_DATA_KEY)
3396 goto delete;
3397
3398 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3399 u64 num_dec;
3400 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
3401 if (!del_item) {
3402 u64 orig_num_bytes =
3403 btrfs_file_extent_num_bytes(leaf, fi);
3404 extent_num_bytes = new_size -
3405 found_key.offset + root->sectorsize - 1;
3406 extent_num_bytes = extent_num_bytes &
3407 ~((u64)root->sectorsize - 1);
3408 btrfs_set_file_extent_num_bytes(leaf, fi,
3409 extent_num_bytes);
3410 num_dec = (orig_num_bytes -
3411 extent_num_bytes);
3412 if (root->ref_cows && extent_start != 0)
3413 inode_sub_bytes(inode, num_dec);
3414 btrfs_mark_buffer_dirty(leaf);
3415 } else {
3416 extent_num_bytes =
3417 btrfs_file_extent_disk_num_bytes(leaf,
3418 fi);
3419 extent_offset = found_key.offset -
3420 btrfs_file_extent_offset(leaf, fi);
3421
3422 /* FIXME blocksize != 4096 */
3423 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
3424 if (extent_start != 0) {
3425 found_extent = 1;
3426 if (root->ref_cows)
3427 inode_sub_bytes(inode, num_dec);
3428 }
3429 }
3430 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3431 /*
3432 * we can't truncate inline items that have had
3433 * special encodings
3434 */
3435 if (!del_item &&
3436 btrfs_file_extent_compression(leaf, fi) == 0 &&
3437 btrfs_file_extent_encryption(leaf, fi) == 0 &&
3438 btrfs_file_extent_other_encoding(leaf, fi) == 0) {
3439 u32 size = new_size - found_key.offset;
3440
3441 if (root->ref_cows) {
3442 inode_sub_bytes(inode, item_end + 1 -
3443 new_size);
3444 }
3445 size =
3446 btrfs_file_extent_calc_inline_size(size);
3447 btrfs_truncate_item(trans, root, path,
3448 size, 1);
3449 } else if (root->ref_cows) {
3450 inode_sub_bytes(inode, item_end + 1 -
3451 found_key.offset);
3452 }
3453 }
3454 delete:
3455 if (del_item) {
3456 if (!pending_del_nr) {
3457 /* no pending yet, add ourselves */
3458 pending_del_slot = path->slots[0];
3459 pending_del_nr = 1;
3460 } else if (pending_del_nr &&
3461 path->slots[0] + 1 == pending_del_slot) {
3462 /* hop on the pending chunk */
3463 pending_del_nr++;
3464 pending_del_slot = path->slots[0];
3465 } else {
3466 BUG();
3467 }
3468 } else {
3469 break;
3470 }
3471 if (found_extent && (root->ref_cows ||
3472 root == root->fs_info->tree_root)) {
3473 btrfs_set_path_blocking(path);
3474 ret = btrfs_free_extent(trans, root, extent_start,
3475 extent_num_bytes, 0,
3476 btrfs_header_owner(leaf),
3477 ino, extent_offset, 0);
3478 BUG_ON(ret);
3479 }
3480
3481 if (found_type == BTRFS_INODE_ITEM_KEY)
3482 break;
3483
3484 if (path->slots[0] == 0 ||
3485 path->slots[0] != pending_del_slot) {
3486 if (pending_del_nr) {
3487 ret = btrfs_del_items(trans, root, path,
3488 pending_del_slot,
3489 pending_del_nr);
3490 if (ret) {
3491 btrfs_abort_transaction(trans,
3492 root, ret);
3493 goto error;
3494 }
3495 pending_del_nr = 0;
3496 }
3497 btrfs_release_path(path);
3498 goto search_again;
3499 } else {
3500 path->slots[0]--;
3501 }
3502 }
3503 out:
3504 if (pending_del_nr) {
3505 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3506 pending_del_nr);
3507 if (ret)
3508 btrfs_abort_transaction(trans, root, ret);
3509 }
3510 error:
3511 btrfs_free_path(path);
3512 return err;
3513 }
3514
3515 /*
3516 * btrfs_truncate_page - read, zero a chunk and write a page
3517 * @inode - inode that we're zeroing
3518 * @from - the offset to start zeroing
3519 * @len - the length to zero, 0 to zero the entire range respective to the
3520 * offset
3521 * @front - zero up to the offset instead of from the offset on
3522 *
3523 * This will find the page for the "from" offset and cow the page and zero the
3524 * part we want to zero. This is used with truncate and hole punching.
3525 */
3526 int btrfs_truncate_page(struct inode *inode, loff_t from, loff_t len,
3527 int front)
3528 {
3529 struct address_space *mapping = inode->i_mapping;
3530 struct btrfs_root *root = BTRFS_I(inode)->root;
3531 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3532 struct btrfs_ordered_extent *ordered;
3533 struct extent_state *cached_state = NULL;
3534 char *kaddr;
3535 u32 blocksize = root->sectorsize;
3536 pgoff_t index = from >> PAGE_CACHE_SHIFT;
3537 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3538 struct page *page;
3539 gfp_t mask = btrfs_alloc_write_mask(mapping);
3540 int ret = 0;
3541 u64 page_start;
3542 u64 page_end;
3543
3544 if ((offset & (blocksize - 1)) == 0 &&
3545 (!len || ((len & (blocksize - 1)) == 0)))
3546 goto out;
3547 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
3548 if (ret)
3549 goto out;
3550
3551 again:
3552 page = find_or_create_page(mapping, index, mask);
3553 if (!page) {
3554 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
3555 ret = -ENOMEM;
3556 goto out;
3557 }
3558
3559 page_start = page_offset(page);
3560 page_end = page_start + PAGE_CACHE_SIZE - 1;
3561
3562 if (!PageUptodate(page)) {
3563 ret = btrfs_readpage(NULL, page);
3564 lock_page(page);
3565 if (page->mapping != mapping) {
3566 unlock_page(page);
3567 page_cache_release(page);
3568 goto again;
3569 }
3570 if (!PageUptodate(page)) {
3571 ret = -EIO;
3572 goto out_unlock;
3573 }
3574 }
3575 wait_on_page_writeback(page);
3576
3577 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
3578 set_page_extent_mapped(page);
3579
3580 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3581 if (ordered) {
3582 unlock_extent_cached(io_tree, page_start, page_end,
3583 &cached_state, GFP_NOFS);
3584 unlock_page(page);
3585 page_cache_release(page);
3586 btrfs_start_ordered_extent(inode, ordered, 1);
3587 btrfs_put_ordered_extent(ordered);
3588 goto again;
3589 }
3590
3591 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
3592 EXTENT_DIRTY | EXTENT_DELALLOC |
3593 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
3594 0, 0, &cached_state, GFP_NOFS);
3595
3596 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3597 &cached_state);
3598 if (ret) {
3599 unlock_extent_cached(io_tree, page_start, page_end,
3600 &cached_state, GFP_NOFS);
3601 goto out_unlock;
3602 }
3603
3604 if (offset != PAGE_CACHE_SIZE) {
3605 if (!len)
3606 len = PAGE_CACHE_SIZE - offset;
3607 kaddr = kmap(page);
3608 if (front)
3609 memset(kaddr, 0, offset);
3610 else
3611 memset(kaddr + offset, 0, len);
3612 flush_dcache_page(page);
3613 kunmap(page);
3614 }
3615 ClearPageChecked(page);
3616 set_page_dirty(page);
3617 unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3618 GFP_NOFS);
3619
3620 out_unlock:
3621 if (ret)
3622 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
3623 unlock_page(page);
3624 page_cache_release(page);
3625 out:
3626 return ret;
3627 }
3628
3629 /*
3630 * This function puts in dummy file extents for the area we're creating a hole
3631 * for. So if we are truncating this file to a larger size we need to insert
3632 * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3633 * the range between oldsize and size
3634 */
3635 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3636 {
3637 struct btrfs_trans_handle *trans;
3638 struct btrfs_root *root = BTRFS_I(inode)->root;
3639 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3640 struct extent_map *em = NULL;
3641 struct extent_state *cached_state = NULL;
3642 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3643 u64 mask = root->sectorsize - 1;
3644 u64 hole_start = (oldsize + mask) & ~mask;
3645 u64 block_end = (size + mask) & ~mask;
3646 u64 last_byte;
3647 u64 cur_offset;
3648 u64 hole_size;
3649 int err = 0;
3650
3651 if (size <= hole_start)
3652 return 0;
3653
3654 while (1) {
3655 struct btrfs_ordered_extent *ordered;
3656 btrfs_wait_ordered_range(inode, hole_start,
3657 block_end - hole_start);
3658 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3659 &cached_state);
3660 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3661 if (!ordered)
3662 break;
3663 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3664 &cached_state, GFP_NOFS);
3665 btrfs_put_ordered_extent(ordered);
3666 }
3667
3668 cur_offset = hole_start;
3669 while (1) {
3670 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3671 block_end - cur_offset, 0);
3672 if (IS_ERR(em)) {
3673 err = PTR_ERR(em);
3674 break;
3675 }
3676 last_byte = min(extent_map_end(em), block_end);
3677 last_byte = (last_byte + mask) & ~mask;
3678 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3679 struct extent_map *hole_em;
3680 hole_size = last_byte - cur_offset;
3681
3682 trans = btrfs_start_transaction(root, 3);
3683 if (IS_ERR(trans)) {
3684 err = PTR_ERR(trans);
3685 break;
3686 }
3687
3688 err = btrfs_drop_extents(trans, root, inode,
3689 cur_offset,
3690 cur_offset + hole_size, 1);
3691 if (err) {
3692 btrfs_abort_transaction(trans, root, err);
3693 btrfs_end_transaction(trans, root);
3694 break;
3695 }
3696
3697 err = btrfs_insert_file_extent(trans, root,
3698 btrfs_ino(inode), cur_offset, 0,
3699 0, hole_size, 0, hole_size,
3700 0, 0, 0);
3701 if (err) {
3702 btrfs_abort_transaction(trans, root, err);
3703 btrfs_end_transaction(trans, root);
3704 break;
3705 }
3706
3707 btrfs_drop_extent_cache(inode, cur_offset,
3708 cur_offset + hole_size - 1, 0);
3709 hole_em = alloc_extent_map();
3710 if (!hole_em) {
3711 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3712 &BTRFS_I(inode)->runtime_flags);
3713 goto next;
3714 }
3715 hole_em->start = cur_offset;
3716 hole_em->len = hole_size;
3717 hole_em->orig_start = cur_offset;
3718
3719 hole_em->block_start = EXTENT_MAP_HOLE;
3720 hole_em->block_len = 0;
3721 hole_em->orig_block_len = 0;
3722 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
3723 hole_em->compress_type = BTRFS_COMPRESS_NONE;
3724 hole_em->generation = trans->transid;
3725
3726 while (1) {
3727 write_lock(&em_tree->lock);
3728 err = add_extent_mapping(em_tree, hole_em);
3729 if (!err)
3730 list_move(&hole_em->list,
3731 &em_tree->modified_extents);
3732 write_unlock(&em_tree->lock);
3733 if (err != -EEXIST)
3734 break;
3735 btrfs_drop_extent_cache(inode, cur_offset,
3736 cur_offset +
3737 hole_size - 1, 0);
3738 }
3739 free_extent_map(hole_em);
3740 next:
3741 btrfs_update_inode(trans, root, inode);
3742 btrfs_end_transaction(trans, root);
3743 }
3744 free_extent_map(em);
3745 em = NULL;
3746 cur_offset = last_byte;
3747 if (cur_offset >= block_end)
3748 break;
3749 }
3750
3751 free_extent_map(em);
3752 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3753 GFP_NOFS);
3754 return err;
3755 }
3756
3757 static int btrfs_setsize(struct inode *inode, loff_t newsize)
3758 {
3759 struct btrfs_root *root = BTRFS_I(inode)->root;
3760 struct btrfs_trans_handle *trans;
3761 loff_t oldsize = i_size_read(inode);
3762 int ret;
3763
3764 if (newsize == oldsize)
3765 return 0;
3766
3767 if (newsize > oldsize) {
3768 truncate_pagecache(inode, oldsize, newsize);
3769 ret = btrfs_cont_expand(inode, oldsize, newsize);
3770 if (ret)
3771 return ret;
3772
3773 trans = btrfs_start_transaction(root, 1);
3774 if (IS_ERR(trans))
3775 return PTR_ERR(trans);
3776
3777 i_size_write(inode, newsize);
3778 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3779 ret = btrfs_update_inode(trans, root, inode);
3780 btrfs_end_transaction(trans, root);
3781 } else {
3782
3783 /*
3784 * We're truncating a file that used to have good data down to
3785 * zero. Make sure it gets into the ordered flush list so that
3786 * any new writes get down to disk quickly.
3787 */
3788 if (newsize == 0)
3789 set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
3790 &BTRFS_I(inode)->runtime_flags);
3791
3792 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3793 truncate_setsize(inode, newsize);
3794 ret = btrfs_truncate(inode);
3795 }
3796
3797 return ret;
3798 }
3799
3800 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3801 {
3802 struct inode *inode = dentry->d_inode;
3803 struct btrfs_root *root = BTRFS_I(inode)->root;
3804 int err;
3805
3806 if (btrfs_root_readonly(root))
3807 return -EROFS;
3808
3809 err = inode_change_ok(inode, attr);
3810 if (err)
3811 return err;
3812
3813 if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3814 err = btrfs_setsize(inode, attr->ia_size);
3815 if (err)
3816 return err;
3817 }
3818
3819 if (attr->ia_valid) {
3820 setattr_copy(inode, attr);
3821 inode_inc_iversion(inode);
3822 err = btrfs_dirty_inode(inode);
3823
3824 if (!err && attr->ia_valid & ATTR_MODE)
3825 err = btrfs_acl_chmod(inode);
3826 }
3827
3828 return err;
3829 }
3830
3831 void btrfs_evict_inode(struct inode *inode)
3832 {
3833 struct btrfs_trans_handle *trans;
3834 struct btrfs_root *root = BTRFS_I(inode)->root;
3835 struct btrfs_block_rsv *rsv, *global_rsv;
3836 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
3837 int ret;
3838
3839 trace_btrfs_inode_evict(inode);
3840
3841 truncate_inode_pages(&inode->i_data, 0);
3842 if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3843 btrfs_is_free_space_inode(inode)))
3844 goto no_delete;
3845
3846 if (is_bad_inode(inode)) {
3847 btrfs_orphan_del(NULL, inode);
3848 goto no_delete;
3849 }
3850 /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3851 btrfs_wait_ordered_range(inode, 0, (u64)-1);
3852
3853 if (root->fs_info->log_root_recovering) {
3854 BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3855 &BTRFS_I(inode)->runtime_flags));
3856 goto no_delete;
3857 }
3858
3859 if (inode->i_nlink > 0) {
3860 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3861 goto no_delete;
3862 }
3863
3864 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
3865 if (!rsv) {
3866 btrfs_orphan_del(NULL, inode);
3867 goto no_delete;
3868 }
3869 rsv->size = min_size;
3870 rsv->failfast = 1;
3871 global_rsv = &root->fs_info->global_block_rsv;
3872
3873 btrfs_i_size_write(inode, 0);
3874
3875 /*
3876 * This is a bit simpler than btrfs_truncate since we've already
3877 * reserved our space for our orphan item in the unlink, so we just
3878 * need to reserve some slack space in case we add bytes and update
3879 * inode item when doing the truncate.
3880 */
3881 while (1) {
3882 ret = btrfs_block_rsv_refill(root, rsv, min_size,
3883 BTRFS_RESERVE_FLUSH_LIMIT);
3884
3885 /*
3886 * Try and steal from the global reserve since we will
3887 * likely not use this space anyway, we want to try as
3888 * hard as possible to get this to work.
3889 */
3890 if (ret)
3891 ret = btrfs_block_rsv_migrate(global_rsv, rsv, min_size);
3892
3893 if (ret) {
3894 printk(KERN_WARNING "Could not get space for a "
3895 "delete, will truncate on mount %d\n", ret);
3896 btrfs_orphan_del(NULL, inode);
3897 btrfs_free_block_rsv(root, rsv);
3898 goto no_delete;
3899 }
3900
3901 trans = btrfs_start_transaction_lflush(root, 1);
3902 if (IS_ERR(trans)) {
3903 btrfs_orphan_del(NULL, inode);
3904 btrfs_free_block_rsv(root, rsv);
3905 goto no_delete;
3906 }
3907
3908 trans->block_rsv = rsv;
3909
3910 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3911 if (ret != -ENOSPC)
3912 break;
3913
3914 trans->block_rsv = &root->fs_info->trans_block_rsv;
3915 ret = btrfs_update_inode(trans, root, inode);
3916 BUG_ON(ret);
3917
3918 btrfs_end_transaction(trans, root);
3919 trans = NULL;
3920 btrfs_btree_balance_dirty(root);
3921 }
3922
3923 btrfs_free_block_rsv(root, rsv);
3924
3925 if (ret == 0) {
3926 trans->block_rsv = root->orphan_block_rsv;
3927 ret = btrfs_orphan_del(trans, inode);
3928 BUG_ON(ret);
3929 }
3930
3931 trans->block_rsv = &root->fs_info->trans_block_rsv;
3932 if (!(root == root->fs_info->tree_root ||
3933 root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
3934 btrfs_return_ino(root, btrfs_ino(inode));
3935
3936 btrfs_end_transaction(trans, root);
3937 btrfs_btree_balance_dirty(root);
3938 no_delete:
3939 clear_inode(inode);
3940 return;
3941 }
3942
3943 /*
3944 * this returns the key found in the dir entry in the location pointer.
3945 * If no dir entries were found, location->objectid is 0.
3946 */
3947 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3948 struct btrfs_key *location)
3949 {
3950 const char *name = dentry->d_name.name;
3951 int namelen = dentry->d_name.len;
3952 struct btrfs_dir_item *di;
3953 struct btrfs_path *path;
3954 struct btrfs_root *root = BTRFS_I(dir)->root;
3955 int ret = 0;
3956
3957 path = btrfs_alloc_path();
3958 if (!path)
3959 return -ENOMEM;
3960
3961 di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
3962 namelen, 0);
3963 if (IS_ERR(di))
3964 ret = PTR_ERR(di);
3965
3966 if (IS_ERR_OR_NULL(di))
3967 goto out_err;
3968
3969 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
3970 out:
3971 btrfs_free_path(path);
3972 return ret;
3973 out_err:
3974 location->objectid = 0;
3975 goto out;
3976 }
3977
3978 /*
3979 * when we hit a tree root in a directory, the btrfs part of the inode
3980 * needs to be changed to reflect the root directory of the tree root. This
3981 * is kind of like crossing a mount point.
3982 */
3983 static int fixup_tree_root_location(struct btrfs_root *root,
3984 struct inode *dir,
3985 struct dentry *dentry,
3986 struct btrfs_key *location,
3987 struct btrfs_root **sub_root)
3988 {
3989 struct btrfs_path *path;
3990 struct btrfs_root *new_root;
3991 struct btrfs_root_ref *ref;
3992 struct extent_buffer *leaf;
3993 int ret;
3994 int err = 0;
3995
3996 path = btrfs_alloc_path();
3997 if (!path) {
3998 err = -ENOMEM;
3999 goto out;
4000 }
4001
4002 err = -ENOENT;
4003 ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
4004 BTRFS_I(dir)->root->root_key.objectid,
4005 location->objectid);
4006 if (ret) {
4007 if (ret < 0)
4008 err = ret;
4009 goto out;
4010 }
4011
4012 leaf = path->nodes[0];
4013 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
4014 if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
4015 btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
4016 goto out;
4017
4018 ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
4019 (unsigned long)(ref + 1),
4020 dentry->d_name.len);
4021 if (ret)
4022 goto out;
4023
4024 btrfs_release_path(path);
4025
4026 new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
4027 if (IS_ERR(new_root)) {
4028 err = PTR_ERR(new_root);
4029 goto out;
4030 }
4031
4032 if (btrfs_root_refs(&new_root->root_item) == 0) {
4033 err = -ENOENT;
4034 goto out;
4035 }
4036
4037 *sub_root = new_root;
4038 location->objectid = btrfs_root_dirid(&new_root->root_item);
4039 location->type = BTRFS_INODE_ITEM_KEY;
4040 location->offset = 0;
4041 err = 0;
4042 out:
4043 btrfs_free_path(path);
4044 return err;
4045 }
4046
4047 static void inode_tree_add(struct inode *inode)
4048 {
4049 struct btrfs_root *root = BTRFS_I(inode)->root;
4050 struct btrfs_inode *entry;
4051 struct rb_node **p;
4052 struct rb_node *parent;
4053 u64 ino = btrfs_ino(inode);
4054 again:
4055 p = &root->inode_tree.rb_node;
4056 parent = NULL;
4057
4058 if (inode_unhashed(inode))
4059 return;
4060
4061 spin_lock(&root->inode_lock);
4062 while (*p) {
4063 parent = *p;
4064 entry = rb_entry(parent, struct btrfs_inode, rb_node);
4065
4066 if (ino < btrfs_ino(&entry->vfs_inode))
4067 p = &parent->rb_left;
4068 else if (ino > btrfs_ino(&entry->vfs_inode))
4069 p = &parent->rb_right;
4070 else {
4071 WARN_ON(!(entry->vfs_inode.i_state &
4072 (I_WILL_FREE | I_FREEING)));
4073 rb_erase(parent, &root->inode_tree);
4074 RB_CLEAR_NODE(parent);
4075 spin_unlock(&root->inode_lock);
4076 goto again;
4077 }
4078 }
4079 rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
4080 rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4081 spin_unlock(&root->inode_lock);
4082 }
4083
4084 static void inode_tree_del(struct inode *inode)
4085 {
4086 struct btrfs_root *root = BTRFS_I(inode)->root;
4087 int empty = 0;
4088
4089 spin_lock(&root->inode_lock);
4090 if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
4091 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4092 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
4093 empty = RB_EMPTY_ROOT(&root->inode_tree);
4094 }
4095 spin_unlock(&root->inode_lock);
4096
4097 /*
4098 * Free space cache has inodes in the tree root, but the tree root has a
4099 * root_refs of 0, so this could end up dropping the tree root as a
4100 * snapshot, so we need the extra !root->fs_info->tree_root check to
4101 * make sure we don't drop it.
4102 */
4103 if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4104 root != root->fs_info->tree_root) {
4105 synchronize_srcu(&root->fs_info->subvol_srcu);
4106 spin_lock(&root->inode_lock);
4107 empty = RB_EMPTY_ROOT(&root->inode_tree);
4108 spin_unlock(&root->inode_lock);
4109 if (empty)
4110 btrfs_add_dead_root(root);
4111 }
4112 }
4113
4114 void btrfs_invalidate_inodes(struct btrfs_root *root)
4115 {
4116 struct rb_node *node;
4117 struct rb_node *prev;
4118 struct btrfs_inode *entry;
4119 struct inode *inode;
4120 u64 objectid = 0;
4121
4122 WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4123
4124 spin_lock(&root->inode_lock);
4125 again:
4126 node = root->inode_tree.rb_node;
4127 prev = NULL;
4128 while (node) {
4129 prev = node;
4130 entry = rb_entry(node, struct btrfs_inode, rb_node);
4131
4132 if (objectid < btrfs_ino(&entry->vfs_inode))
4133 node = node->rb_left;
4134 else if (objectid > btrfs_ino(&entry->vfs_inode))
4135 node = node->rb_right;
4136 else
4137 break;
4138 }
4139 if (!node) {
4140 while (prev) {
4141 entry = rb_entry(prev, struct btrfs_inode, rb_node);
4142 if (objectid <= btrfs_ino(&entry->vfs_inode)) {
4143 node = prev;
4144 break;
4145 }
4146 prev = rb_next(prev);
4147 }
4148 }
4149 while (node) {
4150 entry = rb_entry(node, struct btrfs_inode, rb_node);
4151 objectid = btrfs_ino(&entry->vfs_inode) + 1;
4152 inode = igrab(&entry->vfs_inode);
4153 if (inode) {
4154 spin_unlock(&root->inode_lock);
4155 if (atomic_read(&inode->i_count) > 1)
4156 d_prune_aliases(inode);
4157 /*
4158 * btrfs_drop_inode will have it removed from
4159 * the inode cache when its usage count
4160 * hits zero.
4161 */
4162 iput(inode);
4163 cond_resched();
4164 spin_lock(&root->inode_lock);
4165 goto again;
4166 }
4167
4168 if (cond_resched_lock(&root->inode_lock))
4169 goto again;
4170
4171 node = rb_next(node);
4172 }
4173 spin_unlock(&root->inode_lock);
4174 }
4175
4176 static int btrfs_init_locked_inode(struct inode *inode, void *p)
4177 {
4178 struct btrfs_iget_args *args = p;
4179 inode->i_ino = args->ino;
4180 BTRFS_I(inode)->root = args->root;
4181 return 0;
4182 }
4183
4184 static int btrfs_find_actor(struct inode *inode, void *opaque)
4185 {
4186 struct btrfs_iget_args *args = opaque;
4187 return args->ino == btrfs_ino(inode) &&
4188 args->root == BTRFS_I(inode)->root;
4189 }
4190
4191 static struct inode *btrfs_iget_locked(struct super_block *s,
4192 u64 objectid,
4193 struct btrfs_root *root)
4194 {
4195 struct inode *inode;
4196 struct btrfs_iget_args args;
4197 args.ino = objectid;
4198 args.root = root;
4199
4200 inode = iget5_locked(s, objectid, btrfs_find_actor,
4201 btrfs_init_locked_inode,
4202 (void *)&args);
4203 return inode;
4204 }
4205
4206 /* Get an inode object given its location and corresponding root.
4207 * Returns in *is_new if the inode was read from disk
4208 */
4209 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
4210 struct btrfs_root *root, int *new)
4211 {
4212 struct inode *inode;
4213
4214 inode = btrfs_iget_locked(s, location->objectid, root);
4215 if (!inode)
4216 return ERR_PTR(-ENOMEM);
4217
4218 if (inode->i_state & I_NEW) {
4219 BTRFS_I(inode)->root = root;
4220 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4221 btrfs_read_locked_inode(inode);
4222 if (!is_bad_inode(inode)) {
4223 inode_tree_add(inode);
4224 unlock_new_inode(inode);
4225 if (new)
4226 *new = 1;
4227 } else {
4228 unlock_new_inode(inode);
4229 iput(inode);
4230 inode = ERR_PTR(-ESTALE);
4231 }
4232 }
4233
4234 return inode;
4235 }
4236
4237 static struct inode *new_simple_dir(struct super_block *s,
4238 struct btrfs_key *key,
4239 struct btrfs_root *root)
4240 {
4241 struct inode *inode = new_inode(s);
4242
4243 if (!inode)
4244 return ERR_PTR(-ENOMEM);
4245
4246 BTRFS_I(inode)->root = root;
4247 memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4248 set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
4249
4250 inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4251 inode->i_op = &btrfs_dir_ro_inode_operations;
4252 inode->i_fop = &simple_dir_operations;
4253 inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4254 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4255
4256 return inode;
4257 }
4258
4259 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4260 {
4261 struct inode *inode;
4262 struct btrfs_root *root = BTRFS_I(dir)->root;
4263 struct btrfs_root *sub_root = root;
4264 struct btrfs_key location;
4265 int index;
4266 int ret = 0;
4267
4268 if (dentry->d_name.len > BTRFS_NAME_LEN)
4269 return ERR_PTR(-ENAMETOOLONG);
4270
4271 if (unlikely(d_need_lookup(dentry))) {
4272 memcpy(&location, dentry->d_fsdata, sizeof(struct btrfs_key));
4273 kfree(dentry->d_fsdata);
4274 dentry->d_fsdata = NULL;
4275 /* This thing is hashed, drop it for now */
4276 d_drop(dentry);
4277 } else {
4278 ret = btrfs_inode_by_name(dir, dentry, &location);
4279 }
4280
4281 if (ret < 0)
4282 return ERR_PTR(ret);
4283
4284 if (location.objectid == 0)
4285 return NULL;
4286
4287 if (location.type == BTRFS_INODE_ITEM_KEY) {
4288 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
4289 return inode;
4290 }
4291
4292 BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4293
4294 index = srcu_read_lock(&root->fs_info->subvol_srcu);
4295 ret = fixup_tree_root_location(root, dir, dentry,
4296 &location, &sub_root);
4297 if (ret < 0) {
4298 if (ret != -ENOENT)
4299 inode = ERR_PTR(ret);
4300 else
4301 inode = new_simple_dir(dir->i_sb, &location, sub_root);
4302 } else {
4303 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
4304 }
4305 srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4306
4307 if (!IS_ERR(inode) && root != sub_root) {
4308 down_read(&root->fs_info->cleanup_work_sem);
4309 if (!(inode->i_sb->s_flags & MS_RDONLY))
4310 ret = btrfs_orphan_cleanup(sub_root);
4311 up_read(&root->fs_info->cleanup_work_sem);
4312 if (ret)
4313 inode = ERR_PTR(ret);
4314 }
4315
4316 return inode;
4317 }
4318
4319 static int btrfs_dentry_delete(const struct dentry *dentry)
4320 {
4321 struct btrfs_root *root;
4322 struct inode *inode = dentry->d_inode;
4323
4324 if (!inode && !IS_ROOT(dentry))
4325 inode = dentry->d_parent->d_inode;
4326
4327 if (inode) {
4328 root = BTRFS_I(inode)->root;
4329 if (btrfs_root_refs(&root->root_item) == 0)
4330 return 1;
4331
4332 if (btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
4333 return 1;
4334 }
4335 return 0;
4336 }
4337
4338 static void btrfs_dentry_release(struct dentry *dentry)
4339 {
4340 if (dentry->d_fsdata)
4341 kfree(dentry->d_fsdata);
4342 }
4343
4344 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4345 unsigned int flags)
4346 {
4347 struct dentry *ret;
4348
4349 ret = d_splice_alias(btrfs_lookup_dentry(dir, dentry), dentry);
4350 if (unlikely(d_need_lookup(dentry))) {
4351 spin_lock(&dentry->d_lock);
4352 dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
4353 spin_unlock(&dentry->d_lock);
4354 }
4355 return ret;
4356 }
4357
4358 unsigned char btrfs_filetype_table[] = {
4359 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4360 };
4361
4362 static int btrfs_real_readdir(struct file *filp, void *dirent,
4363 filldir_t filldir)
4364 {
4365 struct inode *inode = filp->f_dentry->d_inode;
4366 struct btrfs_root *root = BTRFS_I(inode)->root;
4367 struct btrfs_item *item;
4368 struct btrfs_dir_item *di;
4369 struct btrfs_key key;
4370 struct btrfs_key found_key;
4371 struct btrfs_path *path;
4372 struct list_head ins_list;
4373 struct list_head del_list;
4374 int ret;
4375 struct extent_buffer *leaf;
4376 int slot;
4377 unsigned char d_type;
4378 int over = 0;
4379 u32 di_cur;
4380 u32 di_total;
4381 u32 di_len;
4382 int key_type = BTRFS_DIR_INDEX_KEY;
4383 char tmp_name[32];
4384 char *name_ptr;
4385 int name_len;
4386 int is_curr = 0; /* filp->f_pos points to the current index? */
4387
4388 /* FIXME, use a real flag for deciding about the key type */
4389 if (root->fs_info->tree_root == root)
4390 key_type = BTRFS_DIR_ITEM_KEY;
4391
4392 /* special case for "." */
4393 if (filp->f_pos == 0) {
4394 over = filldir(dirent, ".", 1,
4395 filp->f_pos, btrfs_ino(inode), DT_DIR);
4396 if (over)
4397 return 0;
4398 filp->f_pos = 1;
4399 }
4400 /* special case for .., just use the back ref */
4401 if (filp->f_pos == 1) {
4402 u64 pino = parent_ino(filp->f_path.dentry);
4403 over = filldir(dirent, "..", 2,
4404 filp->f_pos, pino, DT_DIR);
4405 if (over)
4406 return 0;
4407 filp->f_pos = 2;
4408 }
4409 path = btrfs_alloc_path();
4410 if (!path)
4411 return -ENOMEM;
4412
4413 path->reada = 1;
4414
4415 if (key_type == BTRFS_DIR_INDEX_KEY) {
4416 INIT_LIST_HEAD(&ins_list);
4417 INIT_LIST_HEAD(&del_list);
4418 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4419 }
4420
4421 btrfs_set_key_type(&key, key_type);
4422 key.offset = filp->f_pos;
4423 key.objectid = btrfs_ino(inode);
4424
4425 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4426 if (ret < 0)
4427 goto err;
4428
4429 while (1) {
4430 leaf = path->nodes[0];
4431 slot = path->slots[0];
4432 if (slot >= btrfs_header_nritems(leaf)) {
4433 ret = btrfs_next_leaf(root, path);
4434 if (ret < 0)
4435 goto err;
4436 else if (ret > 0)
4437 break;
4438 continue;
4439 }
4440
4441 item = btrfs_item_nr(leaf, slot);
4442 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4443
4444 if (found_key.objectid != key.objectid)
4445 break;
4446 if (btrfs_key_type(&found_key) != key_type)
4447 break;
4448 if (found_key.offset < filp->f_pos)
4449 goto next;
4450 if (key_type == BTRFS_DIR_INDEX_KEY &&
4451 btrfs_should_delete_dir_index(&del_list,
4452 found_key.offset))
4453 goto next;
4454
4455 filp->f_pos = found_key.offset;
4456 is_curr = 1;
4457
4458 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4459 di_cur = 0;
4460 di_total = btrfs_item_size(leaf, item);
4461
4462 while (di_cur < di_total) {
4463 struct btrfs_key location;
4464
4465 if (verify_dir_item(root, leaf, di))
4466 break;
4467
4468 name_len = btrfs_dir_name_len(leaf, di);
4469 if (name_len <= sizeof(tmp_name)) {
4470 name_ptr = tmp_name;
4471 } else {
4472 name_ptr = kmalloc(name_len, GFP_NOFS);
4473 if (!name_ptr) {
4474 ret = -ENOMEM;
4475 goto err;
4476 }
4477 }
4478 read_extent_buffer(leaf, name_ptr,
4479 (unsigned long)(di + 1), name_len);
4480
4481 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4482 btrfs_dir_item_key_to_cpu(leaf, di, &location);
4483
4484
4485 /* is this a reference to our own snapshot? If so
4486 * skip it.
4487 *
4488 * In contrast to old kernels, we insert the snapshot's
4489 * dir item and dir index after it has been created, so
4490 * we won't find a reference to our own snapshot. We
4491 * still keep the following code for backward
4492 * compatibility.
4493 */
4494 if (location.type == BTRFS_ROOT_ITEM_KEY &&
4495 location.objectid == root->root_key.objectid) {
4496 over = 0;
4497 goto skip;
4498 }
4499 over = filldir(dirent, name_ptr, name_len,
4500 found_key.offset, location.objectid,
4501 d_type);
4502
4503 skip:
4504 if (name_ptr != tmp_name)
4505 kfree(name_ptr);
4506
4507 if (over)
4508 goto nopos;
4509 di_len = btrfs_dir_name_len(leaf, di) +
4510 btrfs_dir_data_len(leaf, di) + sizeof(*di);
4511 di_cur += di_len;
4512 di = (struct btrfs_dir_item *)((char *)di + di_len);
4513 }
4514 next:
4515 path->slots[0]++;
4516 }
4517
4518 if (key_type == BTRFS_DIR_INDEX_KEY) {
4519 if (is_curr)
4520 filp->f_pos++;
4521 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4522 &ins_list);
4523 if (ret)
4524 goto nopos;
4525 }
4526
4527 /* Reached end of directory/root. Bump pos past the last item. */
4528 if (key_type == BTRFS_DIR_INDEX_KEY)
4529 /*
4530 * 32-bit glibc will use getdents64, but then strtol -
4531 * so the last number we can serve is this.
4532 */
4533 filp->f_pos = 0x7fffffff;
4534 else
4535 filp->f_pos++;
4536 nopos:
4537 ret = 0;
4538 err:
4539 if (key_type == BTRFS_DIR_INDEX_KEY)
4540 btrfs_put_delayed_items(&ins_list, &del_list);
4541 btrfs_free_path(path);
4542 return ret;
4543 }
4544
4545 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4546 {
4547 struct btrfs_root *root = BTRFS_I(inode)->root;
4548 struct btrfs_trans_handle *trans;
4549 int ret = 0;
4550 bool nolock = false;
4551
4552 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
4553 return 0;
4554
4555 if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
4556 nolock = true;
4557
4558 if (wbc->sync_mode == WB_SYNC_ALL) {
4559 if (nolock)
4560 trans = btrfs_join_transaction_nolock(root);
4561 else
4562 trans = btrfs_join_transaction(root);
4563 if (IS_ERR(trans))
4564 return PTR_ERR(trans);
4565 ret = btrfs_commit_transaction(trans, root);
4566 }
4567 return ret;
4568 }
4569
4570 /*
4571 * This is somewhat expensive, updating the tree every time the
4572 * inode changes. But, it is most likely to find the inode in cache.
4573 * FIXME, needs more benchmarking...there are no reasons other than performance
4574 * to keep or drop this code.
4575 */
4576 int btrfs_dirty_inode(struct inode *inode)
4577 {
4578 struct btrfs_root *root = BTRFS_I(inode)->root;
4579 struct btrfs_trans_handle *trans;
4580 int ret;
4581
4582 if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
4583 return 0;
4584
4585 trans = btrfs_join_transaction(root);
4586 if (IS_ERR(trans))
4587 return PTR_ERR(trans);
4588
4589 ret = btrfs_update_inode(trans, root, inode);
4590 if (ret && ret == -ENOSPC) {
4591 /* whoops, lets try again with the full transaction */
4592 btrfs_end_transaction(trans, root);
4593 trans = btrfs_start_transaction(root, 1);
4594 if (IS_ERR(trans))
4595 return PTR_ERR(trans);
4596
4597 ret = btrfs_update_inode(trans, root, inode);
4598 }
4599 btrfs_end_transaction(trans, root);
4600 if (BTRFS_I(inode)->delayed_node)
4601 btrfs_balance_delayed_items(root);
4602
4603 return ret;
4604 }
4605
4606 /*
4607 * This is a copy of file_update_time. We need this so we can return error on
4608 * ENOSPC for updating the inode in the case of file write and mmap writes.
4609 */
4610 static int btrfs_update_time(struct inode *inode, struct timespec *now,
4611 int flags)
4612 {
4613 struct btrfs_root *root = BTRFS_I(inode)->root;
4614
4615 if (btrfs_root_readonly(root))
4616 return -EROFS;
4617
4618 if (flags & S_VERSION)
4619 inode_inc_iversion(inode);
4620 if (flags & S_CTIME)
4621 inode->i_ctime = *now;
4622 if (flags & S_MTIME)
4623 inode->i_mtime = *now;
4624 if (flags & S_ATIME)
4625 inode->i_atime = *now;
4626 return btrfs_dirty_inode(inode);
4627 }
4628
4629 /*
4630 * find the highest existing sequence number in a directory
4631 * and then set the in-memory index_cnt variable to reflect
4632 * free sequence numbers
4633 */
4634 static int btrfs_set_inode_index_count(struct inode *inode)
4635 {
4636 struct btrfs_root *root = BTRFS_I(inode)->root;
4637 struct btrfs_key key, found_key;
4638 struct btrfs_path *path;
4639 struct extent_buffer *leaf;
4640 int ret;
4641
4642 key.objectid = btrfs_ino(inode);
4643 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4644 key.offset = (u64)-1;
4645
4646 path = btrfs_alloc_path();
4647 if (!path)
4648 return -ENOMEM;
4649
4650 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4651 if (ret < 0)
4652 goto out;
4653 /* FIXME: we should be able to handle this */
4654 if (ret == 0)
4655 goto out;
4656 ret = 0;
4657
4658 /*
4659 * MAGIC NUMBER EXPLANATION:
4660 * since we search a directory based on f_pos we have to start at 2
4661 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4662 * else has to start at 2
4663 */
4664 if (path->slots[0] == 0) {
4665 BTRFS_I(inode)->index_cnt = 2;
4666 goto out;
4667 }
4668
4669 path->slots[0]--;
4670
4671 leaf = path->nodes[0];
4672 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4673
4674 if (found_key.objectid != btrfs_ino(inode) ||
4675 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4676 BTRFS_I(inode)->index_cnt = 2;
4677 goto out;
4678 }
4679
4680 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4681 out:
4682 btrfs_free_path(path);
4683 return ret;
4684 }
4685
4686 /*
4687 * helper to find a free sequence number in a given directory. This current
4688 * code is very simple, later versions will do smarter things in the btree
4689 */
4690 int btrfs_set_inode_index(struct inode *dir, u64 *index)
4691 {
4692 int ret = 0;
4693
4694 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4695 ret = btrfs_inode_delayed_dir_index_count(dir);
4696 if (ret) {
4697 ret = btrfs_set_inode_index_count(dir);
4698 if (ret)
4699 return ret;
4700 }
4701 }
4702
4703 *index = BTRFS_I(dir)->index_cnt;
4704 BTRFS_I(dir)->index_cnt++;
4705
4706 return ret;
4707 }
4708
4709 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4710 struct btrfs_root *root,
4711 struct inode *dir,
4712 const char *name, int name_len,
4713 u64 ref_objectid, u64 objectid,
4714 umode_t mode, u64 *index)
4715 {
4716 struct inode *inode;
4717 struct btrfs_inode_item *inode_item;
4718 struct btrfs_key *location;
4719 struct btrfs_path *path;
4720 struct btrfs_inode_ref *ref;
4721 struct btrfs_key key[2];
4722 u32 sizes[2];
4723 unsigned long ptr;
4724 int ret;
4725 int owner;
4726
4727 path = btrfs_alloc_path();
4728 if (!path)
4729 return ERR_PTR(-ENOMEM);
4730
4731 inode = new_inode(root->fs_info->sb);
4732 if (!inode) {
4733 btrfs_free_path(path);
4734 return ERR_PTR(-ENOMEM);
4735 }
4736
4737 /*
4738 * we have to initialize this early, so we can reclaim the inode
4739 * number if we fail afterwards in this function.
4740 */
4741 inode->i_ino = objectid;
4742
4743 if (dir) {
4744 trace_btrfs_inode_request(dir);
4745
4746 ret = btrfs_set_inode_index(dir, index);
4747 if (ret) {
4748 btrfs_free_path(path);
4749 iput(inode);
4750 return ERR_PTR(ret);
4751 }
4752 }
4753 /*
4754 * index_cnt is ignored for everything but a dir,
4755 * btrfs_get_inode_index_count has an explanation for the magic
4756 * number
4757 */
4758 BTRFS_I(inode)->index_cnt = 2;
4759 BTRFS_I(inode)->root = root;
4760 BTRFS_I(inode)->generation = trans->transid;
4761 inode->i_generation = BTRFS_I(inode)->generation;
4762
4763 /*
4764 * We could have gotten an inode number from somebody who was fsynced
4765 * and then removed in this same transaction, so let's just set full
4766 * sync since it will be a full sync anyway and this will blow away the
4767 * old info in the log.
4768 */
4769 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
4770
4771 if (S_ISDIR(mode))
4772 owner = 0;
4773 else
4774 owner = 1;
4775
4776 key[0].objectid = objectid;
4777 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4778 key[0].offset = 0;
4779
4780 /*
4781 * Start new inodes with an inode_ref. This is slightly more
4782 * efficient for small numbers of hard links since they will
4783 * be packed into one item. Extended refs will kick in if we
4784 * add more hard links than can fit in the ref item.
4785 */
4786 key[1].objectid = objectid;
4787 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4788 key[1].offset = ref_objectid;
4789
4790 sizes[0] = sizeof(struct btrfs_inode_item);
4791 sizes[1] = name_len + sizeof(*ref);
4792
4793 path->leave_spinning = 1;
4794 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4795 if (ret != 0)
4796 goto fail;
4797
4798 inode_init_owner(inode, dir, mode);
4799 inode_set_bytes(inode, 0);
4800 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4801 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4802 struct btrfs_inode_item);
4803 memset_extent_buffer(path->nodes[0], 0, (unsigned long)inode_item,
4804 sizeof(*inode_item));
4805 fill_inode_item(trans, path->nodes[0], inode_item, inode);
4806
4807 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4808 struct btrfs_inode_ref);
4809 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
4810 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
4811 ptr = (unsigned long)(ref + 1);
4812 write_extent_buffer(path->nodes[0], name, ptr, name_len);
4813
4814 btrfs_mark_buffer_dirty(path->nodes[0]);
4815 btrfs_free_path(path);
4816
4817 location = &BTRFS_I(inode)->location;
4818 location->objectid = objectid;
4819 location->offset = 0;
4820 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4821
4822 btrfs_inherit_iflags(inode, dir);
4823
4824 if (S_ISREG(mode)) {
4825 if (btrfs_test_opt(root, NODATASUM))
4826 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4827 if (btrfs_test_opt(root, NODATACOW) ||
4828 (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
4829 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4830 }
4831
4832 insert_inode_hash(inode);
4833 inode_tree_add(inode);
4834
4835 trace_btrfs_inode_new(inode);
4836 btrfs_set_inode_last_trans(trans, inode);
4837
4838 btrfs_update_root_times(trans, root);
4839
4840 return inode;
4841 fail:
4842 if (dir)
4843 BTRFS_I(dir)->index_cnt--;
4844 btrfs_free_path(path);
4845 iput(inode);
4846 return ERR_PTR(ret);
4847 }
4848
4849 static inline u8 btrfs_inode_type(struct inode *inode)
4850 {
4851 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4852 }
4853
4854 /*
4855 * utility function to add 'inode' into 'parent_inode' with
4856 * a give name and a given sequence number.
4857 * if 'add_backref' is true, also insert a backref from the
4858 * inode to the parent directory.
4859 */
4860 int btrfs_add_link(struct btrfs_trans_handle *trans,
4861 struct inode *parent_inode, struct inode *inode,
4862 const char *name, int name_len, int add_backref, u64 index)
4863 {
4864 int ret = 0;
4865 struct btrfs_key key;
4866 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4867 u64 ino = btrfs_ino(inode);
4868 u64 parent_ino = btrfs_ino(parent_inode);
4869
4870 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4871 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4872 } else {
4873 key.objectid = ino;
4874 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4875 key.offset = 0;
4876 }
4877
4878 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4879 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4880 key.objectid, root->root_key.objectid,
4881 parent_ino, index, name, name_len);
4882 } else if (add_backref) {
4883 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4884 parent_ino, index);
4885 }
4886
4887 /* Nothing to clean up yet */
4888 if (ret)
4889 return ret;
4890
4891 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4892 parent_inode, &key,
4893 btrfs_inode_type(inode), index);
4894 if (ret == -EEXIST)
4895 goto fail_dir_item;
4896 else if (ret) {
4897 btrfs_abort_transaction(trans, root, ret);
4898 return ret;
4899 }
4900
4901 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4902 name_len * 2);
4903 inode_inc_iversion(parent_inode);
4904 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4905 ret = btrfs_update_inode(trans, root, parent_inode);
4906 if (ret)
4907 btrfs_abort_transaction(trans, root, ret);
4908 return ret;
4909
4910 fail_dir_item:
4911 if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4912 u64 local_index;
4913 int err;
4914 err = btrfs_del_root_ref(trans, root->fs_info->tree_root,
4915 key.objectid, root->root_key.objectid,
4916 parent_ino, &local_index, name, name_len);
4917
4918 } else if (add_backref) {
4919 u64 local_index;
4920 int err;
4921
4922 err = btrfs_del_inode_ref(trans, root, name, name_len,
4923 ino, parent_ino, &local_index);
4924 }
4925 return ret;
4926 }
4927
4928 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
4929 struct inode *dir, struct dentry *dentry,
4930 struct inode *inode, int backref, u64 index)
4931 {
4932 int err = btrfs_add_link(trans, dir, inode,
4933 dentry->d_name.name, dentry->d_name.len,
4934 backref, index);
4935 if (err > 0)
4936 err = -EEXIST;
4937 return err;
4938 }
4939
4940 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4941 umode_t mode, dev_t rdev)
4942 {
4943 struct btrfs_trans_handle *trans;
4944 struct btrfs_root *root = BTRFS_I(dir)->root;
4945 struct inode *inode = NULL;
4946 int err;
4947 int drop_inode = 0;
4948 u64 objectid;
4949 u64 index = 0;
4950
4951 if (!new_valid_dev(rdev))
4952 return -EINVAL;
4953
4954 /*
4955 * 2 for inode item and ref
4956 * 2 for dir items
4957 * 1 for xattr if selinux is on
4958 */
4959 trans = btrfs_start_transaction(root, 5);
4960 if (IS_ERR(trans))
4961 return PTR_ERR(trans);
4962
4963 err = btrfs_find_free_ino(root, &objectid);
4964 if (err)
4965 goto out_unlock;
4966
4967 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4968 dentry->d_name.len, btrfs_ino(dir), objectid,
4969 mode, &index);
4970 if (IS_ERR(inode)) {
4971 err = PTR_ERR(inode);
4972 goto out_unlock;
4973 }
4974
4975 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
4976 if (err) {
4977 drop_inode = 1;
4978 goto out_unlock;
4979 }
4980
4981 err = btrfs_update_inode(trans, root, inode);
4982 if (err) {
4983 drop_inode = 1;
4984 goto out_unlock;
4985 }
4986
4987 /*
4988 * If the active LSM wants to access the inode during
4989 * d_instantiate it needs these. Smack checks to see
4990 * if the filesystem supports xattrs by looking at the
4991 * ops vector.
4992 */
4993
4994 inode->i_op = &btrfs_special_inode_operations;
4995 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
4996 if (err)
4997 drop_inode = 1;
4998 else {
4999 init_special_inode(inode, inode->i_mode, rdev);
5000 btrfs_update_inode(trans, root, inode);
5001 d_instantiate(dentry, inode);
5002 }
5003 out_unlock:
5004 btrfs_end_transaction(trans, root);
5005 btrfs_btree_balance_dirty(root);
5006 if (drop_inode) {
5007 inode_dec_link_count(inode);
5008 iput(inode);
5009 }
5010 return err;
5011 }
5012
5013 static int btrfs_create(struct inode *dir, struct dentry *dentry,
5014 umode_t mode, bool excl)
5015 {
5016 struct btrfs_trans_handle *trans;
5017 struct btrfs_root *root = BTRFS_I(dir)->root;
5018 struct inode *inode = NULL;
5019 int drop_inode_on_err = 0;
5020 int err;
5021 u64 objectid;
5022 u64 index = 0;
5023
5024 /*
5025 * 2 for inode item and ref
5026 * 2 for dir items
5027 * 1 for xattr if selinux is on
5028 */
5029 trans = btrfs_start_transaction(root, 5);
5030 if (IS_ERR(trans))
5031 return PTR_ERR(trans);
5032
5033 err = btrfs_find_free_ino(root, &objectid);
5034 if (err)
5035 goto out_unlock;
5036
5037 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
5038 dentry->d_name.len, btrfs_ino(dir), objectid,
5039 mode, &index);
5040 if (IS_ERR(inode)) {
5041 err = PTR_ERR(inode);
5042 goto out_unlock;
5043 }
5044 drop_inode_on_err = 1;
5045
5046 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5047 if (err)
5048 goto out_unlock;
5049
5050 /*
5051 * If the active LSM wants to access the inode during
5052 * d_instantiate it needs these. Smack checks to see
5053 * if the filesystem supports xattrs by looking at the
5054 * ops vector.
5055 */
5056 inode->i_fop = &btrfs_file_operations;
5057 inode->i_op = &btrfs_file_inode_operations;
5058
5059 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
5060 if (err)
5061 goto out_unlock;
5062
5063 inode->i_mapping->a_ops = &btrfs_aops;
5064 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5065 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5066 d_instantiate(dentry, inode);
5067
5068 out_unlock:
5069 btrfs_end_transaction(trans, root);
5070 if (err && drop_inode_on_err) {
5071 inode_dec_link_count(inode);
5072 iput(inode);
5073 }
5074 btrfs_btree_balance_dirty(root);
5075 return err;
5076 }
5077
5078 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
5079 struct dentry *dentry)
5080 {
5081 struct btrfs_trans_handle *trans;
5082 struct btrfs_root *root = BTRFS_I(dir)->root;
5083 struct inode *inode = old_dentry->d_inode;
5084 u64 index;
5085 int err;
5086 int drop_inode = 0;
5087
5088 /* do not allow sys_link's with other subvols of the same device */
5089 if (root->objectid != BTRFS_I(inode)->root->objectid)
5090 return -EXDEV;
5091
5092 if (inode->i_nlink >= BTRFS_LINK_MAX)
5093 return -EMLINK;
5094
5095 err = btrfs_set_inode_index(dir, &index);
5096 if (err)
5097 goto fail;
5098
5099 /*
5100 * 2 items for inode and inode ref
5101 * 2 items for dir items
5102 * 1 item for parent inode
5103 */
5104 trans = btrfs_start_transaction(root, 5);
5105 if (IS_ERR(trans)) {
5106 err = PTR_ERR(trans);
5107 goto fail;
5108 }
5109
5110 btrfs_inc_nlink(inode);
5111 inode_inc_iversion(inode);
5112 inode->i_ctime = CURRENT_TIME;
5113 ihold(inode);
5114 set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
5115
5116 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
5117
5118 if (err) {
5119 drop_inode = 1;
5120 } else {
5121 struct dentry *parent = dentry->d_parent;
5122 err = btrfs_update_inode(trans, root, inode);
5123 if (err)
5124 goto fail;
5125 d_instantiate(dentry, inode);
5126 btrfs_log_new_name(trans, inode, NULL, parent);
5127 }
5128
5129 btrfs_end_transaction(trans, root);
5130 fail:
5131 if (drop_inode) {
5132 inode_dec_link_count(inode);
5133 iput(inode);
5134 }
5135 btrfs_btree_balance_dirty(root);
5136 return err;
5137 }
5138
5139 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
5140 {
5141 struct inode *inode = NULL;
5142 struct btrfs_trans_handle *trans;
5143 struct btrfs_root *root = BTRFS_I(dir)->root;
5144 int err = 0;
5145 int drop_on_err = 0;
5146 u64 objectid = 0;
5147 u64 index = 0;
5148
5149 /*
5150 * 2 items for inode and ref
5151 * 2 items for dir items
5152 * 1 for xattr if selinux is on
5153 */
5154 trans = btrfs_start_transaction(root, 5);
5155 if (IS_ERR(trans))
5156 return PTR_ERR(trans);
5157
5158 err = btrfs_find_free_ino(root, &objectid);
5159 if (err)
5160 goto out_fail;
5161
5162 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
5163 dentry->d_name.len, btrfs_ino(dir), objectid,
5164 S_IFDIR | mode, &index);
5165 if (IS_ERR(inode)) {
5166 err = PTR_ERR(inode);
5167 goto out_fail;
5168 }
5169
5170 drop_on_err = 1;
5171
5172 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5173 if (err)
5174 goto out_fail;
5175
5176 inode->i_op = &btrfs_dir_inode_operations;
5177 inode->i_fop = &btrfs_dir_file_operations;
5178
5179 btrfs_i_size_write(inode, 0);
5180 err = btrfs_update_inode(trans, root, inode);
5181 if (err)
5182 goto out_fail;
5183
5184 err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5185 dentry->d_name.len, 0, index);
5186 if (err)
5187 goto out_fail;
5188
5189 d_instantiate(dentry, inode);
5190 drop_on_err = 0;
5191
5192 out_fail:
5193 btrfs_end_transaction(trans, root);
5194 if (drop_on_err)
5195 iput(inode);
5196 btrfs_btree_balance_dirty(root);
5197 return err;
5198 }
5199
5200 /* helper for btfs_get_extent. Given an existing extent in the tree,
5201 * and an extent that you want to insert, deal with overlap and insert
5202 * the new extent into the tree.
5203 */
5204 static int merge_extent_mapping(struct extent_map_tree *em_tree,
5205 struct extent_map *existing,
5206 struct extent_map *em,
5207 u64 map_start, u64 map_len)
5208 {
5209 u64 start_diff;
5210
5211 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5212 start_diff = map_start - em->start;
5213 em->start = map_start;
5214 em->len = map_len;
5215 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5216 !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
5217 em->block_start += start_diff;
5218 em->block_len -= start_diff;
5219 }
5220 return add_extent_mapping(em_tree, em);
5221 }
5222
5223 static noinline int uncompress_inline(struct btrfs_path *path,
5224 struct inode *inode, struct page *page,
5225 size_t pg_offset, u64 extent_offset,
5226 struct btrfs_file_extent_item *item)
5227 {
5228 int ret;
5229 struct extent_buffer *leaf = path->nodes[0];
5230 char *tmp;
5231 size_t max_size;
5232 unsigned long inline_size;
5233 unsigned long ptr;
5234 int compress_type;
5235
5236 WARN_ON(pg_offset != 0);
5237 compress_type = btrfs_file_extent_compression(leaf, item);
5238 max_size = btrfs_file_extent_ram_bytes(leaf, item);
5239 inline_size = btrfs_file_extent_inline_item_len(leaf,
5240 btrfs_item_nr(leaf, path->slots[0]));
5241 tmp = kmalloc(inline_size, GFP_NOFS);
5242 if (!tmp)
5243 return -ENOMEM;
5244 ptr = btrfs_file_extent_inline_start(item);
5245
5246 read_extent_buffer(leaf, tmp, ptr, inline_size);
5247
5248 max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
5249 ret = btrfs_decompress(compress_type, tmp, page,
5250 extent_offset, inline_size, max_size);
5251 if (ret) {
5252 char *kaddr = kmap_atomic(page);
5253 unsigned long copy_size = min_t(u64,
5254 PAGE_CACHE_SIZE - pg_offset,
5255 max_size - extent_offset);
5256 memset(kaddr + pg_offset, 0, copy_size);
5257 kunmap_atomic(kaddr);
5258 }
5259 kfree(tmp);
5260 return 0;
5261 }
5262
5263 /*
5264 * a bit scary, this does extent mapping from logical file offset to the disk.
5265 * the ugly parts come from merging extents from the disk with the in-ram
5266 * representation. This gets more complex because of the data=ordered code,
5267 * where the in-ram extents might be locked pending data=ordered completion.
5268 *
5269 * This also copies inline extents directly into the page.
5270 */
5271
5272 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
5273 size_t pg_offset, u64 start, u64 len,
5274 int create)
5275 {
5276 int ret;
5277 int err = 0;
5278 u64 bytenr;
5279 u64 extent_start = 0;
5280 u64 extent_end = 0;
5281 u64 objectid = btrfs_ino(inode);
5282 u32 found_type;
5283 struct btrfs_path *path = NULL;
5284 struct btrfs_root *root = BTRFS_I(inode)->root;
5285 struct btrfs_file_extent_item *item;
5286 struct extent_buffer *leaf;
5287 struct btrfs_key found_key;
5288 struct extent_map *em = NULL;
5289 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5290 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5291 struct btrfs_trans_handle *trans = NULL;
5292 int compress_type;
5293
5294 again:
5295 read_lock(&em_tree->lock);
5296 em = lookup_extent_mapping(em_tree, start, len);
5297 if (em)
5298 em->bdev = root->fs_info->fs_devices->latest_bdev;
5299 read_unlock(&em_tree->lock);
5300
5301 if (em) {
5302 if (em->start > start || em->start + em->len <= start)
5303 free_extent_map(em);
5304 else if (em->block_start == EXTENT_MAP_INLINE && page)
5305 free_extent_map(em);
5306 else
5307 goto out;
5308 }
5309 em = alloc_extent_map();
5310 if (!em) {
5311 err = -ENOMEM;
5312 goto out;
5313 }
5314 em->bdev = root->fs_info->fs_devices->latest_bdev;
5315 em->start = EXTENT_MAP_HOLE;
5316 em->orig_start = EXTENT_MAP_HOLE;
5317 em->len = (u64)-1;
5318 em->block_len = (u64)-1;
5319
5320 if (!path) {
5321 path = btrfs_alloc_path();
5322 if (!path) {
5323 err = -ENOMEM;
5324 goto out;
5325 }
5326 /*
5327 * Chances are we'll be called again, so go ahead and do
5328 * readahead
5329 */
5330 path->reada = 1;
5331 }
5332
5333 ret = btrfs_lookup_file_extent(trans, root, path,
5334 objectid, start, trans != NULL);
5335 if (ret < 0) {
5336 err = ret;
5337 goto out;
5338 }
5339
5340 if (ret != 0) {
5341 if (path->slots[0] == 0)
5342 goto not_found;
5343 path->slots[0]--;
5344 }
5345
5346 leaf = path->nodes[0];
5347 item = btrfs_item_ptr(leaf, path->slots[0],
5348 struct btrfs_file_extent_item);
5349 /* are we inside the extent that was found? */
5350 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5351 found_type = btrfs_key_type(&found_key);
5352 if (found_key.objectid != objectid ||
5353 found_type != BTRFS_EXTENT_DATA_KEY) {
5354 goto not_found;
5355 }
5356
5357 found_type = btrfs_file_extent_type(leaf, item);
5358 extent_start = found_key.offset;
5359 compress_type = btrfs_file_extent_compression(leaf, item);
5360 if (found_type == BTRFS_FILE_EXTENT_REG ||
5361 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5362 extent_end = extent_start +
5363 btrfs_file_extent_num_bytes(leaf, item);
5364 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5365 size_t size;
5366 size = btrfs_file_extent_inline_len(leaf, item);
5367 extent_end = (extent_start + size + root->sectorsize - 1) &
5368 ~((u64)root->sectorsize - 1);
5369 }
5370
5371 if (start >= extent_end) {
5372 path->slots[0]++;
5373 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5374 ret = btrfs_next_leaf(root, path);
5375 if (ret < 0) {
5376 err = ret;
5377 goto out;
5378 }
5379 if (ret > 0)
5380 goto not_found;
5381 leaf = path->nodes[0];
5382 }
5383 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5384 if (found_key.objectid != objectid ||
5385 found_key.type != BTRFS_EXTENT_DATA_KEY)
5386 goto not_found;
5387 if (start + len <= found_key.offset)
5388 goto not_found;
5389 em->start = start;
5390 em->orig_start = start;
5391 em->len = found_key.offset - start;
5392 goto not_found_em;
5393 }
5394
5395 if (found_type == BTRFS_FILE_EXTENT_REG ||
5396 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5397 em->start = extent_start;
5398 em->len = extent_end - extent_start;
5399 em->orig_start = extent_start -
5400 btrfs_file_extent_offset(leaf, item);
5401 em->orig_block_len = btrfs_file_extent_disk_num_bytes(leaf,
5402 item);
5403 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5404 if (bytenr == 0) {
5405 em->block_start = EXTENT_MAP_HOLE;
5406 goto insert;
5407 }
5408 if (compress_type != BTRFS_COMPRESS_NONE) {
5409 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5410 em->compress_type = compress_type;
5411 em->block_start = bytenr;
5412 em->block_len = em->orig_block_len;
5413 } else {
5414 bytenr += btrfs_file_extent_offset(leaf, item);
5415 em->block_start = bytenr;
5416 em->block_len = em->len;
5417 if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5418 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
5419 }
5420 goto insert;
5421 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5422 unsigned long ptr;
5423 char *map;
5424 size_t size;
5425 size_t extent_offset;
5426 size_t copy_size;
5427
5428 em->block_start = EXTENT_MAP_INLINE;
5429 if (!page || create) {
5430 em->start = extent_start;
5431 em->len = extent_end - extent_start;
5432 goto out;
5433 }
5434
5435 size = btrfs_file_extent_inline_len(leaf, item);
5436 extent_offset = page_offset(page) + pg_offset - extent_start;
5437 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
5438 size - extent_offset);
5439 em->start = extent_start + extent_offset;
5440 em->len = (copy_size + root->sectorsize - 1) &
5441 ~((u64)root->sectorsize - 1);
5442 em->orig_block_len = em->len;
5443 em->orig_start = em->start;
5444 if (compress_type) {
5445 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5446 em->compress_type = compress_type;
5447 }
5448 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
5449 if (create == 0 && !PageUptodate(page)) {
5450 if (btrfs_file_extent_compression(leaf, item) !=
5451 BTRFS_COMPRESS_NONE) {
5452 ret = uncompress_inline(path, inode, page,
5453 pg_offset,
5454 extent_offset, item);
5455 BUG_ON(ret); /* -ENOMEM */
5456 } else {
5457 map = kmap(page);
5458 read_extent_buffer(leaf, map + pg_offset, ptr,
5459 copy_size);
5460 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5461 memset(map + pg_offset + copy_size, 0,
5462 PAGE_CACHE_SIZE - pg_offset -
5463 copy_size);
5464 }
5465 kunmap(page);
5466 }
5467 flush_dcache_page(page);
5468 } else if (create && PageUptodate(page)) {
5469 BUG();
5470 if (!trans) {
5471 kunmap(page);
5472 free_extent_map(em);
5473 em = NULL;
5474
5475 btrfs_release_path(path);
5476 trans = btrfs_join_transaction(root);
5477
5478 if (IS_ERR(trans))
5479 return ERR_CAST(trans);
5480 goto again;
5481 }
5482 map = kmap(page);
5483 write_extent_buffer(leaf, map + pg_offset, ptr,
5484 copy_size);
5485 kunmap(page);
5486 btrfs_mark_buffer_dirty(leaf);
5487 }
5488 set_extent_uptodate(io_tree, em->start,
5489 extent_map_end(em) - 1, NULL, GFP_NOFS);
5490 goto insert;
5491 } else {
5492 WARN(1, KERN_ERR "btrfs unknown found_type %d\n", found_type);
5493 }
5494 not_found:
5495 em->start = start;
5496 em->orig_start = start;
5497 em->len = len;
5498 not_found_em:
5499 em->block_start = EXTENT_MAP_HOLE;
5500 set_bit(EXTENT_FLAG_VACANCY, &em->flags);
5501 insert:
5502 btrfs_release_path(path);
5503 if (em->start > start || extent_map_end(em) <= start) {
5504 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5505 "[%llu %llu]\n", (unsigned long long)em->start,
5506 (unsigned long long)em->len,
5507 (unsigned long long)start,
5508 (unsigned long long)len);
5509 err = -EIO;
5510 goto out;
5511 }
5512
5513 err = 0;
5514 write_lock(&em_tree->lock);
5515 ret = add_extent_mapping(em_tree, em);
5516 /* it is possible that someone inserted the extent into the tree
5517 * while we had the lock dropped. It is also possible that
5518 * an overlapping map exists in the tree
5519 */
5520 if (ret == -EEXIST) {
5521 struct extent_map *existing;
5522
5523 ret = 0;
5524
5525 existing = lookup_extent_mapping(em_tree, start, len);
5526 if (existing && (existing->start > start ||
5527 existing->start + existing->len <= start)) {
5528 free_extent_map(existing);
5529 existing = NULL;
5530 }
5531 if (!existing) {
5532 existing = lookup_extent_mapping(em_tree, em->start,
5533 em->len);
5534 if (existing) {
5535 err = merge_extent_mapping(em_tree, existing,
5536 em, start,
5537 root->sectorsize);
5538 free_extent_map(existing);
5539 if (err) {
5540 free_extent_map(em);
5541 em = NULL;
5542 }
5543 } else {
5544 err = -EIO;
5545 free_extent_map(em);
5546 em = NULL;
5547 }
5548 } else {
5549 free_extent_map(em);
5550 em = existing;
5551 err = 0;
5552 }
5553 }
5554 write_unlock(&em_tree->lock);
5555 out:
5556
5557 if (em)
5558 trace_btrfs_get_extent(root, em);
5559
5560 if (path)
5561 btrfs_free_path(path);
5562 if (trans) {
5563 ret = btrfs_end_transaction(trans, root);
5564 if (!err)
5565 err = ret;
5566 }
5567 if (err) {
5568 free_extent_map(em);
5569 return ERR_PTR(err);
5570 }
5571 BUG_ON(!em); /* Error is always set */
5572 return em;
5573 }
5574
5575 struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5576 size_t pg_offset, u64 start, u64 len,
5577 int create)
5578 {
5579 struct extent_map *em;
5580 struct extent_map *hole_em = NULL;
5581 u64 range_start = start;
5582 u64 end;
5583 u64 found;
5584 u64 found_end;
5585 int err = 0;
5586
5587 em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5588 if (IS_ERR(em))
5589 return em;
5590 if (em) {
5591 /*
5592 * if our em maps to a hole, there might
5593 * actually be delalloc bytes behind it
5594 */
5595 if (em->block_start != EXTENT_MAP_HOLE)
5596 return em;
5597 else
5598 hole_em = em;
5599 }
5600
5601 /* check to see if we've wrapped (len == -1 or similar) */
5602 end = start + len;
5603 if (end < start)
5604 end = (u64)-1;
5605 else
5606 end -= 1;
5607
5608 em = NULL;
5609
5610 /* ok, we didn't find anything, lets look for delalloc */
5611 found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5612 end, len, EXTENT_DELALLOC, 1);
5613 found_end = range_start + found;
5614 if (found_end < range_start)
5615 found_end = (u64)-1;
5616
5617 /*
5618 * we didn't find anything useful, return
5619 * the original results from get_extent()
5620 */
5621 if (range_start > end || found_end <= start) {
5622 em = hole_em;
5623 hole_em = NULL;
5624 goto out;
5625 }
5626
5627 /* adjust the range_start to make sure it doesn't
5628 * go backwards from the start they passed in
5629 */
5630 range_start = max(start,range_start);
5631 found = found_end - range_start;
5632
5633 if (found > 0) {
5634 u64 hole_start = start;
5635 u64 hole_len = len;
5636
5637 em = alloc_extent_map();
5638 if (!em) {
5639 err = -ENOMEM;
5640 goto out;
5641 }
5642 /*
5643 * when btrfs_get_extent can't find anything it
5644 * returns one huge hole
5645 *
5646 * make sure what it found really fits our range, and
5647 * adjust to make sure it is based on the start from
5648 * the caller
5649 */
5650 if (hole_em) {
5651 u64 calc_end = extent_map_end(hole_em);
5652
5653 if (calc_end <= start || (hole_em->start > end)) {
5654 free_extent_map(hole_em);
5655 hole_em = NULL;
5656 } else {
5657 hole_start = max(hole_em->start, start);
5658 hole_len = calc_end - hole_start;
5659 }
5660 }
5661 em->bdev = NULL;
5662 if (hole_em && range_start > hole_start) {
5663 /* our hole starts before our delalloc, so we
5664 * have to return just the parts of the hole
5665 * that go until the delalloc starts
5666 */
5667 em->len = min(hole_len,
5668 range_start - hole_start);
5669 em->start = hole_start;
5670 em->orig_start = hole_start;
5671 /*
5672 * don't adjust block start at all,
5673 * it is fixed at EXTENT_MAP_HOLE
5674 */
5675 em->block_start = hole_em->block_start;
5676 em->block_len = hole_len;
5677 } else {
5678 em->start = range_start;
5679 em->len = found;
5680 em->orig_start = range_start;
5681 em->block_start = EXTENT_MAP_DELALLOC;
5682 em->block_len = found;
5683 }
5684 } else if (hole_em) {
5685 return hole_em;
5686 }
5687 out:
5688
5689 free_extent_map(hole_em);
5690 if (err) {
5691 free_extent_map(em);
5692 return ERR_PTR(err);
5693 }
5694 return em;
5695 }
5696
5697 static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5698 u64 start, u64 len)
5699 {
5700 struct btrfs_root *root = BTRFS_I(inode)->root;
5701 struct btrfs_trans_handle *trans;
5702 struct extent_map *em;
5703 struct btrfs_key ins;
5704 u64 alloc_hint;
5705 int ret;
5706
5707 trans = btrfs_join_transaction(root);
5708 if (IS_ERR(trans))
5709 return ERR_CAST(trans);
5710
5711 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5712
5713 alloc_hint = get_extent_allocation_hint(inode, start, len);
5714 ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5715 alloc_hint, &ins, 1);
5716 if (ret) {
5717 em = ERR_PTR(ret);
5718 goto out;
5719 }
5720
5721 em = create_pinned_em(inode, start, ins.offset, start, ins.objectid,
5722 ins.offset, ins.offset, 0);
5723 if (IS_ERR(em))
5724 goto out;
5725
5726 ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5727 ins.offset, ins.offset, 0);
5728 if (ret) {
5729 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5730 em = ERR_PTR(ret);
5731 }
5732 out:
5733 btrfs_end_transaction(trans, root);
5734 return em;
5735 }
5736
5737 /*
5738 * returns 1 when the nocow is safe, < 1 on error, 0 if the
5739 * block must be cow'd
5740 */
5741 static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5742 struct inode *inode, u64 offset, u64 len)
5743 {
5744 struct btrfs_path *path;
5745 int ret;
5746 struct extent_buffer *leaf;
5747 struct btrfs_root *root = BTRFS_I(inode)->root;
5748 struct btrfs_file_extent_item *fi;
5749 struct btrfs_key key;
5750 u64 disk_bytenr;
5751 u64 backref_offset;
5752 u64 extent_end;
5753 u64 num_bytes;
5754 int slot;
5755 int found_type;
5756
5757 path = btrfs_alloc_path();
5758 if (!path)
5759 return -ENOMEM;
5760
5761 ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
5762 offset, 0);
5763 if (ret < 0)
5764 goto out;
5765
5766 slot = path->slots[0];
5767 if (ret == 1) {
5768 if (slot == 0) {
5769 /* can't find the item, must cow */
5770 ret = 0;
5771 goto out;
5772 }
5773 slot--;
5774 }
5775 ret = 0;
5776 leaf = path->nodes[0];
5777 btrfs_item_key_to_cpu(leaf, &key, slot);
5778 if (key.objectid != btrfs_ino(inode) ||
5779 key.type != BTRFS_EXTENT_DATA_KEY) {
5780 /* not our file or wrong item type, must cow */
5781 goto out;
5782 }
5783
5784 if (key.offset > offset) {
5785 /* Wrong offset, must cow */
5786 goto out;
5787 }
5788
5789 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5790 found_type = btrfs_file_extent_type(leaf, fi);
5791 if (found_type != BTRFS_FILE_EXTENT_REG &&
5792 found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5793 /* not a regular extent, must cow */
5794 goto out;
5795 }
5796 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5797 backref_offset = btrfs_file_extent_offset(leaf, fi);
5798
5799 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5800 if (extent_end < offset + len) {
5801 /* extent doesn't include our full range, must cow */
5802 goto out;
5803 }
5804
5805 if (btrfs_extent_readonly(root, disk_bytenr))
5806 goto out;
5807
5808 /*
5809 * look for other files referencing this extent, if we
5810 * find any we must cow
5811 */
5812 if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
5813 key.offset - backref_offset, disk_bytenr))
5814 goto out;
5815
5816 /*
5817 * adjust disk_bytenr and num_bytes to cover just the bytes
5818 * in this extent we are about to write. If there
5819 * are any csums in that range we have to cow in order
5820 * to keep the csums correct
5821 */
5822 disk_bytenr += backref_offset;
5823 disk_bytenr += offset - key.offset;
5824 num_bytes = min(offset + len, extent_end) - offset;
5825 if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5826 goto out;
5827 /*
5828 * all of the above have passed, it is safe to overwrite this extent
5829 * without cow
5830 */
5831 ret = 1;
5832 out:
5833 btrfs_free_path(path);
5834 return ret;
5835 }
5836
5837 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
5838 struct extent_state **cached_state, int writing)
5839 {
5840 struct btrfs_ordered_extent *ordered;
5841 int ret = 0;
5842
5843 while (1) {
5844 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5845 0, cached_state);
5846 /*
5847 * We're concerned with the entire range that we're going to be
5848 * doing DIO to, so we need to make sure theres no ordered
5849 * extents in this range.
5850 */
5851 ordered = btrfs_lookup_ordered_range(inode, lockstart,
5852 lockend - lockstart + 1);
5853
5854 /*
5855 * We need to make sure there are no buffered pages in this
5856 * range either, we could have raced between the invalidate in
5857 * generic_file_direct_write and locking the extent. The
5858 * invalidate needs to happen so that reads after a write do not
5859 * get stale data.
5860 */
5861 if (!ordered && (!writing ||
5862 !test_range_bit(&BTRFS_I(inode)->io_tree,
5863 lockstart, lockend, EXTENT_UPTODATE, 0,
5864 *cached_state)))
5865 break;
5866
5867 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
5868 cached_state, GFP_NOFS);
5869
5870 if (ordered) {
5871 btrfs_start_ordered_extent(inode, ordered, 1);
5872 btrfs_put_ordered_extent(ordered);
5873 } else {
5874 /* Screw you mmap */
5875 ret = filemap_write_and_wait_range(inode->i_mapping,
5876 lockstart,
5877 lockend);
5878 if (ret)
5879 break;
5880
5881 /*
5882 * If we found a page that couldn't be invalidated just
5883 * fall back to buffered.
5884 */
5885 ret = invalidate_inode_pages2_range(inode->i_mapping,
5886 lockstart >> PAGE_CACHE_SHIFT,
5887 lockend >> PAGE_CACHE_SHIFT);
5888 if (ret)
5889 break;
5890 }
5891
5892 cond_resched();
5893 }
5894
5895 return ret;
5896 }
5897
5898 static struct extent_map *create_pinned_em(struct inode *inode, u64 start,
5899 u64 len, u64 orig_start,
5900 u64 block_start, u64 block_len,
5901 u64 orig_block_len, int type)
5902 {
5903 struct extent_map_tree *em_tree;
5904 struct extent_map *em;
5905 struct btrfs_root *root = BTRFS_I(inode)->root;
5906 int ret;
5907
5908 em_tree = &BTRFS_I(inode)->extent_tree;
5909 em = alloc_extent_map();
5910 if (!em)
5911 return ERR_PTR(-ENOMEM);
5912
5913 em->start = start;
5914 em->orig_start = orig_start;
5915 em->len = len;
5916 em->block_len = block_len;
5917 em->block_start = block_start;
5918 em->bdev = root->fs_info->fs_devices->latest_bdev;
5919 em->orig_block_len = orig_block_len;
5920 em->generation = -1;
5921 set_bit(EXTENT_FLAG_PINNED, &em->flags);
5922 if (type == BTRFS_ORDERED_PREALLOC)
5923 set_bit(EXTENT_FLAG_FILLING, &em->flags);
5924
5925 do {
5926 btrfs_drop_extent_cache(inode, em->start,
5927 em->start + em->len - 1, 0);
5928 write_lock(&em_tree->lock);
5929 ret = add_extent_mapping(em_tree, em);
5930 if (!ret)
5931 list_move(&em->list,
5932 &em_tree->modified_extents);
5933 write_unlock(&em_tree->lock);
5934 } while (ret == -EEXIST);
5935
5936 if (ret) {
5937 free_extent_map(em);
5938 return ERR_PTR(ret);
5939 }
5940
5941 return em;
5942 }
5943
5944
5945 static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5946 struct buffer_head *bh_result, int create)
5947 {
5948 struct extent_map *em;
5949 struct btrfs_root *root = BTRFS_I(inode)->root;
5950 struct extent_state *cached_state = NULL;
5951 u64 start = iblock << inode->i_blkbits;
5952 u64 lockstart, lockend;
5953 u64 len = bh_result->b_size;
5954 struct btrfs_trans_handle *trans;
5955 int unlock_bits = EXTENT_LOCKED;
5956 int ret;
5957
5958 if (create) {
5959 ret = btrfs_delalloc_reserve_space(inode, len);
5960 if (ret)
5961 return ret;
5962 unlock_bits |= EXTENT_DELALLOC | EXTENT_DIRTY;
5963 } else {
5964 len = min_t(u64, len, root->sectorsize);
5965 }
5966
5967 lockstart = start;
5968 lockend = start + len - 1;
5969
5970 /*
5971 * If this errors out it's because we couldn't invalidate pagecache for
5972 * this range and we need to fallback to buffered.
5973 */
5974 if (lock_extent_direct(inode, lockstart, lockend, &cached_state, create))
5975 return -ENOTBLK;
5976
5977 if (create) {
5978 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
5979 lockend, EXTENT_DELALLOC, NULL,
5980 &cached_state, GFP_NOFS);
5981 if (ret)
5982 goto unlock_err;
5983 }
5984
5985 em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5986 if (IS_ERR(em)) {
5987 ret = PTR_ERR(em);
5988 goto unlock_err;
5989 }
5990
5991 /*
5992 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5993 * io. INLINE is special, and we could probably kludge it in here, but
5994 * it's still buffered so for safety lets just fall back to the generic
5995 * buffered path.
5996 *
5997 * For COMPRESSED we _have_ to read the entire extent in so we can
5998 * decompress it, so there will be buffering required no matter what we
5999 * do, so go ahead and fallback to buffered.
6000 *
6001 * We return -ENOTBLK because thats what makes DIO go ahead and go back
6002 * to buffered IO. Don't blame me, this is the price we pay for using
6003 * the generic code.
6004 */
6005 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
6006 em->block_start == EXTENT_MAP_INLINE) {
6007 free_extent_map(em);
6008 ret = -ENOTBLK;
6009 goto unlock_err;
6010 }
6011
6012 /* Just a good old fashioned hole, return */
6013 if (!create && (em->block_start == EXTENT_MAP_HOLE ||
6014 test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
6015 free_extent_map(em);
6016 ret = 0;
6017 goto unlock_err;
6018 }
6019
6020 /*
6021 * We don't allocate a new extent in the following cases
6022 *
6023 * 1) The inode is marked as NODATACOW. In this case we'll just use the
6024 * existing extent.
6025 * 2) The extent is marked as PREALLOC. We're good to go here and can
6026 * just use the extent.
6027 *
6028 */
6029 if (!create) {
6030 len = min(len, em->len - (start - em->start));
6031 lockstart = start + len;
6032 goto unlock;
6033 }
6034
6035 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
6036 ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
6037 em->block_start != EXTENT_MAP_HOLE)) {
6038 int type;
6039 int ret;
6040 u64 block_start;
6041
6042 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6043 type = BTRFS_ORDERED_PREALLOC;
6044 else
6045 type = BTRFS_ORDERED_NOCOW;
6046 len = min(len, em->len - (start - em->start));
6047 block_start = em->block_start + (start - em->start);
6048
6049 /*
6050 * we're not going to log anything, but we do need
6051 * to make sure the current transaction stays open
6052 * while we look for nocow cross refs
6053 */
6054 trans = btrfs_join_transaction(root);
6055 if (IS_ERR(trans))
6056 goto must_cow;
6057
6058 if (can_nocow_odirect(trans, inode, start, len) == 1) {
6059 u64 orig_start = em->orig_start;
6060 u64 orig_block_len = em->orig_block_len;
6061
6062 if (type == BTRFS_ORDERED_PREALLOC) {
6063 free_extent_map(em);
6064 em = create_pinned_em(inode, start, len,
6065 orig_start,
6066 block_start, len,
6067 orig_block_len, type);
6068 if (IS_ERR(em)) {
6069 btrfs_end_transaction(trans, root);
6070 goto unlock_err;
6071 }
6072 }
6073
6074 ret = btrfs_add_ordered_extent_dio(inode, start,
6075 block_start, len, len, type);
6076 btrfs_end_transaction(trans, root);
6077 if (ret) {
6078 free_extent_map(em);
6079 goto unlock_err;
6080 }
6081 goto unlock;
6082 }
6083 btrfs_end_transaction(trans, root);
6084 }
6085 must_cow:
6086 /*
6087 * this will cow the extent, reset the len in case we changed
6088 * it above
6089 */
6090 len = bh_result->b_size;
6091 free_extent_map(em);
6092 em = btrfs_new_extent_direct(inode, start, len);
6093 if (IS_ERR(em)) {
6094 ret = PTR_ERR(em);
6095 goto unlock_err;
6096 }
6097 len = min(len, em->len - (start - em->start));
6098 unlock:
6099 bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
6100 inode->i_blkbits;
6101 bh_result->b_size = len;
6102 bh_result->b_bdev = em->bdev;
6103 set_buffer_mapped(bh_result);
6104 if (create) {
6105 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
6106 set_buffer_new(bh_result);
6107
6108 /*
6109 * Need to update the i_size under the extent lock so buffered
6110 * readers will get the updated i_size when we unlock.
6111 */
6112 if (start + len > i_size_read(inode))
6113 i_size_write(inode, start + len);
6114 }
6115
6116 /*
6117 * In the case of write we need to clear and unlock the entire range,
6118 * in the case of read we need to unlock only the end area that we
6119 * aren't using if there is any left over space.
6120 */
6121 if (lockstart < lockend) {
6122 if (create && len < lockend - lockstart) {
6123 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6124 lockstart + len - 1,
6125 unlock_bits | EXTENT_DEFRAG, 1, 0,
6126 &cached_state, GFP_NOFS);
6127 /*
6128 * Beside unlock, we also need to cleanup reserved space
6129 * for the left range by attaching EXTENT_DO_ACCOUNTING.
6130 */
6131 clear_extent_bit(&BTRFS_I(inode)->io_tree,
6132 lockstart + len, lockend,
6133 unlock_bits | EXTENT_DO_ACCOUNTING |
6134 EXTENT_DEFRAG, 1, 0, NULL, GFP_NOFS);
6135 } else {
6136 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6137 lockend, unlock_bits, 1, 0,
6138 &cached_state, GFP_NOFS);
6139 }
6140 } else {
6141 free_extent_state(cached_state);
6142 }
6143
6144 free_extent_map(em);
6145
6146 return 0;
6147
6148 unlock_err:
6149 if (create)
6150 unlock_bits |= EXTENT_DO_ACCOUNTING;
6151
6152 clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6153 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
6154 return ret;
6155 }
6156
6157 struct btrfs_dio_private {
6158 struct inode *inode;
6159 u64 logical_offset;
6160 u64 disk_bytenr;
6161 u64 bytes;
6162 void *private;
6163
6164 /* number of bios pending for this dio */
6165 atomic_t pending_bios;
6166
6167 /* IO errors */
6168 int errors;
6169
6170 struct bio *orig_bio;
6171 };
6172
6173 static void btrfs_endio_direct_read(struct bio *bio, int err)
6174 {
6175 struct btrfs_dio_private *dip = bio->bi_private;
6176 struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
6177 struct bio_vec *bvec = bio->bi_io_vec;
6178 struct inode *inode = dip->inode;
6179 struct btrfs_root *root = BTRFS_I(inode)->root;
6180 u64 start;
6181
6182 start = dip->logical_offset;
6183 do {
6184 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
6185 struct page *page = bvec->bv_page;
6186 char *kaddr;
6187 u32 csum = ~(u32)0;
6188 u64 private = ~(u32)0;
6189 unsigned long flags;
6190
6191 if (get_state_private(&BTRFS_I(inode)->io_tree,
6192 start, &private))
6193 goto failed;
6194 local_irq_save(flags);
6195 kaddr = kmap_atomic(page);
6196 csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
6197 csum, bvec->bv_len);
6198 btrfs_csum_final(csum, (char *)&csum);
6199 kunmap_atomic(kaddr);
6200 local_irq_restore(flags);
6201
6202 flush_dcache_page(bvec->bv_page);
6203 if (csum != private) {
6204 failed:
6205 printk(KERN_ERR "btrfs csum failed ino %llu off"
6206 " %llu csum %u private %u\n",
6207 (unsigned long long)btrfs_ino(inode),
6208 (unsigned long long)start,
6209 csum, (unsigned)private);
6210 err = -EIO;
6211 }
6212 }
6213
6214 start += bvec->bv_len;
6215 bvec++;
6216 } while (bvec <= bvec_end);
6217
6218 unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
6219 dip->logical_offset + dip->bytes - 1);
6220 bio->bi_private = dip->private;
6221
6222 kfree(dip);
6223
6224 /* If we had a csum failure make sure to clear the uptodate flag */
6225 if (err)
6226 clear_bit(BIO_UPTODATE, &bio->bi_flags);
6227 dio_end_io(bio, err);
6228 }
6229
6230 static void btrfs_endio_direct_write(struct bio *bio, int err)
6231 {
6232 struct btrfs_dio_private *dip = bio->bi_private;
6233 struct inode *inode = dip->inode;
6234 struct btrfs_root *root = BTRFS_I(inode)->root;
6235 struct btrfs_ordered_extent *ordered = NULL;
6236 u64 ordered_offset = dip->logical_offset;
6237 u64 ordered_bytes = dip->bytes;
6238 int ret;
6239
6240 if (err)
6241 goto out_done;
6242 again:
6243 ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
6244 &ordered_offset,
6245 ordered_bytes, !err);
6246 if (!ret)
6247 goto out_test;
6248
6249 ordered->work.func = finish_ordered_fn;
6250 ordered->work.flags = 0;
6251 btrfs_queue_worker(&root->fs_info->endio_write_workers,
6252 &ordered->work);
6253 out_test:
6254 /*
6255 * our bio might span multiple ordered extents. If we haven't
6256 * completed the accounting for the whole dio, go back and try again
6257 */
6258 if (ordered_offset < dip->logical_offset + dip->bytes) {
6259 ordered_bytes = dip->logical_offset + dip->bytes -
6260 ordered_offset;
6261 ordered = NULL;
6262 goto again;
6263 }
6264 out_done:
6265 bio->bi_private = dip->private;
6266
6267 kfree(dip);
6268
6269 /* If we had an error make sure to clear the uptodate flag */
6270 if (err)
6271 clear_bit(BIO_UPTODATE, &bio->bi_flags);
6272 dio_end_io(bio, err);
6273 }
6274
6275 static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6276 struct bio *bio, int mirror_num,
6277 unsigned long bio_flags, u64 offset)
6278 {
6279 int ret;
6280 struct btrfs_root *root = BTRFS_I(inode)->root;
6281 ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
6282 BUG_ON(ret); /* -ENOMEM */
6283 return 0;
6284 }
6285
6286 static void btrfs_end_dio_bio(struct bio *bio, int err)
6287 {
6288 struct btrfs_dio_private *dip = bio->bi_private;
6289
6290 if (err) {
6291 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
6292 "sector %#Lx len %u err no %d\n",
6293 (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
6294 (unsigned long long)bio->bi_sector, bio->bi_size, err);
6295 dip->errors = 1;
6296
6297 /*
6298 * before atomic variable goto zero, we must make sure
6299 * dip->errors is perceived to be set.
6300 */
6301 smp_mb__before_atomic_dec();
6302 }
6303
6304 /* if there are more bios still pending for this dio, just exit */
6305 if (!atomic_dec_and_test(&dip->pending_bios))
6306 goto out;
6307
6308 if (dip->errors)
6309 bio_io_error(dip->orig_bio);
6310 else {
6311 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6312 bio_endio(dip->orig_bio, 0);
6313 }
6314 out:
6315 bio_put(bio);
6316 }
6317
6318 static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6319 u64 first_sector, gfp_t gfp_flags)
6320 {
6321 int nr_vecs = bio_get_nr_vecs(bdev);
6322 return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6323 }
6324
6325 static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6326 int rw, u64 file_offset, int skip_sum,
6327 int async_submit)
6328 {
6329 int write = rw & REQ_WRITE;
6330 struct btrfs_root *root = BTRFS_I(inode)->root;
6331 int ret;
6332
6333 if (async_submit)
6334 async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
6335
6336 bio_get(bio);
6337
6338 if (!write) {
6339 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6340 if (ret)
6341 goto err;
6342 }
6343
6344 if (skip_sum)
6345 goto map;
6346
6347 if (write && async_submit) {
6348 ret = btrfs_wq_submit_bio(root->fs_info,
6349 inode, rw, bio, 0, 0,
6350 file_offset,
6351 __btrfs_submit_bio_start_direct_io,
6352 __btrfs_submit_bio_done);
6353 goto err;
6354 } else if (write) {
6355 /*
6356 * If we aren't doing async submit, calculate the csum of the
6357 * bio now.
6358 */
6359 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6360 if (ret)
6361 goto err;
6362 } else if (!skip_sum) {
6363 ret = btrfs_lookup_bio_sums_dio(root, inode, bio, file_offset);
6364 if (ret)
6365 goto err;
6366 }
6367
6368 map:
6369 ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
6370 err:
6371 bio_put(bio);
6372 return ret;
6373 }
6374
6375 static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6376 int skip_sum)
6377 {
6378 struct inode *inode = dip->inode;
6379 struct btrfs_root *root = BTRFS_I(inode)->root;
6380 struct bio *bio;
6381 struct bio *orig_bio = dip->orig_bio;
6382 struct bio_vec *bvec = orig_bio->bi_io_vec;
6383 u64 start_sector = orig_bio->bi_sector;
6384 u64 file_offset = dip->logical_offset;
6385 u64 submit_len = 0;
6386 u64 map_length;
6387 int nr_pages = 0;
6388 int ret = 0;
6389 int async_submit = 0;
6390
6391 map_length = orig_bio->bi_size;
6392 ret = btrfs_map_block(root->fs_info, READ, start_sector << 9,
6393 &map_length, NULL, 0);
6394 if (ret) {
6395 bio_put(orig_bio);
6396 return -EIO;
6397 }
6398
6399 if (map_length >= orig_bio->bi_size) {
6400 bio = orig_bio;
6401 goto submit;
6402 }
6403
6404 async_submit = 1;
6405 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6406 if (!bio)
6407 return -ENOMEM;
6408 bio->bi_private = dip;
6409 bio->bi_end_io = btrfs_end_dio_bio;
6410 atomic_inc(&dip->pending_bios);
6411
6412 while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6413 if (unlikely(map_length < submit_len + bvec->bv_len ||
6414 bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6415 bvec->bv_offset) < bvec->bv_len)) {
6416 /*
6417 * inc the count before we submit the bio so
6418 * we know the end IO handler won't happen before
6419 * we inc the count. Otherwise, the dip might get freed
6420 * before we're done setting it up
6421 */
6422 atomic_inc(&dip->pending_bios);
6423 ret = __btrfs_submit_dio_bio(bio, inode, rw,
6424 file_offset, skip_sum,
6425 async_submit);
6426 if (ret) {
6427 bio_put(bio);
6428 atomic_dec(&dip->pending_bios);
6429 goto out_err;
6430 }
6431
6432 start_sector += submit_len >> 9;
6433 file_offset += submit_len;
6434
6435 submit_len = 0;
6436 nr_pages = 0;
6437
6438 bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6439 start_sector, GFP_NOFS);
6440 if (!bio)
6441 goto out_err;
6442 bio->bi_private = dip;
6443 bio->bi_end_io = btrfs_end_dio_bio;
6444
6445 map_length = orig_bio->bi_size;
6446 ret = btrfs_map_block(root->fs_info, READ,
6447 start_sector << 9,
6448 &map_length, NULL, 0);
6449 if (ret) {
6450 bio_put(bio);
6451 goto out_err;
6452 }
6453 } else {
6454 submit_len += bvec->bv_len;
6455 nr_pages ++;
6456 bvec++;
6457 }
6458 }
6459
6460 submit:
6461 ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
6462 async_submit);
6463 if (!ret)
6464 return 0;
6465
6466 bio_put(bio);
6467 out_err:
6468 dip->errors = 1;
6469 /*
6470 * before atomic variable goto zero, we must
6471 * make sure dip->errors is perceived to be set.
6472 */
6473 smp_mb__before_atomic_dec();
6474 if (atomic_dec_and_test(&dip->pending_bios))
6475 bio_io_error(dip->orig_bio);
6476
6477 /* bio_end_io() will handle error, so we needn't return it */
6478 return 0;
6479 }
6480
6481 static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6482 loff_t file_offset)
6483 {
6484 struct btrfs_root *root = BTRFS_I(inode)->root;
6485 struct btrfs_dio_private *dip;
6486 struct bio_vec *bvec = bio->bi_io_vec;
6487 int skip_sum;
6488 int write = rw & REQ_WRITE;
6489 int ret = 0;
6490
6491 skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6492
6493 dip = kmalloc(sizeof(*dip), GFP_NOFS);
6494 if (!dip) {
6495 ret = -ENOMEM;
6496 goto free_ordered;
6497 }
6498
6499 dip->private = bio->bi_private;
6500 dip->inode = inode;
6501 dip->logical_offset = file_offset;
6502
6503 dip->bytes = 0;
6504 do {
6505 dip->bytes += bvec->bv_len;
6506 bvec++;
6507 } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6508
6509 dip->disk_bytenr = (u64)bio->bi_sector << 9;
6510 bio->bi_private = dip;
6511 dip->errors = 0;
6512 dip->orig_bio = bio;
6513 atomic_set(&dip->pending_bios, 0);
6514
6515 if (write)
6516 bio->bi_end_io = btrfs_endio_direct_write;
6517 else
6518 bio->bi_end_io = btrfs_endio_direct_read;
6519
6520 ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6521 if (!ret)
6522 return;
6523 free_ordered:
6524 /*
6525 * If this is a write, we need to clean up the reserved space and kill
6526 * the ordered extent.
6527 */
6528 if (write) {
6529 struct btrfs_ordered_extent *ordered;
6530 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
6531 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6532 !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6533 btrfs_free_reserved_extent(root, ordered->start,
6534 ordered->disk_len);
6535 btrfs_put_ordered_extent(ordered);
6536 btrfs_put_ordered_extent(ordered);
6537 }
6538 bio_endio(bio, ret);
6539 }
6540
6541 static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6542 const struct iovec *iov, loff_t offset,
6543 unsigned long nr_segs)
6544 {
6545 int seg;
6546 int i;
6547 size_t size;
6548 unsigned long addr;
6549 unsigned blocksize_mask = root->sectorsize - 1;
6550 ssize_t retval = -EINVAL;
6551 loff_t end = offset;
6552
6553 if (offset & blocksize_mask)
6554 goto out;
6555
6556 /* Check the memory alignment. Blocks cannot straddle pages */
6557 for (seg = 0; seg < nr_segs; seg++) {
6558 addr = (unsigned long)iov[seg].iov_base;
6559 size = iov[seg].iov_len;
6560 end += size;
6561 if ((addr & blocksize_mask) || (size & blocksize_mask))
6562 goto out;
6563
6564 /* If this is a write we don't need to check anymore */
6565 if (rw & WRITE)
6566 continue;
6567
6568 /*
6569 * Check to make sure we don't have duplicate iov_base's in this
6570 * iovec, if so return EINVAL, otherwise we'll get csum errors
6571 * when reading back.
6572 */
6573 for (i = seg + 1; i < nr_segs; i++) {
6574 if (iov[seg].iov_base == iov[i].iov_base)
6575 goto out;
6576 }
6577 }
6578 retval = 0;
6579 out:
6580 return retval;
6581 }
6582
6583 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6584 const struct iovec *iov, loff_t offset,
6585 unsigned long nr_segs)
6586 {
6587 struct file *file = iocb->ki_filp;
6588 struct inode *inode = file->f_mapping->host;
6589
6590 if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6591 offset, nr_segs))
6592 return 0;
6593
6594 return __blockdev_direct_IO(rw, iocb, inode,
6595 BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6596 iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6597 btrfs_submit_direct, 0);
6598 }
6599
6600 #define BTRFS_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC)
6601
6602 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6603 __u64 start, __u64 len)
6604 {
6605 int ret;
6606
6607 ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
6608 if (ret)
6609 return ret;
6610
6611 return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
6612 }
6613
6614 int btrfs_readpage(struct file *file, struct page *page)
6615 {
6616 struct extent_io_tree *tree;
6617 tree = &BTRFS_I(page->mapping->host)->io_tree;
6618 return extent_read_full_page(tree, page, btrfs_get_extent, 0);
6619 }
6620
6621 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6622 {
6623 struct extent_io_tree *tree;
6624
6625
6626 if (current->flags & PF_MEMALLOC) {
6627 redirty_page_for_writepage(wbc, page);
6628 unlock_page(page);
6629 return 0;
6630 }
6631 tree = &BTRFS_I(page->mapping->host)->io_tree;
6632 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6633 }
6634
6635 int btrfs_writepages(struct address_space *mapping,
6636 struct writeback_control *wbc)
6637 {
6638 struct extent_io_tree *tree;
6639
6640 tree = &BTRFS_I(mapping->host)->io_tree;
6641 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6642 }
6643
6644 static int
6645 btrfs_readpages(struct file *file, struct address_space *mapping,
6646 struct list_head *pages, unsigned nr_pages)
6647 {
6648 struct extent_io_tree *tree;
6649 tree = &BTRFS_I(mapping->host)->io_tree;
6650 return extent_readpages(tree, mapping, pages, nr_pages,
6651 btrfs_get_extent);
6652 }
6653 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6654 {
6655 struct extent_io_tree *tree;
6656 struct extent_map_tree *map;
6657 int ret;
6658
6659 tree = &BTRFS_I(page->mapping->host)->io_tree;
6660 map = &BTRFS_I(page->mapping->host)->extent_tree;
6661 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
6662 if (ret == 1) {
6663 ClearPagePrivate(page);
6664 set_page_private(page, 0);
6665 page_cache_release(page);
6666 }
6667 return ret;
6668 }
6669
6670 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6671 {
6672 if (PageWriteback(page) || PageDirty(page))
6673 return 0;
6674 return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
6675 }
6676
6677 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6678 {
6679 struct inode *inode = page->mapping->host;
6680 struct extent_io_tree *tree;
6681 struct btrfs_ordered_extent *ordered;
6682 struct extent_state *cached_state = NULL;
6683 u64 page_start = page_offset(page);
6684 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
6685
6686 /*
6687 * we have the page locked, so new writeback can't start,
6688 * and the dirty bit won't be cleared while we are here.
6689 *
6690 * Wait for IO on this page so that we can safely clear
6691 * the PagePrivate2 bit and do ordered accounting
6692 */
6693 wait_on_page_writeback(page);
6694
6695 tree = &BTRFS_I(inode)->io_tree;
6696 if (offset) {
6697 btrfs_releasepage(page, GFP_NOFS);
6698 return;
6699 }
6700 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
6701 ordered = btrfs_lookup_ordered_extent(inode,
6702 page_offset(page));
6703 if (ordered) {
6704 /*
6705 * IO on this page will never be started, so we need
6706 * to account for any ordered extents now
6707 */
6708 clear_extent_bit(tree, page_start, page_end,
6709 EXTENT_DIRTY | EXTENT_DELALLOC |
6710 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
6711 EXTENT_DEFRAG, 1, 0, &cached_state, GFP_NOFS);
6712 /*
6713 * whoever cleared the private bit is responsible
6714 * for the finish_ordered_io
6715 */
6716 if (TestClearPagePrivate2(page) &&
6717 btrfs_dec_test_ordered_pending(inode, &ordered, page_start,
6718 PAGE_CACHE_SIZE, 1)) {
6719 btrfs_finish_ordered_io(ordered);
6720 }
6721 btrfs_put_ordered_extent(ordered);
6722 cached_state = NULL;
6723 lock_extent_bits(tree, page_start, page_end, 0, &cached_state);
6724 }
6725 clear_extent_bit(tree, page_start, page_end,
6726 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
6727 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG, 1, 1,
6728 &cached_state, GFP_NOFS);
6729 __btrfs_releasepage(page, GFP_NOFS);
6730
6731 ClearPageChecked(page);
6732 if (PagePrivate(page)) {
6733 ClearPagePrivate(page);
6734 set_page_private(page, 0);
6735 page_cache_release(page);
6736 }
6737 }
6738
6739 /*
6740 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6741 * called from a page fault handler when a page is first dirtied. Hence we must
6742 * be careful to check for EOF conditions here. We set the page up correctly
6743 * for a written page which means we get ENOSPC checking when writing into
6744 * holes and correct delalloc and unwritten extent mapping on filesystems that
6745 * support these features.
6746 *
6747 * We are not allowed to take the i_mutex here so we have to play games to
6748 * protect against truncate races as the page could now be beyond EOF. Because
6749 * vmtruncate() writes the inode size before removing pages, once we have the
6750 * page lock we can determine safely if the page is beyond EOF. If it is not
6751 * beyond EOF, then the page is guaranteed safe against truncation until we
6752 * unlock the page.
6753 */
6754 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
6755 {
6756 struct page *page = vmf->page;
6757 struct inode *inode = fdentry(vma->vm_file)->d_inode;
6758 struct btrfs_root *root = BTRFS_I(inode)->root;
6759 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6760 struct btrfs_ordered_extent *ordered;
6761 struct extent_state *cached_state = NULL;
6762 char *kaddr;
6763 unsigned long zero_start;
6764 loff_t size;
6765 int ret;
6766 int reserved = 0;
6767 u64 page_start;
6768 u64 page_end;
6769
6770 sb_start_pagefault(inode->i_sb);
6771 ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
6772 if (!ret) {
6773 ret = file_update_time(vma->vm_file);
6774 reserved = 1;
6775 }
6776 if (ret) {
6777 if (ret == -ENOMEM)
6778 ret = VM_FAULT_OOM;
6779 else /* -ENOSPC, -EIO, etc */
6780 ret = VM_FAULT_SIGBUS;
6781 if (reserved)
6782 goto out;
6783 goto out_noreserve;
6784 }
6785
6786 ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
6787 again:
6788 lock_page(page);
6789 size = i_size_read(inode);
6790 page_start = page_offset(page);
6791 page_end = page_start + PAGE_CACHE_SIZE - 1;
6792
6793 if ((page->mapping != inode->i_mapping) ||
6794 (page_start >= size)) {
6795 /* page got truncated out from underneath us */
6796 goto out_unlock;
6797 }
6798 wait_on_page_writeback(page);
6799
6800 lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state);
6801 set_page_extent_mapped(page);
6802
6803 /*
6804 * we can't set the delalloc bits if there are pending ordered
6805 * extents. Drop our locks and wait for them to finish
6806 */
6807 ordered = btrfs_lookup_ordered_extent(inode, page_start);
6808 if (ordered) {
6809 unlock_extent_cached(io_tree, page_start, page_end,
6810 &cached_state, GFP_NOFS);
6811 unlock_page(page);
6812 btrfs_start_ordered_extent(inode, ordered, 1);
6813 btrfs_put_ordered_extent(ordered);
6814 goto again;
6815 }
6816
6817 /*
6818 * XXX - page_mkwrite gets called every time the page is dirtied, even
6819 * if it was already dirty, so for space accounting reasons we need to
6820 * clear any delalloc bits for the range we are fixing to save. There
6821 * is probably a better way to do this, but for now keep consistent with
6822 * prepare_pages in the normal write path.
6823 */
6824 clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
6825 EXTENT_DIRTY | EXTENT_DELALLOC |
6826 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
6827 0, 0, &cached_state, GFP_NOFS);
6828
6829 ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6830 &cached_state);
6831 if (ret) {
6832 unlock_extent_cached(io_tree, page_start, page_end,
6833 &cached_state, GFP_NOFS);
6834 ret = VM_FAULT_SIGBUS;
6835 goto out_unlock;
6836 }
6837 ret = 0;
6838
6839 /* page is wholly or partially inside EOF */
6840 if (page_start + PAGE_CACHE_SIZE > size)
6841 zero_start = size & ~PAGE_CACHE_MASK;
6842 else
6843 zero_start = PAGE_CACHE_SIZE;
6844
6845 if (zero_start != PAGE_CACHE_SIZE) {
6846 kaddr = kmap(page);
6847 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6848 flush_dcache_page(page);
6849 kunmap(page);
6850 }
6851 ClearPageChecked(page);
6852 set_page_dirty(page);
6853 SetPageUptodate(page);
6854
6855 BTRFS_I(inode)->last_trans = root->fs_info->generation;
6856 BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6857 BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
6858
6859 unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
6860
6861 out_unlock:
6862 if (!ret) {
6863 sb_end_pagefault(inode->i_sb);
6864 return VM_FAULT_LOCKED;
6865 }
6866 unlock_page(page);
6867 out:
6868 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
6869 out_noreserve:
6870 sb_end_pagefault(inode->i_sb);
6871 return ret;
6872 }
6873
6874 static int btrfs_truncate(struct inode *inode)
6875 {
6876 struct btrfs_root *root = BTRFS_I(inode)->root;
6877 struct btrfs_block_rsv *rsv;
6878 int ret;
6879 int err = 0;
6880 struct btrfs_trans_handle *trans;
6881 u64 mask = root->sectorsize - 1;
6882 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
6883
6884 ret = btrfs_truncate_page(inode, inode->i_size, 0, 0);
6885 if (ret)
6886 return ret;
6887
6888 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
6889 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
6890
6891 /*
6892 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6893 * 3 things going on here
6894 *
6895 * 1) We need to reserve space for our orphan item and the space to
6896 * delete our orphan item. Lord knows we don't want to have a dangling
6897 * orphan item because we didn't reserve space to remove it.
6898 *
6899 * 2) We need to reserve space to update our inode.
6900 *
6901 * 3) We need to have something to cache all the space that is going to
6902 * be free'd up by the truncate operation, but also have some slack
6903 * space reserved in case it uses space during the truncate (thank you
6904 * very much snapshotting).
6905 *
6906 * And we need these to all be seperate. The fact is we can use alot of
6907 * space doing the truncate, and we have no earthly idea how much space
6908 * we will use, so we need the truncate reservation to be seperate so it
6909 * doesn't end up using space reserved for updating the inode or
6910 * removing the orphan item. We also need to be able to stop the
6911 * transaction and start a new one, which means we need to be able to
6912 * update the inode several times, and we have no idea of knowing how
6913 * many times that will be, so we can't just reserve 1 item for the
6914 * entirety of the opration, so that has to be done seperately as well.
6915 * Then there is the orphan item, which does indeed need to be held on
6916 * to for the whole operation, and we need nobody to touch this reserved
6917 * space except the orphan code.
6918 *
6919 * So that leaves us with
6920 *
6921 * 1) root->orphan_block_rsv - for the orphan deletion.
6922 * 2) rsv - for the truncate reservation, which we will steal from the
6923 * transaction reservation.
6924 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6925 * updating the inode.
6926 */
6927 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
6928 if (!rsv)
6929 return -ENOMEM;
6930 rsv->size = min_size;
6931 rsv->failfast = 1;
6932
6933 /*
6934 * 1 for the truncate slack space
6935 * 1 for the orphan item we're going to add
6936 * 1 for the orphan item deletion
6937 * 1 for updating the inode.
6938 */
6939 trans = btrfs_start_transaction(root, 4);
6940 if (IS_ERR(trans)) {
6941 err = PTR_ERR(trans);
6942 goto out;
6943 }
6944
6945 /* Migrate the slack space for the truncate to our reserve */
6946 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
6947 min_size);
6948 BUG_ON(ret);
6949
6950 ret = btrfs_orphan_add(trans, inode);
6951 if (ret) {
6952 btrfs_end_transaction(trans, root);
6953 goto out;
6954 }
6955
6956 /*
6957 * setattr is responsible for setting the ordered_data_close flag,
6958 * but that is only tested during the last file release. That
6959 * could happen well after the next commit, leaving a great big
6960 * window where new writes may get lost if someone chooses to write
6961 * to this file after truncating to zero
6962 *
6963 * The inode doesn't have any dirty data here, and so if we commit
6964 * this is a noop. If someone immediately starts writing to the inode
6965 * it is very likely we'll catch some of their writes in this
6966 * transaction, and the commit will find this file on the ordered
6967 * data list with good things to send down.
6968 *
6969 * This is a best effort solution, there is still a window where
6970 * using truncate to replace the contents of the file will
6971 * end up with a zero length file after a crash.
6972 */
6973 if (inode->i_size == 0 && test_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
6974 &BTRFS_I(inode)->runtime_flags))
6975 btrfs_add_ordered_operation(trans, root, inode);
6976
6977 /*
6978 * So if we truncate and then write and fsync we normally would just
6979 * write the extents that changed, which is a problem if we need to
6980 * first truncate that entire inode. So set this flag so we write out
6981 * all of the extents in the inode to the sync log so we're completely
6982 * safe.
6983 */
6984 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6985 trans->block_rsv = rsv;
6986
6987 while (1) {
6988 ret = btrfs_truncate_inode_items(trans, root, inode,
6989 inode->i_size,
6990 BTRFS_EXTENT_DATA_KEY);
6991 if (ret != -ENOSPC) {
6992 err = ret;
6993 break;
6994 }
6995
6996 trans->block_rsv = &root->fs_info->trans_block_rsv;
6997 ret = btrfs_update_inode(trans, root, inode);
6998 if (ret) {
6999 err = ret;
7000 break;
7001 }
7002
7003 btrfs_end_transaction(trans, root);
7004 btrfs_btree_balance_dirty(root);
7005
7006 trans = btrfs_start_transaction(root, 2);
7007 if (IS_ERR(trans)) {
7008 ret = err = PTR_ERR(trans);
7009 trans = NULL;
7010 break;
7011 }
7012
7013 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
7014 rsv, min_size);
7015 BUG_ON(ret); /* shouldn't happen */
7016 trans->block_rsv = rsv;
7017 }
7018
7019 if (ret == 0 && inode->i_nlink > 0) {
7020 trans->block_rsv = root->orphan_block_rsv;
7021 ret = btrfs_orphan_del(trans, inode);
7022 if (ret)
7023 err = ret;
7024 } else if (ret && inode->i_nlink > 0) {
7025 /*
7026 * Failed to do the truncate, remove us from the in memory
7027 * orphan list.
7028 */
7029 ret = btrfs_orphan_del(NULL, inode);
7030 }
7031
7032 if (trans) {
7033 trans->block_rsv = &root->fs_info->trans_block_rsv;
7034 ret = btrfs_update_inode(trans, root, inode);
7035 if (ret && !err)
7036 err = ret;
7037
7038 ret = btrfs_end_transaction(trans, root);
7039 btrfs_btree_balance_dirty(root);
7040 }
7041
7042 out:
7043 btrfs_free_block_rsv(root, rsv);
7044
7045 if (ret && !err)
7046 err = ret;
7047
7048 return err;
7049 }
7050
7051 /*
7052 * create a new subvolume directory/inode (helper for the ioctl).
7053 */
7054 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
7055 struct btrfs_root *new_root, u64 new_dirid)
7056 {
7057 struct inode *inode;
7058 int err;
7059 u64 index = 0;
7060
7061 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
7062 new_dirid, new_dirid,
7063 S_IFDIR | (~current_umask() & S_IRWXUGO),
7064 &index);
7065 if (IS_ERR(inode))
7066 return PTR_ERR(inode);
7067 inode->i_op = &btrfs_dir_inode_operations;
7068 inode->i_fop = &btrfs_dir_file_operations;
7069
7070 set_nlink(inode, 1);
7071 btrfs_i_size_write(inode, 0);
7072
7073 err = btrfs_update_inode(trans, new_root, inode);
7074
7075 iput(inode);
7076 return err;
7077 }
7078
7079 struct inode *btrfs_alloc_inode(struct super_block *sb)
7080 {
7081 struct btrfs_inode *ei;
7082 struct inode *inode;
7083
7084 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
7085 if (!ei)
7086 return NULL;
7087
7088 ei->root = NULL;
7089 ei->generation = 0;
7090 ei->last_trans = 0;
7091 ei->last_sub_trans = 0;
7092 ei->logged_trans = 0;
7093 ei->delalloc_bytes = 0;
7094 ei->disk_i_size = 0;
7095 ei->flags = 0;
7096 ei->csum_bytes = 0;
7097 ei->index_cnt = (u64)-1;
7098 ei->last_unlink_trans = 0;
7099 ei->last_log_commit = 0;
7100
7101 spin_lock_init(&ei->lock);
7102 ei->outstanding_extents = 0;
7103 ei->reserved_extents = 0;
7104
7105 ei->runtime_flags = 0;
7106 ei->force_compress = BTRFS_COMPRESS_NONE;
7107
7108 ei->delayed_node = NULL;
7109
7110 inode = &ei->vfs_inode;
7111 extent_map_tree_init(&ei->extent_tree);
7112 extent_io_tree_init(&ei->io_tree, &inode->i_data);
7113 extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
7114 ei->io_tree.track_uptodate = 1;
7115 ei->io_failure_tree.track_uptodate = 1;
7116 atomic_set(&ei->sync_writers, 0);
7117 mutex_init(&ei->log_mutex);
7118 mutex_init(&ei->delalloc_mutex);
7119 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
7120 INIT_LIST_HEAD(&ei->delalloc_inodes);
7121 INIT_LIST_HEAD(&ei->ordered_operations);
7122 RB_CLEAR_NODE(&ei->rb_node);
7123
7124 return inode;
7125 }
7126
7127 static void btrfs_i_callback(struct rcu_head *head)
7128 {
7129 struct inode *inode = container_of(head, struct inode, i_rcu);
7130 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
7131 }
7132
7133 void btrfs_destroy_inode(struct inode *inode)
7134 {
7135 struct btrfs_ordered_extent *ordered;
7136 struct btrfs_root *root = BTRFS_I(inode)->root;
7137
7138 WARN_ON(!hlist_empty(&inode->i_dentry));
7139 WARN_ON(inode->i_data.nrpages);
7140 WARN_ON(BTRFS_I(inode)->outstanding_extents);
7141 WARN_ON(BTRFS_I(inode)->reserved_extents);
7142 WARN_ON(BTRFS_I(inode)->delalloc_bytes);
7143 WARN_ON(BTRFS_I(inode)->csum_bytes);
7144
7145 /*
7146 * This can happen where we create an inode, but somebody else also
7147 * created the same inode and we need to destroy the one we already
7148 * created.
7149 */
7150 if (!root)
7151 goto free;
7152
7153 /*
7154 * Make sure we're properly removed from the ordered operation
7155 * lists.
7156 */
7157 smp_mb();
7158 if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
7159 spin_lock(&root->fs_info->ordered_extent_lock);
7160 list_del_init(&BTRFS_I(inode)->ordered_operations);
7161 spin_unlock(&root->fs_info->ordered_extent_lock);
7162 }
7163
7164 if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
7165 &BTRFS_I(inode)->runtime_flags)) {
7166 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
7167 (unsigned long long)btrfs_ino(inode));
7168 atomic_dec(&root->orphan_inodes);
7169 }
7170
7171 while (1) {
7172 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
7173 if (!ordered)
7174 break;
7175 else {
7176 printk(KERN_ERR "btrfs found ordered "
7177 "extent %llu %llu on inode cleanup\n",
7178 (unsigned long long)ordered->file_offset,
7179 (unsigned long long)ordered->len);
7180 btrfs_remove_ordered_extent(inode, ordered);
7181 btrfs_put_ordered_extent(ordered);
7182 btrfs_put_ordered_extent(ordered);
7183 }
7184 }
7185 inode_tree_del(inode);
7186 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
7187 free:
7188 btrfs_remove_delayed_node(inode);
7189 call_rcu(&inode->i_rcu, btrfs_i_callback);
7190 }
7191
7192 int btrfs_drop_inode(struct inode *inode)
7193 {
7194 struct btrfs_root *root = BTRFS_I(inode)->root;
7195
7196 if (btrfs_root_refs(&root->root_item) == 0 &&
7197 !btrfs_is_free_space_inode(inode))
7198 return 1;
7199 else
7200 return generic_drop_inode(inode);
7201 }
7202
7203 static void init_once(void *foo)
7204 {
7205 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
7206
7207 inode_init_once(&ei->vfs_inode);
7208 }
7209
7210 void btrfs_destroy_cachep(void)
7211 {
7212 /*
7213 * Make sure all delayed rcu free inodes are flushed before we
7214 * destroy cache.
7215 */
7216 rcu_barrier();
7217 if (btrfs_inode_cachep)
7218 kmem_cache_destroy(btrfs_inode_cachep);
7219 if (btrfs_trans_handle_cachep)
7220 kmem_cache_destroy(btrfs_trans_handle_cachep);
7221 if (btrfs_transaction_cachep)
7222 kmem_cache_destroy(btrfs_transaction_cachep);
7223 if (btrfs_path_cachep)
7224 kmem_cache_destroy(btrfs_path_cachep);
7225 if (btrfs_free_space_cachep)
7226 kmem_cache_destroy(btrfs_free_space_cachep);
7227 if (btrfs_delalloc_work_cachep)
7228 kmem_cache_destroy(btrfs_delalloc_work_cachep);
7229 }
7230
7231 int btrfs_init_cachep(void)
7232 {
7233 btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
7234 sizeof(struct btrfs_inode), 0,
7235 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
7236 if (!btrfs_inode_cachep)
7237 goto fail;
7238
7239 btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
7240 sizeof(struct btrfs_trans_handle), 0,
7241 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7242 if (!btrfs_trans_handle_cachep)
7243 goto fail;
7244
7245 btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
7246 sizeof(struct btrfs_transaction), 0,
7247 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7248 if (!btrfs_transaction_cachep)
7249 goto fail;
7250
7251 btrfs_path_cachep = kmem_cache_create("btrfs_path",
7252 sizeof(struct btrfs_path), 0,
7253 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7254 if (!btrfs_path_cachep)
7255 goto fail;
7256
7257 btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
7258 sizeof(struct btrfs_free_space), 0,
7259 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7260 if (!btrfs_free_space_cachep)
7261 goto fail;
7262
7263 btrfs_delalloc_work_cachep = kmem_cache_create("btrfs_delalloc_work",
7264 sizeof(struct btrfs_delalloc_work), 0,
7265 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
7266 NULL);
7267 if (!btrfs_delalloc_work_cachep)
7268 goto fail;
7269
7270 return 0;
7271 fail:
7272 btrfs_destroy_cachep();
7273 return -ENOMEM;
7274 }
7275
7276 static int btrfs_getattr(struct vfsmount *mnt,
7277 struct dentry *dentry, struct kstat *stat)
7278 {
7279 struct inode *inode = dentry->d_inode;
7280 u32 blocksize = inode->i_sb->s_blocksize;
7281
7282 generic_fillattr(inode, stat);
7283 stat->dev = BTRFS_I(inode)->root->anon_dev;
7284 stat->blksize = PAGE_CACHE_SIZE;
7285 stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
7286 ALIGN(BTRFS_I(inode)->delalloc_bytes, blocksize)) >> 9;
7287 return 0;
7288 }
7289
7290 /*
7291 * If a file is moved, it will inherit the cow and compression flags of the new
7292 * directory.
7293 */
7294 static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7295 {
7296 struct btrfs_inode *b_dir = BTRFS_I(dir);
7297 struct btrfs_inode *b_inode = BTRFS_I(inode);
7298
7299 if (b_dir->flags & BTRFS_INODE_NODATACOW)
7300 b_inode->flags |= BTRFS_INODE_NODATACOW;
7301 else
7302 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7303
7304 if (b_dir->flags & BTRFS_INODE_COMPRESS) {
7305 b_inode->flags |= BTRFS_INODE_COMPRESS;
7306 b_inode->flags &= ~BTRFS_INODE_NOCOMPRESS;
7307 } else {
7308 b_inode->flags &= ~(BTRFS_INODE_COMPRESS |
7309 BTRFS_INODE_NOCOMPRESS);
7310 }
7311 }
7312
7313 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7314 struct inode *new_dir, struct dentry *new_dentry)
7315 {
7316 struct btrfs_trans_handle *trans;
7317 struct btrfs_root *root = BTRFS_I(old_dir)->root;
7318 struct btrfs_root *dest = BTRFS_I(new_dir)->root;
7319 struct inode *new_inode = new_dentry->d_inode;
7320 struct inode *old_inode = old_dentry->d_inode;
7321 struct timespec ctime = CURRENT_TIME;
7322 u64 index = 0;
7323 u64 root_objectid;
7324 int ret;
7325 u64 old_ino = btrfs_ino(old_inode);
7326
7327 if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
7328 return -EPERM;
7329
7330 /* we only allow rename subvolume link between subvolumes */
7331 if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
7332 return -EXDEV;
7333
7334 if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7335 (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
7336 return -ENOTEMPTY;
7337
7338 if (S_ISDIR(old_inode->i_mode) && new_inode &&
7339 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
7340 return -ENOTEMPTY;
7341 /*
7342 * we're using rename to replace one file with another.
7343 * and the replacement file is large. Start IO on it now so
7344 * we don't add too much work to the end of the transaction
7345 */
7346 if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
7347 old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7348 filemap_flush(old_inode->i_mapping);
7349
7350 /* close the racy window with snapshot create/destroy ioctl */
7351 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7352 down_read(&root->fs_info->subvol_sem);
7353 /*
7354 * We want to reserve the absolute worst case amount of items. So if
7355 * both inodes are subvols and we need to unlink them then that would
7356 * require 4 item modifications, but if they are both normal inodes it
7357 * would require 5 item modifications, so we'll assume their normal
7358 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7359 * should cover the worst case number of items we'll modify.
7360 */
7361 trans = btrfs_start_transaction(root, 20);
7362 if (IS_ERR(trans)) {
7363 ret = PTR_ERR(trans);
7364 goto out_notrans;
7365 }
7366
7367 if (dest != root)
7368 btrfs_record_root_in_trans(trans, dest);
7369
7370 ret = btrfs_set_inode_index(new_dir, &index);
7371 if (ret)
7372 goto out_fail;
7373
7374 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7375 /* force full log commit if subvolume involved. */
7376 root->fs_info->last_trans_log_full_commit = trans->transid;
7377 } else {
7378 ret = btrfs_insert_inode_ref(trans, dest,
7379 new_dentry->d_name.name,
7380 new_dentry->d_name.len,
7381 old_ino,
7382 btrfs_ino(new_dir), index);
7383 if (ret)
7384 goto out_fail;
7385 /*
7386 * this is an ugly little race, but the rename is required
7387 * to make sure that if we crash, the inode is either at the
7388 * old name or the new one. pinning the log transaction lets
7389 * us make sure we don't allow a log commit to come in after
7390 * we unlink the name but before we add the new name back in.
7391 */
7392 btrfs_pin_log_trans(root);
7393 }
7394 /*
7395 * make sure the inode gets flushed if it is replacing
7396 * something.
7397 */
7398 if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
7399 btrfs_add_ordered_operation(trans, root, old_inode);
7400
7401 inode_inc_iversion(old_dir);
7402 inode_inc_iversion(new_dir);
7403 inode_inc_iversion(old_inode);
7404 old_dir->i_ctime = old_dir->i_mtime = ctime;
7405 new_dir->i_ctime = new_dir->i_mtime = ctime;
7406 old_inode->i_ctime = ctime;
7407
7408 if (old_dentry->d_parent != new_dentry->d_parent)
7409 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7410
7411 if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7412 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7413 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7414 old_dentry->d_name.name,
7415 old_dentry->d_name.len);
7416 } else {
7417 ret = __btrfs_unlink_inode(trans, root, old_dir,
7418 old_dentry->d_inode,
7419 old_dentry->d_name.name,
7420 old_dentry->d_name.len);
7421 if (!ret)
7422 ret = btrfs_update_inode(trans, root, old_inode);
7423 }
7424 if (ret) {
7425 btrfs_abort_transaction(trans, root, ret);
7426 goto out_fail;
7427 }
7428
7429 if (new_inode) {
7430 inode_inc_iversion(new_inode);
7431 new_inode->i_ctime = CURRENT_TIME;
7432 if (unlikely(btrfs_ino(new_inode) ==
7433 BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7434 root_objectid = BTRFS_I(new_inode)->location.objectid;
7435 ret = btrfs_unlink_subvol(trans, dest, new_dir,
7436 root_objectid,
7437 new_dentry->d_name.name,
7438 new_dentry->d_name.len);
7439 BUG_ON(new_inode->i_nlink == 0);
7440 } else {
7441 ret = btrfs_unlink_inode(trans, dest, new_dir,
7442 new_dentry->d_inode,
7443 new_dentry->d_name.name,
7444 new_dentry->d_name.len);
7445 }
7446 if (!ret && new_inode->i_nlink == 0) {
7447 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
7448 BUG_ON(ret);
7449 }
7450 if (ret) {
7451 btrfs_abort_transaction(trans, root, ret);
7452 goto out_fail;
7453 }
7454 }
7455
7456 fixup_inode_flags(new_dir, old_inode);
7457
7458 ret = btrfs_add_link(trans, new_dir, old_inode,
7459 new_dentry->d_name.name,
7460 new_dentry->d_name.len, 0, index);
7461 if (ret) {
7462 btrfs_abort_transaction(trans, root, ret);
7463 goto out_fail;
7464 }
7465
7466 if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
7467 struct dentry *parent = new_dentry->d_parent;
7468 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7469 btrfs_end_log_trans(root);
7470 }
7471 out_fail:
7472 btrfs_end_transaction(trans, root);
7473 out_notrans:
7474 if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7475 up_read(&root->fs_info->subvol_sem);
7476
7477 return ret;
7478 }
7479
7480 static void btrfs_run_delalloc_work(struct btrfs_work *work)
7481 {
7482 struct btrfs_delalloc_work *delalloc_work;
7483
7484 delalloc_work = container_of(work, struct btrfs_delalloc_work,
7485 work);
7486 if (delalloc_work->wait)
7487 btrfs_wait_ordered_range(delalloc_work->inode, 0, (u64)-1);
7488 else
7489 filemap_flush(delalloc_work->inode->i_mapping);
7490
7491 if (delalloc_work->delay_iput)
7492 btrfs_add_delayed_iput(delalloc_work->inode);
7493 else
7494 iput(delalloc_work->inode);
7495 complete(&delalloc_work->completion);
7496 }
7497
7498 struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
7499 int wait, int delay_iput)
7500 {
7501 struct btrfs_delalloc_work *work;
7502
7503 work = kmem_cache_zalloc(btrfs_delalloc_work_cachep, GFP_NOFS);
7504 if (!work)
7505 return NULL;
7506
7507 init_completion(&work->completion);
7508 INIT_LIST_HEAD(&work->list);
7509 work->inode = inode;
7510 work->wait = wait;
7511 work->delay_iput = delay_iput;
7512 work->work.func = btrfs_run_delalloc_work;
7513
7514 return work;
7515 }
7516
7517 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
7518 {
7519 wait_for_completion(&work->completion);
7520 kmem_cache_free(btrfs_delalloc_work_cachep, work);
7521 }
7522
7523 /*
7524 * some fairly slow code that needs optimization. This walks the list
7525 * of all the inodes with pending delalloc and forces them to disk.
7526 */
7527 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7528 {
7529 struct list_head *head = &root->fs_info->delalloc_inodes;
7530 struct btrfs_inode *binode;
7531 struct inode *inode;
7532 struct btrfs_delalloc_work *work, *next;
7533 struct list_head works;
7534 int ret = 0;
7535
7536 if (root->fs_info->sb->s_flags & MS_RDONLY)
7537 return -EROFS;
7538
7539 INIT_LIST_HEAD(&works);
7540
7541 spin_lock(&root->fs_info->delalloc_lock);
7542 while (!list_empty(head)) {
7543 binode = list_entry(head->next, struct btrfs_inode,
7544 delalloc_inodes);
7545 inode = igrab(&binode->vfs_inode);
7546 if (!inode)
7547 list_del_init(&binode->delalloc_inodes);
7548 spin_unlock(&root->fs_info->delalloc_lock);
7549 if (inode) {
7550 work = btrfs_alloc_delalloc_work(inode, 0, delay_iput);
7551 if (!work) {
7552 ret = -ENOMEM;
7553 goto out;
7554 }
7555 list_add_tail(&work->list, &works);
7556 btrfs_queue_worker(&root->fs_info->flush_workers,
7557 &work->work);
7558 }
7559 cond_resched();
7560 spin_lock(&root->fs_info->delalloc_lock);
7561 }
7562 spin_unlock(&root->fs_info->delalloc_lock);
7563
7564 /* the filemap_flush will queue IO into the worker threads, but
7565 * we have to make sure the IO is actually started and that
7566 * ordered extents get created before we return
7567 */
7568 atomic_inc(&root->fs_info->async_submit_draining);
7569 while (atomic_read(&root->fs_info->nr_async_submits) ||
7570 atomic_read(&root->fs_info->async_delalloc_pages)) {
7571 wait_event(root->fs_info->async_submit_wait,
7572 (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7573 atomic_read(&root->fs_info->async_delalloc_pages) == 0));
7574 }
7575 atomic_dec(&root->fs_info->async_submit_draining);
7576 out:
7577 list_for_each_entry_safe(work, next, &works, list) {
7578 list_del_init(&work->list);
7579 btrfs_wait_and_free_delalloc_work(work);
7580 }
7581 return ret;
7582 }
7583
7584 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7585 const char *symname)
7586 {
7587 struct btrfs_trans_handle *trans;
7588 struct btrfs_root *root = BTRFS_I(dir)->root;
7589 struct btrfs_path *path;
7590 struct btrfs_key key;
7591 struct inode *inode = NULL;
7592 int err;
7593 int drop_inode = 0;
7594 u64 objectid;
7595 u64 index = 0 ;
7596 int name_len;
7597 int datasize;
7598 unsigned long ptr;
7599 struct btrfs_file_extent_item *ei;
7600 struct extent_buffer *leaf;
7601
7602 name_len = strlen(symname) + 1;
7603 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7604 return -ENAMETOOLONG;
7605
7606 /*
7607 * 2 items for inode item and ref
7608 * 2 items for dir items
7609 * 1 item for xattr if selinux is on
7610 */
7611 trans = btrfs_start_transaction(root, 5);
7612 if (IS_ERR(trans))
7613 return PTR_ERR(trans);
7614
7615 err = btrfs_find_free_ino(root, &objectid);
7616 if (err)
7617 goto out_unlock;
7618
7619 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7620 dentry->d_name.len, btrfs_ino(dir), objectid,
7621 S_IFLNK|S_IRWXUGO, &index);
7622 if (IS_ERR(inode)) {
7623 err = PTR_ERR(inode);
7624 goto out_unlock;
7625 }
7626
7627 err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
7628 if (err) {
7629 drop_inode = 1;
7630 goto out_unlock;
7631 }
7632
7633 /*
7634 * If the active LSM wants to access the inode during
7635 * d_instantiate it needs these. Smack checks to see
7636 * if the filesystem supports xattrs by looking at the
7637 * ops vector.
7638 */
7639 inode->i_fop = &btrfs_file_operations;
7640 inode->i_op = &btrfs_file_inode_operations;
7641
7642 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
7643 if (err)
7644 drop_inode = 1;
7645 else {
7646 inode->i_mapping->a_ops = &btrfs_aops;
7647 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
7648 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
7649 }
7650 if (drop_inode)
7651 goto out_unlock;
7652
7653 path = btrfs_alloc_path();
7654 if (!path) {
7655 err = -ENOMEM;
7656 drop_inode = 1;
7657 goto out_unlock;
7658 }
7659 key.objectid = btrfs_ino(inode);
7660 key.offset = 0;
7661 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7662 datasize = btrfs_file_extent_calc_inline_size(name_len);
7663 err = btrfs_insert_empty_item(trans, root, path, &key,
7664 datasize);
7665 if (err) {
7666 drop_inode = 1;
7667 btrfs_free_path(path);
7668 goto out_unlock;
7669 }
7670 leaf = path->nodes[0];
7671 ei = btrfs_item_ptr(leaf, path->slots[0],
7672 struct btrfs_file_extent_item);
7673 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7674 btrfs_set_file_extent_type(leaf, ei,
7675 BTRFS_FILE_EXTENT_INLINE);
7676 btrfs_set_file_extent_encryption(leaf, ei, 0);
7677 btrfs_set_file_extent_compression(leaf, ei, 0);
7678 btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7679 btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7680
7681 ptr = btrfs_file_extent_inline_start(ei);
7682 write_extent_buffer(leaf, symname, ptr, name_len);
7683 btrfs_mark_buffer_dirty(leaf);
7684 btrfs_free_path(path);
7685
7686 inode->i_op = &btrfs_symlink_inode_operations;
7687 inode->i_mapping->a_ops = &btrfs_symlink_aops;
7688 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
7689 inode_set_bytes(inode, name_len);
7690 btrfs_i_size_write(inode, name_len - 1);
7691 err = btrfs_update_inode(trans, root, inode);
7692 if (err)
7693 drop_inode = 1;
7694
7695 out_unlock:
7696 if (!err)
7697 d_instantiate(dentry, inode);
7698 btrfs_end_transaction(trans, root);
7699 if (drop_inode) {
7700 inode_dec_link_count(inode);
7701 iput(inode);
7702 }
7703 btrfs_btree_balance_dirty(root);
7704 return err;
7705 }
7706
7707 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7708 u64 start, u64 num_bytes, u64 min_size,
7709 loff_t actual_len, u64 *alloc_hint,
7710 struct btrfs_trans_handle *trans)
7711 {
7712 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
7713 struct extent_map *em;
7714 struct btrfs_root *root = BTRFS_I(inode)->root;
7715 struct btrfs_key ins;
7716 u64 cur_offset = start;
7717 u64 i_size;
7718 int ret = 0;
7719 bool own_trans = true;
7720
7721 if (trans)
7722 own_trans = false;
7723 while (num_bytes > 0) {
7724 if (own_trans) {
7725 trans = btrfs_start_transaction(root, 3);
7726 if (IS_ERR(trans)) {
7727 ret = PTR_ERR(trans);
7728 break;
7729 }
7730 }
7731
7732 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7733 0, *alloc_hint, &ins, 1);
7734 if (ret) {
7735 if (own_trans)
7736 btrfs_end_transaction(trans, root);
7737 break;
7738 }
7739
7740 ret = insert_reserved_file_extent(trans, inode,
7741 cur_offset, ins.objectid,
7742 ins.offset, ins.offset,
7743 ins.offset, 0, 0, 0,
7744 BTRFS_FILE_EXTENT_PREALLOC);
7745 if (ret) {
7746 btrfs_abort_transaction(trans, root, ret);
7747 if (own_trans)
7748 btrfs_end_transaction(trans, root);
7749 break;
7750 }
7751 btrfs_drop_extent_cache(inode, cur_offset,
7752 cur_offset + ins.offset -1, 0);
7753
7754 em = alloc_extent_map();
7755 if (!em) {
7756 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
7757 &BTRFS_I(inode)->runtime_flags);
7758 goto next;
7759 }
7760
7761 em->start = cur_offset;
7762 em->orig_start = cur_offset;
7763 em->len = ins.offset;
7764 em->block_start = ins.objectid;
7765 em->block_len = ins.offset;
7766 em->orig_block_len = ins.offset;
7767 em->bdev = root->fs_info->fs_devices->latest_bdev;
7768 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7769 em->generation = trans->transid;
7770
7771 while (1) {
7772 write_lock(&em_tree->lock);
7773 ret = add_extent_mapping(em_tree, em);
7774 if (!ret)
7775 list_move(&em->list,
7776 &em_tree->modified_extents);
7777 write_unlock(&em_tree->lock);
7778 if (ret != -EEXIST)
7779 break;
7780 btrfs_drop_extent_cache(inode, cur_offset,
7781 cur_offset + ins.offset - 1,
7782 0);
7783 }
7784 free_extent_map(em);
7785 next:
7786 num_bytes -= ins.offset;
7787 cur_offset += ins.offset;
7788 *alloc_hint = ins.objectid + ins.offset;
7789
7790 inode_inc_iversion(inode);
7791 inode->i_ctime = CURRENT_TIME;
7792 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
7793 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
7794 (actual_len > inode->i_size) &&
7795 (cur_offset > inode->i_size)) {
7796 if (cur_offset > actual_len)
7797 i_size = actual_len;
7798 else
7799 i_size = cur_offset;
7800 i_size_write(inode, i_size);
7801 btrfs_ordered_update_i_size(inode, i_size, NULL);
7802 }
7803
7804 ret = btrfs_update_inode(trans, root, inode);
7805
7806 if (ret) {
7807 btrfs_abort_transaction(trans, root, ret);
7808 if (own_trans)
7809 btrfs_end_transaction(trans, root);
7810 break;
7811 }
7812
7813 if (own_trans)
7814 btrfs_end_transaction(trans, root);
7815 }
7816 return ret;
7817 }
7818
7819 int btrfs_prealloc_file_range(struct inode *inode, int mode,
7820 u64 start, u64 num_bytes, u64 min_size,
7821 loff_t actual_len, u64 *alloc_hint)
7822 {
7823 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7824 min_size, actual_len, alloc_hint,
7825 NULL);
7826 }
7827
7828 int btrfs_prealloc_file_range_trans(struct inode *inode,
7829 struct btrfs_trans_handle *trans, int mode,
7830 u64 start, u64 num_bytes, u64 min_size,
7831 loff_t actual_len, u64 *alloc_hint)
7832 {
7833 return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7834 min_size, actual_len, alloc_hint, trans);
7835 }
7836
7837 static int btrfs_set_page_dirty(struct page *page)
7838 {
7839 return __set_page_dirty_nobuffers(page);
7840 }
7841
7842 static int btrfs_permission(struct inode *inode, int mask)
7843 {
7844 struct btrfs_root *root = BTRFS_I(inode)->root;
7845 umode_t mode = inode->i_mode;
7846
7847 if (mask & MAY_WRITE &&
7848 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
7849 if (btrfs_root_readonly(root))
7850 return -EROFS;
7851 if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
7852 return -EACCES;
7853 }
7854 return generic_permission(inode, mask);
7855 }
7856
7857 static const struct inode_operations btrfs_dir_inode_operations = {
7858 .getattr = btrfs_getattr,
7859 .lookup = btrfs_lookup,
7860 .create = btrfs_create,
7861 .unlink = btrfs_unlink,
7862 .link = btrfs_link,
7863 .mkdir = btrfs_mkdir,
7864 .rmdir = btrfs_rmdir,
7865 .rename = btrfs_rename,
7866 .symlink = btrfs_symlink,
7867 .setattr = btrfs_setattr,
7868 .mknod = btrfs_mknod,
7869 .setxattr = btrfs_setxattr,
7870 .getxattr = btrfs_getxattr,
7871 .listxattr = btrfs_listxattr,
7872 .removexattr = btrfs_removexattr,
7873 .permission = btrfs_permission,
7874 .get_acl = btrfs_get_acl,
7875 };
7876 static const struct inode_operations btrfs_dir_ro_inode_operations = {
7877 .lookup = btrfs_lookup,
7878 .permission = btrfs_permission,
7879 .get_acl = btrfs_get_acl,
7880 };
7881
7882 static const struct file_operations btrfs_dir_file_operations = {
7883 .llseek = generic_file_llseek,
7884 .read = generic_read_dir,
7885 .readdir = btrfs_real_readdir,
7886 .unlocked_ioctl = btrfs_ioctl,
7887 #ifdef CONFIG_COMPAT
7888 .compat_ioctl = btrfs_ioctl,
7889 #endif
7890 .release = btrfs_release_file,
7891 .fsync = btrfs_sync_file,
7892 };
7893
7894 static struct extent_io_ops btrfs_extent_io_ops = {
7895 .fill_delalloc = run_delalloc_range,
7896 .submit_bio_hook = btrfs_submit_bio_hook,
7897 .merge_bio_hook = btrfs_merge_bio_hook,
7898 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
7899 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
7900 .writepage_start_hook = btrfs_writepage_start_hook,
7901 .set_bit_hook = btrfs_set_bit_hook,
7902 .clear_bit_hook = btrfs_clear_bit_hook,
7903 .merge_extent_hook = btrfs_merge_extent_hook,
7904 .split_extent_hook = btrfs_split_extent_hook,
7905 };
7906
7907 /*
7908 * btrfs doesn't support the bmap operation because swapfiles
7909 * use bmap to make a mapping of extents in the file. They assume
7910 * these extents won't change over the life of the file and they
7911 * use the bmap result to do IO directly to the drive.
7912 *
7913 * the btrfs bmap call would return logical addresses that aren't
7914 * suitable for IO and they also will change frequently as COW
7915 * operations happen. So, swapfile + btrfs == corruption.
7916 *
7917 * For now we're avoiding this by dropping bmap.
7918 */
7919 static const struct address_space_operations btrfs_aops = {
7920 .readpage = btrfs_readpage,
7921 .writepage = btrfs_writepage,
7922 .writepages = btrfs_writepages,
7923 .readpages = btrfs_readpages,
7924 .direct_IO = btrfs_direct_IO,
7925 .invalidatepage = btrfs_invalidatepage,
7926 .releasepage = btrfs_releasepage,
7927 .set_page_dirty = btrfs_set_page_dirty,
7928 .error_remove_page = generic_error_remove_page,
7929 };
7930
7931 static const struct address_space_operations btrfs_symlink_aops = {
7932 .readpage = btrfs_readpage,
7933 .writepage = btrfs_writepage,
7934 .invalidatepage = btrfs_invalidatepage,
7935 .releasepage = btrfs_releasepage,
7936 };
7937
7938 static const struct inode_operations btrfs_file_inode_operations = {
7939 .getattr = btrfs_getattr,
7940 .setattr = btrfs_setattr,
7941 .setxattr = btrfs_setxattr,
7942 .getxattr = btrfs_getxattr,
7943 .listxattr = btrfs_listxattr,
7944 .removexattr = btrfs_removexattr,
7945 .permission = btrfs_permission,
7946 .fiemap = btrfs_fiemap,
7947 .get_acl = btrfs_get_acl,
7948 .update_time = btrfs_update_time,
7949 };
7950 static const struct inode_operations btrfs_special_inode_operations = {
7951 .getattr = btrfs_getattr,
7952 .setattr = btrfs_setattr,
7953 .permission = btrfs_permission,
7954 .setxattr = btrfs_setxattr,
7955 .getxattr = btrfs_getxattr,
7956 .listxattr = btrfs_listxattr,
7957 .removexattr = btrfs_removexattr,
7958 .get_acl = btrfs_get_acl,
7959 .update_time = btrfs_update_time,
7960 };
7961 static const struct inode_operations btrfs_symlink_inode_operations = {
7962 .readlink = generic_readlink,
7963 .follow_link = page_follow_link_light,
7964 .put_link = page_put_link,
7965 .getattr = btrfs_getattr,
7966 .setattr = btrfs_setattr,
7967 .permission = btrfs_permission,
7968 .setxattr = btrfs_setxattr,
7969 .getxattr = btrfs_getxattr,
7970 .listxattr = btrfs_listxattr,
7971 .removexattr = btrfs_removexattr,
7972 .get_acl = btrfs_get_acl,
7973 .update_time = btrfs_update_time,
7974 };
7975
7976 const struct dentry_operations btrfs_dentry_operations = {
7977 .d_delete = btrfs_dentry_delete,
7978 .d_release = btrfs_dentry_release,
7979 };
This page took 0.255138 seconds and 5 git commands to generate.