Btrfs: Optimize compressed writeback and reads
[linux-2.6-block.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/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/version.h>
38 #include <linux/xattr.h>
39 #include <linux/posix_acl.h>
40 #include <linux/falloc.h>
41 #include "ctree.h"
42 #include "disk-io.h"
43 #include "transaction.h"
44 #include "btrfs_inode.h"
45 #include "ioctl.h"
46 #include "print-tree.h"
47 #include "volumes.h"
48 #include "ordered-data.h"
49 #include "xattr.h"
50 #include "compat.h"
51 #include "tree-log.h"
52 #include "ref-cache.h"
53 #include "compression.h"
54
55 struct btrfs_iget_args {
56         u64 ino;
57         struct btrfs_root *root;
58 };
59
60 static struct inode_operations btrfs_dir_inode_operations;
61 static struct inode_operations btrfs_symlink_inode_operations;
62 static struct inode_operations btrfs_dir_ro_inode_operations;
63 static struct inode_operations btrfs_special_inode_operations;
64 static struct inode_operations btrfs_file_inode_operations;
65 static struct address_space_operations btrfs_aops;
66 static struct address_space_operations btrfs_symlink_aops;
67 static struct file_operations btrfs_dir_file_operations;
68 static struct extent_io_ops btrfs_extent_io_ops;
69
70 static struct kmem_cache *btrfs_inode_cachep;
71 struct kmem_cache *btrfs_trans_handle_cachep;
72 struct kmem_cache *btrfs_transaction_cachep;
73 struct kmem_cache *btrfs_bit_radix_cachep;
74 struct kmem_cache *btrfs_path_cachep;
75
76 #define S_SHIFT 12
77 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
78         [S_IFREG >> S_SHIFT]    = BTRFS_FT_REG_FILE,
79         [S_IFDIR >> S_SHIFT]    = BTRFS_FT_DIR,
80         [S_IFCHR >> S_SHIFT]    = BTRFS_FT_CHRDEV,
81         [S_IFBLK >> S_SHIFT]    = BTRFS_FT_BLKDEV,
82         [S_IFIFO >> S_SHIFT]    = BTRFS_FT_FIFO,
83         [S_IFSOCK >> S_SHIFT]   = BTRFS_FT_SOCK,
84         [S_IFLNK >> S_SHIFT]    = BTRFS_FT_SYMLINK,
85 };
86
87 static void btrfs_truncate(struct inode *inode);
88 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
89 static noinline int cow_file_range(struct inode *inode,
90                                    struct page *locked_page,
91                                    u64 start, u64 end, int *page_started,
92                                    unsigned long *nr_written, int unlock);
93
94 /*
95  * a very lame attempt at stopping writes when the FS is 85% full.  There
96  * are countless ways this is incorrect, but it is better than nothing.
97  */
98 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
99                            int for_del)
100 {
101         u64 total;
102         u64 used;
103         u64 thresh;
104         unsigned long flags;
105         int ret = 0;
106
107         spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
108         total = btrfs_super_total_bytes(&root->fs_info->super_copy);
109         used = btrfs_super_bytes_used(&root->fs_info->super_copy);
110         if (for_del)
111                 thresh = total * 90;
112         else
113                 thresh = total * 85;
114
115         do_div(thresh, 100);
116
117         if (used + root->fs_info->delalloc_bytes + num_required > thresh)
118                 ret = -ENOSPC;
119         spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
120         return ret;
121 }
122
123 /*
124  * this does all the hard work for inserting an inline extent into
125  * the btree.  The caller should have done a btrfs_drop_extents so that
126  * no overlapping inline items exist in the btree
127  */
128 static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
129                                 struct btrfs_root *root, struct inode *inode,
130                                 u64 start, size_t size, size_t compressed_size,
131                                 struct page **compressed_pages)
132 {
133         struct btrfs_key key;
134         struct btrfs_path *path;
135         struct extent_buffer *leaf;
136         struct page *page = NULL;
137         char *kaddr;
138         unsigned long ptr;
139         struct btrfs_file_extent_item *ei;
140         int err = 0;
141         int ret;
142         size_t cur_size = size;
143         size_t datasize;
144         unsigned long offset;
145         int use_compress = 0;
146
147         if (compressed_size && compressed_pages) {
148                 use_compress = 1;
149                 cur_size = compressed_size;
150         }
151
152         path = btrfs_alloc_path(); if (!path)
153                 return -ENOMEM;
154
155         btrfs_set_trans_block_group(trans, inode);
156
157         key.objectid = inode->i_ino;
158         key.offset = start;
159         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
160         inode_add_bytes(inode, size);
161         datasize = btrfs_file_extent_calc_inline_size(cur_size);
162
163         inode_add_bytes(inode, size);
164         ret = btrfs_insert_empty_item(trans, root, path, &key,
165                                       datasize);
166         BUG_ON(ret);
167         if (ret) {
168                 err = ret;
169                 printk("got bad ret %d\n", ret);
170                 goto fail;
171         }
172         leaf = path->nodes[0];
173         ei = btrfs_item_ptr(leaf, path->slots[0],
174                             struct btrfs_file_extent_item);
175         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
176         btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
177         btrfs_set_file_extent_encryption(leaf, ei, 0);
178         btrfs_set_file_extent_other_encoding(leaf, ei, 0);
179         btrfs_set_file_extent_ram_bytes(leaf, ei, size);
180         ptr = btrfs_file_extent_inline_start(ei);
181
182         if (use_compress) {
183                 struct page *cpage;
184                 int i = 0;
185                 while(compressed_size > 0) {
186                         cpage = compressed_pages[i];
187                         cur_size = min(compressed_size,
188                                        PAGE_CACHE_SIZE);
189
190                         kaddr = kmap(cpage);
191                         write_extent_buffer(leaf, kaddr, ptr, cur_size);
192                         kunmap(cpage);
193
194                         i++;
195                         ptr += cur_size;
196                         compressed_size -= cur_size;
197                 }
198                 btrfs_set_file_extent_compression(leaf, ei,
199                                                   BTRFS_COMPRESS_ZLIB);
200         } else {
201                 page = find_get_page(inode->i_mapping,
202                                      start >> PAGE_CACHE_SHIFT);
203                 btrfs_set_file_extent_compression(leaf, ei, 0);
204                 kaddr = kmap_atomic(page, KM_USER0);
205                 offset = start & (PAGE_CACHE_SIZE - 1);
206                 write_extent_buffer(leaf, kaddr + offset, ptr, size);
207                 kunmap_atomic(kaddr, KM_USER0);
208                 page_cache_release(page);
209         }
210         btrfs_mark_buffer_dirty(leaf);
211         btrfs_free_path(path);
212
213         BTRFS_I(inode)->disk_i_size = inode->i_size;
214         btrfs_update_inode(trans, root, inode);
215         return 0;
216 fail:
217         btrfs_free_path(path);
218         return err;
219 }
220
221
222 /*
223  * conditionally insert an inline extent into the file.  This
224  * does the checks required to make sure the data is small enough
225  * to fit as an inline extent.
226  */
227 static int cow_file_range_inline(struct btrfs_trans_handle *trans,
228                                  struct btrfs_root *root,
229                                  struct inode *inode, u64 start, u64 end,
230                                  size_t compressed_size,
231                                  struct page **compressed_pages)
232 {
233         u64 isize = i_size_read(inode);
234         u64 actual_end = min(end + 1, isize);
235         u64 inline_len = actual_end - start;
236         u64 aligned_end = (end + root->sectorsize - 1) &
237                         ~((u64)root->sectorsize - 1);
238         u64 hint_byte;
239         u64 data_len = inline_len;
240         int ret;
241
242         if (compressed_size)
243                 data_len = compressed_size;
244
245         if (start > 0 ||
246             actual_end >= PAGE_CACHE_SIZE ||
247             data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
248             (!compressed_size &&
249             (actual_end & (root->sectorsize - 1)) == 0) ||
250             end + 1 < isize ||
251             data_len > root->fs_info->max_inline) {
252                 return 1;
253         }
254
255         ret = btrfs_drop_extents(trans, root, inode, start,
256                                  aligned_end, start, &hint_byte);
257         BUG_ON(ret);
258
259         if (isize > actual_end)
260                 inline_len = min_t(u64, isize, actual_end);
261         ret = insert_inline_extent(trans, root, inode, start,
262                                    inline_len, compressed_size,
263                                    compressed_pages);
264         BUG_ON(ret);
265         btrfs_drop_extent_cache(inode, start, aligned_end, 0);
266         return 0;
267 }
268
269 struct async_extent {
270         u64 start;
271         u64 ram_size;
272         u64 compressed_size;
273         struct page **pages;
274         unsigned long nr_pages;
275         struct list_head list;
276 };
277
278 struct async_cow {
279         struct inode *inode;
280         struct btrfs_root *root;
281         struct page *locked_page;
282         u64 start;
283         u64 end;
284         struct list_head extents;
285         struct btrfs_work work;
286 };
287
288 static noinline int add_async_extent(struct async_cow *cow,
289                                      u64 start, u64 ram_size,
290                                      u64 compressed_size,
291                                      struct page **pages,
292                                      unsigned long nr_pages)
293 {
294         struct async_extent *async_extent;
295
296         async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
297         async_extent->start = start;
298         async_extent->ram_size = ram_size;
299         async_extent->compressed_size = compressed_size;
300         async_extent->pages = pages;
301         async_extent->nr_pages = nr_pages;
302         list_add_tail(&async_extent->list, &cow->extents);
303         return 0;
304 }
305
306 /*
307  * we create compressed extents in two phases.  The first
308  * phase compresses a range of pages that have already been
309  * locked (both pages and state bits are locked).
310  *
311  * This is done inside an ordered work queue, and the compression
312  * is spread across many cpus.  The actual IO submission is step
313  * two, and the ordered work queue takes care of making sure that
314  * happens in the same order things were put onto the queue by
315  * writepages and friends.
316  *
317  * If this code finds it can't get good compression, it puts an
318  * entry onto the work queue to write the uncompressed bytes.  This
319  * makes sure that both compressed inodes and uncompressed inodes
320  * are written in the same order that pdflush sent them down.
321  */
322 static noinline int compress_file_range(struct inode *inode,
323                                         struct page *locked_page,
324                                         u64 start, u64 end,
325                                         struct async_cow *async_cow,
326                                         int *num_added)
327 {
328         struct btrfs_root *root = BTRFS_I(inode)->root;
329         struct btrfs_trans_handle *trans;
330         u64 num_bytes;
331         u64 orig_start;
332         u64 disk_num_bytes;
333         u64 blocksize = root->sectorsize;
334         u64 actual_end;
335         int ret = 0;
336         struct page **pages = NULL;
337         unsigned long nr_pages;
338         unsigned long nr_pages_ret = 0;
339         unsigned long total_compressed = 0;
340         unsigned long total_in = 0;
341         unsigned long max_compressed = 128 * 1024;
342         unsigned long max_uncompressed = 128 * 1024;
343         int i;
344         int will_compress;
345
346         orig_start = start;
347
348 again:
349         will_compress = 0;
350         nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
351         nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
352
353         actual_end = min_t(u64, i_size_read(inode), end + 1);
354         total_compressed = actual_end - start;
355
356         /* we want to make sure that amount of ram required to uncompress
357          * an extent is reasonable, so we limit the total size in ram
358          * of a compressed extent to 128k.  This is a crucial number
359          * because it also controls how easily we can spread reads across
360          * cpus for decompression.
361          *
362          * We also want to make sure the amount of IO required to do
363          * a random read is reasonably small, so we limit the size of
364          * a compressed extent to 128k.
365          */
366         total_compressed = min(total_compressed, max_uncompressed);
367         num_bytes = (end - start + blocksize) & ~(blocksize - 1);
368         num_bytes = max(blocksize,  num_bytes);
369         disk_num_bytes = num_bytes;
370         total_in = 0;
371         ret = 0;
372
373         /*
374          * we do compression for mount -o compress and when the
375          * inode has not been flagged as nocompress.  This flag can
376          * change at any time if we discover bad compression ratios.
377          */
378         if (!btrfs_test_flag(inode, NOCOMPRESS) &&
379             btrfs_test_opt(root, COMPRESS)) {
380                 WARN_ON(pages);
381                 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
382
383                 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
384                                                 total_compressed, pages,
385                                                 nr_pages, &nr_pages_ret,
386                                                 &total_in,
387                                                 &total_compressed,
388                                                 max_compressed);
389
390                 if (!ret) {
391                         unsigned long offset = total_compressed &
392                                 (PAGE_CACHE_SIZE - 1);
393                         struct page *page = pages[nr_pages_ret - 1];
394                         char *kaddr;
395
396                         /* zero the tail end of the last page, we might be
397                          * sending it down to disk
398                          */
399                         if (offset) {
400                                 kaddr = kmap_atomic(page, KM_USER0);
401                                 memset(kaddr + offset, 0,
402                                        PAGE_CACHE_SIZE - offset);
403                                 kunmap_atomic(kaddr, KM_USER0);
404                         }
405                         will_compress = 1;
406                 }
407         }
408         if (start == 0) {
409                 trans = btrfs_join_transaction(root, 1);
410                 BUG_ON(!trans);
411                 btrfs_set_trans_block_group(trans, inode);
412
413                 /* lets try to make an inline extent */
414                 if (ret || total_in < (actual_end - start)) {
415                         /* we didn't compress the entire range, try
416                          * to make an uncompressed inline extent.
417                          */
418                         ret = cow_file_range_inline(trans, root, inode,
419                                                     start, end, 0, NULL);
420                 } else {
421                         /* try making a compressed inline extent */
422                         ret = cow_file_range_inline(trans, root, inode,
423                                                     start, end,
424                                                     total_compressed, pages);
425                 }
426                 btrfs_end_transaction(trans, root);
427                 if (ret == 0) {
428                         /*
429                          * inline extent creation worked, we don't need
430                          * to create any more async work items.  Unlock
431                          * and free up our temp pages.
432                          */
433                         extent_clear_unlock_delalloc(inode,
434                                                      &BTRFS_I(inode)->io_tree,
435                                                      start, end, NULL, 1, 0,
436                                                      0, 1, 1, 1);
437                         ret = 0;
438                         goto free_pages_out;
439                 }
440         }
441
442         if (will_compress) {
443                 /*
444                  * we aren't doing an inline extent round the compressed size
445                  * up to a block size boundary so the allocator does sane
446                  * things
447                  */
448                 total_compressed = (total_compressed + blocksize - 1) &
449                         ~(blocksize - 1);
450
451                 /*
452                  * one last check to make sure the compression is really a
453                  * win, compare the page count read with the blocks on disk
454                  */
455                 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
456                         ~(PAGE_CACHE_SIZE - 1);
457                 if (total_compressed >= total_in) {
458                         will_compress = 0;
459                 } else {
460                         disk_num_bytes = total_compressed;
461                         num_bytes = total_in;
462                 }
463         }
464         if (!will_compress && pages) {
465                 /*
466                  * the compression code ran but failed to make things smaller,
467                  * free any pages it allocated and our page pointer array
468                  */
469                 for (i = 0; i < nr_pages_ret; i++) {
470                         WARN_ON(pages[i]->mapping);
471                         page_cache_release(pages[i]);
472                 }
473                 kfree(pages);
474                 pages = NULL;
475                 total_compressed = 0;
476                 nr_pages_ret = 0;
477
478                 /* flag the file so we don't compress in the future */
479                 btrfs_set_flag(inode, NOCOMPRESS);
480         }
481         if (will_compress) {
482                 *num_added += 1;
483
484                 /* the async work queues will take care of doing actual
485                  * allocation on disk for these compressed pages,
486                  * and will submit them to the elevator.
487                  */
488                 add_async_extent(async_cow, start, num_bytes,
489                                  total_compressed, pages, nr_pages_ret);
490
491                 if (start + num_bytes < end) {
492                         start += num_bytes;
493                         pages = NULL;
494                         cond_resched();
495                         goto again;
496                 }
497         } else {
498                 /*
499                  * No compression, but we still need to write the pages in
500                  * the file we've been given so far.  redirty the locked
501                  * page if it corresponds to our extent and set things up
502                  * for the async work queue to run cow_file_range to do
503                  * the normal delalloc dance
504                  */
505                 if (page_offset(locked_page) >= start &&
506                     page_offset(locked_page) <= end) {
507                         __set_page_dirty_nobuffers(locked_page);
508                         /* unlocked later on in the async handlers */
509                 }
510                 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
511                 *num_added += 1;
512         }
513
514 out:
515         return 0;
516
517 free_pages_out:
518         for (i = 0; i < nr_pages_ret; i++) {
519                 WARN_ON(pages[i]->mapping);
520                 page_cache_release(pages[i]);
521         }
522         if (pages)
523                 kfree(pages);
524
525         goto out;
526 }
527
528 /*
529  * phase two of compressed writeback.  This is the ordered portion
530  * of the code, which only gets called in the order the work was
531  * queued.  We walk all the async extents created by compress_file_range
532  * and send them down to the disk.
533  */
534 static noinline int submit_compressed_extents(struct inode *inode,
535                                               struct async_cow *async_cow)
536 {
537         struct async_extent *async_extent;
538         u64 alloc_hint = 0;
539         struct btrfs_trans_handle *trans;
540         struct btrfs_key ins;
541         struct extent_map *em;
542         struct btrfs_root *root = BTRFS_I(inode)->root;
543         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
544         struct extent_io_tree *io_tree;
545         int ret;
546
547         if (list_empty(&async_cow->extents))
548                 return 0;
549
550         trans = btrfs_join_transaction(root, 1);
551
552         while(!list_empty(&async_cow->extents)) {
553                 async_extent = list_entry(async_cow->extents.next,
554                                           struct async_extent, list);
555                 list_del(&async_extent->list);
556
557                 io_tree = &BTRFS_I(inode)->io_tree;
558
559                 /* did the compression code fall back to uncompressed IO? */
560                 if (!async_extent->pages) {
561                         int page_started = 0;
562                         unsigned long nr_written = 0;
563
564                         lock_extent(io_tree, async_extent->start,
565                                     async_extent->start + async_extent->ram_size - 1,
566                                     GFP_NOFS);
567
568                         /* allocate blocks */
569                         cow_file_range(inode, async_cow->locked_page,
570                                        async_extent->start,
571                                        async_extent->start +
572                                        async_extent->ram_size - 1,
573                                        &page_started, &nr_written, 0);
574
575                         /*
576                          * if page_started, cow_file_range inserted an
577                          * inline extent and took care of all the unlocking
578                          * and IO for us.  Otherwise, we need to submit
579                          * all those pages down to the drive.
580                          */
581                         if (!page_started)
582                                 extent_write_locked_range(io_tree,
583                                                   inode, async_extent->start,
584                                                   async_extent->start +
585                                                   async_extent->ram_size - 1,
586                                                   btrfs_get_extent,
587                                                   WB_SYNC_ALL);
588                         kfree(async_extent);
589                         cond_resched();
590                         continue;
591                 }
592
593                 lock_extent(io_tree, async_extent->start,
594                             async_extent->start + async_extent->ram_size - 1,
595                             GFP_NOFS);
596                 /*
597                  * here we're doing allocation and writeback of the
598                  * compressed pages
599                  */
600                 btrfs_drop_extent_cache(inode, async_extent->start,
601                                         async_extent->start +
602                                         async_extent->ram_size - 1, 0);
603
604                 ret = btrfs_reserve_extent(trans, root,
605                                            async_extent->compressed_size,
606                                            async_extent->compressed_size,
607                                            0, alloc_hint,
608                                            (u64)-1, &ins, 1);
609                 BUG_ON(ret);
610                 em = alloc_extent_map(GFP_NOFS);
611                 em->start = async_extent->start;
612                 em->len = async_extent->ram_size;
613
614                 em->block_start = ins.objectid;
615                 em->block_len = ins.offset;
616                 em->bdev = root->fs_info->fs_devices->latest_bdev;
617                 set_bit(EXTENT_FLAG_PINNED, &em->flags);
618                 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
619
620                 while(1) {
621                         spin_lock(&em_tree->lock);
622                         ret = add_extent_mapping(em_tree, em);
623                         spin_unlock(&em_tree->lock);
624                         if (ret != -EEXIST) {
625                                 free_extent_map(em);
626                                 break;
627                         }
628                         btrfs_drop_extent_cache(inode, async_extent->start,
629                                                 async_extent->start +
630                                                 async_extent->ram_size - 1, 0);
631                 }
632
633                 ret = btrfs_add_ordered_extent(inode, async_extent->start,
634                                                ins.objectid,
635                                                async_extent->ram_size,
636                                                ins.offset,
637                                                BTRFS_ORDERED_COMPRESSED);
638                 BUG_ON(ret);
639
640                 btrfs_end_transaction(trans, root);
641
642                 /*
643                  * clear dirty, set writeback and unlock the pages.
644                  */
645                 extent_clear_unlock_delalloc(inode,
646                                              &BTRFS_I(inode)->io_tree,
647                                              async_extent->start,
648                                              async_extent->start +
649                                              async_extent->ram_size - 1,
650                                              NULL, 1, 1, 0, 1, 1, 0);
651
652                 ret = btrfs_submit_compressed_write(inode,
653                                          async_extent->start,
654                                          async_extent->ram_size,
655                                          ins.objectid,
656                                          ins.offset, async_extent->pages,
657                                          async_extent->nr_pages);
658
659                 BUG_ON(ret);
660                 trans = btrfs_join_transaction(root, 1);
661                 alloc_hint = ins.objectid + ins.offset;
662                 kfree(async_extent);
663                 cond_resched();
664         }
665
666         btrfs_end_transaction(trans, root);
667         return 0;
668 }
669
670 /*
671  * when extent_io.c finds a delayed allocation range in the file,
672  * the call backs end up in this code.  The basic idea is to
673  * allocate extents on disk for the range, and create ordered data structs
674  * in ram to track those extents.
675  *
676  * locked_page is the page that writepage had locked already.  We use
677  * it to make sure we don't do extra locks or unlocks.
678  *
679  * *page_started is set to one if we unlock locked_page and do everything
680  * required to start IO on it.  It may be clean and already done with
681  * IO when we return.
682  */
683 static noinline int cow_file_range(struct inode *inode,
684                                    struct page *locked_page,
685                                    u64 start, u64 end, int *page_started,
686                                    unsigned long *nr_written,
687                                    int unlock)
688 {
689         struct btrfs_root *root = BTRFS_I(inode)->root;
690         struct btrfs_trans_handle *trans;
691         u64 alloc_hint = 0;
692         u64 num_bytes;
693         unsigned long ram_size;
694         u64 disk_num_bytes;
695         u64 cur_alloc_size;
696         u64 blocksize = root->sectorsize;
697         u64 actual_end;
698         struct btrfs_key ins;
699         struct extent_map *em;
700         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
701         int ret = 0;
702
703         trans = btrfs_join_transaction(root, 1);
704         BUG_ON(!trans);
705         btrfs_set_trans_block_group(trans, inode);
706
707         actual_end = min_t(u64, i_size_read(inode), end + 1);
708
709         num_bytes = (end - start + blocksize) & ~(blocksize - 1);
710         num_bytes = max(blocksize,  num_bytes);
711         disk_num_bytes = num_bytes;
712         ret = 0;
713
714         if (start == 0) {
715                 /* lets try to make an inline extent */
716                 ret = cow_file_range_inline(trans, root, inode,
717                                             start, end, 0, NULL);
718                 if (ret == 0) {
719                         extent_clear_unlock_delalloc(inode,
720                                                      &BTRFS_I(inode)->io_tree,
721                                                      start, end, NULL, 1, 1,
722                                                      1, 1, 1, 1);
723                         *nr_written = *nr_written +
724                              (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
725                         *page_started = 1;
726                         ret = 0;
727                         goto out;
728                 }
729         }
730
731         BUG_ON(disk_num_bytes >
732                btrfs_super_total_bytes(&root->fs_info->super_copy));
733
734         btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
735
736         while(disk_num_bytes > 0) {
737                 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
738                 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
739                                            root->sectorsize, 0, alloc_hint,
740                                            (u64)-1, &ins, 1);
741                 if (ret) {
742                         BUG();
743                 }
744                 em = alloc_extent_map(GFP_NOFS);
745                 em->start = start;
746
747                 ram_size = ins.offset;
748                 em->len = ins.offset;
749
750                 em->block_start = ins.objectid;
751                 em->block_len = ins.offset;
752                 em->bdev = root->fs_info->fs_devices->latest_bdev;
753                 set_bit(EXTENT_FLAG_PINNED, &em->flags);
754
755                 while(1) {
756                         spin_lock(&em_tree->lock);
757                         ret = add_extent_mapping(em_tree, em);
758                         spin_unlock(&em_tree->lock);
759                         if (ret != -EEXIST) {
760                                 free_extent_map(em);
761                                 break;
762                         }
763                         btrfs_drop_extent_cache(inode, start,
764                                                 start + ram_size - 1, 0);
765                 }
766
767                 cur_alloc_size = ins.offset;
768                 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
769                                                ram_size, cur_alloc_size, 0);
770                 BUG_ON(ret);
771
772                 if (disk_num_bytes < cur_alloc_size) {
773                         printk("num_bytes %Lu cur_alloc %Lu\n", disk_num_bytes,
774                                cur_alloc_size);
775                         break;
776                 }
777                 /* we're not doing compressed IO, don't unlock the first
778                  * page (which the caller expects to stay locked), don't
779                  * clear any dirty bits and don't set any writeback bits
780                  */
781                 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
782                                              start, start + ram_size - 1,
783                                              locked_page, unlock, 1,
784                                              1, 0, 0, 0);
785                 disk_num_bytes -= cur_alloc_size;
786                 num_bytes -= cur_alloc_size;
787                 alloc_hint = ins.objectid + ins.offset;
788                 start += cur_alloc_size;
789         }
790 out:
791         ret = 0;
792         btrfs_end_transaction(trans, root);
793
794         return ret;
795 }
796
797 /*
798  * work queue call back to started compression on a file and pages
799  */
800 static noinline void async_cow_start(struct btrfs_work *work)
801 {
802         struct async_cow *async_cow;
803         int num_added = 0;
804         async_cow = container_of(work, struct async_cow, work);
805
806         compress_file_range(async_cow->inode, async_cow->locked_page,
807                             async_cow->start, async_cow->end, async_cow,
808                             &num_added);
809         if (num_added == 0)
810                 async_cow->inode = NULL;
811 }
812
813 /*
814  * work queue call back to submit previously compressed pages
815  */
816 static noinline void async_cow_submit(struct btrfs_work *work)
817 {
818         struct async_cow *async_cow;
819         struct btrfs_root *root;
820         unsigned long nr_pages;
821
822         async_cow = container_of(work, struct async_cow, work);
823
824         root = async_cow->root;
825         nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
826                 PAGE_CACHE_SHIFT;
827
828         atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
829
830         if (atomic_read(&root->fs_info->async_delalloc_pages) <
831             5 * 1042 * 1024 &&
832             waitqueue_active(&root->fs_info->async_submit_wait))
833                 wake_up(&root->fs_info->async_submit_wait);
834
835         if (async_cow->inode) {
836                 submit_compressed_extents(async_cow->inode, async_cow);
837         }
838 }
839
840 static noinline void async_cow_free(struct btrfs_work *work)
841 {
842         struct async_cow *async_cow;
843         async_cow = container_of(work, struct async_cow, work);
844         kfree(async_cow);
845 }
846
847 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
848                                 u64 start, u64 end, int *page_started,
849                                 unsigned long *nr_written)
850 {
851         struct async_cow *async_cow;
852         struct btrfs_root *root = BTRFS_I(inode)->root;
853         unsigned long nr_pages;
854         u64 cur_end;
855         int limit = 10 * 1024 * 1042;
856
857         if (!btrfs_test_opt(root, COMPRESS)) {
858                 return cow_file_range(inode, locked_page, start, end,
859                                       page_started, nr_written, 1);
860         }
861
862         clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED |
863                          EXTENT_DELALLOC, 1, 0, GFP_NOFS);
864         while(start < end) {
865                 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
866                 async_cow->inode = inode;
867                 async_cow->root = root;
868                 async_cow->locked_page = locked_page;
869                 async_cow->start = start;
870
871                 if (btrfs_test_flag(inode, NOCOMPRESS))
872                         cur_end = end;
873                 else
874                         cur_end = min(end, start + 512 * 1024 - 1);
875
876                 async_cow->end = cur_end;
877                 INIT_LIST_HEAD(&async_cow->extents);
878
879                 async_cow->work.func = async_cow_start;
880                 async_cow->work.ordered_func = async_cow_submit;
881                 async_cow->work.ordered_free = async_cow_free;
882                 async_cow->work.flags = 0;
883
884                 while(atomic_read(&root->fs_info->async_submit_draining) &&
885                       atomic_read(&root->fs_info->async_delalloc_pages)) {
886                         wait_event(root->fs_info->async_submit_wait,
887                              (atomic_read(&root->fs_info->async_delalloc_pages)
888                               == 0));
889                 }
890
891                 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
892                         PAGE_CACHE_SHIFT;
893                 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
894
895                 btrfs_queue_worker(&root->fs_info->delalloc_workers,
896                                    &async_cow->work);
897
898                 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
899                         wait_event(root->fs_info->async_submit_wait,
900                            (atomic_read(&root->fs_info->async_delalloc_pages) <
901                             limit));
902                 }
903
904                 while(atomic_read(&root->fs_info->async_submit_draining) &&
905                       atomic_read(&root->fs_info->async_delalloc_pages)) {
906                         wait_event(root->fs_info->async_submit_wait,
907                           (atomic_read(&root->fs_info->async_delalloc_pages) ==
908                            0));
909                 }
910
911                 *nr_written += nr_pages;
912                 start = cur_end + 1;
913         }
914         *page_started = 1;
915         return 0;
916 }
917
918 /*
919  * when nowcow writeback call back.  This checks for snapshots or COW copies
920  * of the extents that exist in the file, and COWs the file as required.
921  *
922  * If no cow copies or snapshots exist, we write directly to the existing
923  * blocks on disk
924  */
925 static int run_delalloc_nocow(struct inode *inode, struct page *locked_page,
926                               u64 start, u64 end, int *page_started, int force,
927                               unsigned long *nr_written)
928 {
929         struct btrfs_root *root = BTRFS_I(inode)->root;
930         struct btrfs_trans_handle *trans;
931         struct extent_buffer *leaf;
932         struct btrfs_path *path;
933         struct btrfs_file_extent_item *fi;
934         struct btrfs_key found_key;
935         u64 cow_start;
936         u64 cur_offset;
937         u64 extent_end;
938         u64 disk_bytenr;
939         u64 num_bytes;
940         int extent_type;
941         int ret;
942         int type;
943         int nocow;
944         int check_prev = 1;
945
946         path = btrfs_alloc_path();
947         BUG_ON(!path);
948         trans = btrfs_join_transaction(root, 1);
949         BUG_ON(!trans);
950
951         cow_start = (u64)-1;
952         cur_offset = start;
953         while (1) {
954                 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
955                                                cur_offset, 0);
956                 BUG_ON(ret < 0);
957                 if (ret > 0 && path->slots[0] > 0 && check_prev) {
958                         leaf = path->nodes[0];
959                         btrfs_item_key_to_cpu(leaf, &found_key,
960                                               path->slots[0] - 1);
961                         if (found_key.objectid == inode->i_ino &&
962                             found_key.type == BTRFS_EXTENT_DATA_KEY)
963                                 path->slots[0]--;
964                 }
965                 check_prev = 0;
966 next_slot:
967                 leaf = path->nodes[0];
968                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
969                         ret = btrfs_next_leaf(root, path);
970                         if (ret < 0)
971                                 BUG_ON(1);
972                         if (ret > 0)
973                                 break;
974                         leaf = path->nodes[0];
975                 }
976
977                 nocow = 0;
978                 disk_bytenr = 0;
979                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
980
981                 if (found_key.objectid > inode->i_ino ||
982                     found_key.type > BTRFS_EXTENT_DATA_KEY ||
983                     found_key.offset > end)
984                         break;
985
986                 if (found_key.offset > cur_offset) {
987                         extent_end = found_key.offset;
988                         goto out_check;
989                 }
990
991                 fi = btrfs_item_ptr(leaf, path->slots[0],
992                                     struct btrfs_file_extent_item);
993                 extent_type = btrfs_file_extent_type(leaf, fi);
994
995                 if (extent_type == BTRFS_FILE_EXTENT_REG ||
996                     extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
997                         struct btrfs_block_group_cache *block_group;
998                         disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
999                         extent_end = found_key.offset +
1000                                 btrfs_file_extent_num_bytes(leaf, fi);
1001                         if (extent_end <= start) {
1002                                 path->slots[0]++;
1003                                 goto next_slot;
1004                         }
1005                         if (btrfs_file_extent_compression(leaf, fi) ||
1006                             btrfs_file_extent_encryption(leaf, fi) ||
1007                             btrfs_file_extent_other_encoding(leaf, fi))
1008                                 goto out_check;
1009                         if (disk_bytenr == 0)
1010                                 goto out_check;
1011                         if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1012                                 goto out_check;
1013                         if (btrfs_cross_ref_exist(trans, root, disk_bytenr))
1014                                 goto out_check;
1015                         block_group = btrfs_lookup_block_group(root->fs_info,
1016                                                                disk_bytenr);
1017                         if (!block_group || block_group->ro)
1018                                 goto out_check;
1019                         disk_bytenr += btrfs_file_extent_offset(leaf, fi);
1020                         nocow = 1;
1021                 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1022                         extent_end = found_key.offset +
1023                                 btrfs_file_extent_inline_len(leaf, fi);
1024                         extent_end = ALIGN(extent_end, root->sectorsize);
1025                 } else {
1026                         BUG_ON(1);
1027                 }
1028 out_check:
1029                 if (extent_end <= start) {
1030                         path->slots[0]++;
1031                         goto next_slot;
1032                 }
1033                 if (!nocow) {
1034                         if (cow_start == (u64)-1)
1035                                 cow_start = cur_offset;
1036                         cur_offset = extent_end;
1037                         if (cur_offset > end)
1038                                 break;
1039                         path->slots[0]++;
1040                         goto next_slot;
1041                 }
1042
1043                 btrfs_release_path(root, path);
1044                 if (cow_start != (u64)-1) {
1045                         ret = cow_file_range(inode, locked_page, cow_start,
1046                                         found_key.offset - 1, page_started,
1047                                         nr_written, 1);
1048                         BUG_ON(ret);
1049                         cow_start = (u64)-1;
1050                 }
1051
1052                 disk_bytenr += cur_offset - found_key.offset;
1053                 num_bytes = min(end + 1, extent_end) - cur_offset;
1054                 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1055                         struct extent_map *em;
1056                         struct extent_map_tree *em_tree;
1057                         em_tree = &BTRFS_I(inode)->extent_tree;
1058                         em = alloc_extent_map(GFP_NOFS);
1059                         em->start = cur_offset;
1060                         em->len = num_bytes;
1061                         em->block_len = num_bytes;
1062                         em->block_start = disk_bytenr;
1063                         em->bdev = root->fs_info->fs_devices->latest_bdev;
1064                         set_bit(EXTENT_FLAG_PINNED, &em->flags);
1065                         while (1) {
1066                                 spin_lock(&em_tree->lock);
1067                                 ret = add_extent_mapping(em_tree, em);
1068                                 spin_unlock(&em_tree->lock);
1069                                 if (ret != -EEXIST) {
1070                                         free_extent_map(em);
1071                                         break;
1072                                 }
1073                                 btrfs_drop_extent_cache(inode, em->start,
1074                                                 em->start + em->len - 1, 0);
1075                         }
1076                         type = BTRFS_ORDERED_PREALLOC;
1077                 } else {
1078                         type = BTRFS_ORDERED_NOCOW;
1079                 }
1080
1081                 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1082                                                num_bytes, num_bytes, type);
1083                 BUG_ON(ret);
1084
1085                 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1086                                         cur_offset, cur_offset + num_bytes - 1,
1087                                         locked_page, 1, 1, 1, 0, 0, 0);
1088                 cur_offset = extent_end;
1089                 if (cur_offset > end)
1090                         break;
1091         }
1092         btrfs_release_path(root, path);
1093
1094         if (cur_offset <= end && cow_start == (u64)-1)
1095                 cow_start = cur_offset;
1096         if (cow_start != (u64)-1) {
1097                 ret = cow_file_range(inode, locked_page, cow_start, end,
1098                                      page_started, nr_written, 1);
1099                 BUG_ON(ret);
1100         }
1101
1102         ret = btrfs_end_transaction(trans, root);
1103         BUG_ON(ret);
1104         btrfs_free_path(path);
1105         return 0;
1106 }
1107
1108 /*
1109  * extent_io.c call back to do delayed allocation processing
1110  */
1111 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1112                               u64 start, u64 end, int *page_started,
1113                               unsigned long *nr_written)
1114 {
1115         struct btrfs_root *root = BTRFS_I(inode)->root;
1116         int ret;
1117
1118         if (btrfs_test_opt(root, NODATACOW) ||
1119             btrfs_test_flag(inode, NODATACOW))
1120                 ret = run_delalloc_nocow(inode, locked_page, start, end,
1121                                          page_started, 0, nr_written);
1122         else if (btrfs_test_flag(inode, PREALLOC))
1123                 ret = run_delalloc_nocow(inode, locked_page, start, end,
1124                                          page_started, 1, nr_written);
1125         else
1126                 ret = cow_file_range_async(inode, locked_page, start, end,
1127                                      page_started, nr_written);
1128
1129         return ret;
1130 }
1131
1132 /*
1133  * extent_io.c set_bit_hook, used to track delayed allocation
1134  * bytes in this file, and to maintain the list of inodes that
1135  * have pending delalloc work to be done.
1136  */
1137 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1138                        unsigned long old, unsigned long bits)
1139 {
1140         unsigned long flags;
1141         if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1142                 struct btrfs_root *root = BTRFS_I(inode)->root;
1143                 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
1144                 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
1145                 root->fs_info->delalloc_bytes += end - start + 1;
1146                 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1147                         list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1148                                       &root->fs_info->delalloc_inodes);
1149                 }
1150                 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
1151         }
1152         return 0;
1153 }
1154
1155 /*
1156  * extent_io.c clear_bit_hook, see set_bit_hook for why
1157  */
1158 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
1159                          unsigned long old, unsigned long bits)
1160 {
1161         if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1162                 struct btrfs_root *root = BTRFS_I(inode)->root;
1163                 unsigned long flags;
1164
1165                 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
1166                 if (end - start + 1 > root->fs_info->delalloc_bytes) {
1167                         printk("warning: delalloc account %Lu %Lu\n",
1168                                end - start + 1, root->fs_info->delalloc_bytes);
1169                         root->fs_info->delalloc_bytes = 0;
1170                         BTRFS_I(inode)->delalloc_bytes = 0;
1171                 } else {
1172                         root->fs_info->delalloc_bytes -= end - start + 1;
1173                         BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
1174                 }
1175                 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1176                     !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1177                         list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1178                 }
1179                 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
1180         }
1181         return 0;
1182 }
1183
1184 /*
1185  * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1186  * we don't create bios that span stripes or chunks
1187  */
1188 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1189                          size_t size, struct bio *bio,
1190                          unsigned long bio_flags)
1191 {
1192         struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1193         struct btrfs_mapping_tree *map_tree;
1194         u64 logical = (u64)bio->bi_sector << 9;
1195         u64 length = 0;
1196         u64 map_length;
1197         int ret;
1198
1199         if (bio_flags & EXTENT_BIO_COMPRESSED)
1200                 return 0;
1201
1202         length = bio->bi_size;
1203         map_tree = &root->fs_info->mapping_tree;
1204         map_length = length;
1205         ret = btrfs_map_block(map_tree, READ, logical,
1206                               &map_length, NULL, 0);
1207
1208         if (map_length < length + size) {
1209                 return 1;
1210         }
1211         return 0;
1212 }
1213
1214 /*
1215  * in order to insert checksums into the metadata in large chunks,
1216  * we wait until bio submission time.   All the pages in the bio are
1217  * checksummed and sums are attached onto the ordered extent record.
1218  *
1219  * At IO completion time the cums attached on the ordered extent record
1220  * are inserted into the btree
1221  */
1222 int __btrfs_submit_bio_start(struct inode *inode, int rw, struct bio *bio,
1223                           int mirror_num, unsigned long bio_flags)
1224 {
1225         struct btrfs_root *root = BTRFS_I(inode)->root;
1226         int ret = 0;
1227
1228         ret = btrfs_csum_one_bio(root, inode, bio);
1229         BUG_ON(ret);
1230         return 0;
1231 }
1232
1233 /*
1234  * in order to insert checksums into the metadata in large chunks,
1235  * we wait until bio submission time.   All the pages in the bio are
1236  * checksummed and sums are attached onto the ordered extent record.
1237  *
1238  * At IO completion time the cums attached on the ordered extent record
1239  * are inserted into the btree
1240  */
1241 int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1242                           int mirror_num, unsigned long bio_flags)
1243 {
1244         struct btrfs_root *root = BTRFS_I(inode)->root;
1245         return btrfs_map_bio(root, rw, bio, mirror_num, 1);
1246 }
1247
1248 /*
1249  * extent_io.c submission hook. This does the right thing for csum calculation on write,
1250  * or reading the csums from the tree before a read
1251  */
1252 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1253                           int mirror_num, unsigned long bio_flags)
1254 {
1255         struct btrfs_root *root = BTRFS_I(inode)->root;
1256         int ret = 0;
1257         int skip_sum;
1258
1259         ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1260         BUG_ON(ret);
1261
1262         skip_sum = btrfs_test_opt(root, NODATASUM) ||
1263                 btrfs_test_flag(inode, NODATASUM);
1264
1265         if (!(rw & (1 << BIO_RW))) {
1266
1267                 if (bio_flags & EXTENT_BIO_COMPRESSED)
1268                         return btrfs_submit_compressed_read(inode, bio,
1269                                                     mirror_num, bio_flags);
1270                 else if (!skip_sum)
1271                         btrfs_lookup_bio_sums(root, inode, bio);
1272                 goto mapit;
1273         } else if (!skip_sum) {
1274                 /* we're doing a write, do the async checksumming */
1275                 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1276                                    inode, rw, bio, mirror_num,
1277                                    bio_flags, __btrfs_submit_bio_start,
1278                                    __btrfs_submit_bio_done);
1279         }
1280
1281 mapit:
1282         return btrfs_map_bio(root, rw, bio, mirror_num, 0);
1283 }
1284
1285 /*
1286  * given a list of ordered sums record them in the inode.  This happens
1287  * at IO completion time based on sums calculated at bio submission time.
1288  */
1289 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1290                              struct inode *inode, u64 file_offset,
1291                              struct list_head *list)
1292 {
1293         struct list_head *cur;
1294         struct btrfs_ordered_sum *sum;
1295
1296         btrfs_set_trans_block_group(trans, inode);
1297         list_for_each(cur, list) {
1298                 sum = list_entry(cur, struct btrfs_ordered_sum, list);
1299                 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
1300                                        inode, sum);
1301         }
1302         return 0;
1303 }
1304
1305 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1306 {
1307         if ((end & (PAGE_CACHE_SIZE - 1)) == 0) {
1308                 WARN_ON(1);
1309         }
1310         return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1311                                    GFP_NOFS);
1312 }
1313
1314 /* see btrfs_writepage_start_hook for details on why this is required */
1315 struct btrfs_writepage_fixup {
1316         struct page *page;
1317         struct btrfs_work work;
1318 };
1319
1320 void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1321 {
1322         struct btrfs_writepage_fixup *fixup;
1323         struct btrfs_ordered_extent *ordered;
1324         struct page *page;
1325         struct inode *inode;
1326         u64 page_start;
1327         u64 page_end;
1328
1329         fixup = container_of(work, struct btrfs_writepage_fixup, work);
1330         page = fixup->page;
1331 again:
1332         lock_page(page);
1333         if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1334                 ClearPageChecked(page);
1335                 goto out_page;
1336         }
1337
1338         inode = page->mapping->host;
1339         page_start = page_offset(page);
1340         page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1341
1342         lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1343
1344         /* already ordered? We're done */
1345         if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
1346                              EXTENT_ORDERED, 0)) {
1347                 goto out;
1348         }
1349
1350         ordered = btrfs_lookup_ordered_extent(inode, page_start);
1351         if (ordered) {
1352                 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1353                               page_end, GFP_NOFS);
1354                 unlock_page(page);
1355                 btrfs_start_ordered_extent(inode, ordered, 1);
1356                 goto again;
1357         }
1358
1359         btrfs_set_extent_delalloc(inode, page_start, page_end);
1360         ClearPageChecked(page);
1361 out:
1362         unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1363 out_page:
1364         unlock_page(page);
1365         page_cache_release(page);
1366 }
1367
1368 /*
1369  * There are a few paths in the higher layers of the kernel that directly
1370  * set the page dirty bit without asking the filesystem if it is a
1371  * good idea.  This causes problems because we want to make sure COW
1372  * properly happens and the data=ordered rules are followed.
1373  *
1374  * In our case any range that doesn't have the ORDERED bit set
1375  * hasn't been properly setup for IO.  We kick off an async process
1376  * to fix it up.  The async helper will wait for ordered extents, set
1377  * the delalloc bit and make it safe to write the page.
1378  */
1379 int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1380 {
1381         struct inode *inode = page->mapping->host;
1382         struct btrfs_writepage_fixup *fixup;
1383         struct btrfs_root *root = BTRFS_I(inode)->root;
1384         int ret;
1385
1386         ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1387                              EXTENT_ORDERED, 0);
1388         if (ret)
1389                 return 0;
1390
1391         if (PageChecked(page))
1392                 return -EAGAIN;
1393
1394         fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1395         if (!fixup)
1396                 return -EAGAIN;
1397
1398         SetPageChecked(page);
1399         page_cache_get(page);
1400         fixup->work.func = btrfs_writepage_fixup_worker;
1401         fixup->page = page;
1402         btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1403         return -EAGAIN;
1404 }
1405
1406 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1407                                        struct inode *inode, u64 file_pos,
1408                                        u64 disk_bytenr, u64 disk_num_bytes,
1409                                        u64 num_bytes, u64 ram_bytes,
1410                                        u8 compression, u8 encryption,
1411                                        u16 other_encoding, int extent_type)
1412 {
1413         struct btrfs_root *root = BTRFS_I(inode)->root;
1414         struct btrfs_file_extent_item *fi;
1415         struct btrfs_path *path;
1416         struct extent_buffer *leaf;
1417         struct btrfs_key ins;
1418         u64 hint;
1419         int ret;
1420
1421         path = btrfs_alloc_path();
1422         BUG_ON(!path);
1423
1424         ret = btrfs_drop_extents(trans, root, inode, file_pos,
1425                                  file_pos + num_bytes, file_pos, &hint);
1426         BUG_ON(ret);
1427
1428         ins.objectid = inode->i_ino;
1429         ins.offset = file_pos;
1430         ins.type = BTRFS_EXTENT_DATA_KEY;
1431         ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1432         BUG_ON(ret);
1433         leaf = path->nodes[0];
1434         fi = btrfs_item_ptr(leaf, path->slots[0],
1435                             struct btrfs_file_extent_item);
1436         btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1437         btrfs_set_file_extent_type(leaf, fi, extent_type);
1438         btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1439         btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1440         btrfs_set_file_extent_offset(leaf, fi, 0);
1441         btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1442         btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1443         btrfs_set_file_extent_compression(leaf, fi, compression);
1444         btrfs_set_file_extent_encryption(leaf, fi, encryption);
1445         btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1446         btrfs_mark_buffer_dirty(leaf);
1447
1448         inode_add_bytes(inode, num_bytes);
1449         btrfs_drop_extent_cache(inode, file_pos, file_pos + num_bytes - 1, 0);
1450
1451         ins.objectid = disk_bytenr;
1452         ins.offset = disk_num_bytes;
1453         ins.type = BTRFS_EXTENT_ITEM_KEY;
1454         ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
1455                                           root->root_key.objectid,
1456                                           trans->transid, inode->i_ino, &ins);
1457         BUG_ON(ret);
1458
1459         btrfs_free_path(path);
1460         return 0;
1461 }
1462
1463 /* as ordered data IO finishes, this gets called so we can finish
1464  * an ordered extent if the range of bytes in the file it covers are
1465  * fully written.
1466  */
1467 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1468 {
1469         struct btrfs_root *root = BTRFS_I(inode)->root;
1470         struct btrfs_trans_handle *trans;
1471         struct btrfs_ordered_extent *ordered_extent;
1472         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1473         int compressed = 0;
1474         int ret;
1475
1476         ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
1477         if (!ret)
1478                 return 0;
1479
1480         trans = btrfs_join_transaction(root, 1);
1481
1482         ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1483         BUG_ON(!ordered_extent);
1484         if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
1485                 goto nocow;
1486
1487         lock_extent(io_tree, ordered_extent->file_offset,
1488                     ordered_extent->file_offset + ordered_extent->len - 1,
1489                     GFP_NOFS);
1490
1491         if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1492                 compressed = 1;
1493         if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1494                 BUG_ON(compressed);
1495                 ret = btrfs_mark_extent_written(trans, root, inode,
1496                                                 ordered_extent->file_offset,
1497                                                 ordered_extent->file_offset +
1498                                                 ordered_extent->len);
1499                 BUG_ON(ret);
1500         } else {
1501                 ret = insert_reserved_file_extent(trans, inode,
1502                                                 ordered_extent->file_offset,
1503                                                 ordered_extent->start,
1504                                                 ordered_extent->disk_len,
1505                                                 ordered_extent->len,
1506                                                 ordered_extent->len,
1507                                                 compressed, 0, 0,
1508                                                 BTRFS_FILE_EXTENT_REG);
1509                 BUG_ON(ret);
1510         }
1511         unlock_extent(io_tree, ordered_extent->file_offset,
1512                     ordered_extent->file_offset + ordered_extent->len - 1,
1513                     GFP_NOFS);
1514 nocow:
1515         add_pending_csums(trans, inode, ordered_extent->file_offset,
1516                           &ordered_extent->list);
1517
1518         mutex_lock(&BTRFS_I(inode)->extent_mutex);
1519         btrfs_ordered_update_i_size(inode, ordered_extent);
1520         btrfs_update_inode(trans, root, inode);
1521         btrfs_remove_ordered_extent(inode, ordered_extent);
1522         mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1523
1524         /* once for us */
1525         btrfs_put_ordered_extent(ordered_extent);
1526         /* once for the tree */
1527         btrfs_put_ordered_extent(ordered_extent);
1528
1529         btrfs_end_transaction(trans, root);
1530         return 0;
1531 }
1532
1533 int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1534                                 struct extent_state *state, int uptodate)
1535 {
1536         return btrfs_finish_ordered_io(page->mapping->host, start, end);
1537 }
1538
1539 /*
1540  * When IO fails, either with EIO or csum verification fails, we
1541  * try other mirrors that might have a good copy of the data.  This
1542  * io_failure_record is used to record state as we go through all the
1543  * mirrors.  If another mirror has good data, the page is set up to date
1544  * and things continue.  If a good mirror can't be found, the original
1545  * bio end_io callback is called to indicate things have failed.
1546  */
1547 struct io_failure_record {
1548         struct page *page;
1549         u64 start;
1550         u64 len;
1551         u64 logical;
1552         int last_mirror;
1553 };
1554
1555 int btrfs_io_failed_hook(struct bio *failed_bio,
1556                          struct page *page, u64 start, u64 end,
1557                          struct extent_state *state)
1558 {
1559         struct io_failure_record *failrec = NULL;
1560         u64 private;
1561         struct extent_map *em;
1562         struct inode *inode = page->mapping->host;
1563         struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1564         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1565         struct bio *bio;
1566         int num_copies;
1567         int ret;
1568         int rw;
1569         u64 logical;
1570         unsigned long bio_flags = 0;
1571
1572         ret = get_state_private(failure_tree, start, &private);
1573         if (ret) {
1574                 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1575                 if (!failrec)
1576                         return -ENOMEM;
1577                 failrec->start = start;
1578                 failrec->len = end - start + 1;
1579                 failrec->last_mirror = 0;
1580
1581                 spin_lock(&em_tree->lock);
1582                 em = lookup_extent_mapping(em_tree, start, failrec->len);
1583                 if (em->start > start || em->start + em->len < start) {
1584                         free_extent_map(em);
1585                         em = NULL;
1586                 }
1587                 spin_unlock(&em_tree->lock);
1588
1589                 if (!em || IS_ERR(em)) {
1590                         kfree(failrec);
1591                         return -EIO;
1592                 }
1593                 logical = start - em->start;
1594                 logical = em->block_start + logical;
1595                 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
1596                         bio_flags = EXTENT_BIO_COMPRESSED;
1597                 failrec->logical = logical;
1598                 free_extent_map(em);
1599                 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1600                                 EXTENT_DIRTY, GFP_NOFS);
1601                 set_state_private(failure_tree, start,
1602                                  (u64)(unsigned long)failrec);
1603         } else {
1604                 failrec = (struct io_failure_record *)(unsigned long)private;
1605         }
1606         num_copies = btrfs_num_copies(
1607                               &BTRFS_I(inode)->root->fs_info->mapping_tree,
1608                               failrec->logical, failrec->len);
1609         failrec->last_mirror++;
1610         if (!state) {
1611                 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
1612                 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1613                                                     failrec->start,
1614                                                     EXTENT_LOCKED);
1615                 if (state && state->start != failrec->start)
1616                         state = NULL;
1617                 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
1618         }
1619         if (!state || failrec->last_mirror > num_copies) {
1620                 set_state_private(failure_tree, failrec->start, 0);
1621                 clear_extent_bits(failure_tree, failrec->start,
1622                                   failrec->start + failrec->len - 1,
1623                                   EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1624                 kfree(failrec);
1625                 return -EIO;
1626         }
1627         bio = bio_alloc(GFP_NOFS, 1);
1628         bio->bi_private = state;
1629         bio->bi_end_io = failed_bio->bi_end_io;
1630         bio->bi_sector = failrec->logical >> 9;
1631         bio->bi_bdev = failed_bio->bi_bdev;
1632         bio->bi_size = 0;
1633         bio_add_page(bio, page, failrec->len, start - page_offset(page));
1634         if (failed_bio->bi_rw & (1 << BIO_RW))
1635                 rw = WRITE;
1636         else
1637                 rw = READ;
1638
1639         BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
1640                                                       failrec->last_mirror,
1641                                                       bio_flags);
1642         return 0;
1643 }
1644
1645 /*
1646  * each time an IO finishes, we do a fast check in the IO failure tree
1647  * to see if we need to process or clean up an io_failure_record
1648  */
1649 int btrfs_clean_io_failures(struct inode *inode, u64 start)
1650 {
1651         u64 private;
1652         u64 private_failure;
1653         struct io_failure_record *failure;
1654         int ret;
1655
1656         private = 0;
1657         if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1658                              (u64)-1, 1, EXTENT_DIRTY)) {
1659                 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1660                                         start, &private_failure);
1661                 if (ret == 0) {
1662                         failure = (struct io_failure_record *)(unsigned long)
1663                                    private_failure;
1664                         set_state_private(&BTRFS_I(inode)->io_failure_tree,
1665                                           failure->start, 0);
1666                         clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1667                                           failure->start,
1668                                           failure->start + failure->len - 1,
1669                                           EXTENT_DIRTY | EXTENT_LOCKED,
1670                                           GFP_NOFS);
1671                         kfree(failure);
1672                 }
1673         }
1674         return 0;
1675 }
1676
1677 /*
1678  * when reads are done, we need to check csums to verify the data is correct
1679  * if there's a match, we allow the bio to finish.  If not, we go through
1680  * the io_failure_record routines to find good copies
1681  */
1682 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1683                                struct extent_state *state)
1684 {
1685         size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
1686         struct inode *inode = page->mapping->host;
1687         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1688         char *kaddr;
1689         u64 private = ~(u32)0;
1690         int ret;
1691         struct btrfs_root *root = BTRFS_I(inode)->root;
1692         u32 csum = ~(u32)0;
1693         unsigned long flags;
1694
1695         if (btrfs_test_opt(root, NODATASUM) ||
1696             btrfs_test_flag(inode, NODATASUM))
1697                 return 0;
1698         if (state && state->start == start) {
1699                 private = state->private;
1700                 ret = 0;
1701         } else {
1702                 ret = get_state_private(io_tree, start, &private);
1703         }
1704         local_irq_save(flags);
1705         kaddr = kmap_atomic(page, KM_IRQ0);
1706         if (ret) {
1707                 goto zeroit;
1708         }
1709         csum = btrfs_csum_data(root, kaddr + offset, csum,  end - start + 1);
1710         btrfs_csum_final(csum, (char *)&csum);
1711         if (csum != private) {
1712                 goto zeroit;
1713         }
1714         kunmap_atomic(kaddr, KM_IRQ0);
1715         local_irq_restore(flags);
1716
1717         /* if the io failure tree for this inode is non-empty,
1718          * check to see if we've recovered from a failed IO
1719          */
1720         btrfs_clean_io_failures(inode, start);
1721         return 0;
1722
1723 zeroit:
1724         printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
1725                page->mapping->host->i_ino, (unsigned long long)start, csum,
1726                private);
1727         memset(kaddr + offset, 1, end - start + 1);
1728         flush_dcache_page(page);
1729         kunmap_atomic(kaddr, KM_IRQ0);
1730         local_irq_restore(flags);
1731         if (private == 0)
1732                 return 0;
1733         return -EIO;
1734 }
1735
1736 /*
1737  * This creates an orphan entry for the given inode in case something goes
1738  * wrong in the middle of an unlink/truncate.
1739  */
1740 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
1741 {
1742         struct btrfs_root *root = BTRFS_I(inode)->root;
1743         int ret = 0;
1744
1745         spin_lock(&root->list_lock);
1746
1747         /* already on the orphan list, we're good */
1748         if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
1749                 spin_unlock(&root->list_lock);
1750                 return 0;
1751         }
1752
1753         list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1754
1755         spin_unlock(&root->list_lock);
1756
1757         /*
1758          * insert an orphan item to track this unlinked/truncated file
1759          */
1760         ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
1761
1762         return ret;
1763 }
1764
1765 /*
1766  * We have done the truncate/delete so we can go ahead and remove the orphan
1767  * item for this particular inode.
1768  */
1769 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
1770 {
1771         struct btrfs_root *root = BTRFS_I(inode)->root;
1772         int ret = 0;
1773
1774         spin_lock(&root->list_lock);
1775
1776         if (list_empty(&BTRFS_I(inode)->i_orphan)) {
1777                 spin_unlock(&root->list_lock);
1778                 return 0;
1779         }
1780
1781         list_del_init(&BTRFS_I(inode)->i_orphan);
1782         if (!trans) {
1783                 spin_unlock(&root->list_lock);
1784                 return 0;
1785         }
1786
1787         spin_unlock(&root->list_lock);
1788
1789         ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
1790
1791         return ret;
1792 }
1793
1794 /*
1795  * this cleans up any orphans that may be left on the list from the last use
1796  * of this root.
1797  */
1798 void btrfs_orphan_cleanup(struct btrfs_root *root)
1799 {
1800         struct btrfs_path *path;
1801         struct extent_buffer *leaf;
1802         struct btrfs_item *item;
1803         struct btrfs_key key, found_key;
1804         struct btrfs_trans_handle *trans;
1805         struct inode *inode;
1806         int ret = 0, nr_unlink = 0, nr_truncate = 0;
1807
1808         /* don't do orphan cleanup if the fs is readonly. */
1809         if (root->fs_info->sb->s_flags & MS_RDONLY)
1810                 return;
1811
1812         path = btrfs_alloc_path();
1813         if (!path)
1814                 return;
1815         path->reada = -1;
1816
1817         key.objectid = BTRFS_ORPHAN_OBJECTID;
1818         btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
1819         key.offset = (u64)-1;
1820
1821
1822         while (1) {
1823                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1824                 if (ret < 0) {
1825                         printk(KERN_ERR "Error searching slot for orphan: %d"
1826                                "\n", ret);
1827                         break;
1828                 }
1829
1830                 /*
1831                  * if ret == 0 means we found what we were searching for, which
1832                  * is weird, but possible, so only screw with path if we didnt
1833                  * find the key and see if we have stuff that matches
1834                  */
1835                 if (ret > 0) {
1836                         if (path->slots[0] == 0)
1837                                 break;
1838                         path->slots[0]--;
1839                 }
1840
1841                 /* pull out the item */
1842                 leaf = path->nodes[0];
1843                 item = btrfs_item_nr(leaf, path->slots[0]);
1844                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1845
1846                 /* make sure the item matches what we want */
1847                 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
1848                         break;
1849                 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
1850                         break;
1851
1852                 /* release the path since we're done with it */
1853                 btrfs_release_path(root, path);
1854
1855                 /*
1856                  * this is where we are basically btrfs_lookup, without the
1857                  * crossing root thing.  we store the inode number in the
1858                  * offset of the orphan item.
1859                  */
1860                 inode = btrfs_iget_locked(root->fs_info->sb,
1861                                           found_key.offset, root);
1862                 if (!inode)
1863                         break;
1864
1865                 if (inode->i_state & I_NEW) {
1866                         BTRFS_I(inode)->root = root;
1867
1868                         /* have to set the location manually */
1869                         BTRFS_I(inode)->location.objectid = inode->i_ino;
1870                         BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1871                         BTRFS_I(inode)->location.offset = 0;
1872
1873                         btrfs_read_locked_inode(inode);
1874                         unlock_new_inode(inode);
1875                 }
1876
1877                 /*
1878                  * add this inode to the orphan list so btrfs_orphan_del does
1879                  * the proper thing when we hit it
1880                  */
1881                 spin_lock(&root->list_lock);
1882                 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
1883                 spin_unlock(&root->list_lock);
1884
1885                 /*
1886                  * if this is a bad inode, means we actually succeeded in
1887                  * removing the inode, but not the orphan record, which means
1888                  * we need to manually delete the orphan since iput will just
1889                  * do a destroy_inode
1890                  */
1891                 if (is_bad_inode(inode)) {
1892                         trans = btrfs_start_transaction(root, 1);
1893                         btrfs_orphan_del(trans, inode);
1894                         btrfs_end_transaction(trans, root);
1895                         iput(inode);
1896                         continue;
1897                 }
1898
1899                 /* if we have links, this was a truncate, lets do that */
1900                 if (inode->i_nlink) {
1901                         nr_truncate++;
1902                         btrfs_truncate(inode);
1903                 } else {
1904                         nr_unlink++;
1905                 }
1906
1907                 /* this will do delete_inode and everything for us */
1908                 iput(inode);
1909         }
1910
1911         if (nr_unlink)
1912                 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1913         if (nr_truncate)
1914                 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1915
1916         btrfs_free_path(path);
1917 }
1918
1919 /*
1920  * read an inode from the btree into the in-memory inode
1921  */
1922 void btrfs_read_locked_inode(struct inode *inode)
1923 {
1924         struct btrfs_path *path;
1925         struct extent_buffer *leaf;
1926         struct btrfs_inode_item *inode_item;
1927         struct btrfs_timespec *tspec;
1928         struct btrfs_root *root = BTRFS_I(inode)->root;
1929         struct btrfs_key location;
1930         u64 alloc_group_block;
1931         u32 rdev;
1932         int ret;
1933
1934         path = btrfs_alloc_path();
1935         BUG_ON(!path);
1936         memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
1937
1938         ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
1939         if (ret)
1940                 goto make_bad;
1941
1942         leaf = path->nodes[0];
1943         inode_item = btrfs_item_ptr(leaf, path->slots[0],
1944                                     struct btrfs_inode_item);
1945
1946         inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1947         inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1948         inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1949         inode->i_gid = btrfs_inode_gid(leaf, inode_item);
1950         btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
1951
1952         tspec = btrfs_inode_atime(inode_item);
1953         inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1954         inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1955
1956         tspec = btrfs_inode_mtime(inode_item);
1957         inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1958         inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1959
1960         tspec = btrfs_inode_ctime(inode_item);
1961         inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1962         inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1963
1964         inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
1965         BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
1966         inode->i_generation = BTRFS_I(inode)->generation;
1967         inode->i_rdev = 0;
1968         rdev = btrfs_inode_rdev(leaf, inode_item);
1969
1970         BTRFS_I(inode)->index_cnt = (u64)-1;
1971
1972         alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
1973         BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1974                                                        alloc_group_block);
1975         BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
1976         if (!BTRFS_I(inode)->block_group) {
1977                 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
1978                                                  NULL, 0,
1979                                                  BTRFS_BLOCK_GROUP_METADATA, 0);
1980         }
1981         btrfs_free_path(path);
1982         inode_item = NULL;
1983
1984         switch (inode->i_mode & S_IFMT) {
1985         case S_IFREG:
1986                 inode->i_mapping->a_ops = &btrfs_aops;
1987                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1988                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1989                 inode->i_fop = &btrfs_file_operations;
1990                 inode->i_op = &btrfs_file_inode_operations;
1991                 break;
1992         case S_IFDIR:
1993                 inode->i_fop = &btrfs_dir_file_operations;
1994                 if (root == root->fs_info->tree_root)
1995                         inode->i_op = &btrfs_dir_ro_inode_operations;
1996                 else
1997                         inode->i_op = &btrfs_dir_inode_operations;
1998                 break;
1999         case S_IFLNK:
2000                 inode->i_op = &btrfs_symlink_inode_operations;
2001                 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2002                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2003                 break;
2004         default:
2005                 init_special_inode(inode, inode->i_mode, rdev);
2006                 break;
2007         }
2008         return;
2009
2010 make_bad:
2011         btrfs_free_path(path);
2012         make_bad_inode(inode);
2013 }
2014
2015 /*
2016  * given a leaf and an inode, copy the inode fields into the leaf
2017  */
2018 static void fill_inode_item(struct btrfs_trans_handle *trans,
2019                             struct extent_buffer *leaf,
2020                             struct btrfs_inode_item *item,
2021                             struct inode *inode)
2022 {
2023         btrfs_set_inode_uid(leaf, item, inode->i_uid);
2024         btrfs_set_inode_gid(leaf, item, inode->i_gid);
2025         btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
2026         btrfs_set_inode_mode(leaf, item, inode->i_mode);
2027         btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2028
2029         btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2030                                inode->i_atime.tv_sec);
2031         btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2032                                 inode->i_atime.tv_nsec);
2033
2034         btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2035                                inode->i_mtime.tv_sec);
2036         btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2037                                 inode->i_mtime.tv_nsec);
2038
2039         btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2040                                inode->i_ctime.tv_sec);
2041         btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2042                                 inode->i_ctime.tv_nsec);
2043
2044         btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
2045         btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
2046         btrfs_set_inode_transid(leaf, item, trans->transid);
2047         btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
2048         btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
2049         btrfs_set_inode_block_group(leaf, item,
2050                                     BTRFS_I(inode)->block_group->key.objectid);
2051 }
2052
2053 /*
2054  * copy everything in the in-memory inode into the btree.
2055  */
2056 int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
2057                               struct btrfs_root *root,
2058                               struct inode *inode)
2059 {
2060         struct btrfs_inode_item *inode_item;
2061         struct btrfs_path *path;
2062         struct extent_buffer *leaf;
2063         int ret;
2064
2065         path = btrfs_alloc_path();
2066         BUG_ON(!path);
2067         ret = btrfs_lookup_inode(trans, root, path,
2068                                  &BTRFS_I(inode)->location, 1);
2069         if (ret) {
2070                 if (ret > 0)
2071                         ret = -ENOENT;
2072                 goto failed;
2073         }
2074
2075         leaf = path->nodes[0];
2076         inode_item = btrfs_item_ptr(leaf, path->slots[0],
2077                                   struct btrfs_inode_item);
2078
2079         fill_inode_item(trans, leaf, inode_item, inode);
2080         btrfs_mark_buffer_dirty(leaf);
2081         btrfs_set_inode_last_trans(trans, inode);
2082         ret = 0;
2083 failed:
2084         btrfs_free_path(path);
2085         return ret;
2086 }
2087
2088
2089 /*
2090  * unlink helper that gets used here in inode.c and in the tree logging
2091  * recovery code.  It remove a link in a directory with a given name, and
2092  * also drops the back refs in the inode to the directory
2093  */
2094 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2095                        struct btrfs_root *root,
2096                        struct inode *dir, struct inode *inode,
2097                        const char *name, int name_len)
2098 {
2099         struct btrfs_path *path;
2100         int ret = 0;
2101         struct extent_buffer *leaf;
2102         struct btrfs_dir_item *di;
2103         struct btrfs_key key;
2104         u64 index;
2105
2106         path = btrfs_alloc_path();
2107         if (!path) {
2108                 ret = -ENOMEM;
2109                 goto err;
2110         }
2111
2112         di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2113                                     name, name_len, -1);
2114         if (IS_ERR(di)) {
2115                 ret = PTR_ERR(di);
2116                 goto err;
2117         }
2118         if (!di) {
2119                 ret = -ENOENT;
2120                 goto err;
2121         }
2122         leaf = path->nodes[0];
2123         btrfs_dir_item_key_to_cpu(leaf, di, &key);
2124         ret = btrfs_delete_one_dir_name(trans, root, path, di);
2125         if (ret)
2126                 goto err;
2127         btrfs_release_path(root, path);
2128
2129         ret = btrfs_del_inode_ref(trans, root, name, name_len,
2130                                   inode->i_ino,
2131                                   dir->i_ino, &index);
2132         if (ret) {
2133                 printk("failed to delete reference to %.*s, "
2134                        "inode %lu parent %lu\n", name_len, name,
2135                        inode->i_ino, dir->i_ino);
2136                 goto err;
2137         }
2138
2139         di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2140                                          index, name, name_len, -1);
2141         if (IS_ERR(di)) {
2142                 ret = PTR_ERR(di);
2143                 goto err;
2144         }
2145         if (!di) {
2146                 ret = -ENOENT;
2147                 goto err;
2148         }
2149         ret = btrfs_delete_one_dir_name(trans, root, path, di);
2150         btrfs_release_path(root, path);
2151
2152         ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2153                                          inode, dir->i_ino);
2154         BUG_ON(ret != 0 && ret != -ENOENT);
2155         if (ret != -ENOENT)
2156                 BTRFS_I(dir)->log_dirty_trans = trans->transid;
2157
2158         ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2159                                            dir, index);
2160         BUG_ON(ret);
2161 err:
2162         btrfs_free_path(path);
2163         if (ret)
2164                 goto out;
2165
2166         btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2167         inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2168         btrfs_update_inode(trans, root, dir);
2169         btrfs_drop_nlink(inode);
2170         ret = btrfs_update_inode(trans, root, inode);
2171         dir->i_sb->s_dirt = 1;
2172 out:
2173         return ret;
2174 }
2175
2176 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2177 {
2178         struct btrfs_root *root;
2179         struct btrfs_trans_handle *trans;
2180         struct inode *inode = dentry->d_inode;
2181         int ret;
2182         unsigned long nr = 0;
2183
2184         root = BTRFS_I(dir)->root;
2185
2186         ret = btrfs_check_free_space(root, 1, 1);
2187         if (ret)
2188                 goto fail;
2189
2190         trans = btrfs_start_transaction(root, 1);
2191
2192         btrfs_set_trans_block_group(trans, dir);
2193         ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2194                                  dentry->d_name.name, dentry->d_name.len);
2195
2196         if (inode->i_nlink == 0)
2197                 ret = btrfs_orphan_add(trans, inode);
2198
2199         nr = trans->blocks_used;
2200
2201         btrfs_end_transaction_throttle(trans, root);
2202 fail:
2203         btrfs_btree_balance_dirty(root, nr);
2204         return ret;
2205 }
2206
2207 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2208 {
2209         struct inode *inode = dentry->d_inode;
2210         int err = 0;
2211         int ret;
2212         struct btrfs_root *root = BTRFS_I(dir)->root;
2213         struct btrfs_trans_handle *trans;
2214         unsigned long nr = 0;
2215
2216         if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2217                 return -ENOTEMPTY;
2218         }
2219
2220         ret = btrfs_check_free_space(root, 1, 1);
2221         if (ret)
2222                 goto fail;
2223
2224         trans = btrfs_start_transaction(root, 1);
2225         btrfs_set_trans_block_group(trans, dir);
2226
2227         err = btrfs_orphan_add(trans, inode);
2228         if (err)
2229                 goto fail_trans;
2230
2231         /* now the directory is empty */
2232         err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2233                                  dentry->d_name.name, dentry->d_name.len);
2234         if (!err) {
2235                 btrfs_i_size_write(inode, 0);
2236         }
2237
2238 fail_trans:
2239         nr = trans->blocks_used;
2240         ret = btrfs_end_transaction_throttle(trans, root);
2241 fail:
2242         btrfs_btree_balance_dirty(root, nr);
2243
2244         if (ret && !err)
2245                 err = ret;
2246         return err;
2247 }
2248
2249 /*
2250  * when truncating bytes in a file, it is possible to avoid reading
2251  * the leaves that contain only checksum items.  This can be the
2252  * majority of the IO required to delete a large file, but it must
2253  * be done carefully.
2254  *
2255  * The keys in the level just above the leaves are checked to make sure
2256  * the lowest key in a given leaf is a csum key, and starts at an offset
2257  * after the new  size.
2258  *
2259  * Then the key for the next leaf is checked to make sure it also has
2260  * a checksum item for the same file.  If it does, we know our target leaf
2261  * contains only checksum items, and it can be safely freed without reading
2262  * it.
2263  *
2264  * This is just an optimization targeted at large files.  It may do
2265  * nothing.  It will return 0 unless things went badly.
2266  */
2267 static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2268                                      struct btrfs_root *root,
2269                                      struct btrfs_path *path,
2270                                      struct inode *inode, u64 new_size)
2271 {
2272         struct btrfs_key key;
2273         int ret;
2274         int nritems;
2275         struct btrfs_key found_key;
2276         struct btrfs_key other_key;
2277         struct btrfs_leaf_ref *ref;
2278         u64 leaf_gen;
2279         u64 leaf_start;
2280
2281         path->lowest_level = 1;
2282         key.objectid = inode->i_ino;
2283         key.type = BTRFS_CSUM_ITEM_KEY;
2284         key.offset = new_size;
2285 again:
2286         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2287         if (ret < 0)
2288                 goto out;
2289
2290         if (path->nodes[1] == NULL) {
2291                 ret = 0;
2292                 goto out;
2293         }
2294         ret = 0;
2295         btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2296         nritems = btrfs_header_nritems(path->nodes[1]);
2297
2298         if (!nritems)
2299                 goto out;
2300
2301         if (path->slots[1] >= nritems)
2302                 goto next_node;
2303
2304         /* did we find a key greater than anything we want to delete? */
2305         if (found_key.objectid > inode->i_ino ||
2306            (found_key.objectid == inode->i_ino && found_key.type > key.type))
2307                 goto out;
2308
2309         /* we check the next key in the node to make sure the leave contains
2310          * only checksum items.  This comparison doesn't work if our
2311          * leaf is the last one in the node
2312          */
2313         if (path->slots[1] + 1 >= nritems) {
2314 next_node:
2315                 /* search forward from the last key in the node, this
2316                  * will bring us into the next node in the tree
2317                  */
2318                 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2319
2320                 /* unlikely, but we inc below, so check to be safe */
2321                 if (found_key.offset == (u64)-1)
2322                         goto out;
2323
2324                 /* search_forward needs a path with locks held, do the
2325                  * search again for the original key.  It is possible
2326                  * this will race with a balance and return a path that
2327                  * we could modify, but this drop is just an optimization
2328                  * and is allowed to miss some leaves.
2329                  */
2330                 btrfs_release_path(root, path);
2331                 found_key.offset++;
2332
2333                 /* setup a max key for search_forward */
2334                 other_key.offset = (u64)-1;
2335                 other_key.type = key.type;
2336                 other_key.objectid = key.objectid;
2337
2338                 path->keep_locks = 1;
2339                 ret = btrfs_search_forward(root, &found_key, &other_key,
2340                                            path, 0, 0);
2341                 path->keep_locks = 0;
2342                 if (ret || found_key.objectid != key.objectid ||
2343                     found_key.type != key.type) {
2344                         ret = 0;
2345                         goto out;
2346                 }
2347
2348                 key.offset = found_key.offset;
2349                 btrfs_release_path(root, path);
2350                 cond_resched();
2351                 goto again;
2352         }
2353
2354         /* we know there's one more slot after us in the tree,
2355          * read that key so we can verify it is also a checksum item
2356          */
2357         btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2358
2359         if (found_key.objectid < inode->i_ino)
2360                 goto next_key;
2361
2362         if (found_key.type != key.type || found_key.offset < new_size)
2363                 goto next_key;
2364
2365         /*
2366          * if the key for the next leaf isn't a csum key from this objectid,
2367          * we can't be sure there aren't good items inside this leaf.
2368          * Bail out
2369          */
2370         if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2371                 goto out;
2372
2373         leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2374         leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
2375         /*
2376          * it is safe to delete this leaf, it contains only
2377          * csum items from this inode at an offset >= new_size
2378          */
2379         ret = btrfs_del_leaf(trans, root, path, leaf_start);
2380         BUG_ON(ret);
2381
2382         if (root->ref_cows && leaf_gen < trans->transid) {
2383                 ref = btrfs_alloc_leaf_ref(root, 0);
2384                 if (ref) {
2385                         ref->root_gen = root->root_key.offset;
2386                         ref->bytenr = leaf_start;
2387                         ref->owner = 0;
2388                         ref->generation = leaf_gen;
2389                         ref->nritems = 0;
2390
2391                         ret = btrfs_add_leaf_ref(root, ref, 0);
2392                         WARN_ON(ret);
2393                         btrfs_free_leaf_ref(root, ref);
2394                 } else {
2395                         WARN_ON(1);
2396                 }
2397         }
2398 next_key:
2399         btrfs_release_path(root, path);
2400
2401         if (other_key.objectid == inode->i_ino &&
2402             other_key.type == key.type && other_key.offset > key.offset) {
2403                 key.offset = other_key.offset;
2404                 cond_resched();
2405                 goto again;
2406         }
2407         ret = 0;
2408 out:
2409         /* fixup any changes we've made to the path */
2410         path->lowest_level = 0;
2411         path->keep_locks = 0;
2412         btrfs_release_path(root, path);
2413         return ret;
2414 }
2415
2416 /*
2417  * this can truncate away extent items, csum items and directory items.
2418  * It starts at a high offset and removes keys until it can't find
2419  * any higher than new_size
2420  *
2421  * csum items that cross the new i_size are truncated to the new size
2422  * as well.
2423  *
2424  * min_type is the minimum key type to truncate down to.  If set to 0, this
2425  * will kill all the items on this inode, including the INODE_ITEM_KEY.
2426  */
2427 noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2428                                         struct btrfs_root *root,
2429                                         struct inode *inode,
2430                                         u64 new_size, u32 min_type)
2431 {
2432         int ret;
2433         struct btrfs_path *path;
2434         struct btrfs_key key;
2435         struct btrfs_key found_key;
2436         u32 found_type;
2437         struct extent_buffer *leaf;
2438         struct btrfs_file_extent_item *fi;
2439         u64 extent_start = 0;
2440         u64 extent_num_bytes = 0;
2441         u64 item_end = 0;
2442         u64 root_gen = 0;
2443         u64 root_owner = 0;
2444         int found_extent;
2445         int del_item;
2446         int pending_del_nr = 0;
2447         int pending_del_slot = 0;
2448         int extent_type = -1;
2449         int encoding;
2450         u64 mask = root->sectorsize - 1;
2451
2452         if (root->ref_cows)
2453                 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
2454         path = btrfs_alloc_path();
2455         path->reada = -1;
2456         BUG_ON(!path);
2457
2458         /* FIXME, add redo link to tree so we don't leak on crash */
2459         key.objectid = inode->i_ino;
2460         key.offset = (u64)-1;
2461         key.type = (u8)-1;
2462
2463         btrfs_init_path(path);
2464
2465         ret = drop_csum_leaves(trans, root, path, inode, new_size);
2466         BUG_ON(ret);
2467
2468 search_again:
2469         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2470         if (ret < 0) {
2471                 goto error;
2472         }
2473         if (ret > 0) {
2474                 /* there are no items in the tree for us to truncate, we're
2475                  * done
2476                  */
2477                 if (path->slots[0] == 0) {
2478                         ret = 0;
2479                         goto error;
2480                 }
2481                 path->slots[0]--;
2482         }
2483
2484         while(1) {
2485                 fi = NULL;
2486                 leaf = path->nodes[0];
2487                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2488                 found_type = btrfs_key_type(&found_key);
2489                 encoding = 0;
2490
2491                 if (found_key.objectid != inode->i_ino)
2492                         break;
2493
2494                 if (found_type < min_type)
2495                         break;
2496
2497                 item_end = found_key.offset;
2498                 if (found_type == BTRFS_EXTENT_DATA_KEY) {
2499                         fi = btrfs_item_ptr(leaf, path->slots[0],
2500                                             struct btrfs_file_extent_item);
2501                         extent_type = btrfs_file_extent_type(leaf, fi);
2502                         encoding = btrfs_file_extent_compression(leaf, fi);
2503                         encoding |= btrfs_file_extent_encryption(leaf, fi);
2504                         encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2505
2506                         if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
2507                                 item_end +=
2508                                     btrfs_file_extent_num_bytes(leaf, fi);
2509                         } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
2510                                 item_end += btrfs_file_extent_inline_len(leaf,
2511                                                                          fi);
2512                         }
2513                         item_end--;
2514                 }
2515                 if (found_type == BTRFS_CSUM_ITEM_KEY) {
2516                         ret = btrfs_csum_truncate(trans, root, path,
2517                                                   new_size);
2518                         BUG_ON(ret);
2519                 }
2520                 if (item_end < new_size) {
2521                         if (found_type == BTRFS_DIR_ITEM_KEY) {
2522                                 found_type = BTRFS_INODE_ITEM_KEY;
2523                         } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
2524                                 found_type = BTRFS_CSUM_ITEM_KEY;
2525                         } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
2526                                 found_type = BTRFS_XATTR_ITEM_KEY;
2527                         } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
2528                                 found_type = BTRFS_INODE_REF_KEY;
2529                         } else if (found_type) {
2530                                 found_type--;
2531                         } else {
2532                                 break;
2533                         }
2534                         btrfs_set_key_type(&key, found_type);
2535                         goto next;
2536                 }
2537                 if (found_key.offset >= new_size)
2538                         del_item = 1;
2539                 else
2540                         del_item = 0;
2541                 found_extent = 0;
2542
2543                 /* FIXME, shrink the extent if the ref count is only 1 */
2544                 if (found_type != BTRFS_EXTENT_DATA_KEY)
2545                         goto delete;
2546
2547                 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
2548                         u64 num_dec;
2549                         extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
2550                         if (!del_item && !encoding) {
2551                                 u64 orig_num_bytes =
2552                                         btrfs_file_extent_num_bytes(leaf, fi);
2553                                 extent_num_bytes = new_size -
2554                                         found_key.offset + root->sectorsize - 1;
2555                                 extent_num_bytes = extent_num_bytes &
2556                                         ~((u64)root->sectorsize - 1);
2557                                 btrfs_set_file_extent_num_bytes(leaf, fi,
2558                                                          extent_num_bytes);
2559                                 num_dec = (orig_num_bytes -
2560                                            extent_num_bytes);
2561                                 if (root->ref_cows && extent_start != 0)
2562                                         inode_sub_bytes(inode, num_dec);
2563                                 btrfs_mark_buffer_dirty(leaf);
2564                         } else {
2565                                 extent_num_bytes =
2566                                         btrfs_file_extent_disk_num_bytes(leaf,
2567                                                                          fi);
2568                                 /* FIXME blocksize != 4096 */
2569                                 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
2570                                 if (extent_start != 0) {
2571                                         found_extent = 1;
2572                                         if (root->ref_cows)
2573                                                 inode_sub_bytes(inode, num_dec);
2574                                 }
2575                                 root_gen = btrfs_header_generation(leaf);
2576                                 root_owner = btrfs_header_owner(leaf);
2577                         }
2578                 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
2579                         /*
2580                          * we can't truncate inline items that have had
2581                          * special encodings
2582                          */
2583                         if (!del_item &&
2584                             btrfs_file_extent_compression(leaf, fi) == 0 &&
2585                             btrfs_file_extent_encryption(leaf, fi) == 0 &&
2586                             btrfs_file_extent_other_encoding(leaf, fi) == 0) {
2587                                 u32 size = new_size - found_key.offset;
2588
2589                                 if (root->ref_cows) {
2590                                         inode_sub_bytes(inode, item_end + 1 -
2591                                                         new_size);
2592                                 }
2593                                 size =
2594                                     btrfs_file_extent_calc_inline_size(size);
2595                                 ret = btrfs_truncate_item(trans, root, path,
2596                                                           size, 1);
2597                                 BUG_ON(ret);
2598                         } else if (root->ref_cows) {
2599                                 inode_sub_bytes(inode, item_end + 1 -
2600                                                 found_key.offset);
2601                         }
2602                 }
2603 delete:
2604                 if (del_item) {
2605                         if (!pending_del_nr) {
2606                                 /* no pending yet, add ourselves */
2607                                 pending_del_slot = path->slots[0];
2608                                 pending_del_nr = 1;
2609                         } else if (pending_del_nr &&
2610                                    path->slots[0] + 1 == pending_del_slot) {
2611                                 /* hop on the pending chunk */
2612                                 pending_del_nr++;
2613                                 pending_del_slot = path->slots[0];
2614                         } else {
2615                                 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
2616                         }
2617                 } else {
2618                         break;
2619                 }
2620                 if (found_extent) {
2621                         ret = btrfs_free_extent(trans, root, extent_start,
2622                                                 extent_num_bytes,
2623                                                 leaf->start, root_owner,
2624                                                 root_gen, inode->i_ino, 0);
2625                         BUG_ON(ret);
2626                 }
2627 next:
2628                 if (path->slots[0] == 0) {
2629                         if (pending_del_nr)
2630                                 goto del_pending;
2631                         btrfs_release_path(root, path);
2632                         goto search_again;
2633                 }
2634
2635                 path->slots[0]--;
2636                 if (pending_del_nr &&
2637                     path->slots[0] + 1 != pending_del_slot) {
2638                         struct btrfs_key debug;
2639 del_pending:
2640                         btrfs_item_key_to_cpu(path->nodes[0], &debug,
2641                                               pending_del_slot);
2642                         ret = btrfs_del_items(trans, root, path,
2643                                               pending_del_slot,
2644                                               pending_del_nr);
2645                         BUG_ON(ret);
2646                         pending_del_nr = 0;
2647                         btrfs_release_path(root, path);
2648                         goto search_again;
2649                 }
2650         }
2651         ret = 0;
2652 error:
2653         if (pending_del_nr) {
2654                 ret = btrfs_del_items(trans, root, path, pending_del_slot,
2655                                       pending_del_nr);
2656         }
2657         btrfs_free_path(path);
2658         inode->i_sb->s_dirt = 1;
2659         return ret;
2660 }
2661
2662 /*
2663  * taken from block_truncate_page, but does cow as it zeros out
2664  * any bytes left in the last page in the file.
2665  */
2666 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
2667 {
2668         struct inode *inode = mapping->host;
2669         struct btrfs_root *root = BTRFS_I(inode)->root;
2670         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2671         struct btrfs_ordered_extent *ordered;
2672         char *kaddr;
2673         u32 blocksize = root->sectorsize;
2674         pgoff_t index = from >> PAGE_CACHE_SHIFT;
2675         unsigned offset = from & (PAGE_CACHE_SIZE-1);
2676         struct page *page;
2677         int ret = 0;
2678         u64 page_start;
2679         u64 page_end;
2680
2681         if ((offset & (blocksize - 1)) == 0)
2682                 goto out;
2683
2684         ret = -ENOMEM;
2685 again:
2686         page = grab_cache_page(mapping, index);
2687         if (!page)
2688                 goto out;
2689
2690         page_start = page_offset(page);
2691         page_end = page_start + PAGE_CACHE_SIZE - 1;
2692
2693         if (!PageUptodate(page)) {
2694                 ret = btrfs_readpage(NULL, page);
2695                 lock_page(page);
2696                 if (page->mapping != mapping) {
2697                         unlock_page(page);
2698                         page_cache_release(page);
2699                         goto again;
2700                 }
2701                 if (!PageUptodate(page)) {
2702                         ret = -EIO;
2703                         goto out_unlock;
2704                 }
2705         }
2706         wait_on_page_writeback(page);
2707
2708         lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2709         set_page_extent_mapped(page);
2710
2711         ordered = btrfs_lookup_ordered_extent(inode, page_start);
2712         if (ordered) {
2713                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2714                 unlock_page(page);
2715                 page_cache_release(page);
2716                 btrfs_start_ordered_extent(inode, ordered, 1);
2717                 btrfs_put_ordered_extent(ordered);
2718                 goto again;
2719         }
2720
2721         btrfs_set_extent_delalloc(inode, page_start, page_end);
2722         ret = 0;
2723         if (offset != PAGE_CACHE_SIZE) {
2724                 kaddr = kmap(page);
2725                 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
2726                 flush_dcache_page(page);
2727                 kunmap(page);
2728         }
2729         ClearPageChecked(page);
2730         set_page_dirty(page);
2731         unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2732
2733 out_unlock:
2734         unlock_page(page);
2735         page_cache_release(page);
2736 out:
2737         return ret;
2738 }
2739
2740 int btrfs_cont_expand(struct inode *inode, loff_t size)
2741 {
2742         struct btrfs_trans_handle *trans;
2743         struct btrfs_root *root = BTRFS_I(inode)->root;
2744         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2745         struct extent_map *em;
2746         u64 mask = root->sectorsize - 1;
2747         u64 hole_start = (inode->i_size + mask) & ~mask;
2748         u64 block_end = (size + mask) & ~mask;
2749         u64 last_byte;
2750         u64 cur_offset;
2751         u64 hole_size;
2752         int err;
2753
2754         if (size <= hole_start)
2755                 return 0;
2756
2757         err = btrfs_check_free_space(root, 1, 0);
2758         if (err)
2759                 return err;
2760
2761         btrfs_truncate_page(inode->i_mapping, inode->i_size);
2762
2763         while (1) {
2764                 struct btrfs_ordered_extent *ordered;
2765                 btrfs_wait_ordered_range(inode, hole_start,
2766                                          block_end - hole_start);
2767                 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2768                 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
2769                 if (!ordered)
2770                         break;
2771                 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2772                 btrfs_put_ordered_extent(ordered);
2773         }
2774
2775         trans = btrfs_start_transaction(root, 1);
2776         btrfs_set_trans_block_group(trans, inode);
2777
2778         cur_offset = hole_start;
2779         while (1) {
2780                 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2781                                 block_end - cur_offset, 0);
2782                 BUG_ON(IS_ERR(em) || !em);
2783                 last_byte = min(extent_map_end(em), block_end);
2784                 last_byte = (last_byte + mask) & ~mask;
2785                 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2786                         u64 hint_byte = 0;
2787                         hole_size = last_byte - cur_offset;
2788                         err = btrfs_drop_extents(trans, root, inode,
2789                                                  cur_offset,
2790                                                  cur_offset + hole_size,
2791                                                  cur_offset, &hint_byte);
2792                         if (err)
2793                                 break;
2794                         err = btrfs_insert_file_extent(trans, root,
2795                                         inode->i_ino, cur_offset, 0,
2796                                         0, hole_size, 0, hole_size,
2797                                         0, 0, 0);
2798                         btrfs_drop_extent_cache(inode, hole_start,
2799                                         last_byte - 1, 0);
2800                 }
2801                 free_extent_map(em);
2802                 cur_offset = last_byte;
2803                 if (err || cur_offset >= block_end)
2804                         break;
2805         }
2806
2807         btrfs_end_transaction(trans, root);
2808         unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
2809         return err;
2810 }
2811
2812 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
2813 {
2814         struct inode *inode = dentry->d_inode;
2815         int err;
2816
2817         err = inode_change_ok(inode, attr);
2818         if (err)
2819                 return err;
2820
2821         if (S_ISREG(inode->i_mode) &&
2822             attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
2823                 err = btrfs_cont_expand(inode, attr->ia_size);
2824                 if (err)
2825                         return err;
2826         }
2827
2828         err = inode_setattr(inode, attr);
2829
2830         if (!err && ((attr->ia_valid & ATTR_MODE)))
2831                 err = btrfs_acl_chmod(inode);
2832         return err;
2833 }
2834
2835 void btrfs_delete_inode(struct inode *inode)
2836 {
2837         struct btrfs_trans_handle *trans;
2838         struct btrfs_root *root = BTRFS_I(inode)->root;
2839         unsigned long nr;
2840         int ret;
2841
2842         truncate_inode_pages(&inode->i_data, 0);
2843         if (is_bad_inode(inode)) {
2844                 btrfs_orphan_del(NULL, inode);
2845                 goto no_delete;
2846         }
2847         btrfs_wait_ordered_range(inode, 0, (u64)-1);
2848
2849         btrfs_i_size_write(inode, 0);
2850         trans = btrfs_start_transaction(root, 1);
2851
2852         btrfs_set_trans_block_group(trans, inode);
2853         ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
2854         if (ret) {
2855                 btrfs_orphan_del(NULL, inode);
2856                 goto no_delete_lock;
2857         }
2858
2859         btrfs_orphan_del(trans, inode);
2860
2861         nr = trans->blocks_used;
2862         clear_inode(inode);
2863
2864         btrfs_end_transaction(trans, root);
2865         btrfs_btree_balance_dirty(root, nr);
2866         return;
2867
2868 no_delete_lock:
2869         nr = trans->blocks_used;
2870         btrfs_end_transaction(trans, root);
2871         btrfs_btree_balance_dirty(root, nr);
2872 no_delete:
2873         clear_inode(inode);
2874 }
2875
2876 /*
2877  * this returns the key found in the dir entry in the location pointer.
2878  * If no dir entries were found, location->objectid is 0.
2879  */
2880 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
2881                                struct btrfs_key *location)
2882 {
2883         const char *name = dentry->d_name.name;
2884         int namelen = dentry->d_name.len;
2885         struct btrfs_dir_item *di;
2886         struct btrfs_path *path;
2887         struct btrfs_root *root = BTRFS_I(dir)->root;
2888         int ret = 0;
2889
2890         path = btrfs_alloc_path();
2891         BUG_ON(!path);
2892
2893         di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2894                                     namelen, 0);
2895         if (IS_ERR(di))
2896                 ret = PTR_ERR(di);
2897         if (!di || IS_ERR(di)) {
2898                 goto out_err;
2899         }
2900         btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
2901 out:
2902         btrfs_free_path(path);
2903         return ret;
2904 out_err:
2905         location->objectid = 0;
2906         goto out;
2907 }
2908
2909 /*
2910  * when we hit a tree root in a directory, the btrfs part of the inode
2911  * needs to be changed to reflect the root directory of the tree root.  This
2912  * is kind of like crossing a mount point.
2913  */
2914 static int fixup_tree_root_location(struct btrfs_root *root,
2915                              struct btrfs_key *location,
2916                              struct btrfs_root **sub_root,
2917                              struct dentry *dentry)
2918 {
2919         struct btrfs_root_item *ri;
2920
2921         if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2922                 return 0;
2923         if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2924                 return 0;
2925
2926         *sub_root = btrfs_read_fs_root(root->fs_info, location,
2927                                         dentry->d_name.name,
2928                                         dentry->d_name.len);
2929         if (IS_ERR(*sub_root))
2930                 return PTR_ERR(*sub_root);
2931
2932         ri = &(*sub_root)->root_item;
2933         location->objectid = btrfs_root_dirid(ri);
2934         btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2935         location->offset = 0;
2936
2937         return 0;
2938 }
2939
2940 static noinline void init_btrfs_i(struct inode *inode)
2941 {
2942         struct btrfs_inode *bi = BTRFS_I(inode);
2943
2944         bi->i_acl = NULL;
2945         bi->i_default_acl = NULL;
2946
2947         bi->generation = 0;
2948         bi->last_trans = 0;
2949         bi->logged_trans = 0;
2950         bi->delalloc_bytes = 0;
2951         bi->disk_i_size = 0;
2952         bi->flags = 0;
2953         bi->index_cnt = (u64)-1;
2954         bi->log_dirty_trans = 0;
2955         extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2956         extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2957                              inode->i_mapping, GFP_NOFS);
2958         extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2959                              inode->i_mapping, GFP_NOFS);
2960         INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
2961         btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
2962         mutex_init(&BTRFS_I(inode)->csum_mutex);
2963         mutex_init(&BTRFS_I(inode)->extent_mutex);
2964         mutex_init(&BTRFS_I(inode)->log_mutex);
2965 }
2966
2967 static int btrfs_init_locked_inode(struct inode *inode, void *p)
2968 {
2969         struct btrfs_iget_args *args = p;
2970         inode->i_ino = args->ino;
2971         init_btrfs_i(inode);
2972         BTRFS_I(inode)->root = args->root;
2973         return 0;
2974 }
2975
2976 static int btrfs_find_actor(struct inode *inode, void *opaque)
2977 {
2978         struct btrfs_iget_args *args = opaque;
2979         return (args->ino == inode->i_ino &&
2980                 args->root == BTRFS_I(inode)->root);
2981 }
2982
2983 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
2984                             struct btrfs_root *root, int wait)
2985 {
2986         struct inode *inode;
2987         struct btrfs_iget_args args;
2988         args.ino = objectid;
2989         args.root = root;
2990
2991         if (wait) {
2992                 inode = ilookup5(s, objectid, btrfs_find_actor,
2993                                  (void *)&args);
2994         } else {
2995                 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
2996                                         (void *)&args);
2997         }
2998         return inode;
2999 }
3000
3001 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
3002                                 struct btrfs_root *root)
3003 {
3004         struct inode *inode;
3005         struct btrfs_iget_args args;
3006         args.ino = objectid;
3007         args.root = root;
3008
3009         inode = iget5_locked(s, objectid, btrfs_find_actor,
3010                              btrfs_init_locked_inode,
3011                              (void *)&args);
3012         return inode;
3013 }
3014
3015 /* Get an inode object given its location and corresponding root.
3016  * Returns in *is_new if the inode was read from disk
3017  */
3018 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3019                          struct btrfs_root *root, int *is_new)
3020 {
3021         struct inode *inode;
3022
3023         inode = btrfs_iget_locked(s, location->objectid, root);
3024         if (!inode)
3025                 return ERR_PTR(-EACCES);
3026
3027         if (inode->i_state & I_NEW) {
3028                 BTRFS_I(inode)->root = root;
3029                 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3030                 btrfs_read_locked_inode(inode);
3031                 unlock_new_inode(inode);
3032                 if (is_new)
3033                         *is_new = 1;
3034         } else {
3035                 if (is_new)
3036                         *is_new = 0;
3037         }
3038
3039         return inode;
3040 }
3041
3042 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3043                                    struct nameidata *nd)
3044 {
3045         struct inode * inode;
3046         struct btrfs_inode *bi = BTRFS_I(dir);
3047         struct btrfs_root *root = bi->root;
3048         struct btrfs_root *sub_root = root;
3049         struct btrfs_key location;
3050         int ret, new, do_orphan = 0;
3051
3052         if (dentry->d_name.len > BTRFS_NAME_LEN)
3053                 return ERR_PTR(-ENAMETOOLONG);
3054
3055         ret = btrfs_inode_by_name(dir, dentry, &location);
3056
3057         if (ret < 0)
3058                 return ERR_PTR(ret);
3059
3060         inode = NULL;
3061         if (location.objectid) {
3062                 ret = fixup_tree_root_location(root, &location, &sub_root,
3063                                                 dentry);
3064                 if (ret < 0)
3065                         return ERR_PTR(ret);
3066                 if (ret > 0)
3067                         return ERR_PTR(-ENOENT);
3068                 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
3069                 if (IS_ERR(inode))
3070                         return ERR_CAST(inode);
3071
3072                 /* the inode and parent dir are two different roots */
3073                 if (new && root != sub_root) {
3074                         igrab(inode);
3075                         sub_root->inode = inode;
3076                         do_orphan = 1;
3077                 }
3078         }
3079
3080         if (unlikely(do_orphan))
3081                 btrfs_orphan_cleanup(sub_root);
3082
3083         return d_splice_alias(inode, dentry);
3084 }
3085
3086 static unsigned char btrfs_filetype_table[] = {
3087         DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3088 };
3089
3090 static int btrfs_real_readdir(struct file *filp, void *dirent,
3091                               filldir_t filldir)
3092 {
3093         struct inode *inode = filp->f_dentry->d_inode;
3094         struct btrfs_root *root = BTRFS_I(inode)->root;
3095         struct btrfs_item *item;
3096         struct btrfs_dir_item *di;
3097         struct btrfs_key key;
3098         struct btrfs_key found_key;
3099         struct btrfs_path *path;
3100         int ret;
3101         u32 nritems;
3102         struct extent_buffer *leaf;
3103         int slot;
3104         int advance;
3105         unsigned char d_type;
3106         int over = 0;
3107         u32 di_cur;
3108         u32 di_total;
3109         u32 di_len;
3110         int key_type = BTRFS_DIR_INDEX_KEY;
3111         char tmp_name[32];
3112         char *name_ptr;
3113         int name_len;
3114
3115         /* FIXME, use a real flag for deciding about the key type */
3116         if (root->fs_info->tree_root == root)
3117                 key_type = BTRFS_DIR_ITEM_KEY;
3118
3119         /* special case for "." */
3120         if (filp->f_pos == 0) {
3121                 over = filldir(dirent, ".", 1,
3122                                1, inode->i_ino,
3123                                DT_DIR);
3124                 if (over)
3125                         return 0;
3126                 filp->f_pos = 1;
3127         }
3128         /* special case for .., just use the back ref */
3129         if (filp->f_pos == 1) {
3130                 u64 pino = parent_ino(filp->f_path.dentry);
3131                 over = filldir(dirent, "..", 2,
3132                                2, pino, DT_DIR);
3133                 if (over)
3134                         return 0;
3135                 filp->f_pos = 2;
3136         }
3137
3138         path = btrfs_alloc_path();
3139         path->reada = 2;
3140
3141         btrfs_set_key_type(&key, key_type);
3142         key.offset = filp->f_pos;
3143         key.objectid = inode->i_ino;
3144
3145         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3146         if (ret < 0)
3147                 goto err;
3148         advance = 0;
3149
3150         while (1) {
3151                 leaf = path->nodes[0];
3152                 nritems = btrfs_header_nritems(leaf);
3153                 slot = path->slots[0];
3154                 if (advance || slot >= nritems) {
3155                         if (slot >= nritems - 1) {
3156                                 ret = btrfs_next_leaf(root, path);
3157                                 if (ret)
3158                                         break;
3159                                 leaf = path->nodes[0];
3160                                 nritems = btrfs_header_nritems(leaf);
3161                                 slot = path->slots[0];
3162                         } else {
3163                                 slot++;
3164                                 path->slots[0]++;
3165                         }
3166                 }
3167                 advance = 1;
3168                 item = btrfs_item_nr(leaf, slot);
3169                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3170
3171                 if (found_key.objectid != key.objectid)
3172                         break;
3173                 if (btrfs_key_type(&found_key) != key_type)
3174                         break;
3175                 if (found_key.offset < filp->f_pos)
3176                         continue;
3177
3178                 filp->f_pos = found_key.offset;
3179
3180                 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3181                 di_cur = 0;
3182                 di_total = btrfs_item_size(leaf, item);
3183
3184                 while (di_cur < di_total) {
3185                         struct btrfs_key location;
3186
3187                         name_len = btrfs_dir_name_len(leaf, di);
3188                         if (name_len <= sizeof(tmp_name)) {
3189                                 name_ptr = tmp_name;
3190                         } else {
3191                                 name_ptr = kmalloc(name_len, GFP_NOFS);
3192                                 if (!name_ptr) {
3193                                         ret = -ENOMEM;
3194                                         goto err;
3195                                 }
3196                         }
3197                         read_extent_buffer(leaf, name_ptr,
3198                                            (unsigned long)(di + 1), name_len);
3199
3200                         d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3201                         btrfs_dir_item_key_to_cpu(leaf, di, &location);
3202                         over = filldir(dirent, name_ptr, name_len,
3203                                        found_key.offset, location.objectid,
3204                                        d_type);
3205
3206                         if (name_ptr != tmp_name)
3207                                 kfree(name_ptr);
3208
3209                         if (over)
3210                                 goto nopos;
3211
3212                         di_len = btrfs_dir_name_len(leaf, di) +
3213                                  btrfs_dir_data_len(leaf, di) + sizeof(*di);
3214                         di_cur += di_len;
3215                         di = (struct btrfs_dir_item *)((char *)di + di_len);
3216                 }
3217         }
3218
3219         /* Reached end of directory/root. Bump pos past the last item. */
3220         if (key_type == BTRFS_DIR_INDEX_KEY)
3221                 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
3222         else
3223                 filp->f_pos++;
3224 nopos:
3225         ret = 0;
3226 err:
3227         btrfs_free_path(path);
3228         return ret;
3229 }
3230
3231 int btrfs_write_inode(struct inode *inode, int wait)
3232 {
3233         struct btrfs_root *root = BTRFS_I(inode)->root;
3234         struct btrfs_trans_handle *trans;
3235         int ret = 0;
3236
3237         if (root->fs_info->closing > 1)
3238                 return 0;
3239
3240         if (wait) {
3241                 trans = btrfs_join_transaction(root, 1);
3242                 btrfs_set_trans_block_group(trans, inode);
3243                 ret = btrfs_commit_transaction(trans, root);
3244         }
3245         return ret;
3246 }
3247
3248 /*
3249  * This is somewhat expensive, updating the tree every time the
3250  * inode changes.  But, it is most likely to find the inode in cache.
3251  * FIXME, needs more benchmarking...there are no reasons other than performance
3252  * to keep or drop this code.
3253  */
3254 void btrfs_dirty_inode(struct inode *inode)
3255 {
3256         struct btrfs_root *root = BTRFS_I(inode)->root;
3257         struct btrfs_trans_handle *trans;
3258
3259         trans = btrfs_join_transaction(root, 1);
3260         btrfs_set_trans_block_group(trans, inode);
3261         btrfs_update_inode(trans, root, inode);
3262         btrfs_end_transaction(trans, root);
3263 }
3264
3265 /*
3266  * find the highest existing sequence number in a directory
3267  * and then set the in-memory index_cnt variable to reflect
3268  * free sequence numbers
3269  */
3270 static int btrfs_set_inode_index_count(struct inode *inode)
3271 {
3272         struct btrfs_root *root = BTRFS_I(inode)->root;
3273         struct btrfs_key key, found_key;
3274         struct btrfs_path *path;
3275         struct extent_buffer *leaf;
3276         int ret;
3277
3278         key.objectid = inode->i_ino;
3279         btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
3280         key.offset = (u64)-1;
3281
3282         path = btrfs_alloc_path();
3283         if (!path)
3284                 return -ENOMEM;
3285
3286         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3287         if (ret < 0)
3288                 goto out;
3289         /* FIXME: we should be able to handle this */
3290         if (ret == 0)
3291                 goto out;
3292         ret = 0;
3293
3294         /*
3295          * MAGIC NUMBER EXPLANATION:
3296          * since we search a directory based on f_pos we have to start at 2
3297          * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
3298          * else has to start at 2
3299          */
3300         if (path->slots[0] == 0) {
3301                 BTRFS_I(inode)->index_cnt = 2;
3302                 goto out;
3303         }
3304
3305         path->slots[0]--;
3306
3307         leaf = path->nodes[0];
3308         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3309
3310         if (found_key.objectid != inode->i_ino ||
3311             btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
3312                 BTRFS_I(inode)->index_cnt = 2;
3313                 goto out;
3314         }
3315
3316         BTRFS_I(inode)->index_cnt = found_key.offset + 1;
3317 out:
3318         btrfs_free_path(path);
3319         return ret;
3320 }
3321
3322 /*
3323  * helper to find a free sequence number in a given directory.  This current
3324  * code is very simple, later versions will do smarter things in the btree
3325  */
3326 static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
3327                                  u64 *index)
3328 {
3329         int ret = 0;
3330
3331         if (BTRFS_I(dir)->index_cnt == (u64)-1) {
3332                 ret = btrfs_set_inode_index_count(dir);
3333                 if (ret) {
3334                         return ret;
3335                 }
3336         }
3337
3338         *index = BTRFS_I(dir)->index_cnt;
3339         BTRFS_I(dir)->index_cnt++;
3340
3341         return ret;
3342 }
3343
3344 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
3345                                      struct btrfs_root *root,
3346                                      struct inode *dir,
3347                                      const char *name, int name_len,
3348                                      u64 ref_objectid,
3349                                      u64 objectid,
3350                                      struct btrfs_block_group_cache *group,
3351                                      int mode, u64 *index)
3352 {
3353         struct inode *inode;
3354         struct btrfs_inode_item *inode_item;
3355         struct btrfs_block_group_cache *new_inode_group;
3356         struct btrfs_key *location;
3357         struct btrfs_path *path;
3358         struct btrfs_inode_ref *ref;
3359         struct btrfs_key key[2];
3360         u32 sizes[2];
3361         unsigned long ptr;
3362         int ret;
3363         int owner;
3364
3365         path = btrfs_alloc_path();
3366         BUG_ON(!path);
3367
3368         inode = new_inode(root->fs_info->sb);
3369         if (!inode)
3370                 return ERR_PTR(-ENOMEM);
3371
3372         if (dir) {
3373                 ret = btrfs_set_inode_index(dir, inode, index);
3374                 if (ret)
3375                         return ERR_PTR(ret);
3376         }
3377         /*
3378          * index_cnt is ignored for everything but a dir,
3379          * btrfs_get_inode_index_count has an explanation for the magic
3380          * number
3381          */
3382         init_btrfs_i(inode);
3383         BTRFS_I(inode)->index_cnt = 2;
3384         BTRFS_I(inode)->root = root;
3385         BTRFS_I(inode)->generation = trans->transid;
3386
3387         if (mode & S_IFDIR)
3388                 owner = 0;
3389         else
3390                 owner = 1;
3391         new_inode_group = btrfs_find_block_group(root, group, 0,
3392                                        BTRFS_BLOCK_GROUP_METADATA, owner);
3393         if (!new_inode_group) {
3394                 printk("find_block group failed\n");
3395                 new_inode_group = group;
3396         }
3397         BTRFS_I(inode)->block_group = new_inode_group;
3398
3399         key[0].objectid = objectid;
3400         btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
3401         key[0].offset = 0;
3402
3403         key[1].objectid = objectid;
3404         btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
3405         key[1].offset = ref_objectid;
3406
3407         sizes[0] = sizeof(struct btrfs_inode_item);
3408         sizes[1] = name_len + sizeof(*ref);
3409
3410         ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
3411         if (ret != 0)
3412                 goto fail;
3413
3414         if (objectid > root->highest_inode)
3415                 root->highest_inode = objectid;
3416
3417         inode->i_uid = current->fsuid;
3418         inode->i_gid = current->fsgid;
3419         inode->i_mode = mode;
3420         inode->i_ino = objectid;
3421         inode_set_bytes(inode, 0);
3422         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3423         inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3424                                   struct btrfs_inode_item);
3425         fill_inode_item(trans, path->nodes[0], inode_item, inode);
3426
3427         ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3428                              struct btrfs_inode_ref);
3429         btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
3430         btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
3431         ptr = (unsigned long)(ref + 1);
3432         write_extent_buffer(path->nodes[0], name, ptr, name_len);
3433
3434         btrfs_mark_buffer_dirty(path->nodes[0]);
3435         btrfs_free_path(path);
3436
3437         location = &BTRFS_I(inode)->location;
3438         location->objectid = objectid;
3439         location->offset = 0;
3440         btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
3441
3442         insert_inode_hash(inode);
3443         return inode;
3444 fail:
3445         if (dir)
3446                 BTRFS_I(dir)->index_cnt--;
3447         btrfs_free_path(path);
3448         return ERR_PTR(ret);
3449 }
3450
3451 static inline u8 btrfs_inode_type(struct inode *inode)
3452 {
3453         return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
3454 }
3455
3456 /*
3457  * utility function to add 'inode' into 'parent_inode' with
3458  * a give name and a given sequence number.
3459  * if 'add_backref' is true, also insert a backref from the
3460  * inode to the parent directory.
3461  */
3462 int btrfs_add_link(struct btrfs_trans_handle *trans,
3463                    struct inode *parent_inode, struct inode *inode,
3464                    const char *name, int name_len, int add_backref, u64 index)
3465 {
3466         int ret;
3467         struct btrfs_key key;
3468         struct btrfs_root *root = BTRFS_I(parent_inode)->root;
3469
3470         key.objectid = inode->i_ino;
3471         btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
3472         key.offset = 0;
3473
3474         ret = btrfs_insert_dir_item(trans, root, name, name_len,
3475                                     parent_inode->i_ino,
3476                                     &key, btrfs_inode_type(inode),
3477                                     index);
3478         if (ret == 0) {
3479                 if (add_backref) {
3480                         ret = btrfs_insert_inode_ref(trans, root,
3481                                                      name, name_len,
3482                                                      inode->i_ino,
3483                                                      parent_inode->i_ino,
3484                                                      index);
3485                 }
3486                 btrfs_i_size_write(parent_inode, parent_inode->i_size +
3487                                    name_len * 2);
3488                 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
3489                 ret = btrfs_update_inode(trans, root, parent_inode);
3490         }
3491         return ret;
3492 }
3493
3494 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
3495                             struct dentry *dentry, struct inode *inode,
3496                             int backref, u64 index)
3497 {
3498         int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3499                                  inode, dentry->d_name.name,
3500                                  dentry->d_name.len, backref, index);
3501         if (!err) {
3502                 d_instantiate(dentry, inode);
3503                 return 0;
3504         }
3505         if (err > 0)
3506                 err = -EEXIST;
3507         return err;
3508 }
3509
3510 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
3511                         int mode, dev_t rdev)
3512 {
3513         struct btrfs_trans_handle *trans;
3514         struct btrfs_root *root = BTRFS_I(dir)->root;
3515         struct inode *inode = NULL;
3516         int err;
3517         int drop_inode = 0;
3518         u64 objectid;
3519         unsigned long nr = 0;
3520         u64 index = 0;
3521
3522         if (!new_valid_dev(rdev))
3523                 return -EINVAL;
3524
3525         err = btrfs_check_free_space(root, 1, 0);
3526         if (err)
3527                 goto fail;
3528
3529         trans = btrfs_start_transaction(root, 1);
3530         btrfs_set_trans_block_group(trans, dir);
3531
3532         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3533         if (err) {
3534                 err = -ENOSPC;
3535                 goto out_unlock;
3536         }
3537
3538         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3539                                 dentry->d_name.len,
3540                                 dentry->d_parent->d_inode->i_ino, objectid,
3541                                 BTRFS_I(dir)->block_group, mode, &index);
3542         err = PTR_ERR(inode);
3543         if (IS_ERR(inode))
3544                 goto out_unlock;
3545
3546         err = btrfs_init_acl(inode, dir);
3547         if (err) {
3548                 drop_inode = 1;
3549                 goto out_unlock;
3550         }
3551
3552         btrfs_set_trans_block_group(trans, inode);
3553         err = btrfs_add_nondir(trans, dentry, inode, 0, index);
3554         if (err)
3555                 drop_inode = 1;
3556         else {
3557                 inode->i_op = &btrfs_special_inode_operations;
3558                 init_special_inode(inode, inode->i_mode, rdev);
3559                 btrfs_update_inode(trans, root, inode);
3560         }
3561         dir->i_sb->s_dirt = 1;
3562         btrfs_update_inode_block_group(trans, inode);
3563         btrfs_update_inode_block_group(trans, dir);
3564 out_unlock:
3565         nr = trans->blocks_used;
3566         btrfs_end_transaction_throttle(trans, root);
3567 fail:
3568         if (drop_inode) {
3569                 inode_dec_link_count(inode);
3570                 iput(inode);
3571         }
3572         btrfs_btree_balance_dirty(root, nr);
3573         return err;
3574 }
3575
3576 static int btrfs_create(struct inode *dir, struct dentry *dentry,
3577                         int mode, struct nameidata *nd)
3578 {
3579         struct btrfs_trans_handle *trans;
3580         struct btrfs_root *root = BTRFS_I(dir)->root;
3581         struct inode *inode = NULL;
3582         int err;
3583         int drop_inode = 0;
3584         unsigned long nr = 0;
3585         u64 objectid;
3586         u64 index = 0;
3587
3588         err = btrfs_check_free_space(root, 1, 0);
3589         if (err)
3590                 goto fail;
3591         trans = btrfs_start_transaction(root, 1);
3592         btrfs_set_trans_block_group(trans, dir);
3593
3594         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3595         if (err) {
3596                 err = -ENOSPC;
3597                 goto out_unlock;
3598         }
3599
3600         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3601                                 dentry->d_name.len,
3602                                 dentry->d_parent->d_inode->i_ino,
3603                                 objectid, BTRFS_I(dir)->block_group, mode,
3604                                 &index);
3605         err = PTR_ERR(inode);
3606         if (IS_ERR(inode))
3607                 goto out_unlock;
3608
3609         err = btrfs_init_acl(inode, dir);
3610         if (err) {
3611                 drop_inode = 1;
3612                 goto out_unlock;
3613         }
3614
3615         btrfs_set_trans_block_group(trans, inode);
3616         err = btrfs_add_nondir(trans, dentry, inode, 0, index);
3617         if (err)
3618                 drop_inode = 1;
3619         else {
3620                 inode->i_mapping->a_ops = &btrfs_aops;
3621                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3622                 inode->i_fop = &btrfs_file_operations;
3623                 inode->i_op = &btrfs_file_inode_operations;
3624                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3625         }
3626         dir->i_sb->s_dirt = 1;
3627         btrfs_update_inode_block_group(trans, inode);
3628         btrfs_update_inode_block_group(trans, dir);
3629 out_unlock:
3630         nr = trans->blocks_used;
3631         btrfs_end_transaction_throttle(trans, root);
3632 fail:
3633         if (drop_inode) {
3634                 inode_dec_link_count(inode);
3635                 iput(inode);
3636         }
3637         btrfs_btree_balance_dirty(root, nr);
3638         return err;
3639 }
3640
3641 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
3642                       struct dentry *dentry)
3643 {
3644         struct btrfs_trans_handle *trans;
3645         struct btrfs_root *root = BTRFS_I(dir)->root;
3646         struct inode *inode = old_dentry->d_inode;
3647         u64 index;
3648         unsigned long nr = 0;
3649         int err;
3650         int drop_inode = 0;
3651
3652         if (inode->i_nlink == 0)
3653                 return -ENOENT;
3654
3655         btrfs_inc_nlink(inode);
3656         err = btrfs_check_free_space(root, 1, 0);
3657         if (err)
3658                 goto fail;
3659         err = btrfs_set_inode_index(dir, inode, &index);
3660         if (err)
3661                 goto fail;
3662
3663         trans = btrfs_start_transaction(root, 1);
3664
3665         btrfs_set_trans_block_group(trans, dir);
3666         atomic_inc(&inode->i_count);
3667
3668         err = btrfs_add_nondir(trans, dentry, inode, 1, index);
3669
3670         if (err)
3671                 drop_inode = 1;
3672
3673         dir->i_sb->s_dirt = 1;
3674         btrfs_update_inode_block_group(trans, dir);
3675         err = btrfs_update_inode(trans, root, inode);
3676
3677         if (err)
3678                 drop_inode = 1;
3679
3680         nr = trans->blocks_used;
3681         btrfs_end_transaction_throttle(trans, root);
3682 fail:
3683         if (drop_inode) {
3684                 inode_dec_link_count(inode);
3685                 iput(inode);
3686         }
3687         btrfs_btree_balance_dirty(root, nr);
3688         return err;
3689 }
3690
3691 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
3692 {
3693         struct inode *inode = NULL;
3694         struct btrfs_trans_handle *trans;
3695         struct btrfs_root *root = BTRFS_I(dir)->root;
3696         int err = 0;
3697         int drop_on_err = 0;
3698         u64 objectid = 0;
3699         u64 index = 0;
3700         unsigned long nr = 1;
3701
3702         err = btrfs_check_free_space(root, 1, 0);
3703         if (err)
3704                 goto out_unlock;
3705
3706         trans = btrfs_start_transaction(root, 1);
3707         btrfs_set_trans_block_group(trans, dir);
3708
3709         if (IS_ERR(trans)) {
3710                 err = PTR_ERR(trans);
3711                 goto out_unlock;
3712         }
3713
3714         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3715         if (err) {
3716                 err = -ENOSPC;
3717                 goto out_unlock;
3718         }
3719
3720         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3721                                 dentry->d_name.len,
3722                                 dentry->d_parent->d_inode->i_ino, objectid,
3723                                 BTRFS_I(dir)->block_group, S_IFDIR | mode,
3724                                 &index);
3725         if (IS_ERR(inode)) {
3726                 err = PTR_ERR(inode);
3727                 goto out_fail;
3728         }
3729
3730         drop_on_err = 1;
3731
3732         err = btrfs_init_acl(inode, dir);
3733         if (err)
3734                 goto out_fail;
3735
3736         inode->i_op = &btrfs_dir_inode_operations;
3737         inode->i_fop = &btrfs_dir_file_operations;
3738         btrfs_set_trans_block_group(trans, inode);
3739
3740         btrfs_i_size_write(inode, 0);
3741         err = btrfs_update_inode(trans, root, inode);
3742         if (err)
3743                 goto out_fail;
3744
3745         err = btrfs_add_link(trans, dentry->d_parent->d_inode,
3746                                  inode, dentry->d_name.name,
3747                                  dentry->d_name.len, 0, index);
3748         if (err)
3749                 goto out_fail;
3750
3751         d_instantiate(dentry, inode);
3752         drop_on_err = 0;
3753         dir->i_sb->s_dirt = 1;
3754         btrfs_update_inode_block_group(trans, inode);
3755         btrfs_update_inode_block_group(trans, dir);
3756
3757 out_fail:
3758         nr = trans->blocks_used;
3759         btrfs_end_transaction_throttle(trans, root);
3760
3761 out_unlock:
3762         if (drop_on_err)
3763                 iput(inode);
3764         btrfs_btree_balance_dirty(root, nr);
3765         return err;
3766 }
3767
3768 /* helper for btfs_get_extent.  Given an existing extent in the tree,
3769  * and an extent that you want to insert, deal with overlap and insert
3770  * the new extent into the tree.
3771  */
3772 static int merge_extent_mapping(struct extent_map_tree *em_tree,
3773                                 struct extent_map *existing,
3774                                 struct extent_map *em,
3775                                 u64 map_start, u64 map_len)
3776 {
3777         u64 start_diff;
3778
3779         BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
3780         start_diff = map_start - em->start;
3781         em->start = map_start;
3782         em->len = map_len;
3783         if (em->block_start < EXTENT_MAP_LAST_BYTE &&
3784             !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
3785                 em->block_start += start_diff;
3786                 em->block_len -= start_diff;
3787         }
3788         return add_extent_mapping(em_tree, em);
3789 }
3790
3791 static noinline int uncompress_inline(struct btrfs_path *path,
3792                                       struct inode *inode, struct page *page,
3793                                       size_t pg_offset, u64 extent_offset,
3794                                       struct btrfs_file_extent_item *item)
3795 {
3796         int ret;
3797         struct extent_buffer *leaf = path->nodes[0];
3798         char *tmp;
3799         size_t max_size;
3800         unsigned long inline_size;
3801         unsigned long ptr;
3802
3803         WARN_ON(pg_offset != 0);
3804         max_size = btrfs_file_extent_ram_bytes(leaf, item);
3805         inline_size = btrfs_file_extent_inline_item_len(leaf,
3806                                         btrfs_item_nr(leaf, path->slots[0]));
3807         tmp = kmalloc(inline_size, GFP_NOFS);
3808         ptr = btrfs_file_extent_inline_start(item);
3809
3810         read_extent_buffer(leaf, tmp, ptr, inline_size);
3811
3812         max_size = min(PAGE_CACHE_SIZE, max_size);
3813         ret = btrfs_zlib_decompress(tmp, page, extent_offset,
3814                                     inline_size, max_size);
3815         if (ret) {
3816                 char *kaddr = kmap_atomic(page, KM_USER0);
3817                 unsigned long copy_size = min_t(u64,
3818                                   PAGE_CACHE_SIZE - pg_offset,
3819                                   max_size - extent_offset);
3820                 memset(kaddr + pg_offset, 0, copy_size);
3821                 kunmap_atomic(kaddr, KM_USER0);
3822         }
3823         kfree(tmp);
3824         return 0;
3825 }
3826
3827 /*
3828  * a bit scary, this does extent mapping from logical file offset to the disk.
3829  * the ugly parts come from merging extents from the disk with the
3830  * in-ram representation.  This gets more complex because of the data=ordered code,
3831  * where the in-ram extents might be locked pending data=ordered completion.
3832  *
3833  * This also copies inline extents directly into the page.
3834  */
3835 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
3836                                     size_t pg_offset, u64 start, u64 len,
3837                                     int create)
3838 {
3839         int ret;
3840         int err = 0;
3841         u64 bytenr;
3842         u64 extent_start = 0;
3843         u64 extent_end = 0;
3844         u64 objectid = inode->i_ino;
3845         u32 found_type;
3846         struct btrfs_path *path = NULL;
3847         struct btrfs_root *root = BTRFS_I(inode)->root;
3848         struct btrfs_file_extent_item *item;
3849         struct extent_buffer *leaf;
3850         struct btrfs_key found_key;
3851         struct extent_map *em = NULL;
3852         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
3853         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3854         struct btrfs_trans_handle *trans = NULL;
3855         int compressed;
3856
3857 again:
3858         spin_lock(&em_tree->lock);
3859         em = lookup_extent_mapping(em_tree, start, len);
3860         if (em)
3861                 em->bdev = root->fs_info->fs_devices->latest_bdev;
3862         spin_unlock(&em_tree->lock);
3863
3864         if (em) {
3865                 if (em->start > start || em->start + em->len <= start)
3866                         free_extent_map(em);
3867                 else if (em->block_start == EXTENT_MAP_INLINE && page)
3868                         free_extent_map(em);
3869                 else
3870                         goto out;
3871         }
3872         em = alloc_extent_map(GFP_NOFS);
3873         if (!em) {
3874                 err = -ENOMEM;
3875                 goto out;
3876         }
3877         em->bdev = root->fs_info->fs_devices->latest_bdev;
3878         em->start = EXTENT_MAP_HOLE;
3879         em->len = (u64)-1;
3880         em->block_len = (u64)-1;
3881
3882         if (!path) {
3883                 path = btrfs_alloc_path();
3884                 BUG_ON(!path);
3885         }
3886
3887         ret = btrfs_lookup_file_extent(trans, root, path,
3888                                        objectid, start, trans != NULL);
3889         if (ret < 0) {
3890                 err = ret;
3891                 goto out;
3892         }
3893
3894         if (ret != 0) {
3895                 if (path->slots[0] == 0)
3896                         goto not_found;
3897                 path->slots[0]--;
3898         }
3899
3900         leaf = path->nodes[0];
3901         item = btrfs_item_ptr(leaf, path->slots[0],
3902                               struct btrfs_file_extent_item);
3903         /* are we inside the extent that was found? */
3904         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3905         found_type = btrfs_key_type(&found_key);
3906         if (found_key.objectid != objectid ||
3907             found_type != BTRFS_EXTENT_DATA_KEY) {
3908                 goto not_found;
3909         }
3910
3911         found_type = btrfs_file_extent_type(leaf, item);
3912         extent_start = found_key.offset;
3913         compressed = btrfs_file_extent_compression(leaf, item);
3914         if (found_type == BTRFS_FILE_EXTENT_REG ||
3915             found_type == BTRFS_FILE_EXTENT_PREALLOC) {
3916                 extent_end = extent_start +
3917                        btrfs_file_extent_num_bytes(leaf, item);
3918         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3919                 size_t size;
3920                 size = btrfs_file_extent_inline_len(leaf, item);
3921                 extent_end = (extent_start + size + root->sectorsize - 1) &
3922                         ~((u64)root->sectorsize - 1);
3923         }
3924
3925         if (start >= extent_end) {
3926                 path->slots[0]++;
3927                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
3928                         ret = btrfs_next_leaf(root, path);
3929                         if (ret < 0) {
3930                                 err = ret;
3931                                 goto out;
3932                         }
3933                         if (ret > 0)
3934                                 goto not_found;
3935                         leaf = path->nodes[0];
3936                 }
3937                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3938                 if (found_key.objectid != objectid ||
3939                     found_key.type != BTRFS_EXTENT_DATA_KEY)
3940                         goto not_found;
3941                 if (start + len <= found_key.offset)
3942                         goto not_found;
3943                 em->start = start;
3944                 em->len = found_key.offset - start;
3945                 goto not_found_em;
3946         }
3947
3948         if (found_type == BTRFS_FILE_EXTENT_REG ||
3949             found_type == BTRFS_FILE_EXTENT_PREALLOC) {
3950                 em->start = extent_start;
3951                 em->len = extent_end - extent_start;
3952                 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
3953                 if (bytenr == 0) {
3954                         em->block_start = EXTENT_MAP_HOLE;
3955                         goto insert;
3956                 }
3957                 if (compressed) {
3958                         set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3959                         em->block_start = bytenr;
3960                         em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
3961                                                                          item);
3962                 } else {
3963                         bytenr += btrfs_file_extent_offset(leaf, item);
3964                         em->block_start = bytenr;
3965                         em->block_len = em->len;
3966                         if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
3967                                 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
3968                 }
3969                 goto insert;
3970         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
3971                 unsigned long ptr;
3972                 char *map;
3973                 size_t size;
3974                 size_t extent_offset;
3975                 size_t copy_size;
3976
3977                 em->block_start = EXTENT_MAP_INLINE;
3978                 if (!page || create) {
3979                         em->start = extent_start;
3980                         em->len = extent_end - extent_start;
3981                         goto out;
3982                 }
3983
3984                 size = btrfs_file_extent_inline_len(leaf, item);
3985                 extent_offset = page_offset(page) + pg_offset - extent_start;
3986                 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
3987                                 size - extent_offset);
3988                 em->start = extent_start + extent_offset;
3989                 em->len = (copy_size + root->sectorsize - 1) &
3990                         ~((u64)root->sectorsize - 1);
3991                 if (compressed)
3992                         set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
3993                 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
3994                 if (create == 0 && !PageUptodate(page)) {
3995                         if (btrfs_file_extent_compression(leaf, item) ==
3996                             BTRFS_COMPRESS_ZLIB) {
3997                                 ret = uncompress_inline(path, inode, page,
3998                                                         pg_offset,
3999                                                         extent_offset, item);
4000                                 BUG_ON(ret);
4001                         } else {
4002                                 map = kmap(page);
4003                                 read_extent_buffer(leaf, map + pg_offset, ptr,
4004                                                    copy_size);
4005                                 kunmap(page);
4006                         }
4007                         flush_dcache_page(page);
4008                 } else if (create && PageUptodate(page)) {
4009                         if (!trans) {
4010                                 kunmap(page);
4011                                 free_extent_map(em);
4012                                 em = NULL;
4013                                 btrfs_release_path(root, path);
4014                                 trans = btrfs_join_transaction(root, 1);
4015                                 goto again;
4016                         }
4017                         map = kmap(page);
4018                         write_extent_buffer(leaf, map + pg_offset, ptr,
4019                                             copy_size);
4020                         kunmap(page);
4021                         btrfs_mark_buffer_dirty(leaf);
4022                 }
4023                 set_extent_uptodate(io_tree, em->start,
4024                                     extent_map_end(em) - 1, GFP_NOFS);
4025                 goto insert;
4026         } else {
4027                 printk("unkknown found_type %d\n", found_type);
4028                 WARN_ON(1);
4029         }
4030 not_found:
4031         em->start = start;
4032         em->len = len;
4033 not_found_em:
4034         em->block_start = EXTENT_MAP_HOLE;
4035         set_bit(EXTENT_FLAG_VACANCY, &em->flags);
4036 insert:
4037         btrfs_release_path(root, path);
4038         if (em->start > start || extent_map_end(em) <= start) {
4039                 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
4040                 err = -EIO;
4041                 goto out;
4042         }
4043
4044         err = 0;
4045         spin_lock(&em_tree->lock);
4046         ret = add_extent_mapping(em_tree, em);
4047         /* it is possible that someone inserted the extent into the tree
4048          * while we had the lock dropped.  It is also possible that
4049          * an overlapping map exists in the tree
4050          */
4051         if (ret == -EEXIST) {
4052                 struct extent_map *existing;
4053
4054                 ret = 0;
4055
4056                 existing = lookup_extent_mapping(em_tree, start, len);
4057                 if (existing && (existing->start > start ||
4058                     existing->start + existing->len <= start)) {
4059                         free_extent_map(existing);
4060                         existing = NULL;
4061                 }
4062                 if (!existing) {
4063                         existing = lookup_extent_mapping(em_tree, em->start,
4064                                                          em->len);
4065                         if (existing) {
4066                                 err = merge_extent_mapping(em_tree, existing,
4067                                                            em, start,
4068                                                            root->sectorsize);
4069                                 free_extent_map(existing);
4070                                 if (err) {
4071                                         free_extent_map(em);
4072                                         em = NULL;
4073                                 }
4074                         } else {
4075                                 err = -EIO;
4076                                 printk("failing to insert %Lu %Lu\n",
4077                                        start, len);
4078                                 free_extent_map(em);
4079                                 em = NULL;
4080                         }
4081                 } else {
4082                         free_extent_map(em);
4083                         em = existing;
4084                         err = 0;
4085                 }
4086         }
4087         spin_unlock(&em_tree->lock);
4088 out:
4089         if (path)
4090                 btrfs_free_path(path);
4091         if (trans) {
4092                 ret = btrfs_end_transaction(trans, root);
4093                 if (!err) {
4094                         err = ret;
4095                 }
4096         }
4097         if (err) {
4098                 free_extent_map(em);
4099                 WARN_ON(1);
4100                 return ERR_PTR(err);
4101         }
4102         return em;
4103 }
4104
4105 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4106                         const struct iovec *iov, loff_t offset,
4107                         unsigned long nr_segs)
4108 {
4109         return -EINVAL;
4110 }
4111
4112 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
4113 {
4114         return extent_bmap(mapping, iblock, btrfs_get_extent);
4115 }
4116
4117 int btrfs_readpage(struct file *file, struct page *page)
4118 {
4119         struct extent_io_tree *tree;
4120         tree = &BTRFS_I(page->mapping->host)->io_tree;
4121         return extent_read_full_page(tree, page, btrfs_get_extent);
4122 }
4123
4124 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4125 {
4126         struct extent_io_tree *tree;
4127
4128
4129         if (current->flags & PF_MEMALLOC) {
4130                 redirty_page_for_writepage(wbc, page);
4131                 unlock_page(page);
4132                 return 0;
4133         }
4134         tree = &BTRFS_I(page->mapping->host)->io_tree;
4135         return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4136 }
4137
4138 int btrfs_writepages(struct address_space *mapping,
4139                      struct writeback_control *wbc)
4140 {
4141         struct extent_io_tree *tree;
4142
4143         tree = &BTRFS_I(mapping->host)->io_tree;
4144         return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4145 }
4146
4147 static int
4148 btrfs_readpages(struct file *file, struct address_space *mapping,
4149                 struct list_head *pages, unsigned nr_pages)
4150 {
4151         struct extent_io_tree *tree;
4152         tree = &BTRFS_I(mapping->host)->io_tree;
4153         return extent_readpages(tree, mapping, pages, nr_pages,
4154                                 btrfs_get_extent);
4155 }
4156 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4157 {
4158         struct extent_io_tree *tree;
4159         struct extent_map_tree *map;
4160         int ret;
4161
4162         tree = &BTRFS_I(page->mapping->host)->io_tree;
4163         map = &BTRFS_I(page->mapping->host)->extent_tree;
4164         ret = try_release_extent_mapping(map, tree, page, gfp_flags);
4165         if (ret == 1) {
4166                 ClearPagePrivate(page);
4167                 set_page_private(page, 0);
4168                 page_cache_release(page);
4169         }
4170         return ret;
4171 }
4172
4173 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4174 {
4175         if (PageWriteback(page) || PageDirty(page))
4176                 return 0;
4177         return __btrfs_releasepage(page, gfp_flags);
4178 }
4179
4180 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4181 {
4182         struct extent_io_tree *tree;
4183         struct btrfs_ordered_extent *ordered;
4184         u64 page_start = page_offset(page);
4185         u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
4186
4187         wait_on_page_writeback(page);
4188         tree = &BTRFS_I(page->mapping->host)->io_tree;
4189         if (offset) {
4190                 btrfs_releasepage(page, GFP_NOFS);
4191                 return;
4192         }
4193
4194         lock_extent(tree, page_start, page_end, GFP_NOFS);
4195         ordered = btrfs_lookup_ordered_extent(page->mapping->host,
4196                                            page_offset(page));
4197         if (ordered) {
4198                 /*
4199                  * IO on this page will never be started, so we need
4200                  * to account for any ordered extents now
4201                  */
4202                 clear_extent_bit(tree, page_start, page_end,
4203                                  EXTENT_DIRTY | EXTENT_DELALLOC |
4204                                  EXTENT_LOCKED, 1, 0, GFP_NOFS);
4205                 btrfs_finish_ordered_io(page->mapping->host,
4206                                         page_start, page_end);
4207                 btrfs_put_ordered_extent(ordered);
4208                 lock_extent(tree, page_start, page_end, GFP_NOFS);
4209         }
4210         clear_extent_bit(tree, page_start, page_end,
4211                  EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
4212                  EXTENT_ORDERED,
4213                  1, 1, GFP_NOFS);
4214         __btrfs_releasepage(page, GFP_NOFS);
4215
4216         ClearPageChecked(page);
4217         if (PagePrivate(page)) {
4218                 ClearPagePrivate(page);
4219                 set_page_private(page, 0);
4220                 page_cache_release(page);
4221         }
4222 }
4223
4224 /*
4225  * btrfs_page_mkwrite() is not allowed to change the file size as it gets
4226  * called from a page fault handler when a page is first dirtied. Hence we must
4227  * be careful to check for EOF conditions here. We set the page up correctly
4228  * for a written page which means we get ENOSPC checking when writing into
4229  * holes and correct delalloc and unwritten extent mapping on filesystems that
4230  * support these features.
4231  *
4232  * We are not allowed to take the i_mutex here so we have to play games to
4233  * protect against truncate races as the page could now be beyond EOF.  Because
4234  * vmtruncate() writes the inode size before removing pages, once we have the
4235  * page lock we can determine safely if the page is beyond EOF. If it is not
4236  * beyond EOF, then the page is guaranteed safe against truncation until we
4237  * unlock the page.
4238  */
4239 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
4240 {
4241         struct inode *inode = fdentry(vma->vm_file)->d_inode;
4242         struct btrfs_root *root = BTRFS_I(inode)->root;
4243         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4244         struct btrfs_ordered_extent *ordered;
4245         char *kaddr;
4246         unsigned long zero_start;
4247         loff_t size;
4248         int ret;
4249         u64 page_start;
4250         u64 page_end;
4251
4252         ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
4253         if (ret)
4254                 goto out;
4255
4256         ret = -EINVAL;
4257 again:
4258         lock_page(page);
4259         size = i_size_read(inode);
4260         page_start = page_offset(page);
4261         page_end = page_start + PAGE_CACHE_SIZE - 1;
4262
4263         if ((page->mapping != inode->i_mapping) ||
4264             (page_start >= size)) {
4265                 /* page got truncated out from underneath us */
4266                 goto out_unlock;
4267         }
4268         wait_on_page_writeback(page);
4269
4270         lock_extent(io_tree, page_start, page_end, GFP_NOFS);
4271         set_page_extent_mapped(page);
4272
4273         /*
4274          * we can't set the delalloc bits if there are pending ordered
4275          * extents.  Drop our locks and wait for them to finish
4276          */
4277         ordered = btrfs_lookup_ordered_extent(inode, page_start);
4278         if (ordered) {
4279                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4280                 unlock_page(page);
4281                 btrfs_start_ordered_extent(inode, ordered, 1);
4282                 btrfs_put_ordered_extent(ordered);
4283                 goto again;
4284         }
4285
4286         btrfs_set_extent_delalloc(inode, page_start, page_end);
4287         ret = 0;
4288
4289         /* page is wholly or partially inside EOF */
4290         if (page_start + PAGE_CACHE_SIZE > size)
4291                 zero_start = size & ~PAGE_CACHE_MASK;
4292         else
4293                 zero_start = PAGE_CACHE_SIZE;
4294
4295         if (zero_start != PAGE_CACHE_SIZE) {
4296                 kaddr = kmap(page);
4297                 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
4298                 flush_dcache_page(page);
4299                 kunmap(page);
4300         }
4301         ClearPageChecked(page);
4302         set_page_dirty(page);
4303         unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4304
4305 out_unlock:
4306         unlock_page(page);
4307 out:
4308         return ret;
4309 }
4310
4311 static void btrfs_truncate(struct inode *inode)
4312 {
4313         struct btrfs_root *root = BTRFS_I(inode)->root;
4314         int ret;
4315         struct btrfs_trans_handle *trans;
4316         unsigned long nr;
4317         u64 mask = root->sectorsize - 1;
4318
4319         if (!S_ISREG(inode->i_mode))
4320                 return;
4321         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
4322                 return;
4323
4324         btrfs_truncate_page(inode->i_mapping, inode->i_size);
4325         btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
4326
4327         trans = btrfs_start_transaction(root, 1);
4328         btrfs_set_trans_block_group(trans, inode);
4329         btrfs_i_size_write(inode, inode->i_size);
4330
4331         ret = btrfs_orphan_add(trans, inode);
4332         if (ret)
4333                 goto out;
4334         /* FIXME, add redo link to tree so we don't leak on crash */
4335         ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
4336                                       BTRFS_EXTENT_DATA_KEY);
4337         btrfs_update_inode(trans, root, inode);
4338
4339         ret = btrfs_orphan_del(trans, inode);
4340         BUG_ON(ret);
4341
4342 out:
4343         nr = trans->blocks_used;
4344         ret = btrfs_end_transaction_throttle(trans, root);
4345         BUG_ON(ret);
4346         btrfs_btree_balance_dirty(root, nr);
4347 }
4348
4349 /*
4350  * Invalidate a single dcache entry at the root of the filesystem.
4351  * Needed after creation of snapshot or subvolume.
4352  */
4353 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
4354                                   int namelen)
4355 {
4356         struct dentry *alias, *entry;
4357         struct qstr qstr;
4358
4359         alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
4360         if (alias) {
4361                 qstr.name = name;
4362                 qstr.len = namelen;
4363                 /* change me if btrfs ever gets a d_hash operation */
4364                 qstr.hash = full_name_hash(qstr.name, qstr.len);
4365                 entry = d_lookup(alias, &qstr);
4366                 dput(alias);
4367                 if (entry) {
4368                         d_invalidate(entry);
4369                         dput(entry);
4370                 }
4371         }
4372 }
4373
4374 /*
4375  * create a new subvolume directory/inode (helper for the ioctl).
4376  */
4377 int btrfs_create_subvol_root(struct btrfs_root *new_root, struct dentry *dentry,
4378                 struct btrfs_trans_handle *trans, u64 new_dirid,
4379                 struct btrfs_block_group_cache *block_group)
4380 {
4381         struct inode *inode;
4382         int error;
4383         u64 index = 0;
4384
4385         inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
4386                                 new_dirid, block_group, S_IFDIR | 0700, &index);
4387         if (IS_ERR(inode))
4388                 return PTR_ERR(inode);
4389         inode->i_op = &btrfs_dir_inode_operations;
4390         inode->i_fop = &btrfs_dir_file_operations;
4391         new_root->inode = inode;
4392
4393         inode->i_nlink = 1;
4394         btrfs_i_size_write(inode, 0);
4395
4396         error = btrfs_update_inode(trans, new_root, inode);
4397         if (error)
4398                 return error;
4399
4400         atomic_inc(&inode->i_count);
4401         d_instantiate(dentry, inode);
4402         return 0;
4403 }
4404
4405 /* helper function for file defrag and space balancing.  This
4406  * forces readahead on a given range of bytes in an inode
4407  */
4408 unsigned long btrfs_force_ra(struct address_space *mapping,
4409                               struct file_ra_state *ra, struct file *file,
4410                               pgoff_t offset, pgoff_t last_index)
4411 {
4412         pgoff_t req_size = last_index - offset + 1;
4413
4414         page_cache_sync_readahead(mapping, ra, file, offset, req_size);
4415         return offset + req_size;
4416 }
4417
4418 struct inode *btrfs_alloc_inode(struct super_block *sb)
4419 {
4420         struct btrfs_inode *ei;
4421
4422         ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
4423         if (!ei)
4424                 return NULL;
4425         ei->last_trans = 0;
4426         ei->logged_trans = 0;
4427         btrfs_ordered_inode_tree_init(&ei->ordered_tree);
4428         ei->i_acl = BTRFS_ACL_NOT_CACHED;
4429         ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
4430         INIT_LIST_HEAD(&ei->i_orphan);
4431         return &ei->vfs_inode;
4432 }
4433
4434 void btrfs_destroy_inode(struct inode *inode)
4435 {
4436         struct btrfs_ordered_extent *ordered;
4437         WARN_ON(!list_empty(&inode->i_dentry));
4438         WARN_ON(inode->i_data.nrpages);
4439
4440         if (BTRFS_I(inode)->i_acl &&
4441             BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
4442                 posix_acl_release(BTRFS_I(inode)->i_acl);
4443         if (BTRFS_I(inode)->i_default_acl &&
4444             BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
4445                 posix_acl_release(BTRFS_I(inode)->i_default_acl);
4446
4447         spin_lock(&BTRFS_I(inode)->root->list_lock);
4448         if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
4449                 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
4450                        " list\n", inode->i_ino);
4451                 dump_stack();
4452         }
4453         spin_unlock(&BTRFS_I(inode)->root->list_lock);
4454
4455         while(1) {
4456                 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
4457                 if (!ordered)
4458                         break;
4459                 else {
4460                         printk("found ordered extent %Lu %Lu\n",
4461                                ordered->file_offset, ordered->len);
4462                         btrfs_remove_ordered_extent(inode, ordered);
4463                         btrfs_put_ordered_extent(ordered);
4464                         btrfs_put_ordered_extent(ordered);
4465                 }
4466         }
4467         btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
4468         kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
4469 }
4470
4471 static void init_once(void *foo)
4472 {
4473         struct btrfs_inode *ei = (struct btrfs_inode *) foo;
4474
4475         inode_init_once(&ei->vfs_inode);
4476 }
4477
4478 void btrfs_destroy_cachep(void)
4479 {
4480         if (btrfs_inode_cachep)
4481                 kmem_cache_destroy(btrfs_inode_cachep);
4482         if (btrfs_trans_handle_cachep)
4483                 kmem_cache_destroy(btrfs_trans_handle_cachep);
4484         if (btrfs_transaction_cachep)
4485                 kmem_cache_destroy(btrfs_transaction_cachep);
4486         if (btrfs_bit_radix_cachep)
4487                 kmem_cache_destroy(btrfs_bit_radix_cachep);
4488         if (btrfs_path_cachep)
4489                 kmem_cache_destroy(btrfs_path_cachep);
4490 }
4491
4492 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
4493                                        unsigned long extra_flags,
4494                                        void (*ctor)(void *))
4495 {
4496         return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
4497                                  SLAB_MEM_SPREAD | extra_flags), ctor);
4498 }
4499
4500 int btrfs_init_cachep(void)
4501 {
4502         btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
4503                                           sizeof(struct btrfs_inode),
4504                                           0, init_once);
4505         if (!btrfs_inode_cachep)
4506                 goto fail;
4507         btrfs_trans_handle_cachep =
4508                         btrfs_cache_create("btrfs_trans_handle_cache",
4509                                            sizeof(struct btrfs_trans_handle),
4510                                            0, NULL);
4511         if (!btrfs_trans_handle_cachep)
4512                 goto fail;
4513         btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
4514                                              sizeof(struct btrfs_transaction),
4515                                              0, NULL);
4516         if (!btrfs_transaction_cachep)
4517                 goto fail;
4518         btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
4519                                          sizeof(struct btrfs_path),
4520                                          0, NULL);
4521         if (!btrfs_path_cachep)
4522                 goto fail;
4523         btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
4524                                               SLAB_DESTROY_BY_RCU, NULL);
4525         if (!btrfs_bit_radix_cachep)
4526                 goto fail;
4527         return 0;
4528 fail:
4529         btrfs_destroy_cachep();
4530         return -ENOMEM;
4531 }
4532
4533 static int btrfs_getattr(struct vfsmount *mnt,
4534                          struct dentry *dentry, struct kstat *stat)
4535 {
4536         struct inode *inode = dentry->d_inode;
4537         generic_fillattr(inode, stat);
4538         stat->blksize = PAGE_CACHE_SIZE;
4539         stat->blocks = (inode_get_bytes(inode) +
4540                         BTRFS_I(inode)->delalloc_bytes) >> 9;
4541         return 0;
4542 }
4543
4544 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
4545                            struct inode * new_dir,struct dentry *new_dentry)
4546 {
4547         struct btrfs_trans_handle *trans;
4548         struct btrfs_root *root = BTRFS_I(old_dir)->root;
4549         struct inode *new_inode = new_dentry->d_inode;
4550         struct inode *old_inode = old_dentry->d_inode;
4551         struct timespec ctime = CURRENT_TIME;
4552         u64 index = 0;
4553         int ret;
4554
4555         if (S_ISDIR(old_inode->i_mode) && new_inode &&
4556             new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
4557                 return -ENOTEMPTY;
4558         }
4559
4560         ret = btrfs_check_free_space(root, 1, 0);
4561         if (ret)
4562                 goto out_unlock;
4563
4564         trans = btrfs_start_transaction(root, 1);
4565
4566         btrfs_set_trans_block_group(trans, new_dir);
4567
4568         btrfs_inc_nlink(old_dentry->d_inode);
4569         old_dir->i_ctime = old_dir->i_mtime = ctime;
4570         new_dir->i_ctime = new_dir->i_mtime = ctime;
4571         old_inode->i_ctime = ctime;
4572
4573         ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
4574                                  old_dentry->d_name.name,
4575                                  old_dentry->d_name.len);
4576         if (ret)
4577                 goto out_fail;
4578
4579         if (new_inode) {
4580                 new_inode->i_ctime = CURRENT_TIME;
4581                 ret = btrfs_unlink_inode(trans, root, new_dir,
4582                                          new_dentry->d_inode,
4583                                          new_dentry->d_name.name,
4584                                          new_dentry->d_name.len);
4585                 if (ret)
4586                         goto out_fail;
4587                 if (new_inode->i_nlink == 0) {
4588                         ret = btrfs_orphan_add(trans, new_dentry->d_inode);
4589                         if (ret)
4590                                 goto out_fail;
4591                 }
4592
4593         }
4594         ret = btrfs_set_inode_index(new_dir, old_inode, &index);
4595         if (ret)
4596                 goto out_fail;
4597
4598         ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
4599                              old_inode, new_dentry->d_name.name,
4600                              new_dentry->d_name.len, 1, index);
4601         if (ret)
4602                 goto out_fail;
4603
4604 out_fail:
4605         btrfs_end_transaction_throttle(trans, root);
4606 out_unlock:
4607         return ret;
4608 }
4609
4610 /*
4611  * some fairly slow code that needs optimization. This walks the list
4612  * of all the inodes with pending delalloc and forces them to disk.
4613  */
4614 int btrfs_start_delalloc_inodes(struct btrfs_root *root)
4615 {
4616         struct list_head *head = &root->fs_info->delalloc_inodes;
4617         struct btrfs_inode *binode;
4618         struct inode *inode;
4619         unsigned long flags;
4620
4621         spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
4622         while(!list_empty(head)) {
4623                 binode = list_entry(head->next, struct btrfs_inode,
4624                                     delalloc_inodes);
4625                 inode = igrab(&binode->vfs_inode);
4626                 if (!inode)
4627                         list_del_init(&binode->delalloc_inodes);
4628                 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
4629                 if (inode) {
4630                         filemap_flush(inode->i_mapping);
4631                         iput(inode);
4632                 }
4633                 cond_resched();
4634                 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
4635         }
4636         spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
4637
4638         /* the filemap_flush will queue IO into the worker threads, but
4639          * we have to make sure the IO is actually started and that
4640          * ordered extents get created before we return
4641          */
4642         atomic_inc(&root->fs_info->async_submit_draining);
4643         while(atomic_read(&root->fs_info->nr_async_submits) ||
4644               atomic_read(&root->fs_info->async_delalloc_pages)) {
4645                 wait_event(root->fs_info->async_submit_wait,
4646                    (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
4647                     atomic_read(&root->fs_info->async_delalloc_pages) == 0));
4648         }
4649         atomic_dec(&root->fs_info->async_submit_draining);
4650         return 0;
4651 }
4652
4653 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
4654                          const char *symname)
4655 {
4656         struct btrfs_trans_handle *trans;
4657         struct btrfs_root *root = BTRFS_I(dir)->root;
4658         struct btrfs_path *path;
4659         struct btrfs_key key;
4660         struct inode *inode = NULL;
4661         int err;
4662         int drop_inode = 0;
4663         u64 objectid;
4664         u64 index = 0 ;
4665         int name_len;
4666         int datasize;
4667         unsigned long ptr;
4668         struct btrfs_file_extent_item *ei;
4669         struct extent_buffer *leaf;
4670         unsigned long nr = 0;
4671
4672         name_len = strlen(symname) + 1;
4673         if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
4674                 return -ENAMETOOLONG;
4675
4676         err = btrfs_check_free_space(root, 1, 0);
4677         if (err)
4678                 goto out_fail;
4679
4680         trans = btrfs_start_transaction(root, 1);
4681         btrfs_set_trans_block_group(trans, dir);
4682
4683         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4684         if (err) {
4685                 err = -ENOSPC;
4686                 goto out_unlock;
4687         }
4688
4689         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4690                                 dentry->d_name.len,
4691                                 dentry->d_parent->d_inode->i_ino, objectid,
4692                                 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
4693                                 &index);
4694         err = PTR_ERR(inode);
4695         if (IS_ERR(inode))
4696                 goto out_unlock;
4697
4698         err = btrfs_init_acl(inode, dir);
4699         if (err) {
4700                 drop_inode = 1;
4701                 goto out_unlock;
4702         }
4703
4704         btrfs_set_trans_block_group(trans, inode);
4705         err = btrfs_add_nondir(trans, dentry, inode, 0, index);
4706         if (err)
4707                 drop_inode = 1;
4708         else {
4709                 inode->i_mapping->a_ops = &btrfs_aops;
4710                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
4711                 inode->i_fop = &btrfs_file_operations;
4712                 inode->i_op = &btrfs_file_inode_operations;
4713                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
4714         }
4715         dir->i_sb->s_dirt = 1;
4716         btrfs_update_inode_block_group(trans, inode);
4717         btrfs_update_inode_block_group(trans, dir);
4718         if (drop_inode)
4719                 goto out_unlock;
4720
4721         path = btrfs_alloc_path();
4722         BUG_ON(!path);
4723         key.objectid = inode->i_ino;
4724         key.offset = 0;
4725         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
4726         datasize = btrfs_file_extent_calc_inline_size(name_len);
4727         err = btrfs_insert_empty_item(trans, root, path, &key,
4728                                       datasize);
4729         if (err) {
4730                 drop_inode = 1;
4731                 goto out_unlock;
4732         }
4733         leaf = path->nodes[0];
4734         ei = btrfs_item_ptr(leaf, path->slots[0],
4735                             struct btrfs_file_extent_item);
4736         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
4737         btrfs_set_file_extent_type(leaf, ei,
4738                                    BTRFS_FILE_EXTENT_INLINE);
4739         btrfs_set_file_extent_encryption(leaf, ei, 0);
4740         btrfs_set_file_extent_compression(leaf, ei, 0);
4741         btrfs_set_file_extent_other_encoding(leaf, ei, 0);
4742         btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
4743
4744         ptr = btrfs_file_extent_inline_start(ei);
4745         write_extent_buffer(leaf, symname, ptr, name_len);
4746         btrfs_mark_buffer_dirty(leaf);
4747         btrfs_free_path(path);
4748
4749         inode->i_op = &btrfs_symlink_inode_operations;
4750         inode->i_mapping->a_ops = &btrfs_symlink_aops;
4751         inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
4752         inode_set_bytes(inode, name_len);
4753         btrfs_i_size_write(inode, name_len - 1);
4754         err = btrfs_update_inode(trans, root, inode);
4755         if (err)
4756                 drop_inode = 1;
4757
4758 out_unlock:
4759         nr = trans->blocks_used;
4760         btrfs_end_transaction_throttle(trans, root);
4761 out_fail:
4762         if (drop_inode) {
4763                 inode_dec_link_count(inode);
4764                 iput(inode);
4765         }
4766         btrfs_btree_balance_dirty(root, nr);
4767         return err;
4768 }
4769
4770 static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
4771                                u64 alloc_hint, int mode)
4772 {
4773         struct btrfs_trans_handle *trans;
4774         struct btrfs_root *root = BTRFS_I(inode)->root;
4775         struct btrfs_key ins;
4776         u64 alloc_size;
4777         u64 cur_offset = start;
4778         u64 num_bytes = end - start;
4779         int ret = 0;
4780
4781         trans = btrfs_join_transaction(root, 1);
4782         BUG_ON(!trans);
4783         btrfs_set_trans_block_group(trans, inode);
4784
4785         while (num_bytes > 0) {
4786                 alloc_size = min(num_bytes, root->fs_info->max_extent);
4787                 ret = btrfs_reserve_extent(trans, root, alloc_size,
4788                                            root->sectorsize, 0, alloc_hint,
4789                                            (u64)-1, &ins, 1);
4790                 if (ret) {
4791                         WARN_ON(1);
4792                         goto out;
4793                 }
4794                 ret = insert_reserved_file_extent(trans, inode,
4795                                                   cur_offset, ins.objectid,
4796                                                   ins.offset, ins.offset,
4797                                                   ins.offset, 0, 0, 0,
4798                                                   BTRFS_FILE_EXTENT_PREALLOC);
4799                 BUG_ON(ret);
4800                 num_bytes -= ins.offset;
4801                 cur_offset += ins.offset;
4802                 alloc_hint = ins.objectid + ins.offset;
4803         }
4804 out:
4805         if (cur_offset > start) {
4806                 inode->i_ctime = CURRENT_TIME;
4807                 btrfs_set_flag(inode, PREALLOC);
4808                 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
4809                     cur_offset > i_size_read(inode))
4810                         btrfs_i_size_write(inode, cur_offset);
4811                 ret = btrfs_update_inode(trans, root, inode);
4812                 BUG_ON(ret);
4813         }
4814
4815         btrfs_end_transaction(trans, root);
4816         return ret;
4817 }
4818
4819 static long btrfs_fallocate(struct inode *inode, int mode,
4820                             loff_t offset, loff_t len)
4821 {
4822         u64 cur_offset;
4823         u64 last_byte;
4824         u64 alloc_start;
4825         u64 alloc_end;
4826         u64 alloc_hint = 0;
4827         u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
4828         struct extent_map *em;
4829         int ret;
4830
4831         alloc_start = offset & ~mask;
4832         alloc_end =  (offset + len + mask) & ~mask;
4833
4834         mutex_lock(&inode->i_mutex);
4835         if (alloc_start > inode->i_size) {
4836                 ret = btrfs_cont_expand(inode, alloc_start);
4837                 if (ret)
4838                         goto out;
4839         }
4840
4841         while (1) {
4842                 struct btrfs_ordered_extent *ordered;
4843                 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start,
4844                             alloc_end - 1, GFP_NOFS);
4845                 ordered = btrfs_lookup_first_ordered_extent(inode,
4846                                                             alloc_end - 1);
4847                 if (ordered &&
4848                     ordered->file_offset + ordered->len > alloc_start &&
4849                     ordered->file_offset < alloc_end) {
4850                         btrfs_put_ordered_extent(ordered);
4851                         unlock_extent(&BTRFS_I(inode)->io_tree,
4852                                       alloc_start, alloc_end - 1, GFP_NOFS);
4853                         btrfs_wait_ordered_range(inode, alloc_start,
4854                                                  alloc_end - alloc_start);
4855                 } else {
4856                         if (ordered)
4857                                 btrfs_put_ordered_extent(ordered);
4858                         break;
4859                 }
4860         }
4861
4862         cur_offset = alloc_start;
4863         while (1) {
4864                 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4865                                       alloc_end - cur_offset, 0);
4866                 BUG_ON(IS_ERR(em) || !em);
4867                 last_byte = min(extent_map_end(em), alloc_end);
4868                 last_byte = (last_byte + mask) & ~mask;
4869                 if (em->block_start == EXTENT_MAP_HOLE) {
4870                         ret = prealloc_file_range(inode, cur_offset,
4871                                         last_byte, alloc_hint, mode);
4872                         if (ret < 0) {
4873                                 free_extent_map(em);
4874                                 break;
4875                         }
4876                 }
4877                 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
4878                         alloc_hint = em->block_start;
4879                 free_extent_map(em);
4880
4881                 cur_offset = last_byte;
4882                 if (cur_offset >= alloc_end) {
4883                         ret = 0;
4884                         break;
4885                 }
4886         }
4887         unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, alloc_end - 1,
4888                       GFP_NOFS);
4889 out:
4890         mutex_unlock(&inode->i_mutex);
4891         return ret;
4892 }
4893
4894 static int btrfs_set_page_dirty(struct page *page)
4895 {
4896         return __set_page_dirty_nobuffers(page);
4897 }
4898
4899 static int btrfs_permission(struct inode *inode, int mask)
4900 {
4901         if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
4902                 return -EACCES;
4903         return generic_permission(inode, mask, btrfs_check_acl);
4904 }
4905
4906 static struct inode_operations btrfs_dir_inode_operations = {
4907         .lookup         = btrfs_lookup,
4908         .create         = btrfs_create,
4909         .unlink         = btrfs_unlink,
4910         .link           = btrfs_link,
4911         .mkdir          = btrfs_mkdir,
4912         .rmdir          = btrfs_rmdir,
4913         .rename         = btrfs_rename,
4914         .symlink        = btrfs_symlink,
4915         .setattr        = btrfs_setattr,
4916         .mknod          = btrfs_mknod,
4917         .setxattr       = btrfs_setxattr,
4918         .getxattr       = btrfs_getxattr,
4919         .listxattr      = btrfs_listxattr,
4920         .removexattr    = btrfs_removexattr,
4921         .permission     = btrfs_permission,
4922 };
4923 static struct inode_operations btrfs_dir_ro_inode_operations = {
4924         .lookup         = btrfs_lookup,
4925         .permission     = btrfs_permission,
4926 };
4927 static struct file_operations btrfs_dir_file_operations = {
4928         .llseek         = generic_file_llseek,
4929         .read           = generic_read_dir,
4930         .readdir        = btrfs_real_readdir,
4931         .unlocked_ioctl = btrfs_ioctl,
4932 #ifdef CONFIG_COMPAT
4933         .compat_ioctl   = btrfs_ioctl,
4934 #endif
4935         .release        = btrfs_release_file,
4936         .fsync          = btrfs_sync_file,
4937 };
4938
4939 static struct extent_io_ops btrfs_extent_io_ops = {
4940         .fill_delalloc = run_delalloc_range,
4941         .submit_bio_hook = btrfs_submit_bio_hook,
4942         .merge_bio_hook = btrfs_merge_bio_hook,
4943         .readpage_end_io_hook = btrfs_readpage_end_io_hook,
4944         .writepage_end_io_hook = btrfs_writepage_end_io_hook,
4945         .writepage_start_hook = btrfs_writepage_start_hook,
4946         .readpage_io_failed_hook = btrfs_io_failed_hook,
4947         .set_bit_hook = btrfs_set_bit_hook,
4948         .clear_bit_hook = btrfs_clear_bit_hook,
4949 };
4950
4951 static struct address_space_operations btrfs_aops = {
4952         .readpage       = btrfs_readpage,
4953         .writepage      = btrfs_writepage,
4954         .writepages     = btrfs_writepages,
4955         .readpages      = btrfs_readpages,
4956         .sync_page      = block_sync_page,
4957         .bmap           = btrfs_bmap,
4958         .direct_IO      = btrfs_direct_IO,
4959         .invalidatepage = btrfs_invalidatepage,
4960         .releasepage    = btrfs_releasepage,
4961         .set_page_dirty = btrfs_set_page_dirty,
4962 };
4963
4964 static struct address_space_operations btrfs_symlink_aops = {
4965         .readpage       = btrfs_readpage,
4966         .writepage      = btrfs_writepage,
4967         .invalidatepage = btrfs_invalidatepage,
4968         .releasepage    = btrfs_releasepage,
4969 };
4970
4971 static struct inode_operations btrfs_file_inode_operations = {
4972         .truncate       = btrfs_truncate,
4973         .getattr        = btrfs_getattr,
4974         .setattr        = btrfs_setattr,
4975         .setxattr       = btrfs_setxattr,
4976         .getxattr       = btrfs_getxattr,
4977         .listxattr      = btrfs_listxattr,
4978         .removexattr    = btrfs_removexattr,
4979         .permission     = btrfs_permission,
4980         .fallocate      = btrfs_fallocate,
4981 };
4982 static struct inode_operations btrfs_special_inode_operations = {
4983         .getattr        = btrfs_getattr,
4984         .setattr        = btrfs_setattr,
4985         .permission     = btrfs_permission,
4986         .setxattr       = btrfs_setxattr,
4987         .getxattr       = btrfs_getxattr,
4988         .listxattr      = btrfs_listxattr,
4989         .removexattr    = btrfs_removexattr,
4990 };
4991 static struct inode_operations btrfs_symlink_inode_operations = {
4992         .readlink       = generic_readlink,
4993         .follow_link    = page_follow_link_light,
4994         .put_link       = page_put_link,
4995         .permission     = btrfs_permission,
4996 };