Btrfs: Allow tree blocks larger than the page size
[deliverable/linux.git] / fs / btrfs / file.c
CommitLineData
6cbd5570
CM
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
39279cc3
CM
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>
92fee66d 32#include <linux/version.h>
39279cc3
CM
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
41static 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
71static 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
a52d9a80
CM
83static int insert_inline_extent(struct btrfs_trans_handle *trans,
84 struct btrfs_root *root, struct inode *inode,
54aa1f4d 85 u64 offset, ssize_t size,
a52d9a80 86 struct page *page, size_t page_offset)
54aa1f4d
CM
87{
88 struct btrfs_key key;
89 struct btrfs_path *path;
5f39d397
CM
90 struct extent_buffer *leaf;
91 char *kaddr;
92 unsigned long ptr;
54aa1f4d
CM
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
54aa1f4d
CM
102 btrfs_set_trans_block_group(trans, inode);
103
104 key.objectid = inode->i_ino;
105 key.offset = offset;
54aa1f4d
CM
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 }
5f39d397
CM
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);
54aa1f4d
CM
121 ptr = btrfs_file_extent_inline_start(ei);
122
db94535d 123 kaddr = kmap_atomic(page, KM_USER1);
5f39d397 124 write_extent_buffer(leaf, kaddr + page_offset, ptr, size);
db94535d 125 kunmap_atomic(kaddr, KM_USER1);
5f39d397 126 btrfs_mark_buffer_dirty(leaf);
54aa1f4d
CM
127fail:
128 btrfs_free_path(path);
54aa1f4d
CM
129 return err;
130}
131
39279cc3
CM
132static 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{
39279cc3 140 int err = 0;
a52d9a80 141 int i;
39279cc3 142 struct inode *inode = file->f_path.dentry->d_inode;
a52d9a80
CM
143 struct extent_map *em;
144 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
db94535d
CM
145 u64 hint_byte;
146 u64 num_bytes;
a52d9a80
CM
147 u64 start_pos;
148 u64 end_of_last_block;
149 u64 end_pos = pos + write_bytes;
db94535d 150 u32 inline_size;
a52d9a80 151 loff_t isize = i_size_read(inode);
39279cc3 152
a52d9a80
CM
153 em = alloc_extent_map(GFP_NOFS);
154 if (!em)
155 return -ENOMEM;
39279cc3 156
a52d9a80 157 em->bdev = inode->i_sb->s_bdev;
39279cc3 158
5f39d397 159 start_pos = pos & ~((u64)root->sectorsize - 1);
db94535d
CM
160 num_bytes = (write_bytes + pos - start_pos +
161 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
39279cc3 162
011410bd 163 down_read(&BTRFS_I(inode)->root->snap_sem);
db94535d
CM
164 end_of_last_block = start_pos + num_bytes - 1;
165
b888db2b 166 lock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
a52d9a80
CM
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);
db94535d
CM
174 inode->i_blocks += num_bytes >> 9;
175 hint_byte = 0;
a52d9a80
CM
176
177 if ((end_of_last_block & 4095) == 0) {
9433063b 178 printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
a52d9a80
CM
179 }
180 set_extent_uptodate(em_tree, start_pos, end_of_last_block, GFP_NOFS);
181
182 /* FIXME...EIEIO, ENOSPC and more */
183
a52d9a80
CM
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;
5f39d397 188 u64 mask = root->sectorsize - 1;
a52d9a80
CM
189 last_pos_in_file = (isize + mask) & ~mask;
190 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
2bf5a725 191
a52d9a80 192 if (last_pos_in_file < start_pos) {
2bf5a725
CM
193 err = btrfs_drop_extents(trans, root, inode,
194 last_pos_in_file,
195 last_pos_in_file + hole_size,
db94535d 196 &hint_byte);
2bf5a725
CM
197 if (err)
198 goto failed;
199
a52d9a80
CM
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)
39279cc3 206 goto failed;
a52d9a80
CM
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 */
db94535d 213 inline_size = end_pos - start_pos;
a52d9a80 214 if (isize >= PAGE_CACHE_SIZE || pos + write_bytes < inode->i_size ||
db94535d
CM
215 inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
216 inline_size >= PAGE_CACHE_SIZE) {
b888db2b 217 u64 last_end;
a52d9a80
CM
218 for (i = 0; i < num_pages; i++) {
219 struct page *p = pages[i];
220 SetPageUptodate(p);
b888db2b 221 set_page_dirty(p);
39279cc3 222 }
b888db2b
CM
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);
a52d9a80
CM
227 } else {
228 struct page *p = pages[0];
b888db2b 229 /* step one, delete the existing extents in this range */
2bf5a725 230 err = btrfs_drop_extents(trans, root, inode, start_pos,
5f39d397 231 (pos + write_bytes + root->sectorsize -1) &
db94535d 232 ~((u64)root->sectorsize - 1), &hint_byte);
2bf5a725
CM
233 if (err)
234 goto failed;
b888db2b 235
a52d9a80
CM
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;
6af858b2 240 em->end = end_pos - 1;
a52d9a80
CM
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);
39279cc3
CM
248 }
249failed:
a52d9a80
CM
250 err = btrfs_end_transaction(trans, root);
251out_unlock:
252 mutex_unlock(&root->fs_info->fs_mutex);
b888db2b 253 unlock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
a52d9a80 254 free_extent_map(em);
011410bd 255 up_read(&BTRFS_I(inode)->root->snap_sem);
39279cc3
CM
256 return err;
257}
258
a52d9a80
CM
259int 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
39279cc3
CM
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 */
286int btrfs_drop_extents(struct btrfs_trans_handle *trans,
287 struct btrfs_root *root, struct inode *inode,
db94535d 288 u64 start, u64 end, u64 *hint_byte)
39279cc3
CM
289{
290 int ret;
291 struct btrfs_key key;
5f39d397 292 struct extent_buffer *leaf;
39279cc3
CM
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;
ccd467d6 304 int recow;
39279cc3 305
a52d9a80
CM
306 btrfs_drop_extent_cache(inode, start, end - 1);
307
39279cc3
CM
308 path = btrfs_alloc_path();
309 if (!path)
310 return -ENOMEM;
311 while(1) {
ccd467d6 312 recow = 0;
39279cc3
CM
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 }
8c2383c3 325next_slot:
39279cc3
CM
326 keep = 0;
327 bookend = 0;
328 found_extent = 0;
329 found_inline = 0;
330 extent = NULL;
5f39d397 331 leaf = path->nodes[0];
39279cc3 332 slot = path->slots[0];
8c2383c3 333 ret = 0;
5f39d397 334 btrfs_item_key_to_cpu(leaf, &key, slot);
39279cc3 335 if (key.offset >= end || key.objectid != inode->i_ino) {
39279cc3
CM
336 goto out;
337 }
8c2383c3 338 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) {
39279cc3
CM
339 goto out;
340 }
ccd467d6
CM
341 if (recow) {
342 search_start = key.offset;
343 continue;
344 }
8c2383c3
CM
345 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
346 extent = btrfs_item_ptr(leaf, slot,
347 struct btrfs_file_extent_item);
5f39d397 348 found_type = btrfs_file_extent_type(leaf, extent);
8c2383c3
CM
349 if (found_type == BTRFS_FILE_EXTENT_REG) {
350 extent_end = key.offset +
db94535d 351 btrfs_file_extent_num_bytes(leaf, extent);
8c2383c3
CM
352 found_extent = 1;
353 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5f39d397
CM
354 struct btrfs_item *item;
355 item = btrfs_item_nr(leaf, slot);
8c2383c3
CM
356 found_inline = 1;
357 extent_end = key.offset +
5f39d397 358 btrfs_file_extent_inline_len(leaf, item);
8c2383c3
CM
359 }
360 } else {
361 extent_end = search_start;
39279cc3
CM
362 }
363
364 /* we found nothing we can drop */
8c2383c3
CM
365 if ((!found_extent && !found_inline) ||
366 search_start >= extent_end) {
367 int nextret;
368 u32 nritems;
5f39d397 369 nritems = btrfs_header_nritems(leaf);
8c2383c3
CM
370 if (slot >= nritems - 1) {
371 nextret = btrfs_next_leaf(root, path);
372 if (nextret)
373 goto out;
ccd467d6 374 recow = 1;
8c2383c3
CM
375 } else {
376 path->slots[0]++;
377 }
378 goto next_slot;
39279cc3
CM
379 }
380
381 /* FIXME, there's only one inline extent allowed right now */
382 if (found_inline) {
5f39d397 383 u64 mask = root->sectorsize - 1;
39279cc3
CM
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) {
db94535d
CM
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,
5f39d397
CM
394 extent);
395 read_extent_buffer(leaf, &old,
396 (unsigned long)extent,
397 sizeof(old));
db94535d 398 if (disk_bytenr != 0) {
39279cc3 399 ret = btrfs_inc_extent_ref(trans, root,
db94535d 400 disk_bytenr, disk_num_bytes);
39279cc3
CM
401 BUG_ON(ret);
402 }
403 }
404 WARN_ON(found_inline);
405 bookend = 1;
406 }
39279cc3
CM
407 /* truncate existing extent */
408 if (start > key.offset) {
409 u64 new_num;
410 u64 old_num;
411 keep = 1;
5f39d397 412 WARN_ON(start & (root->sectorsize - 1));
39279cc3 413 if (found_extent) {
db94535d
CM
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)) {
39279cc3 422 inode->i_blocks -=
db94535d 423 (old_num - new_num) >> 9;
39279cc3 424 }
db94535d
CM
425 btrfs_set_file_extent_num_bytes(leaf, extent,
426 new_num);
5f39d397 427 btrfs_mark_buffer_dirty(leaf);
39279cc3
CM
428 } else {
429 WARN_ON(1);
430 }
431 }
432 /* delete the entire extent */
433 if (!keep) {
db94535d
CM
434 u64 disk_bytenr = 0;
435 u64 disk_num_bytes = 0;
436 u64 extent_num_bytes = 0;
39279cc3 437 if (found_extent) {
db94535d
CM
438 disk_bytenr =
439 btrfs_file_extent_disk_bytenr(leaf,
5f39d397 440 extent);
db94535d
CM
441 disk_num_bytes =
442 btrfs_file_extent_disk_num_bytes(leaf,
5f39d397 443 extent);
db94535d
CM
444 extent_num_bytes =
445 btrfs_file_extent_num_bytes(leaf, extent);
446 *hint_byte =
447 btrfs_file_extent_disk_bytenr(leaf,
448 extent);
39279cc3
CM
449 }
450 ret = btrfs_del_item(trans, root, path);
54aa1f4d 451 /* TODO update progress marker and return */
39279cc3
CM
452 BUG_ON(ret);
453 btrfs_release_path(root, path);
454 extent = NULL;
db94535d
CM
455 if (found_extent && disk_bytenr != 0) {
456 inode->i_blocks -= extent_num_bytes >> 9;
39279cc3 457 ret = btrfs_free_extent(trans, root,
db94535d
CM
458 disk_bytenr,
459 disk_num_bytes, 0);
39279cc3
CM
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;
39279cc3 475 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
39279cc3
CM
476 btrfs_release_path(root, path);
477 ret = btrfs_insert_empty_item(trans, root, path, &ins,
478 sizeof(*extent));
8c2383c3 479
5f39d397 480 leaf = path->nodes[0];
8c2383c3 481 if (ret) {
5f39d397
CM
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);
8c2383c3 484 }
39279cc3 485 BUG_ON(ret);
5f39d397
CM
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,
db94535d
CM
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);
5f39d397 497 btrfs_set_file_extent_type(leaf, extent,
39279cc3 498 BTRFS_FILE_EXTENT_REG);
db94535d 499
39279cc3 500 btrfs_mark_buffer_dirty(path->nodes[0]);
db94535d 501 if (le64_to_cpu(old.disk_bytenr) != 0) {
39279cc3 502 inode->i_blocks +=
db94535d
CM
503 btrfs_file_extent_num_bytes(leaf,
504 extent) >> 9;
39279cc3
CM
505 }
506 ret = 0;
507 goto out;
508 }
509 }
510out:
511 btrfs_free_path(path);
512 return ret;
513}
514
515/*
516 * this gets pages into the page cache and locks them down
517 */
518static 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,
8c2383c3 525 size_t write_bytes)
39279cc3
CM
526{
527 int i;
528 unsigned long index = pos >> PAGE_CACHE_SHIFT;
529 struct inode *inode = file->f_path.dentry->d_inode;
39279cc3 530 int err = 0;
8c2383c3 531 u64 start_pos;
8c2383c3 532
5f39d397 533 start_pos = pos & ~((u64)root->sectorsize - 1);
39279cc3
CM
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;
a52d9a80 541 BUG_ON(1);
39279cc3 542 }
ccd467d6
CM
543 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
544 wait_on_page_writeback(pages[i]);
b3cfa35a 545 set_page_extent_mapped(pages[i]);
b888db2b 546 WARN_ON(!PageLocked(pages[i]));
39279cc3
CM
547 }
548 return 0;
39279cc3
CM
549}
550
551static 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;
8c2383c3
CM
560 struct page **pages = NULL;
561 int nrptrs;
39279cc3
CM
562 struct page *pinned[2];
563 unsigned long first_index;
564 unsigned long last_index;
8c2383c3
CM
565
566 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
567 PAGE_CACHE_SIZE / (sizeof(struct page *)));
39279cc3
CM
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
8c2383c3 585 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
39279cc3
CM
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])) {
9ebefb18 599 ret = btrfs_readpage(NULL, pinned[0]);
39279cc3
CM
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])) {
9ebefb18 609 ret = btrfs_readpage(NULL, pinned[1]);
39279cc3
CM
610 BUG_ON(ret);
611 wait_on_page_locked(pinned[1]);
612 } else {
613 unlock_page(pinned[1]);
614 }
615 }
616
39279cc3
CM
617 while(count > 0) {
618 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
11bd143f
CM
619 size_t write_bytes = min(count, nrptrs *
620 (size_t)PAGE_CACHE_SIZE -
8c2383c3 621 offset);
39279cc3
CM
622 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
623 PAGE_CACHE_SHIFT;
624
8c2383c3 625 WARN_ON(num_pages > nrptrs);
39279cc3
CM
626 memset(pages, 0, sizeof(pages));
627 ret = prepare_pages(root, file, pages, num_pages,
628 pos, first_index, last_index,
8c2383c3 629 write_bytes);
54aa1f4d
CM
630 if (ret)
631 goto out;
39279cc3 632
39279cc3
CM
633 ret = btrfs_copy_from_user(pos, num_pages,
634 write_bytes, pages, buf);
54aa1f4d
CM
635 if (ret) {
636 btrfs_drop_pages(pages, num_pages);
637 goto out;
638 }
39279cc3
CM
639
640 ret = dirty_and_release_pages(NULL, root, file, pages,
641 num_pages, pos, write_bytes);
39279cc3 642 btrfs_drop_pages(pages, num_pages);
54aa1f4d
CM
643 if (ret)
644 goto out;
39279cc3
CM
645
646 buf += write_bytes;
647 count -= write_bytes;
648 pos += write_bytes;
649 num_written += write_bytes;
650
8c2383c3 651 balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
d3c2fdcf 652 btrfs_btree_balance_dirty(root, 1);
39279cc3
CM
653 cond_resched();
654 }
39279cc3
CM
655 mutex_unlock(&inode->i_mutex);
656out:
8c2383c3 657 kfree(pages);
39279cc3
CM
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;
39279cc3
CM
664 return num_written ? num_written : err;
665}
666
39279cc3
CM
667static 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;
15ee9bc7 672 int ret = 0;
39279cc3
CM
673 struct btrfs_trans_handle *trans;
674
675 /*
15ee9bc7
JB
676 * check the transaction that last modified this inode
677 * and see if its already been committed
39279cc3
CM
678 */
679 mutex_lock(&root->fs_info->fs_mutex);
15ee9bc7
JB
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 /*
a52d9a80
CM
692 * ok we haven't committed the transaction yet, lets do a commit
693 */
39279cc3
CM
694 trans = btrfs_start_transaction(root, 1);
695 if (!trans) {
696 ret = -ENOMEM;
697 goto out;
698 }
699 ret = btrfs_commit_transaction(trans, root);
39279cc3 700out:
15ee9bc7 701 mutex_unlock(&root->fs_info->fs_mutex);
39279cc3
CM
702 return ret > 0 ? EIO : ret;
703}
704
9ebefb18 705static struct vm_operations_struct btrfs_file_vm_ops = {
92fee66d
CM
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
9ebefb18
CM
712 .page_mkwrite = btrfs_page_mkwrite,
713};
714
715static 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
39279cc3
CM
722struct file_operations btrfs_file_operations = {
723 .llseek = generic_file_llseek,
724 .read = do_sync_read,
9ebefb18 725 .aio_read = generic_file_aio_read,
39279cc3 726 .write = btrfs_file_write,
9ebefb18 727 .mmap = btrfs_file_mmap,
39279cc3 728 .open = generic_file_open,
39279cc3 729 .fsync = btrfs_sync_file,
34287aa3 730 .unlocked_ioctl = btrfs_ioctl,
39279cc3 731#ifdef CONFIG_COMPAT
34287aa3 732 .compat_ioctl = btrfs_ioctl,
39279cc3
CM
733#endif
734};
735
This page took 0.060277 seconds and 5 git commands to generate.