blob: bd7f1b01e051f61afbd7456966b71b93fda45a99 [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/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>
Chris Mason39279cc2007-06-12 06:35:45 -040025#include <linux/backing-dev.h>
26#include <linux/mpage.h>
Christoph Hellwig2fe17c12011-01-14 13:07:43 +010027#include <linux/falloc.h>
Chris Mason39279cc2007-06-12 06:35:45 -040028#include <linux/swap.h>
29#include <linux/writeback.h>
30#include <linux/statfs.h>
31#include <linux/compat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#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"
Chris Masone02119d2008-09-05 16:13:11 -040039#include "tree-log.h"
40#include "locking.h"
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -040041#include "compat.h"
Josef Bacik2aaa6652012-08-29 14:27:18 -040042#include "volumes.h"
Chris Mason39279cc2007-06-12 06:35:45 -040043
Chris Mason4cb53002011-05-24 15:35:30 -040044/*
45 * when auto defrag is enabled we
46 * queue up these defrag structs to remember which
47 * inodes need defragging passes
48 */
49struct inode_defrag {
50 struct rb_node rb_node;
51 /* objectid */
52 u64 ino;
53 /*
54 * transid where the defrag was added, we search for
55 * extents newer than this
56 */
57 u64 transid;
58
59 /* root objectid */
60 u64 root;
61
62 /* last offset we were able to defrag */
63 u64 last_offset;
64
65 /* if we've wrapped around back to zero once already */
66 int cycled;
67};
68
Miao Xie762f2262012-05-24 18:58:27 +080069static int __compare_inode_defrag(struct inode_defrag *defrag1,
70 struct inode_defrag *defrag2)
71{
72 if (defrag1->root > defrag2->root)
73 return 1;
74 else if (defrag1->root < defrag2->root)
75 return -1;
76 else if (defrag1->ino > defrag2->ino)
77 return 1;
78 else if (defrag1->ino < defrag2->ino)
79 return -1;
80 else
81 return 0;
82}
83
Chris Mason4cb53002011-05-24 15:35:30 -040084/* pop a record for an inode into the defrag tree. The lock
85 * must be held already
86 *
87 * If you're inserting a record for an older transid than an
88 * existing record, the transid already in the tree is lowered
89 *
90 * If an existing record is found the defrag item you
91 * pass in is freed
92 */
Wanlong Gaoa0f98dd2011-07-18 12:19:35 +000093static void __btrfs_add_inode_defrag(struct inode *inode,
Chris Mason4cb53002011-05-24 15:35:30 -040094 struct inode_defrag *defrag)
95{
96 struct btrfs_root *root = BTRFS_I(inode)->root;
97 struct inode_defrag *entry;
98 struct rb_node **p;
99 struct rb_node *parent = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800100 int ret;
Chris Mason4cb53002011-05-24 15:35:30 -0400101
102 p = &root->fs_info->defrag_inodes.rb_node;
103 while (*p) {
104 parent = *p;
105 entry = rb_entry(parent, struct inode_defrag, rb_node);
106
Miao Xie762f2262012-05-24 18:58:27 +0800107 ret = __compare_inode_defrag(defrag, entry);
108 if (ret < 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400109 p = &parent->rb_left;
Miao Xie762f2262012-05-24 18:58:27 +0800110 else if (ret > 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400111 p = &parent->rb_right;
112 else {
113 /* if we're reinserting an entry for
114 * an old defrag run, make sure to
115 * lower the transid of our existing record
116 */
117 if (defrag->transid < entry->transid)
118 entry->transid = defrag->transid;
119 if (defrag->last_offset > entry->last_offset)
120 entry->last_offset = defrag->last_offset;
121 goto exists;
122 }
123 }
Josef Bacik72ac3c02012-05-23 14:13:11 -0400124 set_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
Chris Mason4cb53002011-05-24 15:35:30 -0400125 rb_link_node(&defrag->rb_node, parent, p);
126 rb_insert_color(&defrag->rb_node, &root->fs_info->defrag_inodes);
Wanlong Gaoa0f98dd2011-07-18 12:19:35 +0000127 return;
Chris Mason4cb53002011-05-24 15:35:30 -0400128
129exists:
130 kfree(defrag);
Wanlong Gaoa0f98dd2011-07-18 12:19:35 +0000131 return;
Chris Mason4cb53002011-05-24 15:35:30 -0400132
133}
134
135/*
136 * insert a defrag record for this inode if auto defrag is
137 * enabled
138 */
139int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
140 struct inode *inode)
141{
142 struct btrfs_root *root = BTRFS_I(inode)->root;
143 struct inode_defrag *defrag;
Chris Mason4cb53002011-05-24 15:35:30 -0400144 u64 transid;
145
146 if (!btrfs_test_opt(root, AUTO_DEFRAG))
147 return 0;
148
David Sterba7841cb22011-05-31 18:07:27 +0200149 if (btrfs_fs_closing(root->fs_info))
Chris Mason4cb53002011-05-24 15:35:30 -0400150 return 0;
151
Josef Bacik72ac3c02012-05-23 14:13:11 -0400152 if (test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags))
Chris Mason4cb53002011-05-24 15:35:30 -0400153 return 0;
154
155 if (trans)
156 transid = trans->transid;
157 else
158 transid = BTRFS_I(inode)->root->last_trans;
159
160 defrag = kzalloc(sizeof(*defrag), GFP_NOFS);
161 if (!defrag)
162 return -ENOMEM;
163
David Sterbaa4689d22011-05-31 17:08:14 +0000164 defrag->ino = btrfs_ino(inode);
Chris Mason4cb53002011-05-24 15:35:30 -0400165 defrag->transid = transid;
166 defrag->root = root->root_key.objectid;
167
168 spin_lock(&root->fs_info->defrag_inodes_lock);
Josef Bacik72ac3c02012-05-23 14:13:11 -0400169 if (!test_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags))
Wanlong Gaoa0f98dd2011-07-18 12:19:35 +0000170 __btrfs_add_inode_defrag(inode, defrag);
Dan Carpenterf4ac9042011-08-05 14:19:00 +0000171 else
172 kfree(defrag);
Chris Mason4cb53002011-05-24 15:35:30 -0400173 spin_unlock(&root->fs_info->defrag_inodes_lock);
Wanlong Gaoa0f98dd2011-07-18 12:19:35 +0000174 return 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400175}
176
177/*
178 * must be called with the defrag_inodes lock held
179 */
Miao Xie762f2262012-05-24 18:58:27 +0800180struct inode_defrag *btrfs_find_defrag_inode(struct btrfs_fs_info *info,
181 u64 root, u64 ino,
Chris Mason4cb53002011-05-24 15:35:30 -0400182 struct rb_node **next)
183{
184 struct inode_defrag *entry = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800185 struct inode_defrag tmp;
Chris Mason4cb53002011-05-24 15:35:30 -0400186 struct rb_node *p;
187 struct rb_node *parent = NULL;
Miao Xie762f2262012-05-24 18:58:27 +0800188 int ret;
189
190 tmp.ino = ino;
191 tmp.root = root;
Chris Mason4cb53002011-05-24 15:35:30 -0400192
193 p = info->defrag_inodes.rb_node;
194 while (p) {
195 parent = p;
196 entry = rb_entry(parent, struct inode_defrag, rb_node);
197
Miao Xie762f2262012-05-24 18:58:27 +0800198 ret = __compare_inode_defrag(&tmp, entry);
199 if (ret < 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400200 p = parent->rb_left;
Miao Xie762f2262012-05-24 18:58:27 +0800201 else if (ret > 0)
Chris Mason4cb53002011-05-24 15:35:30 -0400202 p = parent->rb_right;
203 else
204 return entry;
205 }
206
207 if (next) {
Miao Xie762f2262012-05-24 18:58:27 +0800208 while (parent && __compare_inode_defrag(&tmp, entry) > 0) {
Chris Mason4cb53002011-05-24 15:35:30 -0400209 parent = rb_next(parent);
210 entry = rb_entry(parent, struct inode_defrag, rb_node);
211 }
212 *next = parent;
213 }
214 return NULL;
215}
216
217/*
218 * run through the list of inodes in the FS that need
219 * defragging
220 */
221int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info)
222{
223 struct inode_defrag *defrag;
224 struct btrfs_root *inode_root;
225 struct inode *inode;
226 struct rb_node *n;
227 struct btrfs_key key;
228 struct btrfs_ioctl_defrag_range_args range;
229 u64 first_ino = 0;
Miao Xie762f2262012-05-24 18:58:27 +0800230 u64 root_objectid = 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400231 int num_defrag;
232 int defrag_batch = 1024;
233
234 memset(&range, 0, sizeof(range));
235 range.len = (u64)-1;
236
237 atomic_inc(&fs_info->defrag_running);
238 spin_lock(&fs_info->defrag_inodes_lock);
239 while(1) {
240 n = NULL;
241
242 /* find an inode to defrag */
Miao Xie762f2262012-05-24 18:58:27 +0800243 defrag = btrfs_find_defrag_inode(fs_info, root_objectid,
244 first_ino, &n);
Chris Mason4cb53002011-05-24 15:35:30 -0400245 if (!defrag) {
Miao Xie762f2262012-05-24 18:58:27 +0800246 if (n) {
247 defrag = rb_entry(n, struct inode_defrag,
248 rb_node);
249 } else if (root_objectid || first_ino) {
250 root_objectid = 0;
Chris Mason4cb53002011-05-24 15:35:30 -0400251 first_ino = 0;
252 continue;
253 } else {
254 break;
255 }
256 }
257
258 /* remove it from the rbtree */
259 first_ino = defrag->ino + 1;
Miao Xie762f2262012-05-24 18:58:27 +0800260 root_objectid = defrag->root;
Chris Mason4cb53002011-05-24 15:35:30 -0400261 rb_erase(&defrag->rb_node, &fs_info->defrag_inodes);
262
David Sterba7841cb22011-05-31 18:07:27 +0200263 if (btrfs_fs_closing(fs_info))
Chris Mason4cb53002011-05-24 15:35:30 -0400264 goto next_free;
265
266 spin_unlock(&fs_info->defrag_inodes_lock);
267
268 /* get the inode */
269 key.objectid = defrag->root;
270 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
271 key.offset = (u64)-1;
272 inode_root = btrfs_read_fs_root_no_name(fs_info, &key);
273 if (IS_ERR(inode_root))
274 goto next;
275
276 key.objectid = defrag->ino;
277 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
278 key.offset = 0;
279
280 inode = btrfs_iget(fs_info->sb, &key, inode_root, NULL);
281 if (IS_ERR(inode))
282 goto next;
283
284 /* do a chunk of defrag */
Josef Bacik72ac3c02012-05-23 14:13:11 -0400285 clear_bit(BTRFS_INODE_IN_DEFRAG, &BTRFS_I(inode)->runtime_flags);
Chris Mason4cb53002011-05-24 15:35:30 -0400286 range.start = defrag->last_offset;
287 num_defrag = btrfs_defrag_file(inode, NULL, &range, defrag->transid,
288 defrag_batch);
289 /*
290 * if we filled the whole defrag batch, there
291 * must be more work to do. Queue this defrag
292 * again
293 */
294 if (num_defrag == defrag_batch) {
295 defrag->last_offset = range.start;
296 __btrfs_add_inode_defrag(inode, defrag);
297 /*
298 * we don't want to kfree defrag, we added it back to
299 * the rbtree
300 */
301 defrag = NULL;
302 } else if (defrag->last_offset && !defrag->cycled) {
303 /*
304 * we didn't fill our defrag batch, but
305 * we didn't start at zero. Make sure we loop
306 * around to the start of the file.
307 */
308 defrag->last_offset = 0;
309 defrag->cycled = 1;
310 __btrfs_add_inode_defrag(inode, defrag);
311 defrag = NULL;
312 }
313
314 iput(inode);
315next:
316 spin_lock(&fs_info->defrag_inodes_lock);
317next_free:
318 kfree(defrag);
319 }
320 spin_unlock(&fs_info->defrag_inodes_lock);
321
322 atomic_dec(&fs_info->defrag_running);
323
324 /*
325 * during unmount, we use the transaction_wait queue to
326 * wait for the defragger to stop
327 */
328 wake_up(&fs_info->transaction_wait);
329 return 0;
330}
Chris Mason39279cc2007-06-12 06:35:45 -0400331
Chris Masond352ac62008-09-29 15:18:18 -0400332/* simple helper to fault in pages and copy. This should go away
333 * and be replaced with calls into generic code.
334 */
Chris Masond3977122009-01-05 21:25:51 -0500335static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
Josef Bacikd0215f32011-01-25 14:57:24 -0500336 size_t write_bytes,
Chris Masona1b32a52008-09-05 16:09:51 -0400337 struct page **prepared_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -0400338 struct iov_iter *i)
Chris Mason39279cc2007-06-12 06:35:45 -0400339{
Xin Zhong914ee292010-12-09 09:30:14 +0000340 size_t copied = 0;
Josef Bacikd0215f32011-01-25 14:57:24 -0500341 size_t total_copied = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400342 int pg = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400343 int offset = pos & (PAGE_CACHE_SIZE - 1);
344
Josef Bacik11c65dc2010-05-23 11:07:21 -0400345 while (write_bytes > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400346 size_t count = min_t(size_t,
347 PAGE_CACHE_SIZE - offset, write_bytes);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400348 struct page *page = prepared_pages[pg];
Xin Zhong914ee292010-12-09 09:30:14 +0000349 /*
350 * Copy data from userspace to the current page
351 *
352 * Disable pagefault to avoid recursive lock since
353 * the pages are already locked
354 */
355 pagefault_disable();
356 copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
357 pagefault_enable();
Josef Bacik11c65dc2010-05-23 11:07:21 -0400358
Chris Mason39279cc2007-06-12 06:35:45 -0400359 /* Flush processor's dcache for this page */
360 flush_dcache_page(page);
Chris Mason31339ac2011-03-07 11:10:24 -0500361
362 /*
363 * if we get a partial write, we can end up with
364 * partially up to date pages. These add
365 * a lot of complexity, so make sure they don't
366 * happen by forcing this copy to be retried.
367 *
368 * The rest of the btrfs_file_write code will fall
369 * back to page at a time copies after we return 0.
370 */
371 if (!PageUptodate(page) && copied < count)
372 copied = 0;
373
Josef Bacik11c65dc2010-05-23 11:07:21 -0400374 iov_iter_advance(i, copied);
375 write_bytes -= copied;
Xin Zhong914ee292010-12-09 09:30:14 +0000376 total_copied += copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400377
Xin Zhong914ee292010-12-09 09:30:14 +0000378 /* Return to btrfs_file_aio_write to fault page */
Josef Bacik9f570b82011-01-25 12:42:37 -0500379 if (unlikely(copied == 0))
Xin Zhong914ee292010-12-09 09:30:14 +0000380 break;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400381
382 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
383 offset += copied;
384 } else {
385 pg++;
386 offset = 0;
387 }
Chris Mason39279cc2007-06-12 06:35:45 -0400388 }
Xin Zhong914ee292010-12-09 09:30:14 +0000389 return total_copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400390}
391
Chris Masond352ac62008-09-29 15:18:18 -0400392/*
393 * unlocks pages after btrfs_file_write is done with them
394 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400395void btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -0400396{
397 size_t i;
398 for (i = 0; i < num_pages; i++) {
Chris Masond352ac62008-09-29 15:18:18 -0400399 /* page checked is some magic around finding pages that
400 * have been modified without going through btrfs_set_page_dirty
401 * clear it here
402 */
Chris Mason4a096752008-07-21 10:29:44 -0400403 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400404 unlock_page(pages[i]);
405 mark_page_accessed(pages[i]);
406 page_cache_release(pages[i]);
407 }
408}
409
Chris Masond352ac62008-09-29 15:18:18 -0400410/*
411 * after copy_from_user, pages need to be dirtied and we need to make
412 * sure holes are created between the current EOF and the start of
413 * any next extents (if required).
414 *
415 * this also makes the decision about creating an inline extent vs
416 * doing real data extents, marking pages dirty and delalloc as required.
417 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400418int btrfs_dirty_pages(struct btrfs_root *root, struct inode *inode,
419 struct page **pages, size_t num_pages,
420 loff_t pos, size_t write_bytes,
421 struct extent_state **cached)
Chris Mason39279cc2007-06-12 06:35:45 -0400422{
Chris Mason39279cc2007-06-12 06:35:45 -0400423 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400424 int i;
Chris Masondb945352007-10-15 16:15:53 -0400425 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400426 u64 start_pos;
427 u64 end_of_last_block;
428 u64 end_pos = pos + write_bytes;
429 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400430
Chris Mason5f39d392007-10-15 16:14:19 -0400431 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400432 num_bytes = (write_bytes + pos - start_pos +
433 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400434
Chris Masondb945352007-10-15 16:15:53 -0400435 end_of_last_block = start_pos + num_bytes - 1;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000436 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400437 cached);
Josef Bacikd0215f32011-01-25 14:57:24 -0500438 if (err)
439 return err;
Josef Bacik9ed74f22009-09-11 16:12:44 -0400440
Chris Masonc8b97812008-10-29 14:49:59 -0400441 for (i = 0; i < num_pages; i++) {
442 struct page *p = pages[i];
443 SetPageUptodate(p);
444 ClearPageChecked(p);
445 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400446 }
Josef Bacik9f570b82011-01-25 12:42:37 -0500447
448 /*
449 * we've only changed i_size in ram, and we haven't updated
450 * the disk i_size. There is no need to log the inode
451 * at this time.
452 */
453 if (end_pos > isize)
Chris Masona52d9a82007-08-27 16:49:44 -0400454 i_size_write(inode, end_pos);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400455 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400456}
457
Chris Masond352ac62008-09-29 15:18:18 -0400458/*
459 * this drops all the extents in the cache that intersect the range
460 * [start, end]. Existing extents are split as required.
461 */
Josef Bacik7014cdb2012-08-30 20:06:49 -0400462void btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
463 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400464{
465 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400466 struct extent_map *split = NULL;
467 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400468 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500469 u64 len = end - start + 1;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400470 u64 gen;
Chris Mason3b951512008-04-17 11:29:12 -0400471 int ret;
472 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400473 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400474 int compressed = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400475
Chris Masone6dcd2d2008-07-17 12:53:50 -0400476 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400477 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500478 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400479 testend = 0;
480 }
Chris Masond3977122009-01-05 21:25:51 -0500481 while (1) {
Josef Bacik7014cdb2012-08-30 20:06:49 -0400482 int no_splits = 0;
483
Chris Mason3b951512008-04-17 11:29:12 -0400484 if (!split)
David Sterba172ddd62011-04-21 00:48:27 +0200485 split = alloc_extent_map();
Chris Mason3b951512008-04-17 11:29:12 -0400486 if (!split2)
David Sterba172ddd62011-04-21 00:48:27 +0200487 split2 = alloc_extent_map();
Josef Bacik7014cdb2012-08-30 20:06:49 -0400488 if (!split || !split2)
489 no_splits = 1;
Chris Mason3b951512008-04-17 11:29:12 -0400490
Chris Mason890871b2009-09-02 16:24:52 -0400491 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500492 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500493 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400494 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400495 break;
Chris Masond1310b22008-01-24 16:13:08 -0500496 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400497 flags = em->flags;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400498 gen = em->generation;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400499 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000500 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400501 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400502 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400503 break;
504 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000505 start = em->start + em->len;
506 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400507 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400508 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400509 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400510 continue;
511 }
Chris Masonc8b97812008-10-29 14:49:59 -0400512 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400513 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400514 remove_extent_mapping(em_tree, em);
Josef Bacik7014cdb2012-08-30 20:06:49 -0400515 if (no_splits)
516 goto next;
Chris Mason3b951512008-04-17 11:29:12 -0400517
518 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
519 em->start < start) {
520 split->start = em->start;
521 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500522 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400523 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400524
525 if (compressed)
526 split->block_len = em->block_len;
527 else
528 split->block_len = split->len;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400529 split->generation = gen;
Chris Mason3b951512008-04-17 11:29:12 -0400530 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400531 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800532 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400533 ret = add_extent_mapping(em_tree, split);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100534 BUG_ON(ret); /* Logic error */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400535 list_move(&split->list, &em_tree->modified_extents);
Chris Mason3b951512008-04-17 11:29:12 -0400536 free_extent_map(split);
537 split = split2;
538 split2 = NULL;
539 }
540 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
541 testend && em->start + em->len > start + len) {
542 u64 diff = start + len - em->start;
543
544 split->start = start + len;
545 split->len = em->start + em->len - (start + len);
546 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400547 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800548 split->compress_type = em->compress_type;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400549 split->generation = gen;
Chris Mason3b951512008-04-17 11:29:12 -0400550
Chris Masonc8b97812008-10-29 14:49:59 -0400551 if (compressed) {
552 split->block_len = em->block_len;
553 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500554 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400555 } else {
556 split->block_len = split->len;
557 split->block_start = em->block_start + diff;
Chris Mason445a6942008-11-10 11:53:33 -0500558 split->orig_start = split->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400559 }
Chris Mason3b951512008-04-17 11:29:12 -0400560
561 ret = add_extent_mapping(em_tree, split);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100562 BUG_ON(ret); /* Logic error */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400563 list_move(&split->list, &em_tree->modified_extents);
Chris Mason3b951512008-04-17 11:29:12 -0400564 free_extent_map(split);
565 split = NULL;
566 }
Josef Bacik7014cdb2012-08-30 20:06:49 -0400567next:
Chris Mason890871b2009-09-02 16:24:52 -0400568 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500569
Chris Masona52d9a82007-08-27 16:49:44 -0400570 /* once for us */
571 free_extent_map(em);
572 /* once for the tree*/
573 free_extent_map(em);
574 }
Chris Mason3b951512008-04-17 11:29:12 -0400575 if (split)
576 free_extent_map(split);
577 if (split2)
578 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400579}
580
Chris Mason39279cc2007-06-12 06:35:45 -0400581/*
582 * this is very complex, but the basic idea is to drop all extents
583 * in the range start - end. hint_block is filled in with a block number
584 * that would be a good hint to the block allocator for this file.
585 *
586 * If an extent intersects the range but is not entirely inside the range
587 * it is either truncated or split. Anything entirely inside the range
588 * is deleted from the tree.
589 */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400590int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
591 struct btrfs_root *root, struct inode *inode,
592 struct btrfs_path *path, u64 start, u64 end,
Josef Bacik2aaa6652012-08-29 14:27:18 -0400593 u64 *drop_end, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400594{
Chris Mason00f5c792007-11-30 10:09:33 -0500595 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000596 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500597 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000598 struct btrfs_key new_key;
Li Zefan33345d012011-04-20 10:31:50 +0800599 u64 ino = btrfs_ino(inode);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000600 u64 search_start = start;
601 u64 disk_bytenr = 0;
602 u64 num_bytes = 0;
603 u64 extent_offset = 0;
604 u64 extent_end = 0;
605 int del_nr = 0;
606 int del_slot = 0;
607 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400608 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500609 int ret;
Chris Masondc7fdde2012-04-27 14:31:29 -0400610 int modify_tree = -1;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400611 int update_refs = (root->ref_cows || root == root->fs_info->tree_root);
Josef Bacikc3308f82012-09-14 14:51:22 -0400612 int found = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400613
Chris Masona1ed8352009-09-11 12:27:37 -0400614 if (drop_cache)
615 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400616
Chris Masondc7fdde2012-04-27 14:31:29 -0400617 if (start >= BTRFS_I(inode)->disk_i_size)
618 modify_tree = 0;
619
Chris Masond3977122009-01-05 21:25:51 -0500620 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400621 recow = 0;
Li Zefan33345d012011-04-20 10:31:50 +0800622 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Chris Masondc7fdde2012-04-27 14:31:29 -0400623 search_start, modify_tree);
Chris Mason39279cc2007-06-12 06:35:45 -0400624 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000625 break;
626 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
627 leaf = path->nodes[0];
628 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +0800629 if (key.objectid == ino &&
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000630 key.type == BTRFS_EXTENT_DATA_KEY)
631 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400632 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400633 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000634next_slot:
635 leaf = path->nodes[0];
636 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
637 BUG_ON(del_nr > 0);
638 ret = btrfs_next_leaf(root, path);
639 if (ret < 0)
640 break;
641 if (ret > 0) {
642 ret = 0;
643 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400644 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000645 leaf = path->nodes[0];
646 recow = 1;
647 }
648
649 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +0800650 if (key.objectid > ino ||
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000651 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
652 break;
653
654 fi = btrfs_item_ptr(leaf, path->slots[0],
655 struct btrfs_file_extent_item);
656 extent_type = btrfs_file_extent_type(leaf, fi);
657
658 if (extent_type == BTRFS_FILE_EXTENT_REG ||
659 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
660 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
661 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
662 extent_offset = btrfs_file_extent_offset(leaf, fi);
663 extent_end = key.offset +
664 btrfs_file_extent_num_bytes(leaf, fi);
665 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
666 extent_end = key.offset +
667 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400668 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000669 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400670 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400671 }
672
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000673 if (extent_end <= search_start) {
674 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400675 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400676 }
677
Josef Bacikc3308f82012-09-14 14:51:22 -0400678 found = 1;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000679 search_start = max(key.offset, start);
Chris Masondc7fdde2012-04-27 14:31:29 -0400680 if (recow || !modify_tree) {
681 modify_tree = -1;
David Sterbab3b4aa72011-04-21 01:20:15 +0200682 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000683 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400684 }
Chris Mason771ed682008-11-06 22:02:51 -0500685
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000686 /*
687 * | - range to drop - |
688 * | -------- extent -------- |
689 */
690 if (start > key.offset && end < extent_end) {
691 BUG_ON(del_nr > 0);
692 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
693
694 memcpy(&new_key, &key, sizeof(new_key));
695 new_key.offset = start;
696 ret = btrfs_duplicate_item(trans, root, path,
697 &new_key);
698 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200699 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000700 continue;
701 }
702 if (ret < 0)
703 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400704
Chris Mason5f39d392007-10-15 16:14:19 -0400705 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000706 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
707 struct btrfs_file_extent_item);
708 btrfs_set_file_extent_num_bytes(leaf, fi,
709 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400710
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000711 fi = btrfs_item_ptr(leaf, path->slots[0],
712 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400713
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000714 extent_offset += start - key.offset;
715 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
716 btrfs_set_file_extent_num_bytes(leaf, fi,
717 extent_end - start);
718 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400719
Josef Bacik5dc562c2012-08-17 13:14:17 -0400720 if (update_refs && disk_bytenr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000721 ret = btrfs_inc_extent_ref(trans, root,
722 disk_bytenr, num_bytes, 0,
723 root->root_key.objectid,
724 new_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200725 start - extent_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100726 BUG_ON(ret); /* -ENOMEM */
Zheng Yan31840ae2008-09-23 13:14:14 -0400727 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000728 key.offset = start;
729 }
730 /*
731 * | ---- range to drop ----- |
732 * | -------- extent -------- |
733 */
734 if (start <= key.offset && end < extent_end) {
735 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
736
737 memcpy(&new_key, &key, sizeof(new_key));
738 new_key.offset = end;
739 btrfs_set_item_key_safe(trans, root, path, &new_key);
740
741 extent_offset += end - key.offset;
742 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
743 btrfs_set_file_extent_num_bytes(leaf, fi,
744 extent_end - end);
745 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400746 if (update_refs && disk_bytenr > 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000747 inode_sub_bytes(inode, end - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000748 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400749 }
750
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000751 search_start = extent_end;
752 /*
753 * | ---- range to drop ----- |
754 * | -------- extent -------- |
755 */
756 if (start > key.offset && end >= extent_end) {
757 BUG_ON(del_nr > 0);
758 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
759
760 btrfs_set_file_extent_num_bytes(leaf, fi,
761 start - key.offset);
762 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400763 if (update_refs && disk_bytenr > 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000764 inode_sub_bytes(inode, extent_end - start);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000765 if (end == extent_end)
766 break;
767
768 path->slots[0]++;
769 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400770 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000771
772 /*
773 * | ---- range to drop ----- |
774 * | ------ extent ------ |
775 */
776 if (start <= key.offset && end >= extent_end) {
777 if (del_nr == 0) {
778 del_slot = path->slots[0];
779 del_nr = 1;
780 } else {
781 BUG_ON(del_slot + del_nr != path->slots[0]);
782 del_nr++;
783 }
784
Josef Bacik5dc562c2012-08-17 13:14:17 -0400785 if (update_refs &&
786 extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000787 inode_sub_bytes(inode,
788 extent_end - key.offset);
789 extent_end = ALIGN(extent_end,
790 root->sectorsize);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400791 } else if (update_refs && disk_bytenr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000792 ret = btrfs_free_extent(trans, root,
793 disk_bytenr, num_bytes, 0,
794 root->root_key.objectid,
795 key.objectid, key.offset -
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200796 extent_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100797 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000798 inode_sub_bytes(inode,
799 extent_end - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000800 }
801
802 if (end == extent_end)
803 break;
804
805 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
806 path->slots[0]++;
807 goto next_slot;
808 }
809
810 ret = btrfs_del_items(trans, root, path, del_slot,
811 del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100812 if (ret) {
813 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400814 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100815 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000816
817 del_nr = 0;
818 del_slot = 0;
819
David Sterbab3b4aa72011-04-21 01:20:15 +0200820 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000821 continue;
822 }
823
824 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400825 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000826
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100827 if (!ret && del_nr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000828 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100829 if (ret)
830 btrfs_abort_transaction(trans, root, ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000831 }
832
Josef Bacik2aaa6652012-08-29 14:27:18 -0400833 if (drop_end)
Josef Bacikc3308f82012-09-14 14:51:22 -0400834 *drop_end = found ? min(end, extent_end) : end;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400835 btrfs_release_path(path);
836 return ret;
837}
838
839int btrfs_drop_extents(struct btrfs_trans_handle *trans,
840 struct btrfs_root *root, struct inode *inode, u64 start,
Josef Bacik26714852012-08-29 12:24:27 -0400841 u64 end, int drop_cache)
Josef Bacik5dc562c2012-08-17 13:14:17 -0400842{
843 struct btrfs_path *path;
844 int ret;
845
846 path = btrfs_alloc_path();
847 if (!path)
848 return -ENOMEM;
Josef Bacik2aaa6652012-08-29 14:27:18 -0400849 ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL,
Josef Bacik26714852012-08-29 12:24:27 -0400850 drop_cache);
Chris Mason39279cc2007-06-12 06:35:45 -0400851 btrfs_free_path(path);
852 return ret;
853}
854
Yan Zhengd899e052008-10-30 14:25:28 -0400855static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000856 u64 objectid, u64 bytenr, u64 orig_offset,
857 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400858{
859 struct btrfs_file_extent_item *fi;
860 struct btrfs_key key;
861 u64 extent_end;
862
863 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
864 return 0;
865
866 btrfs_item_key_to_cpu(leaf, &key, slot);
867 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
868 return 0;
869
870 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
871 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
872 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000873 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400874 btrfs_file_extent_compression(leaf, fi) ||
875 btrfs_file_extent_encryption(leaf, fi) ||
876 btrfs_file_extent_other_encoding(leaf, fi))
877 return 0;
878
879 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
880 if ((*start && *start != key.offset) || (*end && *end != extent_end))
881 return 0;
882
883 *start = key.offset;
884 *end = extent_end;
885 return 1;
886}
887
888/*
889 * Mark extent in the range start - end as written.
890 *
891 * This changes extent type from 'pre-allocated' to 'regular'. If only
892 * part of extent is marked as written, the extent will be split into
893 * two or three.
894 */
895int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400896 struct inode *inode, u64 start, u64 end)
897{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000898 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400899 struct extent_buffer *leaf;
900 struct btrfs_path *path;
901 struct btrfs_file_extent_item *fi;
902 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000903 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400904 u64 bytenr;
905 u64 num_bytes;
906 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400907 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400908 u64 other_start;
909 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000910 u64 split;
911 int del_nr = 0;
912 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000913 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400914 int ret;
Li Zefan33345d012011-04-20 10:31:50 +0800915 u64 ino = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -0400916
Yan Zhengd899e052008-10-30 14:25:28 -0400917 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700918 if (!path)
919 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -0400920again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000921 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000922 split = start;
Li Zefan33345d012011-04-20 10:31:50 +0800923 key.objectid = ino;
Yan Zhengd899e052008-10-30 14:25:28 -0400924 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000925 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -0400926
927 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Josef Bacik41415732011-03-16 13:59:32 -0400928 if (ret < 0)
929 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -0400930 if (ret > 0 && path->slots[0] > 0)
931 path->slots[0]--;
932
933 leaf = path->nodes[0];
934 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +0800935 BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
Yan Zhengd899e052008-10-30 14:25:28 -0400936 fi = btrfs_item_ptr(leaf, path->slots[0],
937 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000938 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
939 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -0400940 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
941 BUG_ON(key.offset > start || extent_end < end);
942
943 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
944 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400945 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000946 memcpy(&new_key, &key, sizeof(new_key));
947
948 if (start == key.offset && end < extent_end) {
949 other_start = 0;
950 other_end = start;
951 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +0800952 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000953 &other_start, &other_end)) {
954 new_key.offset = end;
955 btrfs_set_item_key_safe(trans, root, path, &new_key);
956 fi = btrfs_item_ptr(leaf, path->slots[0],
957 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -0400958 btrfs_set_file_extent_generation(leaf, fi,
959 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000960 btrfs_set_file_extent_num_bytes(leaf, fi,
961 extent_end - end);
962 btrfs_set_file_extent_offset(leaf, fi,
963 end - orig_offset);
964 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
965 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -0400966 btrfs_set_file_extent_generation(leaf, fi,
967 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000968 btrfs_set_file_extent_num_bytes(leaf, fi,
969 end - other_start);
970 btrfs_mark_buffer_dirty(leaf);
971 goto out;
972 }
973 }
974
975 if (start > key.offset && end == extent_end) {
976 other_start = end;
977 other_end = 0;
978 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +0800979 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000980 &other_start, &other_end)) {
981 fi = btrfs_item_ptr(leaf, path->slots[0],
982 struct btrfs_file_extent_item);
983 btrfs_set_file_extent_num_bytes(leaf, fi,
984 start - key.offset);
Josef Bacik224ecce2012-08-16 16:32:06 -0400985 btrfs_set_file_extent_generation(leaf, fi,
986 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000987 path->slots[0]++;
988 new_key.offset = start;
989 btrfs_set_item_key_safe(trans, root, path, &new_key);
990
991 fi = btrfs_item_ptr(leaf, path->slots[0],
992 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -0400993 btrfs_set_file_extent_generation(leaf, fi,
994 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000995 btrfs_set_file_extent_num_bytes(leaf, fi,
996 other_end - start);
997 btrfs_set_file_extent_offset(leaf, fi,
998 start - orig_offset);
999 btrfs_mark_buffer_dirty(leaf);
1000 goto out;
1001 }
1002 }
Yan Zhengd899e052008-10-30 14:25:28 -04001003
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001004 while (start > key.offset || end < extent_end) {
1005 if (key.offset == start)
1006 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001007
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001008 new_key.offset = split;
1009 ret = btrfs_duplicate_item(trans, root, path, &new_key);
1010 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001011 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001012 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -04001013 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001014 if (ret < 0) {
1015 btrfs_abort_transaction(trans, root, ret);
1016 goto out;
1017 }
Yan Zhengd899e052008-10-30 14:25:28 -04001018
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001019 leaf = path->nodes[0];
1020 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -04001021 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001022 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan Zhengd899e052008-10-30 14:25:28 -04001023 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001024 split - key.offset);
1025
1026 fi = btrfs_item_ptr(leaf, path->slots[0],
1027 struct btrfs_file_extent_item);
1028
Josef Bacik224ecce2012-08-16 16:32:06 -04001029 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001030 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
1031 btrfs_set_file_extent_num_bytes(leaf, fi,
1032 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -04001033 btrfs_mark_buffer_dirty(leaf);
1034
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001035 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
1036 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001037 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001038 BUG_ON(ret); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001039
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001040 if (split == start) {
1041 key.offset = start;
1042 } else {
1043 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -04001044 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001045 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001046 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001047 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -04001048 }
1049
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001050 other_start = end;
1051 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001052 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +08001053 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001054 &other_start, &other_end)) {
1055 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001056 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001057 goto again;
1058 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001059 extent_end = other_end;
1060 del_slot = path->slots[0] + 1;
1061 del_nr++;
1062 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1063 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001064 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001065 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001066 }
1067 other_start = 0;
1068 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001069 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +08001070 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001071 &other_start, &other_end)) {
1072 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001073 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001074 goto again;
1075 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001076 key.offset = other_start;
1077 del_slot = path->slots[0];
1078 del_nr++;
1079 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1080 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001081 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001082 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001083 }
1084 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001085 fi = btrfs_item_ptr(leaf, path->slots[0],
1086 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001087 btrfs_set_file_extent_type(leaf, fi,
1088 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001089 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001090 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001091 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001092 fi = btrfs_item_ptr(leaf, del_slot - 1,
1093 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001094 btrfs_set_file_extent_type(leaf, fi,
1095 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001096 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001097 btrfs_set_file_extent_num_bytes(leaf, fi,
1098 extent_end - key.offset);
1099 btrfs_mark_buffer_dirty(leaf);
1100
1101 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001102 if (ret < 0) {
1103 btrfs_abort_transaction(trans, root, ret);
1104 goto out;
1105 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001106 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001107out:
Yan Zhengd899e052008-10-30 14:25:28 -04001108 btrfs_free_path(path);
1109 return 0;
1110}
1111
Chris Mason39279cc2007-06-12 06:35:45 -04001112/*
Chris Masonb1bf8622011-02-28 09:52:08 -05001113 * on error we return an unlocked page and the error value
1114 * on success we return a locked page and 0
1115 */
Josef Bacikb63164292011-09-30 15:23:54 -04001116static int prepare_uptodate_page(struct page *page, u64 pos,
1117 bool force_uptodate)
Chris Masonb1bf8622011-02-28 09:52:08 -05001118{
1119 int ret = 0;
1120
Josef Bacikb63164292011-09-30 15:23:54 -04001121 if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
1122 !PageUptodate(page)) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001123 ret = btrfs_readpage(NULL, page);
1124 if (ret)
1125 return ret;
1126 lock_page(page);
1127 if (!PageUptodate(page)) {
1128 unlock_page(page);
1129 return -EIO;
1130 }
1131 }
1132 return 0;
1133}
1134
1135/*
Chris Masond352ac62008-09-29 15:18:18 -04001136 * this gets pages into the page cache and locks them down, it also properly
1137 * waits for data=ordered extents to finish before allowing the pages to be
1138 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -04001139 */
Chris Masond3977122009-01-05 21:25:51 -05001140static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -05001141 struct page **pages, size_t num_pages,
1142 loff_t pos, unsigned long first_index,
Josef Bacikb63164292011-09-30 15:23:54 -04001143 size_t write_bytes, bool force_uptodate)
Chris Mason39279cc2007-06-12 06:35:45 -04001144{
Josef Bacik2ac55d42010-02-03 19:33:23 +00001145 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001146 int i;
1147 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -05001148 struct inode *inode = fdentry(file)->d_inode;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001149 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason39279cc2007-06-12 06:35:45 -04001150 int err = 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001151 int faili = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -04001152 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001153 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -04001154
Chris Mason5f39d392007-10-15 16:14:19 -04001155 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001156 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001157
Chris Masone6dcd2d2008-07-17 12:53:50 -04001158again:
Chris Mason39279cc2007-06-12 06:35:45 -04001159 for (i = 0; i < num_pages; i++) {
Josef Bacika94733d2011-07-11 10:47:06 -04001160 pages[i] = find_or_create_page(inode->i_mapping, index + i,
Johannes Weinere3a41a52012-01-10 15:07:55 -08001161 mask | __GFP_WRITE);
Chris Mason39279cc2007-06-12 06:35:45 -04001162 if (!pages[i]) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001163 faili = i - 1;
1164 err = -ENOMEM;
1165 goto fail;
1166 }
1167
1168 if (i == 0)
Josef Bacikb63164292011-09-30 15:23:54 -04001169 err = prepare_uptodate_page(pages[i], pos,
1170 force_uptodate);
Chris Masonb1bf8622011-02-28 09:52:08 -05001171 if (i == num_pages - 1)
1172 err = prepare_uptodate_page(pages[i],
Josef Bacikb63164292011-09-30 15:23:54 -04001173 pos + write_bytes, false);
Chris Masonb1bf8622011-02-28 09:52:08 -05001174 if (err) {
1175 page_cache_release(pages[i]);
1176 faili = i - 1;
1177 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001178 }
Chris Masonccd467d2007-06-28 15:57:36 -04001179 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -04001180 }
Chris Masonb1bf8622011-02-28 09:52:08 -05001181 err = 0;
Chris Mason07627042008-02-19 11:29:24 -05001182 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04001183 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001184 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001185 start_pos, last_pos - 1, 0, &cached_state);
Chris Masond3977122009-01-05 21:25:51 -05001186 ordered = btrfs_lookup_first_ordered_extent(inode,
1187 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001188 if (ordered &&
1189 ordered->file_offset + ordered->len > start_pos &&
1190 ordered->file_offset < last_pos) {
1191 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00001192 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1193 start_pos, last_pos - 1,
1194 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001195 for (i = 0; i < num_pages; i++) {
1196 unlock_page(pages[i]);
1197 page_cache_release(pages[i]);
1198 }
1199 btrfs_wait_ordered_range(inode, start_pos,
1200 last_pos - start_pos);
1201 goto again;
1202 }
1203 if (ordered)
1204 btrfs_put_ordered_extent(ordered);
1205
Josef Bacik2ac55d42010-02-03 19:33:23 +00001206 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -04001207 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Liu Bo9e8a4a82012-09-05 19:10:51 -06001208 EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
1209 0, 0, &cached_state, GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +00001210 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1211 start_pos, last_pos - 1, &cached_state,
1212 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -05001213 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001214 for (i = 0; i < num_pages; i++) {
Wu Fengguang32c7f202011-08-08 15:19:47 -06001215 if (clear_page_dirty_for_io(pages[i]))
1216 account_page_redirty(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001217 set_page_extent_mapped(pages[i]);
1218 WARN_ON(!PageLocked(pages[i]));
1219 }
Chris Mason39279cc2007-06-12 06:35:45 -04001220 return 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001221fail:
1222 while (faili >= 0) {
1223 unlock_page(pages[faili]);
1224 page_cache_release(pages[faili]);
1225 faili--;
1226 }
1227 return err;
1228
Chris Mason39279cc2007-06-12 06:35:45 -04001229}
1230
Josef Bacikd0215f32011-01-25 14:57:24 -05001231static noinline ssize_t __btrfs_buffered_write(struct file *file,
1232 struct iov_iter *i,
1233 loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -04001234{
Josef Bacik11c65dc2010-05-23 11:07:21 -04001235 struct inode *inode = fdentry(file)->d_inode;
1236 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001237 struct page **pages = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001238 unsigned long first_index;
Josef Bacikd0215f32011-01-25 14:57:24 -05001239 size_t num_written = 0;
1240 int nrptrs;
Tsutomu Itohc9149232011-03-30 00:57:23 +00001241 int ret = 0;
Josef Bacikb63164292011-09-30 15:23:54 -04001242 bool force_page_uptodate = false;
Chris Masoncb843a62008-10-03 12:30:02 -04001243
Josef Bacikd0215f32011-01-25 14:57:24 -05001244 nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
Josef Bacik11c65dc2010-05-23 11:07:21 -04001245 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
1246 (sizeof(struct page *)));
Wu Fengguang142349f2011-12-16 12:32:57 -05001247 nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
1248 nrptrs = max(nrptrs, 8);
Chris Mason8c2383c2007-06-18 09:57:58 -04001249 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Josef Bacikd0215f32011-01-25 14:57:24 -05001250 if (!pages)
1251 return -ENOMEM;
Chris Masonab93dbe2009-10-01 12:29:10 -04001252
Chris Mason39279cc2007-06-12 06:35:45 -04001253 first_index = pos >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001254
Josef Bacikd0215f32011-01-25 14:57:24 -05001255 while (iov_iter_count(i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -04001256 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacikd0215f32011-01-25 14:57:24 -05001257 size_t write_bytes = min(iov_iter_count(i),
Josef Bacik11c65dc2010-05-23 11:07:21 -04001258 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -04001259 offset);
Yan, Zheng3a909832011-01-18 13:34:40 +08001260 size_t num_pages = (write_bytes + offset +
1261 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Josef Bacikd0215f32011-01-25 14:57:24 -05001262 size_t dirty_pages;
1263 size_t copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001264
Chris Mason8c2383c2007-06-18 09:57:58 -04001265 WARN_ON(num_pages > nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -05001266
Xin Zhong914ee292010-12-09 09:30:14 +00001267 /*
1268 * Fault pages before locking them in prepare_pages
1269 * to avoid recursive lock
1270 */
Josef Bacikd0215f32011-01-25 14:57:24 -05001271 if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
Xin Zhong914ee292010-12-09 09:30:14 +00001272 ret = -EFAULT;
Josef Bacikd0215f32011-01-25 14:57:24 -05001273 break;
Xin Zhong914ee292010-12-09 09:30:14 +00001274 }
1275
1276 ret = btrfs_delalloc_reserve_space(inode,
1277 num_pages << PAGE_CACHE_SHIFT);
Chris Mason1832a6d2007-12-21 16:27:21 -05001278 if (ret)
Josef Bacikd0215f32011-01-25 14:57:24 -05001279 break;
Chris Mason1832a6d2007-12-21 16:27:21 -05001280
Josef Bacik4a640012011-01-25 15:10:08 -05001281 /*
1282 * This is going to setup the pages array with the number of
1283 * pages we want, so we don't really need to worry about the
1284 * contents of pages from loop to loop
1285 */
Chris Mason39279cc2007-06-12 06:35:45 -04001286 ret = prepare_pages(root, file, pages, num_pages,
Josef Bacikb63164292011-09-30 15:23:54 -04001287 pos, first_index, write_bytes,
1288 force_page_uptodate);
Josef Bacik6a632092009-02-20 11:00:09 -05001289 if (ret) {
Xin Zhong914ee292010-12-09 09:30:14 +00001290 btrfs_delalloc_release_space(inode,
1291 num_pages << PAGE_CACHE_SHIFT);
Josef Bacikd0215f32011-01-25 14:57:24 -05001292 break;
Josef Bacik6a632092009-02-20 11:00:09 -05001293 }
Chris Mason39279cc2007-06-12 06:35:45 -04001294
Xin Zhong914ee292010-12-09 09:30:14 +00001295 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacikd0215f32011-01-25 14:57:24 -05001296 write_bytes, pages, i);
Chris Masonb1bf8622011-02-28 09:52:08 -05001297
1298 /*
1299 * if we have trouble faulting in the pages, fall
1300 * back to one page at a time
1301 */
1302 if (copied < write_bytes)
1303 nrptrs = 1;
1304
Josef Bacikb63164292011-09-30 15:23:54 -04001305 if (copied == 0) {
1306 force_page_uptodate = true;
Chris Masonb1bf8622011-02-28 09:52:08 -05001307 dirty_pages = 0;
Josef Bacikb63164292011-09-30 15:23:54 -04001308 } else {
1309 force_page_uptodate = false;
Chris Masonb1bf8622011-02-28 09:52:08 -05001310 dirty_pages = (copied + offset +
1311 PAGE_CACHE_SIZE - 1) >>
1312 PAGE_CACHE_SHIFT;
Josef Bacikb63164292011-09-30 15:23:54 -04001313 }
Xin Zhong914ee292010-12-09 09:30:14 +00001314
Josef Bacikd0215f32011-01-25 14:57:24 -05001315 /*
1316 * If we had a short copy we need to release the excess delaloc
1317 * bytes we reserved. We need to increment outstanding_extents
1318 * because btrfs_delalloc_release_space will decrement it, but
1319 * we still have an outstanding extent for the chunk we actually
1320 * managed to copy.
1321 */
Xin Zhong914ee292010-12-09 09:30:14 +00001322 if (num_pages > dirty_pages) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001323 if (copied > 0) {
1324 spin_lock(&BTRFS_I(inode)->lock);
1325 BTRFS_I(inode)->outstanding_extents++;
1326 spin_unlock(&BTRFS_I(inode)->lock);
1327 }
Xin Zhong914ee292010-12-09 09:30:14 +00001328 btrfs_delalloc_release_space(inode,
1329 (num_pages - dirty_pages) <<
1330 PAGE_CACHE_SHIFT);
1331 }
1332
1333 if (copied > 0) {
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001334 ret = btrfs_dirty_pages(root, inode, pages,
1335 dirty_pages, pos, copied,
1336 NULL);
Josef Bacikd0215f32011-01-25 14:57:24 -05001337 if (ret) {
1338 btrfs_delalloc_release_space(inode,
1339 dirty_pages << PAGE_CACHE_SHIFT);
1340 btrfs_drop_pages(pages, num_pages);
1341 break;
1342 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001343 }
Chris Mason39279cc2007-06-12 06:35:45 -04001344
Chris Mason39279cc2007-06-12 06:35:45 -04001345 btrfs_drop_pages(pages, num_pages);
Xin Zhong914ee292010-12-09 09:30:14 +00001346
Josef Bacikd0215f32011-01-25 14:57:24 -05001347 cond_resched();
1348
1349 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
1350 dirty_pages);
1351 if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
Liu Bob53d3f52012-11-14 14:34:34 +00001352 btrfs_btree_balance_dirty(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001353
Xin Zhong914ee292010-12-09 09:30:14 +00001354 pos += copied;
1355 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001356 }
Chris Mason5b92ee72008-01-03 13:46:11 -05001357
Chris Mason8c2383c2007-06-18 09:57:58 -04001358 kfree(pages);
Josef Bacikd0215f32011-01-25 14:57:24 -05001359
1360 return num_written ? num_written : ret;
1361}
1362
1363static ssize_t __btrfs_direct_write(struct kiocb *iocb,
1364 const struct iovec *iov,
1365 unsigned long nr_segs, loff_t pos,
1366 loff_t *ppos, size_t count, size_t ocount)
1367{
1368 struct file *file = iocb->ki_filp;
Josef Bacikd0215f32011-01-25 14:57:24 -05001369 struct iov_iter i;
1370 ssize_t written;
1371 ssize_t written_buffered;
1372 loff_t endbyte;
1373 int err;
1374
1375 written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
1376 count, ocount);
1377
Josef Bacikd0215f32011-01-25 14:57:24 -05001378 if (written < 0 || written == count)
1379 return written;
1380
1381 pos += written;
1382 count -= written;
1383 iov_iter_init(&i, iov, nr_segs, count, written);
1384 written_buffered = __btrfs_buffered_write(file, &i, pos);
1385 if (written_buffered < 0) {
1386 err = written_buffered;
1387 goto out;
1388 }
1389 endbyte = pos + written_buffered - 1;
1390 err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
1391 if (err)
1392 goto out;
1393 written += written_buffered;
1394 *ppos = pos + written_buffered;
1395 invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
1396 endbyte >> PAGE_CACHE_SHIFT);
1397out:
1398 return written ? written : err;
1399}
1400
1401static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
1402 const struct iovec *iov,
1403 unsigned long nr_segs, loff_t pos)
1404{
1405 struct file *file = iocb->ki_filp;
1406 struct inode *inode = fdentry(file)->d_inode;
1407 struct btrfs_root *root = BTRFS_I(inode)->root;
1408 loff_t *ppos = &iocb->ki_pos;
Miao Xie0c1a98c2011-09-11 10:52:24 -04001409 u64 start_pos;
Josef Bacikd0215f32011-01-25 14:57:24 -05001410 ssize_t num_written = 0;
1411 ssize_t err = 0;
1412 size_t count, ocount;
1413
Jan Karab2b5ef52012-06-12 16:20:45 +02001414 sb_start_write(inode->i_sb);
Josef Bacikd0215f32011-01-25 14:57:24 -05001415
1416 mutex_lock(&inode->i_mutex);
1417
1418 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
1419 if (err) {
1420 mutex_unlock(&inode->i_mutex);
1421 goto out;
1422 }
1423 count = ocount;
1424
1425 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1426 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1427 if (err) {
1428 mutex_unlock(&inode->i_mutex);
1429 goto out;
1430 }
1431
1432 if (count == 0) {
1433 mutex_unlock(&inode->i_mutex);
1434 goto out;
1435 }
1436
1437 err = file_remove_suid(file);
1438 if (err) {
1439 mutex_unlock(&inode->i_mutex);
1440 goto out;
1441 }
1442
1443 /*
1444 * If BTRFS flips readonly due to some impossible error
1445 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
1446 * although we have opened a file as writable, we have
1447 * to stop this write operation to ensure FS consistency.
1448 */
1449 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
1450 mutex_unlock(&inode->i_mutex);
1451 err = -EROFS;
1452 goto out;
1453 }
1454
Josef Bacike41f9412012-03-26 09:46:47 -04001455 err = file_update_time(file);
Josef Bacik22c44fe2011-11-30 10:45:38 -05001456 if (err) {
1457 mutex_unlock(&inode->i_mutex);
1458 goto out;
1459 }
Josef Bacikd0215f32011-01-25 14:57:24 -05001460
Miao Xie0c1a98c2011-09-11 10:52:24 -04001461 start_pos = round_down(pos, root->sectorsize);
1462 if (start_pos > i_size_read(inode)) {
1463 err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
1464 if (err) {
1465 mutex_unlock(&inode->i_mutex);
1466 goto out;
1467 }
1468 }
1469
Josef Bacikd0215f32011-01-25 14:57:24 -05001470 if (unlikely(file->f_flags & O_DIRECT)) {
1471 num_written = __btrfs_direct_write(iocb, iov, nr_segs,
1472 pos, ppos, count, ocount);
1473 } else {
1474 struct iov_iter i;
1475
1476 iov_iter_init(&i, iov, nr_segs, count, num_written);
1477
1478 num_written = __btrfs_buffered_write(file, &i, pos);
1479 if (num_written > 0)
1480 *ppos = pos + num_written;
1481 }
1482
1483 mutex_unlock(&inode->i_mutex);
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001484
Chris Mason5a3f23d2009-03-31 13:27:11 -04001485 /*
1486 * we want to make sure fsync finds this change
1487 * but we haven't joined a transaction running right now.
1488 *
1489 * Later on, someone is sure to update the inode and get the
1490 * real transid recorded.
1491 *
1492 * We set last_trans now to the fs_info generation + 1,
1493 * this will either be one more than the running transaction
1494 * or the generation used for the next transaction if there isn't
1495 * one running right now.
1496 */
1497 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
Josef Bacikd0215f32011-01-25 14:57:24 -05001498 if (num_written > 0 || num_written == -EIOCBQUEUED) {
1499 err = generic_write_sync(file, pos, num_written);
1500 if (err < 0 && num_written > 0)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001501 num_written = err;
1502 }
Josef Bacikd0215f32011-01-25 14:57:24 -05001503out:
Jan Karab2b5ef52012-06-12 16:20:45 +02001504 sb_end_write(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04001505 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001506 return num_written ? num_written : err;
1507}
1508
Chris Masond3977122009-01-05 21:25:51 -05001509int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001510{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001511 /*
1512 * ordered_data_close is set by settattr when we are about to truncate
1513 * a file from a non-zero size to a zero size. This tries to
1514 * flush down new bytes that may have been written if the
1515 * application were using truncate to replace a file in place.
1516 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04001517 if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
1518 &BTRFS_I(inode)->runtime_flags)) {
Chris Mason5a3f23d2009-03-31 13:27:11 -04001519 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1520 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1521 filemap_flush(inode->i_mapping);
1522 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001523 if (filp->private_data)
1524 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001525 return 0;
1526}
1527
Chris Masond352ac62008-09-29 15:18:18 -04001528/*
1529 * fsync call for both files and directories. This logs the inode into
1530 * the tree log instead of forcing full commits whenever possible.
1531 *
1532 * It needs to call filemap_fdatawait so that all ordered extent updates are
1533 * in the metadata btree are up to date for copying to the log.
1534 *
1535 * It drops the inode mutex before doing the tree log commit. This is an
1536 * important optimization for directories because holding the mutex prevents
1537 * new operations on the dir while we write to disk.
1538 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001539int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001540{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001541 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001542 struct inode *inode = dentry->d_inode;
1543 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001544 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001545 struct btrfs_trans_handle *trans;
1546
liubo1abe9b82011-03-24 11:18:59 +00001547 trace_btrfs_sync_file(file, datasync);
Chris Mason257c62e2009-10-13 13:21:08 -04001548
Miao Xie90abccf2012-09-13 04:53:47 -06001549 /*
1550 * We write the dirty pages in the range and wait until they complete
1551 * out of the ->i_mutex. If so, we can flush the dirty pages by
1552 * multi-task, and make the performance up.
1553 */
1554 ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
1555 if (ret)
1556 return ret;
1557
Josef Bacik02c24a82011-07-16 20:44:56 -04001558 mutex_lock(&inode->i_mutex);
1559
Josef Bacik0885ef52012-04-23 15:09:39 -04001560 /*
Miao Xie90abccf2012-09-13 04:53:47 -06001561 * We flush the dirty pages again to avoid some dirty pages in the
1562 * range being left.
Josef Bacik0885ef52012-04-23 15:09:39 -04001563 */
Miao Xie2ecb7922012-09-06 04:04:27 -06001564 atomic_inc(&root->log_batch);
Liu Bo9f3959c2012-11-01 06:38:48 +00001565 btrfs_wait_ordered_range(inode, start, end - start + 1);
Miao Xie2ecb7922012-09-06 04:04:27 -06001566 atomic_inc(&root->log_batch);
Chris Mason257c62e2009-10-13 13:21:08 -04001567
Chris Mason39279cc2007-06-12 06:35:45 -04001568 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001569 * check the transaction that last modified this inode
1570 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001571 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001572 if (!BTRFS_I(inode)->last_trans) {
1573 mutex_unlock(&inode->i_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001574 goto out;
Josef Bacik02c24a82011-07-16 20:44:56 -04001575 }
Chris Masona2135012008-06-25 16:01:30 -04001576
Chris Mason257c62e2009-10-13 13:21:08 -04001577 /*
1578 * if the last transaction that changed this file was before
1579 * the current transaction, we can bail out now without any
1580 * syncing
1581 */
Josef Bacika4abeea2011-04-11 17:25:13 -04001582 smp_mb();
Josef Bacik22ee6985d2012-05-29 16:57:49 -04001583 if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
1584 BTRFS_I(inode)->last_trans <=
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001585 root->fs_info->last_trans_committed) {
1586 BTRFS_I(inode)->last_trans = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04001587
1588 /*
1589 * We'v had everything committed since the last time we were
1590 * modified so clear this flag in case it was set for whatever
1591 * reason, it's no longer relevant.
1592 */
1593 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1594 &BTRFS_I(inode)->runtime_flags);
Josef Bacik02c24a82011-07-16 20:44:56 -04001595 mutex_unlock(&inode->i_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001596 goto out;
1597 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001598
1599 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001600 * ok we haven't committed the transaction yet, lets do a commit
1601 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001602 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001603 btrfs_ioctl_trans_end(file);
1604
Yan, Zhenga22285a2010-05-16 10:48:46 -04001605 trans = btrfs_start_transaction(root, 0);
1606 if (IS_ERR(trans)) {
1607 ret = PTR_ERR(trans);
Josef Bacik02c24a82011-07-16 20:44:56 -04001608 mutex_unlock(&inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001609 goto out;
1610 }
Chris Masone02119d2008-09-05 16:13:11 -04001611
Chris Mason2cfbd502009-02-20 10:55:10 -05001612 ret = btrfs_log_dentry_safe(trans, root, dentry);
Josef Bacik02c24a82011-07-16 20:44:56 -04001613 if (ret < 0) {
1614 mutex_unlock(&inode->i_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04001615 goto out;
Josef Bacik02c24a82011-07-16 20:44:56 -04001616 }
Chris Mason49eb7e42008-09-11 15:53:12 -04001617
1618 /* we've logged all the items and now have a consistent
1619 * version of the file in the log. It is possible that
1620 * someone will come in and modify the file, but that's
1621 * fine because the log is consistent on disk, and we
1622 * have references to all of the file's extents
1623 *
1624 * It is possible that someone will come in and log the
1625 * file again, but that will end up using the synchronization
1626 * inside btrfs_sync_log to keep things safe.
1627 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001628 mutex_unlock(&inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001629
Chris Mason257c62e2009-10-13 13:21:08 -04001630 if (ret != BTRFS_NO_LOG_SYNC) {
1631 if (ret > 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001632 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001633 } else {
1634 ret = btrfs_sync_log(trans, root);
1635 if (ret == 0)
1636 ret = btrfs_end_transaction(trans, root);
1637 else
1638 ret = btrfs_commit_transaction(trans, root);
1639 }
1640 } else {
1641 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001642 }
Chris Mason39279cc2007-06-12 06:35:45 -04001643out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001644 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001645}
1646
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001647static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001648 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001649 .page_mkwrite = btrfs_page_mkwrite,
Konstantin Khlebnikov0b173bc2012-10-08 16:28:46 -07001650 .remap_pages = generic_file_remap_pages,
Chris Mason9ebefb182007-06-15 13:50:00 -04001651};
1652
1653static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1654{
Miao Xie058a4572010-05-20 07:21:50 +00001655 struct address_space *mapping = filp->f_mapping;
1656
1657 if (!mapping->a_ops->readpage)
1658 return -ENOEXEC;
1659
Chris Mason9ebefb182007-06-15 13:50:00 -04001660 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00001661 vma->vm_ops = &btrfs_file_vm_ops;
Miao Xie058a4572010-05-20 07:21:50 +00001662
Chris Mason9ebefb182007-06-15 13:50:00 -04001663 return 0;
1664}
1665
Josef Bacik2aaa6652012-08-29 14:27:18 -04001666static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
1667 int slot, u64 start, u64 end)
1668{
1669 struct btrfs_file_extent_item *fi;
1670 struct btrfs_key key;
1671
1672 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
1673 return 0;
1674
1675 btrfs_item_key_to_cpu(leaf, &key, slot);
1676 if (key.objectid != btrfs_ino(inode) ||
1677 key.type != BTRFS_EXTENT_DATA_KEY)
1678 return 0;
1679
1680 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1681
1682 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
1683 return 0;
1684
1685 if (btrfs_file_extent_disk_bytenr(leaf, fi))
1686 return 0;
1687
1688 if (key.offset == end)
1689 return 1;
1690 if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
1691 return 1;
1692 return 0;
1693}
1694
1695static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
1696 struct btrfs_path *path, u64 offset, u64 end)
1697{
1698 struct btrfs_root *root = BTRFS_I(inode)->root;
1699 struct extent_buffer *leaf;
1700 struct btrfs_file_extent_item *fi;
1701 struct extent_map *hole_em;
1702 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1703 struct btrfs_key key;
1704 int ret;
1705
1706 key.objectid = btrfs_ino(inode);
1707 key.type = BTRFS_EXTENT_DATA_KEY;
1708 key.offset = offset;
1709
1710
1711 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1712 if (ret < 0)
1713 return ret;
1714 BUG_ON(!ret);
1715
1716 leaf = path->nodes[0];
1717 if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
1718 u64 num_bytes;
1719
1720 path->slots[0]--;
1721 fi = btrfs_item_ptr(leaf, path->slots[0],
1722 struct btrfs_file_extent_item);
1723 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
1724 end - offset;
1725 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1726 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
1727 btrfs_set_file_extent_offset(leaf, fi, 0);
1728 btrfs_mark_buffer_dirty(leaf);
1729 goto out;
1730 }
1731
1732 if (hole_mergeable(inode, leaf, path->slots[0]+1, offset, end)) {
1733 u64 num_bytes;
1734
1735 path->slots[0]++;
1736 key.offset = offset;
1737 btrfs_set_item_key_safe(trans, root, path, &key);
1738 fi = btrfs_item_ptr(leaf, path->slots[0],
1739 struct btrfs_file_extent_item);
1740 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
1741 offset;
1742 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1743 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
1744 btrfs_set_file_extent_offset(leaf, fi, 0);
1745 btrfs_mark_buffer_dirty(leaf);
1746 goto out;
1747 }
1748 btrfs_release_path(path);
1749
1750 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
1751 0, 0, end - offset, 0, end - offset,
1752 0, 0, 0);
1753 if (ret)
1754 return ret;
1755
1756out:
1757 btrfs_release_path(path);
1758
1759 hole_em = alloc_extent_map();
1760 if (!hole_em) {
1761 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
1762 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1763 &BTRFS_I(inode)->runtime_flags);
1764 } else {
1765 hole_em->start = offset;
1766 hole_em->len = end - offset;
1767 hole_em->orig_start = offset;
1768
1769 hole_em->block_start = EXTENT_MAP_HOLE;
1770 hole_em->block_len = 0;
1771 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
1772 hole_em->compress_type = BTRFS_COMPRESS_NONE;
1773 hole_em->generation = trans->transid;
1774
1775 do {
1776 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
1777 write_lock(&em_tree->lock);
1778 ret = add_extent_mapping(em_tree, hole_em);
1779 if (!ret)
1780 list_move(&hole_em->list,
1781 &em_tree->modified_extents);
1782 write_unlock(&em_tree->lock);
1783 } while (ret == -EEXIST);
1784 free_extent_map(hole_em);
1785 if (ret)
1786 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1787 &BTRFS_I(inode)->runtime_flags);
1788 }
1789
1790 return 0;
1791}
1792
1793static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
1794{
1795 struct btrfs_root *root = BTRFS_I(inode)->root;
1796 struct extent_state *cached_state = NULL;
1797 struct btrfs_path *path;
1798 struct btrfs_block_rsv *rsv;
1799 struct btrfs_trans_handle *trans;
1800 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
1801 u64 lockstart = (offset + mask) & ~mask;
1802 u64 lockend = ((offset + len) & ~mask) - 1;
1803 u64 cur_offset = lockstart;
1804 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
1805 u64 drop_end;
Josef Bacik2aaa6652012-08-29 14:27:18 -04001806 int ret = 0;
1807 int err = 0;
1808 bool same_page = (offset >> PAGE_CACHE_SHIFT) ==
1809 ((offset + len) >> PAGE_CACHE_SHIFT);
1810
1811 btrfs_wait_ordered_range(inode, offset, len);
1812
1813 mutex_lock(&inode->i_mutex);
1814 if (offset >= inode->i_size) {
1815 mutex_unlock(&inode->i_mutex);
1816 return 0;
1817 }
1818
1819 /*
1820 * Only do this if we are in the same page and we aren't doing the
1821 * entire page.
1822 */
1823 if (same_page && len < PAGE_CACHE_SIZE) {
1824 ret = btrfs_truncate_page(inode, offset, len, 0);
1825 mutex_unlock(&inode->i_mutex);
1826 return ret;
1827 }
1828
1829 /* zero back part of the first page */
1830 ret = btrfs_truncate_page(inode, offset, 0, 0);
1831 if (ret) {
1832 mutex_unlock(&inode->i_mutex);
1833 return ret;
1834 }
1835
1836 /* zero the front end of the last page */
1837 ret = btrfs_truncate_page(inode, offset + len, 0, 1);
1838 if (ret) {
1839 mutex_unlock(&inode->i_mutex);
1840 return ret;
1841 }
1842
1843 if (lockend < lockstart) {
1844 mutex_unlock(&inode->i_mutex);
1845 return 0;
1846 }
1847
1848 while (1) {
1849 struct btrfs_ordered_extent *ordered;
1850
1851 truncate_pagecache_range(inode, lockstart, lockend);
1852
1853 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
1854 0, &cached_state);
1855 ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
1856
1857 /*
1858 * We need to make sure we have no ordered extents in this range
1859 * and nobody raced in and read a page in this range, if we did
1860 * we need to try again.
1861 */
1862 if ((!ordered ||
1863 (ordered->file_offset + ordered->len < lockstart ||
1864 ordered->file_offset > lockend)) &&
1865 !test_range_bit(&BTRFS_I(inode)->io_tree, lockstart,
1866 lockend, EXTENT_UPTODATE, 0,
1867 cached_state)) {
1868 if (ordered)
1869 btrfs_put_ordered_extent(ordered);
1870 break;
1871 }
1872 if (ordered)
1873 btrfs_put_ordered_extent(ordered);
1874 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
1875 lockend, &cached_state, GFP_NOFS);
1876 btrfs_wait_ordered_range(inode, lockstart,
1877 lockend - lockstart + 1);
1878 }
1879
1880 path = btrfs_alloc_path();
1881 if (!path) {
1882 ret = -ENOMEM;
1883 goto out;
1884 }
1885
Miao Xie66d8f3d2012-09-06 04:02:28 -06001886 rsv = btrfs_alloc_block_rsv(root, BTRFS_BLOCK_RSV_TEMP);
Josef Bacik2aaa6652012-08-29 14:27:18 -04001887 if (!rsv) {
1888 ret = -ENOMEM;
1889 goto out_free;
1890 }
1891 rsv->size = btrfs_calc_trunc_metadata_size(root, 1);
1892 rsv->failfast = 1;
1893
1894 /*
1895 * 1 - update the inode
1896 * 1 - removing the extents in the range
1897 * 1 - adding the hole extent
1898 */
1899 trans = btrfs_start_transaction(root, 3);
1900 if (IS_ERR(trans)) {
1901 err = PTR_ERR(trans);
1902 goto out_free;
1903 }
1904
1905 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
1906 min_size);
1907 BUG_ON(ret);
1908 trans->block_rsv = rsv;
1909
1910 while (cur_offset < lockend) {
1911 ret = __btrfs_drop_extents(trans, root, inode, path,
1912 cur_offset, lockend + 1,
1913 &drop_end, 1);
1914 if (ret != -ENOSPC)
1915 break;
1916
1917 trans->block_rsv = &root->fs_info->trans_block_rsv;
1918
1919 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
1920 if (ret) {
1921 err = ret;
1922 break;
1923 }
1924
1925 cur_offset = drop_end;
1926
1927 ret = btrfs_update_inode(trans, root, inode);
1928 if (ret) {
1929 err = ret;
1930 break;
1931 }
1932
Josef Bacik2aaa6652012-08-29 14:27:18 -04001933 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00001934 btrfs_btree_balance_dirty(root);
Josef Bacik2aaa6652012-08-29 14:27:18 -04001935
1936 trans = btrfs_start_transaction(root, 3);
1937 if (IS_ERR(trans)) {
1938 ret = PTR_ERR(trans);
1939 trans = NULL;
1940 break;
1941 }
1942
1943 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
1944 rsv, min_size);
1945 BUG_ON(ret); /* shouldn't happen */
1946 trans->block_rsv = rsv;
1947 }
1948
1949 if (ret) {
1950 err = ret;
1951 goto out_trans;
1952 }
1953
1954 trans->block_rsv = &root->fs_info->trans_block_rsv;
1955 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
1956 if (ret) {
1957 err = ret;
1958 goto out_trans;
1959 }
1960
1961out_trans:
1962 if (!trans)
1963 goto out_free;
1964
Tsutomu Itohe1f57902012-11-08 04:47:33 +00001965 inode_inc_iversion(inode);
1966 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1967
Josef Bacik2aaa6652012-08-29 14:27:18 -04001968 trans->block_rsv = &root->fs_info->trans_block_rsv;
1969 ret = btrfs_update_inode(trans, root, inode);
Josef Bacik2aaa6652012-08-29 14:27:18 -04001970 btrfs_end_transaction(trans, root);
Liu Bob53d3f52012-11-14 14:34:34 +00001971 btrfs_btree_balance_dirty(root);
Josef Bacik2aaa6652012-08-29 14:27:18 -04001972out_free:
1973 btrfs_free_path(path);
1974 btrfs_free_block_rsv(root, rsv);
1975out:
1976 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
1977 &cached_state, GFP_NOFS);
1978 mutex_unlock(&inode->i_mutex);
1979 if (ret && !err)
1980 err = ret;
1981 return err;
1982}
1983
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001984static long btrfs_fallocate(struct file *file, int mode,
1985 loff_t offset, loff_t len)
1986{
1987 struct inode *inode = file->f_path.dentry->d_inode;
1988 struct extent_state *cached_state = NULL;
1989 u64 cur_offset;
1990 u64 last_byte;
1991 u64 alloc_start;
1992 u64 alloc_end;
1993 u64 alloc_hint = 0;
1994 u64 locked_end;
1995 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
1996 struct extent_map *em;
1997 int ret;
1998
1999 alloc_start = offset & ~mask;
2000 alloc_end = (offset + len + mask) & ~mask;
2001
Josef Bacik2aaa6652012-08-29 14:27:18 -04002002 /* Make sure we aren't being give some crap mode */
2003 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002004 return -EOPNOTSUPP;
2005
Josef Bacik2aaa6652012-08-29 14:27:18 -04002006 if (mode & FALLOC_FL_PUNCH_HOLE)
2007 return btrfs_punch_hole(inode, offset, len);
2008
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002009 /*
Chris Masond98456f2012-01-31 20:27:41 -05002010 * Make sure we have enough space before we do the
2011 * allocation.
2012 */
Miao Xie903889f42012-09-06 04:04:57 -06002013 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start + 1);
Chris Masond98456f2012-01-31 20:27:41 -05002014 if (ret)
2015 return ret;
2016
2017 /*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002018 * wait for ordered IO before we have any locks. We'll loop again
2019 * below with the locks held.
2020 */
2021 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
2022
2023 mutex_lock(&inode->i_mutex);
2024 ret = inode_newsize_ok(inode, alloc_end);
2025 if (ret)
2026 goto out;
2027
2028 if (alloc_start > inode->i_size) {
Josef Bacika41ad392011-01-31 15:30:16 -05002029 ret = btrfs_cont_expand(inode, i_size_read(inode),
2030 alloc_start);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002031 if (ret)
2032 goto out;
2033 }
2034
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002035 locked_end = alloc_end - 1;
2036 while (1) {
2037 struct btrfs_ordered_extent *ordered;
2038
2039 /* the extent lock is ordered inside the running
2040 * transaction
2041 */
2042 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002043 locked_end, 0, &cached_state);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002044 ordered = btrfs_lookup_first_ordered_extent(inode,
2045 alloc_end - 1);
2046 if (ordered &&
2047 ordered->file_offset + ordered->len > alloc_start &&
2048 ordered->file_offset < alloc_end) {
2049 btrfs_put_ordered_extent(ordered);
2050 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
2051 alloc_start, locked_end,
2052 &cached_state, GFP_NOFS);
2053 /*
2054 * we can't wait on the range with the transaction
2055 * running or with the extent lock held
2056 */
2057 btrfs_wait_ordered_range(inode, alloc_start,
2058 alloc_end - alloc_start);
2059 } else {
2060 if (ordered)
2061 btrfs_put_ordered_extent(ordered);
2062 break;
2063 }
2064 }
2065
2066 cur_offset = alloc_start;
2067 while (1) {
Josef Bacikf1e490a2011-08-18 10:36:39 -04002068 u64 actual_end;
2069
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002070 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2071 alloc_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002072 if (IS_ERR_OR_NULL(em)) {
2073 if (!em)
2074 ret = -ENOMEM;
2075 else
2076 ret = PTR_ERR(em);
2077 break;
2078 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002079 last_byte = min(extent_map_end(em), alloc_end);
Josef Bacikf1e490a2011-08-18 10:36:39 -04002080 actual_end = min_t(u64, extent_map_end(em), offset + len);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002081 last_byte = (last_byte + mask) & ~mask;
Josef Bacikf1e490a2011-08-18 10:36:39 -04002082
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002083 if (em->block_start == EXTENT_MAP_HOLE ||
2084 (cur_offset >= inode->i_size &&
2085 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
2086 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
2087 last_byte - cur_offset,
2088 1 << inode->i_blkbits,
2089 offset + len,
2090 &alloc_hint);
Josef Bacik1b9c3322011-08-17 10:19:52 -04002091
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002092 if (ret < 0) {
2093 free_extent_map(em);
2094 break;
2095 }
Josef Bacikf1e490a2011-08-18 10:36:39 -04002096 } else if (actual_end > inode->i_size &&
2097 !(mode & FALLOC_FL_KEEP_SIZE)) {
2098 /*
2099 * We didn't need to allocate any more space, but we
2100 * still extended the size of the file so we need to
2101 * update i_size.
2102 */
2103 inode->i_ctime = CURRENT_TIME;
2104 i_size_write(inode, actual_end);
2105 btrfs_ordered_update_i_size(inode, actual_end, NULL);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002106 }
2107 free_extent_map(em);
2108
2109 cur_offset = last_byte;
2110 if (cur_offset >= alloc_end) {
2111 ret = 0;
2112 break;
2113 }
2114 }
2115 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
2116 &cached_state, GFP_NOFS);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002117out:
2118 mutex_unlock(&inode->i_mutex);
Chris Masond98456f2012-01-31 20:27:41 -05002119 /* Let go of our reservation. */
Miao Xie903889f42012-09-06 04:04:57 -06002120 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start + 1);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002121 return ret;
2122}
2123
Josef Bacikb2675152011-07-18 13:21:36 -04002124static int find_desired_extent(struct inode *inode, loff_t *offset, int origin)
2125{
2126 struct btrfs_root *root = BTRFS_I(inode)->root;
2127 struct extent_map *em;
2128 struct extent_state *cached_state = NULL;
2129 u64 lockstart = *offset;
2130 u64 lockend = i_size_read(inode);
2131 u64 start = *offset;
2132 u64 orig_start = *offset;
2133 u64 len = i_size_read(inode);
2134 u64 last_end = 0;
2135 int ret = 0;
2136
2137 lockend = max_t(u64, root->sectorsize, lockend);
2138 if (lockend <= lockstart)
2139 lockend = lockstart + root->sectorsize;
2140
2141 len = lockend - lockstart + 1;
2142
2143 len = max_t(u64, len, root->sectorsize);
2144 if (inode->i_size == 0)
2145 return -ENXIO;
2146
2147 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002148 &cached_state);
Josef Bacikb2675152011-07-18 13:21:36 -04002149
2150 /*
2151 * Delalloc is such a pain. If we have a hole and we have pending
2152 * delalloc for a portion of the hole we will get back a hole that
2153 * exists for the entire range since it hasn't been actually written
2154 * yet. So to take care of this case we need to look for an extent just
2155 * before the position we want in case there is outstanding delalloc
2156 * going on here.
2157 */
2158 if (origin == SEEK_HOLE && start != 0) {
2159 if (start <= root->sectorsize)
2160 em = btrfs_get_extent_fiemap(inode, NULL, 0, 0,
2161 root->sectorsize, 0);
2162 else
2163 em = btrfs_get_extent_fiemap(inode, NULL, 0,
2164 start - root->sectorsize,
2165 root->sectorsize, 0);
2166 if (IS_ERR(em)) {
Jeff Liu6af021d2012-02-09 14:25:50 +08002167 ret = PTR_ERR(em);
Josef Bacikb2675152011-07-18 13:21:36 -04002168 goto out;
2169 }
2170 last_end = em->start + em->len;
2171 if (em->block_start == EXTENT_MAP_DELALLOC)
2172 last_end = min_t(u64, last_end, inode->i_size);
2173 free_extent_map(em);
2174 }
2175
2176 while (1) {
2177 em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
2178 if (IS_ERR(em)) {
Jeff Liu6af021d2012-02-09 14:25:50 +08002179 ret = PTR_ERR(em);
Josef Bacikb2675152011-07-18 13:21:36 -04002180 break;
2181 }
2182
2183 if (em->block_start == EXTENT_MAP_HOLE) {
2184 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2185 if (last_end <= orig_start) {
2186 free_extent_map(em);
2187 ret = -ENXIO;
2188 break;
2189 }
2190 }
2191
2192 if (origin == SEEK_HOLE) {
2193 *offset = start;
2194 free_extent_map(em);
2195 break;
2196 }
2197 } else {
2198 if (origin == SEEK_DATA) {
2199 if (em->block_start == EXTENT_MAP_DELALLOC) {
2200 if (start >= inode->i_size) {
2201 free_extent_map(em);
2202 ret = -ENXIO;
2203 break;
2204 }
2205 }
2206
2207 *offset = start;
2208 free_extent_map(em);
2209 break;
2210 }
2211 }
2212
2213 start = em->start + em->len;
2214 last_end = em->start + em->len;
2215
2216 if (em->block_start == EXTENT_MAP_DELALLOC)
2217 last_end = min_t(u64, last_end, inode->i_size);
2218
2219 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2220 free_extent_map(em);
2221 ret = -ENXIO;
2222 break;
2223 }
2224 free_extent_map(em);
2225 cond_resched();
2226 }
2227 if (!ret)
2228 *offset = min(*offset, inode->i_size);
2229out:
2230 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2231 &cached_state, GFP_NOFS);
2232 return ret;
2233}
2234
2235static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int origin)
2236{
2237 struct inode *inode = file->f_mapping->host;
2238 int ret;
2239
2240 mutex_lock(&inode->i_mutex);
2241 switch (origin) {
2242 case SEEK_END:
2243 case SEEK_CUR:
Andi Kleenef3d0fd2011-09-15 16:06:48 -07002244 offset = generic_file_llseek(file, offset, origin);
Josef Bacikb2675152011-07-18 13:21:36 -04002245 goto out;
2246 case SEEK_DATA:
2247 case SEEK_HOLE:
Jeff Liu48802c82011-09-18 10:34:02 -04002248 if (offset >= i_size_read(inode)) {
2249 mutex_unlock(&inode->i_mutex);
2250 return -ENXIO;
2251 }
2252
Josef Bacikb2675152011-07-18 13:21:36 -04002253 ret = find_desired_extent(inode, &offset, origin);
2254 if (ret) {
2255 mutex_unlock(&inode->i_mutex);
2256 return ret;
2257 }
2258 }
2259
Dan Carpenter9a4327c2011-08-18 10:16:05 -04002260 if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) {
Jeff Liu48802c82011-09-18 10:34:02 -04002261 offset = -EINVAL;
Dan Carpenter9a4327c2011-08-18 10:16:05 -04002262 goto out;
2263 }
2264 if (offset > inode->i_sb->s_maxbytes) {
Jeff Liu48802c82011-09-18 10:34:02 -04002265 offset = -EINVAL;
Dan Carpenter9a4327c2011-08-18 10:16:05 -04002266 goto out;
2267 }
Josef Bacikb2675152011-07-18 13:21:36 -04002268
2269 /* Special lock needed here? */
2270 if (offset != file->f_pos) {
2271 file->f_pos = offset;
2272 file->f_version = 0;
2273 }
2274out:
2275 mutex_unlock(&inode->i_mutex);
2276 return offset;
2277}
2278
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002279const struct file_operations btrfs_file_operations = {
Josef Bacikb2675152011-07-18 13:21:36 -04002280 .llseek = btrfs_file_llseek,
Chris Mason39279cc2007-06-12 06:35:45 -04002281 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00002282 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04002283 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05002284 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04002285 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04002286 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04002287 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04002288 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04002289 .fsync = btrfs_sync_file,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002290 .fallocate = btrfs_fallocate,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002291 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002292#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002293 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002294#endif
2295};