blob: acef8cb7fb791253c4de3a2bb31c73eeb2b83679 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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
Chris Mason39279cc2007-06-12 06:35:45 -040019#include <linux/buffer_head.h>
20#include <linux/fs.h>
21#include <linux/pagemap.h>
22#include <linux/highmem.h>
23#include <linux/time.h>
24#include <linux/init.h>
25#include <linux/string.h>
26#include <linux/smp_lock.h>
27#include <linux/backing-dev.h>
28#include <linux/mpage.h>
29#include <linux/swap.h>
30#include <linux/writeback.h>
31#include <linux/statfs.h>
32#include <linux/compat.h>
Chris Mason92fee662007-07-25 12:31:35 -040033#include <linux/version.h>
Chris Mason39279cc2007-06-12 06:35:45 -040034#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
42static 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
72static 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
Chris Masona52d9a82007-08-27 16:49:44 -040084static int insert_inline_extent(struct btrfs_trans_handle *trans,
85 struct btrfs_root *root, struct inode *inode,
Chris Mason54aa1f42007-06-22 14:16:25 -040086 u64 offset, ssize_t size,
Chris Masona52d9a82007-08-27 16:49:44 -040087 struct page *page, size_t page_offset)
Chris Mason54aa1f42007-06-22 14:16:25 -040088{
89 struct btrfs_key key;
90 struct btrfs_path *path;
91 char *ptr, *kaddr;
Chris Mason54aa1f42007-06-22 14:16:25 -040092 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
Chris Mason54aa1f42007-06-22 14:16:25 -0400101 btrfs_set_trans_block_group(trans, inode);
102
103 key.objectid = inode->i_ino;
104 key.offset = offset;
105 key.flags = 0;
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 ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]),
117 path->slots[0], struct btrfs_file_extent_item);
118 btrfs_set_file_extent_generation(ei, trans->transid);
119 btrfs_set_file_extent_type(ei,
120 BTRFS_FILE_EXTENT_INLINE);
121 ptr = btrfs_file_extent_inline_start(ei);
122
Chris Masona52d9a82007-08-27 16:49:44 -0400123 kaddr = kmap_atomic(page, KM_USER0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400124 btrfs_memcpy(root, path->nodes[0]->b_data,
Chris Masona52d9a82007-08-27 16:49:44 -0400125 ptr, kaddr + page_offset, size);
Chris Mason54aa1f42007-06-22 14:16:25 -0400126 kunmap_atomic(kaddr, KM_USER0);
Chris Masonccd467d2007-06-28 15:57:36 -0400127 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason54aa1f42007-06-22 14:16:25 -0400128fail:
129 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400130 return err;
131}
132
Chris Mason39279cc2007-06-12 06:35:45 -0400133static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
134 struct btrfs_root *root,
135 struct file *file,
136 struct page **pages,
137 size_t num_pages,
138 loff_t pos,
139 size_t write_bytes)
140{
Chris Mason39279cc2007-06-12 06:35:45 -0400141 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400142 int i;
Chris Mason39279cc2007-06-12 06:35:45 -0400143 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masona52d9a82007-08-27 16:49:44 -0400144 struct extent_map *em;
145 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masona52d9a82007-08-27 16:49:44 -0400146 u64 hint_block;
147 u64 num_blocks;
148 u64 start_pos;
149 u64 end_of_last_block;
150 u64 end_pos = pos + write_bytes;
151 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400152
Chris Masona52d9a82007-08-27 16:49:44 -0400153 em = alloc_extent_map(GFP_NOFS);
154 if (!em)
155 return -ENOMEM;
Chris Mason39279cc2007-06-12 06:35:45 -0400156
Chris Masona52d9a82007-08-27 16:49:44 -0400157 em->bdev = inode->i_sb->s_bdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400158
Chris Masona52d9a82007-08-27 16:49:44 -0400159 start_pos = pos & ~((u64)root->blocksize - 1);
160 num_blocks = (write_bytes + pos - start_pos + root->blocksize - 1) >>
161 inode->i_blkbits;
Chris Mason39279cc2007-06-12 06:35:45 -0400162
Chris Masona52d9a82007-08-27 16:49:44 -0400163 end_of_last_block = start_pos + (num_blocks << inode->i_blkbits) - 1;
Chris Masonb888db22007-08-27 16:49:44 -0400164 lock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400165 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 %lu %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
Chris Masona52d9a82007-08-27 16:49:44 -0400182 /* 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->blocksize - 1;
187 last_pos_in_file = (isize + mask) & ~mask;
188 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400189
Chris Masona52d9a82007-08-27 16:49:44 -0400190 if (last_pos_in_file < start_pos) {
Chris Mason2bf5a722007-08-30 11:54:02 -0400191 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;
Chris Masona52d9a82007-08-27 16:49:44 -0400199 err = btrfs_insert_file_extent(trans, root,
200 inode->i_ino,
201 last_pos_in_file,
202 0, 0, hole_size);
Chris Mason39279cc2007-06-12 06:35:45 -0400203 }
Chris Masona52d9a82007-08-27 16:49:44 -0400204 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)) {
Chris Masonb888db22007-08-27 16:49:44 -0400214 u64 last_end;
Chris Masona52d9a82007-08-27 16:49:44 -0400215 for (i = 0; i < num_pages; i++) {
216 struct page *p = pages[i];
217 SetPageUptodate(p);
Chris Masonb888db22007-08-27 16:49:44 -0400218 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400219 }
Chris Masonb888db22007-08-27 16:49:44 -0400220 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);
Chris Masona52d9a82007-08-27 16:49:44 -0400224 } else {
225 struct page *p = pages[0];
Chris Masonb888db22007-08-27 16:49:44 -0400226 /* step one, delete the existing extents in this range */
227 /* FIXME blocksize != pagesize */
Chris Mason2bf5a722007-08-30 11:54:02 -0400228 err = btrfs_drop_extents(trans, root, inode, start_pos,
229 (pos + write_bytes + root->blocksize -1) &
230 ~((u64)root->blocksize - 1), &hint_block);
231 if (err)
232 goto failed;
Chris Masonb888db22007-08-27 16:49:44 -0400233
Chris Masona52d9a82007-08-27 16:49:44 -0400234 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;
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);
Chris Mason39279cc2007-06-12 06:35:45 -0400246 }
247failed:
Chris Masona52d9a82007-08-27 16:49:44 -0400248 err = btrfs_end_transaction(trans, root);
249out_unlock:
250 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masonb888db22007-08-27 16:49:44 -0400251 unlock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400252 free_extent_map(em);
Chris Mason39279cc2007-06-12 06:35:45 -0400253 return err;
254}
255
Chris Masona52d9a82007-08-27 16:49:44 -0400256int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
257{
258 struct extent_map *em;
259 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
260
261 while(1) {
262 em = lookup_extent_mapping(em_tree, start, end);
263 if (!em)
264 break;
265 remove_extent_mapping(em_tree, em);
266 /* once for us */
267 free_extent_map(em);
268 /* once for the tree*/
269 free_extent_map(em);
270 }
271 return 0;
272}
273
Chris Mason39279cc2007-06-12 06:35:45 -0400274/*
275 * this is very complex, but the basic idea is to drop all extents
276 * in the range start - end. hint_block is filled in with a block number
277 * that would be a good hint to the block allocator for this file.
278 *
279 * If an extent intersects the range but is not entirely inside the range
280 * it is either truncated or split. Anything entirely inside the range
281 * is deleted from the tree.
282 */
283int btrfs_drop_extents(struct btrfs_trans_handle *trans,
284 struct btrfs_root *root, struct inode *inode,
285 u64 start, u64 end, u64 *hint_block)
286{
287 int ret;
288 struct btrfs_key key;
289 struct btrfs_leaf *leaf;
290 int slot;
291 struct btrfs_file_extent_item *extent;
292 u64 extent_end = 0;
293 int keep;
294 struct btrfs_file_extent_item old;
295 struct btrfs_path *path;
296 u64 search_start = start;
297 int bookend;
298 int found_type;
299 int found_extent;
300 int found_inline;
Chris Masonccd467d2007-06-28 15:57:36 -0400301 int recow;
Chris Mason39279cc2007-06-12 06:35:45 -0400302
Chris Masona52d9a82007-08-27 16:49:44 -0400303 btrfs_drop_extent_cache(inode, start, end - 1);
304
Chris Mason39279cc2007-06-12 06:35:45 -0400305 path = btrfs_alloc_path();
306 if (!path)
307 return -ENOMEM;
308 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400309 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400310 btrfs_release_path(root, path);
311 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
312 search_start, -1);
313 if (ret < 0)
314 goto out;
315 if (ret > 0) {
316 if (path->slots[0] == 0) {
317 ret = 0;
318 goto out;
319 }
320 path->slots[0]--;
321 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400322next_slot:
Chris Mason39279cc2007-06-12 06:35:45 -0400323 keep = 0;
324 bookend = 0;
325 found_extent = 0;
326 found_inline = 0;
327 extent = NULL;
328 leaf = btrfs_buffer_leaf(path->nodes[0]);
329 slot = path->slots[0];
Chris Mason8c2383c2007-06-18 09:57:58 -0400330 ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400331 btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key);
332 if (key.offset >= end || key.objectid != inode->i_ino) {
Chris Mason39279cc2007-06-12 06:35:45 -0400333 goto out;
334 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400335 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) {
Chris Mason39279cc2007-06-12 06:35:45 -0400336 goto out;
337 }
Chris Masonccd467d2007-06-28 15:57:36 -0400338 if (recow) {
339 search_start = key.offset;
340 continue;
341 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400342 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
343 extent = btrfs_item_ptr(leaf, slot,
344 struct btrfs_file_extent_item);
345 found_type = btrfs_file_extent_type(extent);
346 if (found_type == BTRFS_FILE_EXTENT_REG) {
347 extent_end = key.offset +
348 (btrfs_file_extent_num_blocks(extent) <<
349 inode->i_blkbits);
350 found_extent = 1;
351 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
352 found_inline = 1;
353 extent_end = key.offset +
354 btrfs_file_extent_inline_len(leaf->items +
355 slot);
356 }
357 } else {
358 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400359 }
360
361 /* we found nothing we can drop */
Chris Mason8c2383c2007-06-18 09:57:58 -0400362 if ((!found_extent && !found_inline) ||
363 search_start >= extent_end) {
364 int nextret;
365 u32 nritems;
366 nritems = btrfs_header_nritems(
367 btrfs_buffer_header(path->nodes[0]));
368 if (slot >= nritems - 1) {
369 nextret = btrfs_next_leaf(root, path);
370 if (nextret)
371 goto out;
Chris Masonccd467d2007-06-28 15:57:36 -0400372 recow = 1;
Chris Mason8c2383c2007-06-18 09:57:58 -0400373 } else {
374 path->slots[0]++;
375 }
376 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400377 }
378
379 /* FIXME, there's only one inline extent allowed right now */
380 if (found_inline) {
381 u64 mask = root->blocksize - 1;
382 search_start = (extent_end + mask) & ~mask;
383 } else
384 search_start = extent_end;
385
386 if (end < extent_end && end >= key.offset) {
387 if (found_extent) {
388 u64 disk_blocknr =
389 btrfs_file_extent_disk_blocknr(extent);
390 u64 disk_num_blocks =
391 btrfs_file_extent_disk_num_blocks(extent);
392 memcpy(&old, extent, sizeof(old));
393 if (disk_blocknr != 0) {
394 ret = btrfs_inc_extent_ref(trans, root,
395 disk_blocknr, disk_num_blocks);
396 BUG_ON(ret);
397 }
398 }
399 WARN_ON(found_inline);
400 bookend = 1;
401 }
Chris Mason39279cc2007-06-12 06:35:45 -0400402 /* truncate existing extent */
403 if (start > key.offset) {
404 u64 new_num;
405 u64 old_num;
406 keep = 1;
407 WARN_ON(start & (root->blocksize - 1));
408 if (found_extent) {
409 new_num = (start - key.offset) >>
410 inode->i_blkbits;
411 old_num = btrfs_file_extent_num_blocks(extent);
412 *hint_block =
413 btrfs_file_extent_disk_blocknr(extent);
414 if (btrfs_file_extent_disk_blocknr(extent)) {
415 inode->i_blocks -=
416 (old_num - new_num) << 3;
417 }
418 btrfs_set_file_extent_num_blocks(extent,
419 new_num);
Chris Masonccd467d2007-06-28 15:57:36 -0400420 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400421 } else {
422 WARN_ON(1);
423 }
424 }
425 /* delete the entire extent */
426 if (!keep) {
427 u64 disk_blocknr = 0;
428 u64 disk_num_blocks = 0;
429 u64 extent_num_blocks = 0;
430 if (found_extent) {
431 disk_blocknr =
432 btrfs_file_extent_disk_blocknr(extent);
433 disk_num_blocks =
434 btrfs_file_extent_disk_num_blocks(extent);
435 extent_num_blocks =
436 btrfs_file_extent_num_blocks(extent);
437 *hint_block =
438 btrfs_file_extent_disk_blocknr(extent);
439 }
440 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400441 /* TODO update progress marker and return */
Chris Mason39279cc2007-06-12 06:35:45 -0400442 BUG_ON(ret);
443 btrfs_release_path(root, path);
444 extent = NULL;
445 if (found_extent && disk_blocknr != 0) {
446 inode->i_blocks -= extent_num_blocks << 3;
447 ret = btrfs_free_extent(trans, root,
448 disk_blocknr,
449 disk_num_blocks, 0);
450 }
451
452 BUG_ON(ret);
453 if (!bookend && search_start >= end) {
454 ret = 0;
455 goto out;
456 }
457 if (!bookend)
458 continue;
459 }
460 /* create bookend, splitting the extent in two */
461 if (bookend && found_extent) {
462 struct btrfs_key ins;
463 ins.objectid = inode->i_ino;
464 ins.offset = end;
465 ins.flags = 0;
466 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -0400467 btrfs_release_path(root, path);
468 ret = btrfs_insert_empty_item(trans, root, path, &ins,
469 sizeof(*extent));
Chris Mason8c2383c2007-06-18 09:57:58 -0400470
471 if (ret) {
472 btrfs_print_leaf(root, btrfs_buffer_leaf(path->nodes[0]));
Chris Mason2bf5a722007-08-30 11:54:02 -0400473 printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu keep was %d\n", ret , ins.objectid, ins.flags, ins.offset, start, end, key.offset, extent_end, keep);
Chris Mason8c2383c2007-06-18 09:57:58 -0400474 }
Chris Mason39279cc2007-06-12 06:35:45 -0400475 BUG_ON(ret);
476 extent = btrfs_item_ptr(
477 btrfs_buffer_leaf(path->nodes[0]),
478 path->slots[0],
479 struct btrfs_file_extent_item);
480 btrfs_set_file_extent_disk_blocknr(extent,
481 btrfs_file_extent_disk_blocknr(&old));
482 btrfs_set_file_extent_disk_num_blocks(extent,
483 btrfs_file_extent_disk_num_blocks(&old));
484
485 btrfs_set_file_extent_offset(extent,
486 btrfs_file_extent_offset(&old) +
487 ((end - key.offset) >> inode->i_blkbits));
488 WARN_ON(btrfs_file_extent_num_blocks(&old) <
489 (extent_end - end) >> inode->i_blkbits);
490 btrfs_set_file_extent_num_blocks(extent,
491 (extent_end - end) >> inode->i_blkbits);
492
493 btrfs_set_file_extent_type(extent,
494 BTRFS_FILE_EXTENT_REG);
495 btrfs_set_file_extent_generation(extent,
496 btrfs_file_extent_generation(&old));
497 btrfs_mark_buffer_dirty(path->nodes[0]);
498 if (btrfs_file_extent_disk_blocknr(&old) != 0) {
499 inode->i_blocks +=
500 btrfs_file_extent_num_blocks(extent) << 3;
501 }
502 ret = 0;
503 goto out;
504 }
505 }
506out:
507 btrfs_free_path(path);
508 return ret;
509}
510
511/*
512 * this gets pages into the page cache and locks them down
513 */
514static int prepare_pages(struct btrfs_root *root,
515 struct file *file,
516 struct page **pages,
517 size_t num_pages,
518 loff_t pos,
519 unsigned long first_index,
520 unsigned long last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400521 size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400522{
523 int i;
524 unsigned long index = pos >> PAGE_CACHE_SHIFT;
525 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400526 int err = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400527 u64 num_blocks;
Chris Mason8c2383c2007-06-18 09:57:58 -0400528 u64 start_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400529
Chris Masonb888db22007-08-27 16:49:44 -0400530 start_pos = pos & ~((u64)root->blocksize - 1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400531 num_blocks = (write_bytes + pos - start_pos + root->blocksize - 1) >>
532 inode->i_blkbits;
Chris Mason39279cc2007-06-12 06:35:45 -0400533
534 memset(pages, 0, num_pages * sizeof(struct page *));
535
536 for (i = 0; i < num_pages; i++) {
537 pages[i] = grab_cache_page(inode->i_mapping, index + i);
538 if (!pages[i]) {
539 err = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -0400540 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400541 }
Chris Masonccd467d2007-06-28 15:57:36 -0400542 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
543 wait_on_page_writeback(pages[i]);
Chris Masona52d9a82007-08-27 16:49:44 -0400544 if (!PagePrivate(pages[i])) {
545 SetPagePrivate(pages[i]);
546 set_page_private(pages[i], 1);
Chris Masonb888db22007-08-27 16:49:44 -0400547 WARN_ON(!pages[i]->mapping->a_ops->invalidatepage);
Chris Masona52d9a82007-08-27 16:49:44 -0400548 page_cache_get(pages[i]);
Chris Mason8c2383c2007-06-18 09:57:58 -0400549 }
Chris Masonb888db22007-08-27 16:49:44 -0400550 WARN_ON(!PageLocked(pages[i]));
Chris Mason39279cc2007-06-12 06:35:45 -0400551 }
552 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400553}
554
555static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
556 size_t count, loff_t *ppos)
557{
558 loff_t pos;
559 size_t num_written = 0;
560 int err = 0;
561 int ret = 0;
562 struct inode *inode = file->f_path.dentry->d_inode;
563 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8c2383c2007-06-18 09:57:58 -0400564 struct page **pages = NULL;
565 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400566 struct page *pinned[2];
567 unsigned long first_index;
568 unsigned long last_index;
Chris Mason8c2383c2007-06-18 09:57:58 -0400569
570 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
571 PAGE_CACHE_SIZE / (sizeof(struct page *)));
Chris Mason39279cc2007-06-12 06:35:45 -0400572 pinned[0] = NULL;
573 pinned[1] = NULL;
574 if (file->f_flags & O_DIRECT)
575 return -EINVAL;
576 pos = *ppos;
577 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
578 current->backing_dev_info = inode->i_mapping->backing_dev_info;
579 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
580 if (err)
581 goto out;
582 if (count == 0)
583 goto out;
584 err = remove_suid(file->f_path.dentry);
585 if (err)
586 goto out;
587 file_update_time(file);
588
Chris Mason8c2383c2007-06-18 09:57:58 -0400589 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -0400590
591 mutex_lock(&inode->i_mutex);
592 first_index = pos >> PAGE_CACHE_SHIFT;
593 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
594
595 /*
596 * there are lots of better ways to do this, but this code
597 * makes sure the first and last page in the file range are
598 * up to date and ready for cow
599 */
600 if ((pos & (PAGE_CACHE_SIZE - 1))) {
601 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
602 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400603 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400604 BUG_ON(ret);
605 wait_on_page_locked(pinned[0]);
606 } else {
607 unlock_page(pinned[0]);
608 }
609 }
610 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
611 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
612 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400613 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400614 BUG_ON(ret);
615 wait_on_page_locked(pinned[1]);
616 } else {
617 unlock_page(pinned[1]);
618 }
619 }
620
Chris Mason39279cc2007-06-12 06:35:45 -0400621 while(count > 0) {
622 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Chris Mason11bd1432007-06-22 14:16:24 -0400623 size_t write_bytes = min(count, nrptrs *
624 (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400625 offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400626 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
627 PAGE_CACHE_SHIFT;
628
Chris Mason8c2383c2007-06-18 09:57:58 -0400629 WARN_ON(num_pages > nrptrs);
Chris Mason39279cc2007-06-12 06:35:45 -0400630 memset(pages, 0, sizeof(pages));
631 ret = prepare_pages(root, file, pages, num_pages,
632 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400633 write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400634 if (ret)
635 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -0400636
Chris Mason39279cc2007-06-12 06:35:45 -0400637 ret = btrfs_copy_from_user(pos, num_pages,
638 write_bytes, pages, buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400639 if (ret) {
640 btrfs_drop_pages(pages, num_pages);
641 goto out;
642 }
Chris Mason39279cc2007-06-12 06:35:45 -0400643
644 ret = dirty_and_release_pages(NULL, root, file, pages,
645 num_pages, pos, write_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -0400646 btrfs_drop_pages(pages, num_pages);
Chris Mason54aa1f42007-06-22 14:16:25 -0400647 if (ret)
648 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -0400649
650 buf += write_bytes;
651 count -= write_bytes;
652 pos += write_bytes;
653 num_written += write_bytes;
654
Chris Mason8c2383c2007-06-18 09:57:58 -0400655 balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
Chris Mason39279cc2007-06-12 06:35:45 -0400656 btrfs_btree_balance_dirty(root);
657 cond_resched();
658 }
Chris Mason39279cc2007-06-12 06:35:45 -0400659 mutex_unlock(&inode->i_mutex);
660out:
Chris Mason8c2383c2007-06-18 09:57:58 -0400661 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -0400662 if (pinned[0])
663 page_cache_release(pinned[0]);
664 if (pinned[1])
665 page_cache_release(pinned[1]);
666 *ppos = pos;
667 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -0400668 return num_written ? num_written : err;
669}
670
Chris Mason39279cc2007-06-12 06:35:45 -0400671static int btrfs_sync_file(struct file *file,
672 struct dentry *dentry, int datasync)
673{
674 struct inode *inode = dentry->d_inode;
675 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400676 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400677 struct btrfs_trans_handle *trans;
678
679 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400680 * check the transaction that last modified this inode
681 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -0400682 */
683 mutex_lock(&root->fs_info->fs_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400684 if (!BTRFS_I(inode)->last_trans)
685 goto out;
686 mutex_lock(&root->fs_info->trans_mutex);
687 if (BTRFS_I(inode)->last_trans <=
688 root->fs_info->last_trans_committed) {
689 BTRFS_I(inode)->last_trans = 0;
690 mutex_unlock(&root->fs_info->trans_mutex);
691 goto out;
692 }
693 mutex_unlock(&root->fs_info->trans_mutex);
694
695 /*
Chris Masona52d9a82007-08-27 16:49:44 -0400696 * ok we haven't committed the transaction yet, lets do a commit
697 */
Chris Mason39279cc2007-06-12 06:35:45 -0400698 trans = btrfs_start_transaction(root, 1);
699 if (!trans) {
700 ret = -ENOMEM;
701 goto out;
702 }
703 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -0400704out:
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400705 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400706 return ret > 0 ? EIO : ret;
707}
708
Chris Mason9ebefb182007-06-15 13:50:00 -0400709static struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -0400710#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
711 .nopage = filemap_nopage,
712 .populate = filemap_populate,
713#else
714 .fault = filemap_fault,
715#endif
Chris Mason9ebefb182007-06-15 13:50:00 -0400716 .page_mkwrite = btrfs_page_mkwrite,
717};
718
719static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
720{
721 vma->vm_ops = &btrfs_file_vm_ops;
722 file_accessed(filp);
723 return 0;
724}
725
Chris Mason39279cc2007-06-12 06:35:45 -0400726struct file_operations btrfs_file_operations = {
727 .llseek = generic_file_llseek,
728 .read = do_sync_read,
Chris Mason9ebefb182007-06-15 13:50:00 -0400729 .aio_read = generic_file_aio_read,
Chris Mason39279cc2007-06-12 06:35:45 -0400730 .write = btrfs_file_write,
Chris Mason9ebefb182007-06-15 13:50:00 -0400731 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -0400732 .open = generic_file_open,
733 .ioctl = btrfs_ioctl,
734 .fsync = btrfs_sync_file,
735#ifdef CONFIG_COMPAT
736 .compat_ioctl = btrfs_compat_ioctl,
737#endif
738};
739