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