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