Btrfs: Create extent_buffer interface for large blocksizes
[linux-2.6] / fs / btrfs / file.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/fs.h>
20 #include <linux/pagemap.h>
21 #include <linux/highmem.h>
22 #include <linux/time.h>
23 #include <linux/init.h>
24 #include <linux/string.h>
25 #include <linux/smp_lock.h>
26 #include <linux/backing-dev.h>
27 #include <linux/mpage.h>
28 #include <linux/swap.h>
29 #include <linux/writeback.h>
30 #include <linux/statfs.h>
31 #include <linux/compat.h>
32 #include <linux/version.h>
33 #include "ctree.h"
34 #include "disk-io.h"
35 #include "transaction.h"
36 #include "btrfs_inode.h"
37 #include "ioctl.h"
38 #include "print-tree.h"
39
40
41 static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
42                                 struct page **prepared_pages,
43                                 const char __user * buf)
44 {
45         long page_fault = 0;
46         int i;
47         int offset = pos & (PAGE_CACHE_SIZE - 1);
48
49         for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
50                 size_t count = min_t(size_t,
51                                      PAGE_CACHE_SIZE - offset, write_bytes);
52                 struct page *page = prepared_pages[i];
53                 fault_in_pages_readable(buf, count);
54
55                 /* Copy data from userspace to the current page */
56                 kmap(page);
57                 page_fault = __copy_from_user(page_address(page) + offset,
58                                               buf, count);
59                 /* Flush processor's dcache for this page */
60                 flush_dcache_page(page);
61                 kunmap(page);
62                 buf += count;
63                 write_bytes -= count;
64
65                 if (page_fault)
66                         break;
67         }
68         return page_fault ? -EFAULT : 0;
69 }
70
71 static void btrfs_drop_pages(struct page **pages, size_t num_pages)
72 {
73         size_t i;
74         for (i = 0; i < num_pages; i++) {
75                 if (!pages[i])
76                         break;
77                 unlock_page(pages[i]);
78                 mark_page_accessed(pages[i]);
79                 page_cache_release(pages[i]);
80         }
81 }
82
83 static int insert_inline_extent(struct btrfs_trans_handle *trans,
84                                 struct btrfs_root *root, struct inode *inode,
85                                 u64 offset, ssize_t size,
86                                 struct page *page, size_t page_offset)
87 {
88         struct btrfs_key key;
89         struct btrfs_path *path;
90         struct extent_buffer *leaf;
91         char *kaddr;
92         unsigned long ptr;
93         struct btrfs_file_extent_item *ei;
94         u32 datasize;
95         int err = 0;
96         int ret;
97
98         path = btrfs_alloc_path();
99         if (!path)
100                 return -ENOMEM;
101
102         btrfs_set_trans_block_group(trans, inode);
103
104         key.objectid = inode->i_ino;
105         key.offset = offset;
106         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
107         BUG_ON(size >= PAGE_CACHE_SIZE);
108         datasize = btrfs_file_extent_calc_inline_size(size);
109
110         ret = btrfs_insert_empty_item(trans, root, path, &key,
111                                       datasize);
112         if (ret) {
113                 err = ret;
114                 goto fail;
115         }
116         leaf = path->nodes[0];
117         ei = btrfs_item_ptr(leaf, path->slots[0],
118                             struct btrfs_file_extent_item);
119         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
120         btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
121         ptr = btrfs_file_extent_inline_start(ei);
122
123         kaddr = kmap_atomic(page, KM_USER0);
124         write_extent_buffer(leaf, kaddr + page_offset, ptr, size);
125         kunmap_atomic(kaddr, KM_USER0);
126         btrfs_mark_buffer_dirty(leaf);
127 fail:
128         btrfs_free_path(path);
129         return err;
130 }
131
132 static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
133                                    struct btrfs_root *root,
134                                    struct file *file,
135                                    struct page **pages,
136                                    size_t num_pages,
137                                    loff_t pos,
138                                    size_t write_bytes)
139 {
140         int err = 0;
141         int i;
142         struct inode *inode = file->f_path.dentry->d_inode;
143         struct extent_map *em;
144         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
145         u64 hint_block;
146         u64 num_blocks;
147         u64 start_pos;
148         u64 end_of_last_block;
149         u64 end_pos = pos + write_bytes;
150         loff_t isize = i_size_read(inode);
151
152         em = alloc_extent_map(GFP_NOFS);
153         if (!em)
154                 return -ENOMEM;
155
156         em->bdev = inode->i_sb->s_bdev;
157
158         start_pos = pos & ~((u64)root->sectorsize - 1);
159         num_blocks = (write_bytes + pos - start_pos + root->sectorsize - 1) >>
160                         inode->i_blkbits;
161
162         down_read(&BTRFS_I(inode)->root->snap_sem);
163         end_of_last_block = start_pos + (num_blocks << inode->i_blkbits) - 1;
164         lock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
165         mutex_lock(&root->fs_info->fs_mutex);
166         trans = btrfs_start_transaction(root, 1);
167         if (!trans) {
168                 err = -ENOMEM;
169                 goto out_unlock;
170         }
171         btrfs_set_trans_block_group(trans, inode);
172         inode->i_blocks += num_blocks << 3;
173         hint_block = 0;
174
175         if ((end_of_last_block & 4095) == 0) {
176                 printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
177         }
178         set_extent_uptodate(em_tree, start_pos, end_of_last_block, GFP_NOFS);
179
180         /* FIXME...EIEIO, ENOSPC and more */
181
182         /* insert any holes we need to create */
183         if (inode->i_size < start_pos) {
184                 u64 last_pos_in_file;
185                 u64 hole_size;
186                 u64 mask = root->sectorsize - 1;
187                 last_pos_in_file = (isize + mask) & ~mask;
188                 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
189
190                 if (last_pos_in_file < start_pos) {
191                         err = btrfs_drop_extents(trans, root, inode,
192                                                  last_pos_in_file,
193                                                  last_pos_in_file + hole_size,
194                                                  &hint_block);
195                         if (err)
196                                 goto failed;
197
198                         hole_size >>= inode->i_blkbits;
199                         err = btrfs_insert_file_extent(trans, root,
200                                                        inode->i_ino,
201                                                        last_pos_in_file,
202                                                        0, 0, hole_size);
203                 }
204                 if (err)
205                         goto failed;
206         }
207
208         /*
209          * either allocate an extent for the new bytes or setup the key
210          * to show we are doing inline data in the extent
211          */
212         if (isize >= PAGE_CACHE_SIZE || pos + write_bytes < inode->i_size ||
213             pos + write_bytes - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) {
214                 u64 last_end;
215                 for (i = 0; i < num_pages; i++) {
216                         struct page *p = pages[i];
217                         SetPageUptodate(p);
218                         set_page_dirty(p);
219                 }
220                 last_end = pages[num_pages -1]->index << PAGE_CACHE_SHIFT;
221                 last_end += PAGE_CACHE_SIZE - 1;
222                 set_extent_delalloc(em_tree, start_pos, end_of_last_block,
223                                  GFP_NOFS);
224         } else {
225                 struct page *p = pages[0];
226                 /* step one, delete the existing extents in this range */
227                 /* FIXME blocksize != pagesize */
228                 err = btrfs_drop_extents(trans, root, inode, start_pos,
229                          (pos + write_bytes + root->sectorsize -1) &
230                          ~((u64)root->sectorsize - 1), &hint_block);
231                 if (err)
232                         goto failed;
233
234                 err = insert_inline_extent(trans, root, inode, start_pos,
235                                            end_pos - start_pos, p, 0);
236                 BUG_ON(err);
237                 em->start = start_pos;
238                 em->end = end_pos - 1;
239                 em->block_start = EXTENT_MAP_INLINE;
240                 em->block_end = EXTENT_MAP_INLINE;
241                 add_extent_mapping(em_tree, em);
242         }
243         if (end_pos > isize) {
244                 i_size_write(inode, end_pos);
245                 btrfs_update_inode(trans, root, inode);
246         }
247 failed:
248         err = btrfs_end_transaction(trans, root);
249 out_unlock:
250         mutex_unlock(&root->fs_info->fs_mutex);
251         unlock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
252         free_extent_map(em);
253         up_read(&BTRFS_I(inode)->root->snap_sem);
254         return err;
255 }
256
257 int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
258 {
259         struct extent_map *em;
260         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
261
262         while(1) {
263                 em = lookup_extent_mapping(em_tree, start, end);
264                 if (!em)
265                         break;
266                 remove_extent_mapping(em_tree, em);
267                 /* once for us */
268                 free_extent_map(em);
269                 /* once for the tree*/
270                 free_extent_map(em);
271         }
272         return 0;
273 }
274
275 /*
276  * this is very complex, but the basic idea is to drop all extents
277  * in the range start - end.  hint_block is filled in with a block number
278  * that would be a good hint to the block allocator for this file.
279  *
280  * If an extent intersects the range but is not entirely inside the range
281  * it is either truncated or split.  Anything entirely inside the range
282  * is deleted from the tree.
283  */
284 int btrfs_drop_extents(struct btrfs_trans_handle *trans,
285                        struct btrfs_root *root, struct inode *inode,
286                        u64 start, u64 end, u64 *hint_block)
287 {
288         int ret;
289         struct btrfs_key key;
290         struct extent_buffer *leaf;
291         int slot;
292         struct btrfs_file_extent_item *extent;
293         u64 extent_end = 0;
294         int keep;
295         struct btrfs_file_extent_item old;
296         struct btrfs_path *path;
297         u64 search_start = start;
298         int bookend;
299         int found_type;
300         int found_extent;
301         int found_inline;
302         int recow;
303
304         btrfs_drop_extent_cache(inode, start, end - 1);
305
306         path = btrfs_alloc_path();
307         if (!path)
308                 return -ENOMEM;
309         while(1) {
310                 recow = 0;
311                 btrfs_release_path(root, path);
312                 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
313                                                search_start, -1);
314                 if (ret < 0)
315                         goto out;
316                 if (ret > 0) {
317                         if (path->slots[0] == 0) {
318                                 ret = 0;
319                                 goto out;
320                         }
321                         path->slots[0]--;
322                 }
323 next_slot:
324                 keep = 0;
325                 bookend = 0;
326                 found_extent = 0;
327                 found_inline = 0;
328                 extent = NULL;
329                 leaf = path->nodes[0];
330                 slot = path->slots[0];
331                 ret = 0;
332                 btrfs_item_key_to_cpu(leaf, &key, slot);
333                 if (key.offset >= end || key.objectid != inode->i_ino) {
334                         goto out;
335                 }
336                 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) {
337                         goto out;
338                 }
339                 if (recow) {
340                         search_start = key.offset;
341                         continue;
342                 }
343                 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
344                         extent = btrfs_item_ptr(leaf, slot,
345                                                 struct btrfs_file_extent_item);
346                         found_type = btrfs_file_extent_type(leaf, extent);
347                         if (found_type == BTRFS_FILE_EXTENT_REG) {
348                                 extent_end = key.offset +
349                                  (btrfs_file_extent_num_blocks(leaf, extent) <<
350                                          inode->i_blkbits);
351                                 found_extent = 1;
352                         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
353                                 struct btrfs_item *item;
354                                 item = btrfs_item_nr(leaf, slot);
355                                 found_inline = 1;
356                                 extent_end = key.offset +
357                                      btrfs_file_extent_inline_len(leaf, item);
358                         }
359                 } else {
360                         extent_end = search_start;
361                 }
362
363                 /* we found nothing we can drop */
364                 if ((!found_extent && !found_inline) ||
365                     search_start >= extent_end) {
366                         int nextret;
367                         u32 nritems;
368                         nritems = btrfs_header_nritems(leaf);
369                         if (slot >= nritems - 1) {
370                                 nextret = btrfs_next_leaf(root, path);
371                                 if (nextret)
372                                         goto out;
373                                 recow = 1;
374                         } else {
375                                 path->slots[0]++;
376                         }
377                         goto next_slot;
378                 }
379
380                 /* FIXME, there's only one inline extent allowed right now */
381                 if (found_inline) {
382                         u64 mask = root->sectorsize - 1;
383                         search_start = (extent_end + mask) & ~mask;
384                 } else
385                         search_start = extent_end;
386
387                 if (end < extent_end && end >= key.offset) {
388                         if (found_extent) {
389                                 u64 disk_blocknr =
390                                     btrfs_file_extent_disk_blocknr(leaf,extent);
391                                 u64 disk_num_blocks =
392                                     btrfs_file_extent_disk_num_blocks(leaf,
393                                                                       extent);
394                                 read_extent_buffer(leaf, &old,
395                                                    (unsigned long)extent,
396                                                    sizeof(old));
397                                 if (disk_blocknr != 0) {
398                                         ret = btrfs_inc_extent_ref(trans, root,
399                                                  disk_blocknr, disk_num_blocks);
400                                         BUG_ON(ret);
401                                 }
402                         }
403                         WARN_ON(found_inline);
404                         bookend = 1;
405                 }
406                 /* truncate existing extent */
407                 if (start > key.offset) {
408                         u64 new_num;
409                         u64 old_num;
410                         keep = 1;
411                         WARN_ON(start & (root->sectorsize - 1));
412                         if (found_extent) {
413                                 new_num = (start - key.offset) >>
414                                         inode->i_blkbits;
415                                 old_num = btrfs_file_extent_num_blocks(leaf,
416                                                                        extent);
417                                 *hint_block =
418                                         btrfs_file_extent_disk_blocknr(leaf,
419                                                                        extent);
420                                 if (btrfs_file_extent_disk_blocknr(leaf,
421                                                                    extent)) {
422                                         inode->i_blocks -=
423                                                 (old_num - new_num) << 3;
424                                 }
425                                 btrfs_set_file_extent_num_blocks(leaf,
426                                                                  extent,
427                                                                  new_num);
428                                 btrfs_mark_buffer_dirty(leaf);
429                         } else {
430                                 WARN_ON(1);
431                         }
432                 }
433                 /* delete the entire extent */
434                 if (!keep) {
435                         u64 disk_blocknr = 0;
436                         u64 disk_num_blocks = 0;
437                         u64 extent_num_blocks = 0;
438                         if (found_extent) {
439                                 disk_blocknr =
440                                       btrfs_file_extent_disk_blocknr(leaf,
441                                                                      extent);
442                                 disk_num_blocks =
443                                       btrfs_file_extent_disk_num_blocks(leaf,
444                                                                         extent);
445                                 extent_num_blocks =
446                                       btrfs_file_extent_num_blocks(leaf,
447                                                                    extent);
448                                 *hint_block =
449                                         btrfs_file_extent_disk_blocknr(leaf,
450                                                                        extent);
451                         }
452                         ret = btrfs_del_item(trans, root, path);
453                         /* TODO update progress marker and return */
454                         BUG_ON(ret);
455                         btrfs_release_path(root, path);
456                         extent = NULL;
457                         if (found_extent && disk_blocknr != 0) {
458                                 inode->i_blocks -= extent_num_blocks << 3;
459                                 ret = btrfs_free_extent(trans, root,
460                                                         disk_blocknr,
461                                                         disk_num_blocks, 0);
462                         }
463
464                         BUG_ON(ret);
465                         if (!bookend && search_start >= end) {
466                                 ret = 0;
467                                 goto out;
468                         }
469                         if (!bookend)
470                                 continue;
471                 }
472                 /* create bookend, splitting the extent in two */
473                 if (bookend && found_extent) {
474                         struct btrfs_key ins;
475                         ins.objectid = inode->i_ino;
476                         ins.offset = end;
477                         btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
478                         btrfs_release_path(root, path);
479                         ret = btrfs_insert_empty_item(trans, root, path, &ins,
480                                                       sizeof(*extent));
481
482                         leaf = path->nodes[0];
483                         if (ret) {
484                                 btrfs_print_leaf(root, leaf);
485                                 printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu keep was %d\n", ret , ins.objectid, ins.type, ins.offset, start, end, key.offset, extent_end, keep);
486                         }
487                         BUG_ON(ret);
488                         extent = btrfs_item_ptr(leaf, path->slots[0],
489                                                 struct btrfs_file_extent_item);
490                         write_extent_buffer(leaf, &old,
491                                             (unsigned long)extent, sizeof(old));
492
493                         btrfs_set_file_extent_offset(leaf, extent,
494                                     le64_to_cpu(old.offset) +
495                                     ((end - key.offset) >> inode->i_blkbits));
496                         WARN_ON(le64_to_cpu(old.num_blocks) <
497                                 (extent_end - end) >> inode->i_blkbits);
498                         btrfs_set_file_extent_num_blocks(leaf, extent,
499                                     (extent_end - end) >> inode->i_blkbits);
500
501                         btrfs_set_file_extent_type(leaf, extent,
502                                                    BTRFS_FILE_EXTENT_REG);
503                         btrfs_mark_buffer_dirty(path->nodes[0]);
504                         if (le64_to_cpu(old.disk_blocknr) != 0) {
505                                 inode->i_blocks +=
506                                       btrfs_file_extent_num_blocks(leaf,
507                                                                    extent) << 3;
508                         }
509                         ret = 0;
510                         goto out;
511                 }
512         }
513 out:
514         btrfs_free_path(path);
515         return ret;
516 }
517
518 /*
519  * this gets pages into the page cache and locks them down
520  */
521 static int prepare_pages(struct btrfs_root *root,
522                          struct file *file,
523                          struct page **pages,
524                          size_t num_pages,
525                          loff_t pos,
526                          unsigned long first_index,
527                          unsigned long last_index,
528                          size_t write_bytes)
529 {
530         int i;
531         unsigned long index = pos >> PAGE_CACHE_SHIFT;
532         struct inode *inode = file->f_path.dentry->d_inode;
533         int err = 0;
534         u64 num_blocks;
535         u64 start_pos;
536
537         start_pos = pos & ~((u64)root->sectorsize - 1);
538         num_blocks = (write_bytes + pos - start_pos + root->sectorsize - 1) >>
539                         inode->i_blkbits;
540
541         memset(pages, 0, num_pages * sizeof(struct page *));
542
543         for (i = 0; i < num_pages; i++) {
544                 pages[i] = grab_cache_page(inode->i_mapping, index + i);
545                 if (!pages[i]) {
546                         err = -ENOMEM;
547                         BUG_ON(1);
548                 }
549                 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
550                 wait_on_page_writeback(pages[i]);
551                 set_page_extent_mapped(pages[i]);
552                 WARN_ON(!PageLocked(pages[i]));
553         }
554         return 0;
555 }
556
557 static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
558                                 size_t count, loff_t *ppos)
559 {
560         loff_t pos;
561         size_t num_written = 0;
562         int err = 0;
563         int ret = 0;
564         struct inode *inode = file->f_path.dentry->d_inode;
565         struct btrfs_root *root = BTRFS_I(inode)->root;
566         struct page **pages = NULL;
567         int nrptrs;
568         struct page *pinned[2];
569         unsigned long first_index;
570         unsigned long last_index;
571
572         nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
573                      PAGE_CACHE_SIZE / (sizeof(struct page *)));
574         pinned[0] = NULL;
575         pinned[1] = NULL;
576         if (file->f_flags & O_DIRECT)
577                 return -EINVAL;
578         pos = *ppos;
579         vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
580         current->backing_dev_info = inode->i_mapping->backing_dev_info;
581         err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
582         if (err)
583                 goto out;
584         if (count == 0)
585                 goto out;
586         err = remove_suid(file->f_path.dentry);
587         if (err)
588                 goto out;
589         file_update_time(file);
590
591         pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
592
593         mutex_lock(&inode->i_mutex);
594         first_index = pos >> PAGE_CACHE_SHIFT;
595         last_index = (pos + count) >> PAGE_CACHE_SHIFT;
596
597         /*
598          * there are lots of better ways to do this, but this code
599          * makes sure the first and last page in the file range are
600          * up to date and ready for cow
601          */
602         if ((pos & (PAGE_CACHE_SIZE - 1))) {
603                 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
604                 if (!PageUptodate(pinned[0])) {
605                         ret = btrfs_readpage(NULL, pinned[0]);
606                         BUG_ON(ret);
607                         wait_on_page_locked(pinned[0]);
608                 } else {
609                         unlock_page(pinned[0]);
610                 }
611         }
612         if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
613                 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
614                 if (!PageUptodate(pinned[1])) {
615                         ret = btrfs_readpage(NULL, pinned[1]);
616                         BUG_ON(ret);
617                         wait_on_page_locked(pinned[1]);
618                 } else {
619                         unlock_page(pinned[1]);
620                 }
621         }
622
623         while(count > 0) {
624                 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
625                 size_t write_bytes = min(count, nrptrs *
626                                         (size_t)PAGE_CACHE_SIZE -
627                                          offset);
628                 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
629                                         PAGE_CACHE_SHIFT;
630
631                 WARN_ON(num_pages > nrptrs);
632                 memset(pages, 0, sizeof(pages));
633                 ret = prepare_pages(root, file, pages, num_pages,
634                                     pos, first_index, last_index,
635                                     write_bytes);
636                 if (ret)
637                         goto out;
638
639                 ret = btrfs_copy_from_user(pos, num_pages,
640                                            write_bytes, pages, buf);
641                 if (ret) {
642                         btrfs_drop_pages(pages, num_pages);
643                         goto out;
644                 }
645
646                 ret = dirty_and_release_pages(NULL, root, file, pages,
647                                               num_pages, pos, write_bytes);
648                 btrfs_drop_pages(pages, num_pages);
649                 if (ret)
650                         goto out;
651
652                 buf += write_bytes;
653                 count -= write_bytes;
654                 pos += write_bytes;
655                 num_written += write_bytes;
656
657                 balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
658                 btrfs_btree_balance_dirty(root, 1);
659                 cond_resched();
660         }
661         mutex_unlock(&inode->i_mutex);
662 out:
663         kfree(pages);
664         if (pinned[0])
665                 page_cache_release(pinned[0]);
666         if (pinned[1])
667                 page_cache_release(pinned[1]);
668         *ppos = pos;
669         current->backing_dev_info = NULL;
670         return num_written ? num_written : err;
671 }
672
673 static int btrfs_sync_file(struct file *file,
674                            struct dentry *dentry, int datasync)
675 {
676         struct inode *inode = dentry->d_inode;
677         struct btrfs_root *root = BTRFS_I(inode)->root;
678         int ret = 0;
679         struct btrfs_trans_handle *trans;
680
681         /*
682          * check the transaction that last modified this inode
683          * and see if its already been committed
684          */
685         mutex_lock(&root->fs_info->fs_mutex);
686         if (!BTRFS_I(inode)->last_trans)
687                 goto out;
688         mutex_lock(&root->fs_info->trans_mutex);
689         if (BTRFS_I(inode)->last_trans <=
690             root->fs_info->last_trans_committed) {
691                 BTRFS_I(inode)->last_trans = 0;
692                 mutex_unlock(&root->fs_info->trans_mutex);
693                 goto out;
694         }
695         mutex_unlock(&root->fs_info->trans_mutex);
696
697         /*
698          * ok we haven't committed the transaction yet, lets do a commit
699          */
700         trans = btrfs_start_transaction(root, 1);
701         if (!trans) {
702                 ret = -ENOMEM;
703                 goto out;
704         }
705         ret = btrfs_commit_transaction(trans, root);
706 out:
707         mutex_unlock(&root->fs_info->fs_mutex);
708         return ret > 0 ? EIO : ret;
709 }
710
711 static struct vm_operations_struct btrfs_file_vm_ops = {
712 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
713         .nopage         = filemap_nopage,
714         .populate       = filemap_populate,
715 #else
716         .fault          = filemap_fault,
717 #endif
718         .page_mkwrite   = btrfs_page_mkwrite,
719 };
720
721 static int btrfs_file_mmap(struct file  *filp, struct vm_area_struct *vma)
722 {
723         vma->vm_ops = &btrfs_file_vm_ops;
724         file_accessed(filp);
725         return 0;
726 }
727
728 struct file_operations btrfs_file_operations = {
729         .llseek         = generic_file_llseek,
730         .read           = do_sync_read,
731         .aio_read       = generic_file_aio_read,
732         .write          = btrfs_file_write,
733         .mmap           = btrfs_file_mmap,
734         .open           = generic_file_open,
735         .fsync          = btrfs_sync_file,
736         .unlocked_ioctl = btrfs_ioctl,
737 #ifdef CONFIG_COMPAT
738         .compat_ioctl   = btrfs_ioctl,
739 #endif
740 };
741