blob: 57026a6e9c9401454910ea946acb03ae9731e7f8 [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 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400462int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
Josef Bacik5dc562c2012-08-17 13:14:17 -0400463 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) {
Chris Mason3b951512008-04-17 11:29:12 -0400482 if (!split)
David Sterba172ddd62011-04-21 00:48:27 +0200483 split = alloc_extent_map();
Chris Mason3b951512008-04-17 11:29:12 -0400484 if (!split2)
David Sterba172ddd62011-04-21 00:48:27 +0200485 split2 = alloc_extent_map();
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100486 BUG_ON(!split || !split2); /* -ENOMEM */
Chris Mason3b951512008-04-17 11:29:12 -0400487
Chris Mason890871b2009-09-02 16:24:52 -0400488 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500489 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500490 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400491 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400492 break;
Chris Masond1310b22008-01-24 16:13:08 -0500493 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400494 flags = em->flags;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400495 gen = em->generation;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400496 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000497 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400498 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400499 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400500 break;
501 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000502 start = em->start + em->len;
503 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400504 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400505 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400506 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400507 continue;
508 }
Chris Masonc8b97812008-10-29 14:49:59 -0400509 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400510 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400511 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400512
513 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
514 em->start < start) {
515 split->start = em->start;
516 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500517 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400518 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400519
520 if (compressed)
521 split->block_len = em->block_len;
522 else
523 split->block_len = split->len;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400524 split->generation = gen;
Chris Mason3b951512008-04-17 11:29:12 -0400525 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400526 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800527 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400528 ret = add_extent_mapping(em_tree, split);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100529 BUG_ON(ret); /* Logic error */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400530 list_move(&split->list, &em_tree->modified_extents);
Chris Mason3b951512008-04-17 11:29:12 -0400531 free_extent_map(split);
532 split = split2;
533 split2 = NULL;
534 }
535 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
536 testend && em->start + em->len > start + len) {
537 u64 diff = start + len - em->start;
538
539 split->start = start + len;
540 split->len = em->start + em->len - (start + len);
541 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400542 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800543 split->compress_type = em->compress_type;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400544 split->generation = gen;
Chris Mason3b951512008-04-17 11:29:12 -0400545
Chris Masonc8b97812008-10-29 14:49:59 -0400546 if (compressed) {
547 split->block_len = em->block_len;
548 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500549 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400550 } else {
551 split->block_len = split->len;
552 split->block_start = em->block_start + diff;
Chris Mason445a6942008-11-10 11:53:33 -0500553 split->orig_start = split->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400554 }
Chris Mason3b951512008-04-17 11:29:12 -0400555
556 ret = add_extent_mapping(em_tree, split);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100557 BUG_ON(ret); /* Logic error */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400558 list_move(&split->list, &em_tree->modified_extents);
Chris Mason3b951512008-04-17 11:29:12 -0400559 free_extent_map(split);
560 split = NULL;
561 }
Chris Mason890871b2009-09-02 16:24:52 -0400562 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500563
Chris Masona52d9a82007-08-27 16:49:44 -0400564 /* once for us */
565 free_extent_map(em);
566 /* once for the tree*/
567 free_extent_map(em);
568 }
Chris Mason3b951512008-04-17 11:29:12 -0400569 if (split)
570 free_extent_map(split);
571 if (split2)
572 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400573 return 0;
574}
575
Chris Mason39279cc2007-06-12 06:35:45 -0400576/*
577 * this is very complex, but the basic idea is to drop all extents
578 * in the range start - end. hint_block is filled in with a block number
579 * that would be a good hint to the block allocator for this file.
580 *
581 * If an extent intersects the range but is not entirely inside the range
582 * it is either truncated or split. Anything entirely inside the range
583 * is deleted from the tree.
584 */
Josef Bacik5dc562c2012-08-17 13:14:17 -0400585int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
586 struct btrfs_root *root, struct inode *inode,
587 struct btrfs_path *path, u64 start, u64 end,
Josef Bacik2aaa6652012-08-29 14:27:18 -0400588 u64 *drop_end, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400589{
Chris Mason00f5c792007-11-30 10:09:33 -0500590 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000591 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500592 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000593 struct btrfs_key new_key;
Li Zefan33345d012011-04-20 10:31:50 +0800594 u64 ino = btrfs_ino(inode);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000595 u64 search_start = start;
596 u64 disk_bytenr = 0;
597 u64 num_bytes = 0;
598 u64 extent_offset = 0;
599 u64 extent_end = 0;
600 int del_nr = 0;
601 int del_slot = 0;
602 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400603 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500604 int ret;
Chris Masondc7fdde2012-04-27 14:31:29 -0400605 int modify_tree = -1;
Josef Bacik5dc562c2012-08-17 13:14:17 -0400606 int update_refs = (root->ref_cows || root == root->fs_info->tree_root);
Chris Mason39279cc2007-06-12 06:35:45 -0400607
Chris Masona1ed8352009-09-11 12:27:37 -0400608 if (drop_cache)
609 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400610
Chris Masondc7fdde2012-04-27 14:31:29 -0400611 if (start >= BTRFS_I(inode)->disk_i_size)
612 modify_tree = 0;
613
Chris Masond3977122009-01-05 21:25:51 -0500614 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400615 recow = 0;
Li Zefan33345d012011-04-20 10:31:50 +0800616 ret = btrfs_lookup_file_extent(trans, root, path, ino,
Chris Masondc7fdde2012-04-27 14:31:29 -0400617 search_start, modify_tree);
Chris Mason39279cc2007-06-12 06:35:45 -0400618 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000619 break;
620 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
621 leaf = path->nodes[0];
622 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
Li Zefan33345d012011-04-20 10:31:50 +0800623 if (key.objectid == ino &&
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000624 key.type == BTRFS_EXTENT_DATA_KEY)
625 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400626 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400627 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000628next_slot:
629 leaf = path->nodes[0];
630 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
631 BUG_ON(del_nr > 0);
632 ret = btrfs_next_leaf(root, path);
633 if (ret < 0)
634 break;
635 if (ret > 0) {
636 ret = 0;
637 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400638 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000639 leaf = path->nodes[0];
640 recow = 1;
641 }
642
643 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +0800644 if (key.objectid > ino ||
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000645 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
646 break;
647
648 fi = btrfs_item_ptr(leaf, path->slots[0],
649 struct btrfs_file_extent_item);
650 extent_type = btrfs_file_extent_type(leaf, fi);
651
652 if (extent_type == BTRFS_FILE_EXTENT_REG ||
653 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
654 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
655 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
656 extent_offset = btrfs_file_extent_offset(leaf, fi);
657 extent_end = key.offset +
658 btrfs_file_extent_num_bytes(leaf, fi);
659 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
660 extent_end = key.offset +
661 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400662 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000663 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400664 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400665 }
666
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000667 if (extent_end <= search_start) {
668 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400669 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400670 }
671
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000672 search_start = max(key.offset, start);
Chris Masondc7fdde2012-04-27 14:31:29 -0400673 if (recow || !modify_tree) {
674 modify_tree = -1;
David Sterbab3b4aa72011-04-21 01:20:15 +0200675 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000676 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400677 }
Chris Mason771ed682008-11-06 22:02:51 -0500678
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000679 /*
680 * | - range to drop - |
681 * | -------- extent -------- |
682 */
683 if (start > key.offset && end < extent_end) {
684 BUG_ON(del_nr > 0);
685 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
686
687 memcpy(&new_key, &key, sizeof(new_key));
688 new_key.offset = start;
689 ret = btrfs_duplicate_item(trans, root, path,
690 &new_key);
691 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200692 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000693 continue;
694 }
695 if (ret < 0)
696 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400697
Chris Mason5f39d392007-10-15 16:14:19 -0400698 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000699 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
700 struct btrfs_file_extent_item);
701 btrfs_set_file_extent_num_bytes(leaf, fi,
702 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400703
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000704 fi = btrfs_item_ptr(leaf, path->slots[0],
705 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400706
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000707 extent_offset += start - key.offset;
708 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
709 btrfs_set_file_extent_num_bytes(leaf, fi,
710 extent_end - start);
711 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400712
Josef Bacik5dc562c2012-08-17 13:14:17 -0400713 if (update_refs && disk_bytenr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000714 ret = btrfs_inc_extent_ref(trans, root,
715 disk_bytenr, num_bytes, 0,
716 root->root_key.objectid,
717 new_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200718 start - extent_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100719 BUG_ON(ret); /* -ENOMEM */
Zheng Yan31840ae2008-09-23 13:14:14 -0400720 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000721 key.offset = start;
722 }
723 /*
724 * | ---- range to drop ----- |
725 * | -------- extent -------- |
726 */
727 if (start <= key.offset && end < extent_end) {
728 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
729
730 memcpy(&new_key, &key, sizeof(new_key));
731 new_key.offset = end;
732 btrfs_set_item_key_safe(trans, root, path, &new_key);
733
734 extent_offset += end - key.offset;
735 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
736 btrfs_set_file_extent_num_bytes(leaf, fi,
737 extent_end - end);
738 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400739 if (update_refs && disk_bytenr > 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000740 inode_sub_bytes(inode, end - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000741 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400742 }
743
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000744 search_start = extent_end;
745 /*
746 * | ---- range to drop ----- |
747 * | -------- extent -------- |
748 */
749 if (start > key.offset && end >= extent_end) {
750 BUG_ON(del_nr > 0);
751 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
752
753 btrfs_set_file_extent_num_bytes(leaf, fi,
754 start - key.offset);
755 btrfs_mark_buffer_dirty(leaf);
Josef Bacik26714852012-08-29 12:24:27 -0400756 if (update_refs && disk_bytenr > 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000757 inode_sub_bytes(inode, extent_end - start);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000758 if (end == extent_end)
759 break;
760
761 path->slots[0]++;
762 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400763 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000764
765 /*
766 * | ---- range to drop ----- |
767 * | ------ extent ------ |
768 */
769 if (start <= key.offset && end >= extent_end) {
770 if (del_nr == 0) {
771 del_slot = path->slots[0];
772 del_nr = 1;
773 } else {
774 BUG_ON(del_slot + del_nr != path->slots[0]);
775 del_nr++;
776 }
777
Josef Bacik5dc562c2012-08-17 13:14:17 -0400778 if (update_refs &&
779 extent_type == BTRFS_FILE_EXTENT_INLINE) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000780 inode_sub_bytes(inode,
781 extent_end - key.offset);
782 extent_end = ALIGN(extent_end,
783 root->sectorsize);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400784 } else if (update_refs && disk_bytenr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000785 ret = btrfs_free_extent(trans, root,
786 disk_bytenr, num_bytes, 0,
787 root->root_key.objectid,
788 key.objectid, key.offset -
Arne Jansen66d7e7f2011-09-12 15:26:38 +0200789 extent_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100790 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000791 inode_sub_bytes(inode,
792 extent_end - key.offset);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000793 }
794
795 if (end == extent_end)
796 break;
797
798 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
799 path->slots[0]++;
800 goto next_slot;
801 }
802
803 ret = btrfs_del_items(trans, root, path, del_slot,
804 del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100805 if (ret) {
806 btrfs_abort_transaction(trans, root, ret);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400807 break;
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100808 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000809
810 del_nr = 0;
811 del_slot = 0;
812
David Sterbab3b4aa72011-04-21 01:20:15 +0200813 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000814 continue;
815 }
816
817 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400818 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000819
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100820 if (!ret && del_nr > 0) {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000821 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +0100822 if (ret)
823 btrfs_abort_transaction(trans, root, ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000824 }
825
Josef Bacik2aaa6652012-08-29 14:27:18 -0400826 if (drop_end)
827 *drop_end = min(end, extent_end);
Josef Bacik5dc562c2012-08-17 13:14:17 -0400828 btrfs_release_path(path);
829 return ret;
830}
831
832int btrfs_drop_extents(struct btrfs_trans_handle *trans,
833 struct btrfs_root *root, struct inode *inode, u64 start,
Josef Bacik26714852012-08-29 12:24:27 -0400834 u64 end, int drop_cache)
Josef Bacik5dc562c2012-08-17 13:14:17 -0400835{
836 struct btrfs_path *path;
837 int ret;
838
839 path = btrfs_alloc_path();
840 if (!path)
841 return -ENOMEM;
Josef Bacik2aaa6652012-08-29 14:27:18 -0400842 ret = __btrfs_drop_extents(trans, root, inode, path, start, end, NULL,
Josef Bacik26714852012-08-29 12:24:27 -0400843 drop_cache);
Chris Mason39279cc2007-06-12 06:35:45 -0400844 btrfs_free_path(path);
845 return ret;
846}
847
Yan Zhengd899e052008-10-30 14:25:28 -0400848static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000849 u64 objectid, u64 bytenr, u64 orig_offset,
850 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400851{
852 struct btrfs_file_extent_item *fi;
853 struct btrfs_key key;
854 u64 extent_end;
855
856 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
857 return 0;
858
859 btrfs_item_key_to_cpu(leaf, &key, slot);
860 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
861 return 0;
862
863 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
864 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
865 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000866 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400867 btrfs_file_extent_compression(leaf, fi) ||
868 btrfs_file_extent_encryption(leaf, fi) ||
869 btrfs_file_extent_other_encoding(leaf, fi))
870 return 0;
871
872 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
873 if ((*start && *start != key.offset) || (*end && *end != extent_end))
874 return 0;
875
876 *start = key.offset;
877 *end = extent_end;
878 return 1;
879}
880
881/*
882 * Mark extent in the range start - end as written.
883 *
884 * This changes extent type from 'pre-allocated' to 'regular'. If only
885 * part of extent is marked as written, the extent will be split into
886 * two or three.
887 */
888int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400889 struct inode *inode, u64 start, u64 end)
890{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000891 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400892 struct extent_buffer *leaf;
893 struct btrfs_path *path;
894 struct btrfs_file_extent_item *fi;
895 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000896 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400897 u64 bytenr;
898 u64 num_bytes;
899 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400900 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400901 u64 other_start;
902 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000903 u64 split;
904 int del_nr = 0;
905 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000906 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400907 int ret;
Li Zefan33345d012011-04-20 10:31:50 +0800908 u64 ino = btrfs_ino(inode);
Yan Zhengd899e052008-10-30 14:25:28 -0400909
Yan Zhengd899e052008-10-30 14:25:28 -0400910 path = btrfs_alloc_path();
Mark Fashehd8926bb2011-07-13 10:38:47 -0700911 if (!path)
912 return -ENOMEM;
Yan Zhengd899e052008-10-30 14:25:28 -0400913again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000914 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000915 split = start;
Li Zefan33345d012011-04-20 10:31:50 +0800916 key.objectid = ino;
Yan Zhengd899e052008-10-30 14:25:28 -0400917 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000918 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -0400919
920 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
Josef Bacik41415732011-03-16 13:59:32 -0400921 if (ret < 0)
922 goto out;
Yan Zhengd899e052008-10-30 14:25:28 -0400923 if (ret > 0 && path->slots[0] > 0)
924 path->slots[0]--;
925
926 leaf = path->nodes[0];
927 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Li Zefan33345d012011-04-20 10:31:50 +0800928 BUG_ON(key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY);
Yan Zhengd899e052008-10-30 14:25:28 -0400929 fi = btrfs_item_ptr(leaf, path->slots[0],
930 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000931 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
932 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -0400933 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
934 BUG_ON(key.offset > start || extent_end < end);
935
936 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
937 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400938 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000939 memcpy(&new_key, &key, sizeof(new_key));
940
941 if (start == key.offset && end < extent_end) {
942 other_start = 0;
943 other_end = start;
944 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +0800945 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000946 &other_start, &other_end)) {
947 new_key.offset = end;
948 btrfs_set_item_key_safe(trans, root, path, &new_key);
949 fi = btrfs_item_ptr(leaf, path->slots[0],
950 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -0400951 btrfs_set_file_extent_generation(leaf, fi,
952 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000953 btrfs_set_file_extent_num_bytes(leaf, fi,
954 extent_end - end);
955 btrfs_set_file_extent_offset(leaf, fi,
956 end - orig_offset);
957 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
958 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -0400959 btrfs_set_file_extent_generation(leaf, fi,
960 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000961 btrfs_set_file_extent_num_bytes(leaf, fi,
962 end - other_start);
963 btrfs_mark_buffer_dirty(leaf);
964 goto out;
965 }
966 }
967
968 if (start > key.offset && end == extent_end) {
969 other_start = end;
970 other_end = 0;
971 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +0800972 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000973 &other_start, &other_end)) {
974 fi = btrfs_item_ptr(leaf, path->slots[0],
975 struct btrfs_file_extent_item);
976 btrfs_set_file_extent_num_bytes(leaf, fi,
977 start - key.offset);
Josef Bacik224ecce2012-08-16 16:32:06 -0400978 btrfs_set_file_extent_generation(leaf, fi,
979 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000980 path->slots[0]++;
981 new_key.offset = start;
982 btrfs_set_item_key_safe(trans, root, path, &new_key);
983
984 fi = btrfs_item_ptr(leaf, path->slots[0],
985 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -0400986 btrfs_set_file_extent_generation(leaf, fi,
987 trans->transid);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000988 btrfs_set_file_extent_num_bytes(leaf, fi,
989 other_end - start);
990 btrfs_set_file_extent_offset(leaf, fi,
991 start - orig_offset);
992 btrfs_mark_buffer_dirty(leaf);
993 goto out;
994 }
995 }
Yan Zhengd899e052008-10-30 14:25:28 -0400996
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000997 while (start > key.offset || end < extent_end) {
998 if (key.offset == start)
999 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001000
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001001 new_key.offset = split;
1002 ret = btrfs_duplicate_item(trans, root, path, &new_key);
1003 if (ret == -EAGAIN) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001004 btrfs_release_path(path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001005 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -04001006 }
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001007 if (ret < 0) {
1008 btrfs_abort_transaction(trans, root, ret);
1009 goto out;
1010 }
Yan Zhengd899e052008-10-30 14:25:28 -04001011
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001012 leaf = path->nodes[0];
1013 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -04001014 struct btrfs_file_extent_item);
Josef Bacik224ecce2012-08-16 16:32:06 -04001015 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan Zhengd899e052008-10-30 14:25:28 -04001016 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001017 split - key.offset);
1018
1019 fi = btrfs_item_ptr(leaf, path->slots[0],
1020 struct btrfs_file_extent_item);
1021
Josef Bacik224ecce2012-08-16 16:32:06 -04001022 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001023 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
1024 btrfs_set_file_extent_num_bytes(leaf, fi,
1025 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -04001026 btrfs_mark_buffer_dirty(leaf);
1027
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001028 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
1029 root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001030 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001031 BUG_ON(ret); /* -ENOMEM */
Yan Zhengd899e052008-10-30 14:25:28 -04001032
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001033 if (split == start) {
1034 key.offset = start;
1035 } else {
1036 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -04001037 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001038 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -04001039 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001040 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -04001041 }
1042
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001043 other_start = end;
1044 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001045 if (extent_mergeable(leaf, path->slots[0] + 1,
Li Zefan33345d012011-04-20 10:31:50 +08001046 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001047 &other_start, &other_end)) {
1048 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001049 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001050 goto again;
1051 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001052 extent_end = other_end;
1053 del_slot = path->slots[0] + 1;
1054 del_nr++;
1055 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1056 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001057 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001058 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001059 }
1060 other_start = 0;
1061 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001062 if (extent_mergeable(leaf, path->slots[0] - 1,
Li Zefan33345d012011-04-20 10:31:50 +08001063 ino, bytenr, orig_offset,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001064 &other_start, &other_end)) {
1065 if (recow) {
David Sterbab3b4aa72011-04-21 01:20:15 +02001066 btrfs_release_path(path);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001067 goto again;
1068 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001069 key.offset = other_start;
1070 del_slot = path->slots[0];
1071 del_nr++;
1072 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
1073 0, root->root_key.objectid,
Arne Jansen66d7e7f2011-09-12 15:26:38 +02001074 ino, orig_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001075 BUG_ON(ret); /* -ENOMEM */
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001076 }
1077 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001078 fi = btrfs_item_ptr(leaf, path->slots[0],
1079 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001080 btrfs_set_file_extent_type(leaf, fi,
1081 BTRFS_FILE_EXTENT_REG);
Josef Bacik224ecce2012-08-16 16:32:06 -04001082 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001083 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +00001084 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +00001085 fi = btrfs_item_ptr(leaf, del_slot - 1,
1086 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +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, Zheng6c7d54a2010-01-15 08:43:09 +00001090 btrfs_set_file_extent_num_bytes(leaf, fi,
1091 extent_end - key.offset);
1092 btrfs_mark_buffer_dirty(leaf);
1093
1094 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01001095 if (ret < 0) {
1096 btrfs_abort_transaction(trans, root, ret);
1097 goto out;
1098 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001099 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +00001100out:
Yan Zhengd899e052008-10-30 14:25:28 -04001101 btrfs_free_path(path);
1102 return 0;
1103}
1104
Chris Mason39279cc2007-06-12 06:35:45 -04001105/*
Chris Masonb1bf8622011-02-28 09:52:08 -05001106 * on error we return an unlocked page and the error value
1107 * on success we return a locked page and 0
1108 */
Josef Bacikb63164292011-09-30 15:23:54 -04001109static int prepare_uptodate_page(struct page *page, u64 pos,
1110 bool force_uptodate)
Chris Masonb1bf8622011-02-28 09:52:08 -05001111{
1112 int ret = 0;
1113
Josef Bacikb63164292011-09-30 15:23:54 -04001114 if (((pos & (PAGE_CACHE_SIZE - 1)) || force_uptodate) &&
1115 !PageUptodate(page)) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001116 ret = btrfs_readpage(NULL, page);
1117 if (ret)
1118 return ret;
1119 lock_page(page);
1120 if (!PageUptodate(page)) {
1121 unlock_page(page);
1122 return -EIO;
1123 }
1124 }
1125 return 0;
1126}
1127
1128/*
Chris Masond352ac62008-09-29 15:18:18 -04001129 * this gets pages into the page cache and locks them down, it also properly
1130 * waits for data=ordered extents to finish before allowing the pages to be
1131 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -04001132 */
Chris Masond3977122009-01-05 21:25:51 -05001133static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -05001134 struct page **pages, size_t num_pages,
1135 loff_t pos, unsigned long first_index,
Josef Bacikb63164292011-09-30 15:23:54 -04001136 size_t write_bytes, bool force_uptodate)
Chris Mason39279cc2007-06-12 06:35:45 -04001137{
Josef Bacik2ac55d42010-02-03 19:33:23 +00001138 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001139 int i;
1140 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -05001141 struct inode *inode = fdentry(file)->d_inode;
Josef Bacik3b16a4e2011-09-21 15:05:58 -04001142 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
Chris Mason39279cc2007-06-12 06:35:45 -04001143 int err = 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001144 int faili = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -04001145 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001146 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -04001147
Chris Mason5f39d392007-10-15 16:14:19 -04001148 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001149 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001150
Chris Masone6dcd2d2008-07-17 12:53:50 -04001151again:
Chris Mason39279cc2007-06-12 06:35:45 -04001152 for (i = 0; i < num_pages; i++) {
Josef Bacika94733d2011-07-11 10:47:06 -04001153 pages[i] = find_or_create_page(inode->i_mapping, index + i,
Johannes Weinere3a41a52012-01-10 15:07:55 -08001154 mask | __GFP_WRITE);
Chris Mason39279cc2007-06-12 06:35:45 -04001155 if (!pages[i]) {
Chris Masonb1bf8622011-02-28 09:52:08 -05001156 faili = i - 1;
1157 err = -ENOMEM;
1158 goto fail;
1159 }
1160
1161 if (i == 0)
Josef Bacikb63164292011-09-30 15:23:54 -04001162 err = prepare_uptodate_page(pages[i], pos,
1163 force_uptodate);
Chris Masonb1bf8622011-02-28 09:52:08 -05001164 if (i == num_pages - 1)
1165 err = prepare_uptodate_page(pages[i],
Josef Bacikb63164292011-09-30 15:23:54 -04001166 pos + write_bytes, false);
Chris Masonb1bf8622011-02-28 09:52:08 -05001167 if (err) {
1168 page_cache_release(pages[i]);
1169 faili = i - 1;
1170 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001171 }
Chris Masonccd467d2007-06-28 15:57:36 -04001172 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -04001173 }
Chris Masonb1bf8622011-02-28 09:52:08 -05001174 err = 0;
Chris Mason07627042008-02-19 11:29:24 -05001175 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04001176 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +00001177 lock_extent_bits(&BTRFS_I(inode)->io_tree,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01001178 start_pos, last_pos - 1, 0, &cached_state);
Chris Masond3977122009-01-05 21:25:51 -05001179 ordered = btrfs_lookup_first_ordered_extent(inode,
1180 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001181 if (ordered &&
1182 ordered->file_offset + ordered->len > start_pos &&
1183 ordered->file_offset < last_pos) {
1184 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +00001185 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1186 start_pos, last_pos - 1,
1187 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001188 for (i = 0; i < num_pages; i++) {
1189 unlock_page(pages[i]);
1190 page_cache_release(pages[i]);
1191 }
1192 btrfs_wait_ordered_range(inode, start_pos,
1193 last_pos - start_pos);
1194 goto again;
1195 }
1196 if (ordered)
1197 btrfs_put_ordered_extent(ordered);
1198
Josef Bacik2ac55d42010-02-03 19:33:23 +00001199 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -04001200 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +00001201 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
Chris Mason07627042008-02-19 11:29:24 -05001202 GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +00001203 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1204 start_pos, last_pos - 1, &cached_state,
1205 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -05001206 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04001207 for (i = 0; i < num_pages; i++) {
Wu Fengguang32c7f202011-08-08 15:19:47 -06001208 if (clear_page_dirty_for_io(pages[i]))
1209 account_page_redirty(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001210 set_page_extent_mapped(pages[i]);
1211 WARN_ON(!PageLocked(pages[i]));
1212 }
Chris Mason39279cc2007-06-12 06:35:45 -04001213 return 0;
Chris Masonb1bf8622011-02-28 09:52:08 -05001214fail:
1215 while (faili >= 0) {
1216 unlock_page(pages[faili]);
1217 page_cache_release(pages[faili]);
1218 faili--;
1219 }
1220 return err;
1221
Chris Mason39279cc2007-06-12 06:35:45 -04001222}
1223
Josef Bacikd0215f32011-01-25 14:57:24 -05001224static noinline ssize_t __btrfs_buffered_write(struct file *file,
1225 struct iov_iter *i,
1226 loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -04001227{
Josef Bacik11c65dc2010-05-23 11:07:21 -04001228 struct inode *inode = fdentry(file)->d_inode;
1229 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001230 struct page **pages = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001231 unsigned long first_index;
Josef Bacikd0215f32011-01-25 14:57:24 -05001232 size_t num_written = 0;
1233 int nrptrs;
Tsutomu Itohc9149232011-03-30 00:57:23 +00001234 int ret = 0;
Josef Bacikb63164292011-09-30 15:23:54 -04001235 bool force_page_uptodate = false;
Chris Masoncb843a62008-10-03 12:30:02 -04001236
Josef Bacikd0215f32011-01-25 14:57:24 -05001237 nrptrs = min((iov_iter_count(i) + PAGE_CACHE_SIZE - 1) /
Josef Bacik11c65dc2010-05-23 11:07:21 -04001238 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
1239 (sizeof(struct page *)));
Wu Fengguang142349f2011-12-16 12:32:57 -05001240 nrptrs = min(nrptrs, current->nr_dirtied_pause - current->nr_dirtied);
1241 nrptrs = max(nrptrs, 8);
Chris Mason8c2383c2007-06-18 09:57:58 -04001242 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Josef Bacikd0215f32011-01-25 14:57:24 -05001243 if (!pages)
1244 return -ENOMEM;
Chris Masonab93dbe2009-10-01 12:29:10 -04001245
Chris Mason39279cc2007-06-12 06:35:45 -04001246 first_index = pos >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001247
Josef Bacikd0215f32011-01-25 14:57:24 -05001248 while (iov_iter_count(i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -04001249 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacikd0215f32011-01-25 14:57:24 -05001250 size_t write_bytes = min(iov_iter_count(i),
Josef Bacik11c65dc2010-05-23 11:07:21 -04001251 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -04001252 offset);
Yan, Zheng3a909832011-01-18 13:34:40 +08001253 size_t num_pages = (write_bytes + offset +
1254 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Josef Bacikd0215f32011-01-25 14:57:24 -05001255 size_t dirty_pages;
1256 size_t copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001257
Chris Mason8c2383c2007-06-18 09:57:58 -04001258 WARN_ON(num_pages > nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -05001259
Xin Zhong914ee292010-12-09 09:30:14 +00001260 /*
1261 * Fault pages before locking them in prepare_pages
1262 * to avoid recursive lock
1263 */
Josef Bacikd0215f32011-01-25 14:57:24 -05001264 if (unlikely(iov_iter_fault_in_readable(i, write_bytes))) {
Xin Zhong914ee292010-12-09 09:30:14 +00001265 ret = -EFAULT;
Josef Bacikd0215f32011-01-25 14:57:24 -05001266 break;
Xin Zhong914ee292010-12-09 09:30:14 +00001267 }
1268
1269 ret = btrfs_delalloc_reserve_space(inode,
1270 num_pages << PAGE_CACHE_SHIFT);
Chris Mason1832a6d2007-12-21 16:27:21 -05001271 if (ret)
Josef Bacikd0215f32011-01-25 14:57:24 -05001272 break;
Chris Mason1832a6d2007-12-21 16:27:21 -05001273
Josef Bacik4a640012011-01-25 15:10:08 -05001274 /*
1275 * This is going to setup the pages array with the number of
1276 * pages we want, so we don't really need to worry about the
1277 * contents of pages from loop to loop
1278 */
Chris Mason39279cc2007-06-12 06:35:45 -04001279 ret = prepare_pages(root, file, pages, num_pages,
Josef Bacikb63164292011-09-30 15:23:54 -04001280 pos, first_index, write_bytes,
1281 force_page_uptodate);
Josef Bacik6a632092009-02-20 11:00:09 -05001282 if (ret) {
Xin Zhong914ee292010-12-09 09:30:14 +00001283 btrfs_delalloc_release_space(inode,
1284 num_pages << PAGE_CACHE_SHIFT);
Josef Bacikd0215f32011-01-25 14:57:24 -05001285 break;
Josef Bacik6a632092009-02-20 11:00:09 -05001286 }
Chris Mason39279cc2007-06-12 06:35:45 -04001287
Xin Zhong914ee292010-12-09 09:30:14 +00001288 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacikd0215f32011-01-25 14:57:24 -05001289 write_bytes, pages, i);
Chris Masonb1bf8622011-02-28 09:52:08 -05001290
1291 /*
1292 * if we have trouble faulting in the pages, fall
1293 * back to one page at a time
1294 */
1295 if (copied < write_bytes)
1296 nrptrs = 1;
1297
Josef Bacikb63164292011-09-30 15:23:54 -04001298 if (copied == 0) {
1299 force_page_uptodate = true;
Chris Masonb1bf8622011-02-28 09:52:08 -05001300 dirty_pages = 0;
Josef Bacikb63164292011-09-30 15:23:54 -04001301 } else {
1302 force_page_uptodate = false;
Chris Masonb1bf8622011-02-28 09:52:08 -05001303 dirty_pages = (copied + offset +
1304 PAGE_CACHE_SIZE - 1) >>
1305 PAGE_CACHE_SHIFT;
Josef Bacikb63164292011-09-30 15:23:54 -04001306 }
Xin Zhong914ee292010-12-09 09:30:14 +00001307
Josef Bacikd0215f32011-01-25 14:57:24 -05001308 /*
1309 * If we had a short copy we need to release the excess delaloc
1310 * bytes we reserved. We need to increment outstanding_extents
1311 * because btrfs_delalloc_release_space will decrement it, but
1312 * we still have an outstanding extent for the chunk we actually
1313 * managed to copy.
1314 */
Xin Zhong914ee292010-12-09 09:30:14 +00001315 if (num_pages > dirty_pages) {
Josef Bacik9e0baf62011-07-15 15:16:44 +00001316 if (copied > 0) {
1317 spin_lock(&BTRFS_I(inode)->lock);
1318 BTRFS_I(inode)->outstanding_extents++;
1319 spin_unlock(&BTRFS_I(inode)->lock);
1320 }
Xin Zhong914ee292010-12-09 09:30:14 +00001321 btrfs_delalloc_release_space(inode,
1322 (num_pages - dirty_pages) <<
1323 PAGE_CACHE_SHIFT);
1324 }
1325
1326 if (copied > 0) {
Josef Bacikbe1a12a2011-04-06 13:05:22 -04001327 ret = btrfs_dirty_pages(root, inode, pages,
1328 dirty_pages, pos, copied,
1329 NULL);
Josef Bacikd0215f32011-01-25 14:57:24 -05001330 if (ret) {
1331 btrfs_delalloc_release_space(inode,
1332 dirty_pages << PAGE_CACHE_SHIFT);
1333 btrfs_drop_pages(pages, num_pages);
1334 break;
1335 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001336 }
Chris Mason39279cc2007-06-12 06:35:45 -04001337
Chris Mason39279cc2007-06-12 06:35:45 -04001338 btrfs_drop_pages(pages, num_pages);
Xin Zhong914ee292010-12-09 09:30:14 +00001339
Josef Bacikd0215f32011-01-25 14:57:24 -05001340 cond_resched();
1341
1342 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
1343 dirty_pages);
1344 if (dirty_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1345 btrfs_btree_balance_dirty(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001346
Xin Zhong914ee292010-12-09 09:30:14 +00001347 pos += copied;
1348 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001349 }
Chris Mason5b92ee72008-01-03 13:46:11 -05001350
Chris Mason8c2383c2007-06-18 09:57:58 -04001351 kfree(pages);
Josef Bacikd0215f32011-01-25 14:57:24 -05001352
1353 return num_written ? num_written : ret;
1354}
1355
1356static ssize_t __btrfs_direct_write(struct kiocb *iocb,
1357 const struct iovec *iov,
1358 unsigned long nr_segs, loff_t pos,
1359 loff_t *ppos, size_t count, size_t ocount)
1360{
1361 struct file *file = iocb->ki_filp;
Josef Bacikd0215f32011-01-25 14:57:24 -05001362 struct iov_iter i;
1363 ssize_t written;
1364 ssize_t written_buffered;
1365 loff_t endbyte;
1366 int err;
1367
1368 written = generic_file_direct_write(iocb, iov, &nr_segs, pos, ppos,
1369 count, ocount);
1370
Josef Bacikd0215f32011-01-25 14:57:24 -05001371 if (written < 0 || written == count)
1372 return written;
1373
1374 pos += written;
1375 count -= written;
1376 iov_iter_init(&i, iov, nr_segs, count, written);
1377 written_buffered = __btrfs_buffered_write(file, &i, pos);
1378 if (written_buffered < 0) {
1379 err = written_buffered;
1380 goto out;
1381 }
1382 endbyte = pos + written_buffered - 1;
1383 err = filemap_write_and_wait_range(file->f_mapping, pos, endbyte);
1384 if (err)
1385 goto out;
1386 written += written_buffered;
1387 *ppos = pos + written_buffered;
1388 invalidate_mapping_pages(file->f_mapping, pos >> PAGE_CACHE_SHIFT,
1389 endbyte >> PAGE_CACHE_SHIFT);
1390out:
1391 return written ? written : err;
1392}
1393
1394static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
1395 const struct iovec *iov,
1396 unsigned long nr_segs, loff_t pos)
1397{
1398 struct file *file = iocb->ki_filp;
1399 struct inode *inode = fdentry(file)->d_inode;
1400 struct btrfs_root *root = BTRFS_I(inode)->root;
1401 loff_t *ppos = &iocb->ki_pos;
Miao Xie0c1a98c2011-09-11 10:52:24 -04001402 u64 start_pos;
Josef Bacikd0215f32011-01-25 14:57:24 -05001403 ssize_t num_written = 0;
1404 ssize_t err = 0;
1405 size_t count, ocount;
1406
Jan Karab2b5ef52012-06-12 16:20:45 +02001407 sb_start_write(inode->i_sb);
Josef Bacikd0215f32011-01-25 14:57:24 -05001408
1409 mutex_lock(&inode->i_mutex);
1410
1411 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
1412 if (err) {
1413 mutex_unlock(&inode->i_mutex);
1414 goto out;
1415 }
1416 count = ocount;
1417
1418 current->backing_dev_info = inode->i_mapping->backing_dev_info;
1419 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1420 if (err) {
1421 mutex_unlock(&inode->i_mutex);
1422 goto out;
1423 }
1424
1425 if (count == 0) {
1426 mutex_unlock(&inode->i_mutex);
1427 goto out;
1428 }
1429
1430 err = file_remove_suid(file);
1431 if (err) {
1432 mutex_unlock(&inode->i_mutex);
1433 goto out;
1434 }
1435
1436 /*
1437 * If BTRFS flips readonly due to some impossible error
1438 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
1439 * although we have opened a file as writable, we have
1440 * to stop this write operation to ensure FS consistency.
1441 */
1442 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
1443 mutex_unlock(&inode->i_mutex);
1444 err = -EROFS;
1445 goto out;
1446 }
1447
Josef Bacike41f9412012-03-26 09:46:47 -04001448 err = file_update_time(file);
Josef Bacik22c44fe2011-11-30 10:45:38 -05001449 if (err) {
1450 mutex_unlock(&inode->i_mutex);
1451 goto out;
1452 }
Josef Bacikd0215f32011-01-25 14:57:24 -05001453
Miao Xie0c1a98c2011-09-11 10:52:24 -04001454 start_pos = round_down(pos, root->sectorsize);
1455 if (start_pos > i_size_read(inode)) {
1456 err = btrfs_cont_expand(inode, i_size_read(inode), start_pos);
1457 if (err) {
1458 mutex_unlock(&inode->i_mutex);
1459 goto out;
1460 }
1461 }
1462
Josef Bacikd0215f32011-01-25 14:57:24 -05001463 if (unlikely(file->f_flags & O_DIRECT)) {
1464 num_written = __btrfs_direct_write(iocb, iov, nr_segs,
1465 pos, ppos, count, ocount);
1466 } else {
1467 struct iov_iter i;
1468
1469 iov_iter_init(&i, iov, nr_segs, count, num_written);
1470
1471 num_written = __btrfs_buffered_write(file, &i, pos);
1472 if (num_written > 0)
1473 *ppos = pos + num_written;
1474 }
1475
1476 mutex_unlock(&inode->i_mutex);
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001477
Chris Mason5a3f23d2009-03-31 13:27:11 -04001478 /*
1479 * we want to make sure fsync finds this change
1480 * but we haven't joined a transaction running right now.
1481 *
1482 * Later on, someone is sure to update the inode and get the
1483 * real transid recorded.
1484 *
1485 * We set last_trans now to the fs_info generation + 1,
1486 * this will either be one more than the running transaction
1487 * or the generation used for the next transaction if there isn't
1488 * one running right now.
1489 */
1490 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
Josef Bacikd0215f32011-01-25 14:57:24 -05001491 if (num_written > 0 || num_written == -EIOCBQUEUED) {
1492 err = generic_write_sync(file, pos, num_written);
1493 if (err < 0 && num_written > 0)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001494 num_written = err;
1495 }
Josef Bacikd0215f32011-01-25 14:57:24 -05001496out:
Jan Karab2b5ef52012-06-12 16:20:45 +02001497 sb_end_write(inode->i_sb);
Chris Mason39279cc2007-06-12 06:35:45 -04001498 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001499 return num_written ? num_written : err;
1500}
1501
Chris Masond3977122009-01-05 21:25:51 -05001502int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001503{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001504 /*
1505 * ordered_data_close is set by settattr when we are about to truncate
1506 * a file from a non-zero size to a zero size. This tries to
1507 * flush down new bytes that may have been written if the
1508 * application were using truncate to replace a file in place.
1509 */
Josef Bacik72ac3c02012-05-23 14:13:11 -04001510 if (test_and_clear_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
1511 &BTRFS_I(inode)->runtime_flags)) {
Chris Mason5a3f23d2009-03-31 13:27:11 -04001512 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1513 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1514 filemap_flush(inode->i_mapping);
1515 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001516 if (filp->private_data)
1517 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001518 return 0;
1519}
1520
Chris Masond352ac62008-09-29 15:18:18 -04001521/*
1522 * fsync call for both files and directories. This logs the inode into
1523 * the tree log instead of forcing full commits whenever possible.
1524 *
1525 * It needs to call filemap_fdatawait so that all ordered extent updates are
1526 * in the metadata btree are up to date for copying to the log.
1527 *
1528 * It drops the inode mutex before doing the tree log commit. This is an
1529 * important optimization for directories because holding the mutex prevents
1530 * new operations on the dir while we write to disk.
1531 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001532int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001533{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001534 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001535 struct inode *inode = dentry->d_inode;
1536 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001537 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001538 struct btrfs_trans_handle *trans;
1539
liubo1abe9b82011-03-24 11:18:59 +00001540 trace_btrfs_sync_file(file, datasync);
Chris Mason257c62e2009-10-13 13:21:08 -04001541
Josef Bacik02c24a82011-07-16 20:44:56 -04001542 mutex_lock(&inode->i_mutex);
1543
Josef Bacik0885ef52012-04-23 15:09:39 -04001544 /*
1545 * we wait first, since the writeback may change the inode, also wait
1546 * ordered range does a filemape_write_and_wait_range which is why we
1547 * don't do it above like other file systems.
1548 */
Chris Mason257c62e2009-10-13 13:21:08 -04001549 root->log_batch++;
Josef Bacik0885ef52012-04-23 15:09:39 -04001550 btrfs_wait_ordered_range(inode, start, end);
Chris Mason257c62e2009-10-13 13:21:08 -04001551 root->log_batch++;
1552
Chris Mason39279cc2007-06-12 06:35:45 -04001553 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001554 * check the transaction that last modified this inode
1555 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001556 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001557 if (!BTRFS_I(inode)->last_trans) {
1558 mutex_unlock(&inode->i_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001559 goto out;
Josef Bacik02c24a82011-07-16 20:44:56 -04001560 }
Chris Masona2135012008-06-25 16:01:30 -04001561
Chris Mason257c62e2009-10-13 13:21:08 -04001562 /*
1563 * if the last transaction that changed this file was before
1564 * the current transaction, we can bail out now without any
1565 * syncing
1566 */
Josef Bacika4abeea2011-04-11 17:25:13 -04001567 smp_mb();
Josef Bacik22ee6982012-05-29 16:57:49 -04001568 if (btrfs_inode_in_log(inode, root->fs_info->generation) ||
1569 BTRFS_I(inode)->last_trans <=
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001570 root->fs_info->last_trans_committed) {
1571 BTRFS_I(inode)->last_trans = 0;
Josef Bacik5dc562c2012-08-17 13:14:17 -04001572
1573 /*
1574 * We'v had everything committed since the last time we were
1575 * modified so clear this flag in case it was set for whatever
1576 * reason, it's no longer relevant.
1577 */
1578 clear_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1579 &BTRFS_I(inode)->runtime_flags);
Josef Bacik02c24a82011-07-16 20:44:56 -04001580 mutex_unlock(&inode->i_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001581 goto out;
1582 }
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001583
1584 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001585 * ok we haven't committed the transaction yet, lets do a commit
1586 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001587 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001588 btrfs_ioctl_trans_end(file);
1589
Yan, Zhenga22285a2010-05-16 10:48:46 -04001590 trans = btrfs_start_transaction(root, 0);
1591 if (IS_ERR(trans)) {
1592 ret = PTR_ERR(trans);
Josef Bacik02c24a82011-07-16 20:44:56 -04001593 mutex_unlock(&inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001594 goto out;
1595 }
Chris Masone02119d2008-09-05 16:13:11 -04001596
Chris Mason2cfbd502009-02-20 10:55:10 -05001597 ret = btrfs_log_dentry_safe(trans, root, dentry);
Josef Bacik02c24a82011-07-16 20:44:56 -04001598 if (ret < 0) {
1599 mutex_unlock(&inode->i_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04001600 goto out;
Josef Bacik02c24a82011-07-16 20:44:56 -04001601 }
Chris Mason49eb7e42008-09-11 15:53:12 -04001602
1603 /* we've logged all the items and now have a consistent
1604 * version of the file in the log. It is possible that
1605 * someone will come in and modify the file, but that's
1606 * fine because the log is consistent on disk, and we
1607 * have references to all of the file's extents
1608 *
1609 * It is possible that someone will come in and log the
1610 * file again, but that will end up using the synchronization
1611 * inside btrfs_sync_log to keep things safe.
1612 */
Josef Bacik02c24a82011-07-16 20:44:56 -04001613 mutex_unlock(&inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001614
Chris Mason257c62e2009-10-13 13:21:08 -04001615 if (ret != BTRFS_NO_LOG_SYNC) {
1616 if (ret > 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001617 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001618 } else {
1619 ret = btrfs_sync_log(trans, root);
1620 if (ret == 0)
1621 ret = btrfs_end_transaction(trans, root);
1622 else
1623 ret = btrfs_commit_transaction(trans, root);
1624 }
1625 } else {
1626 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001627 }
Chris Mason39279cc2007-06-12 06:35:45 -04001628out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001629 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001630}
1631
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001632static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001633 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001634 .page_mkwrite = btrfs_page_mkwrite,
1635};
1636
1637static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1638{
Miao Xie058a4572010-05-20 07:21:50 +00001639 struct address_space *mapping = filp->f_mapping;
1640
1641 if (!mapping->a_ops->readpage)
1642 return -ENOEXEC;
1643
Chris Mason9ebefb182007-06-15 13:50:00 -04001644 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00001645 vma->vm_ops = &btrfs_file_vm_ops;
1646 vma->vm_flags |= VM_CAN_NONLINEAR;
1647
Chris Mason9ebefb182007-06-15 13:50:00 -04001648 return 0;
1649}
1650
Josef Bacik2aaa6652012-08-29 14:27:18 -04001651static int hole_mergeable(struct inode *inode, struct extent_buffer *leaf,
1652 int slot, u64 start, u64 end)
1653{
1654 struct btrfs_file_extent_item *fi;
1655 struct btrfs_key key;
1656
1657 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
1658 return 0;
1659
1660 btrfs_item_key_to_cpu(leaf, &key, slot);
1661 if (key.objectid != btrfs_ino(inode) ||
1662 key.type != BTRFS_EXTENT_DATA_KEY)
1663 return 0;
1664
1665 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
1666
1667 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
1668 return 0;
1669
1670 if (btrfs_file_extent_disk_bytenr(leaf, fi))
1671 return 0;
1672
1673 if (key.offset == end)
1674 return 1;
1675 if (key.offset + btrfs_file_extent_num_bytes(leaf, fi) == start)
1676 return 1;
1677 return 0;
1678}
1679
1680static int fill_holes(struct btrfs_trans_handle *trans, struct inode *inode,
1681 struct btrfs_path *path, u64 offset, u64 end)
1682{
1683 struct btrfs_root *root = BTRFS_I(inode)->root;
1684 struct extent_buffer *leaf;
1685 struct btrfs_file_extent_item *fi;
1686 struct extent_map *hole_em;
1687 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1688 struct btrfs_key key;
1689 int ret;
1690
1691 key.objectid = btrfs_ino(inode);
1692 key.type = BTRFS_EXTENT_DATA_KEY;
1693 key.offset = offset;
1694
1695
1696 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
1697 if (ret < 0)
1698 return ret;
1699 BUG_ON(!ret);
1700
1701 leaf = path->nodes[0];
1702 if (hole_mergeable(inode, leaf, path->slots[0]-1, offset, end)) {
1703 u64 num_bytes;
1704
1705 path->slots[0]--;
1706 fi = btrfs_item_ptr(leaf, path->slots[0],
1707 struct btrfs_file_extent_item);
1708 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) +
1709 end - offset;
1710 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1711 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
1712 btrfs_set_file_extent_offset(leaf, fi, 0);
1713 btrfs_mark_buffer_dirty(leaf);
1714 goto out;
1715 }
1716
1717 if (hole_mergeable(inode, leaf, path->slots[0]+1, offset, end)) {
1718 u64 num_bytes;
1719
1720 path->slots[0]++;
1721 key.offset = offset;
1722 btrfs_set_item_key_safe(trans, root, path, &key);
1723 fi = btrfs_item_ptr(leaf, path->slots[0],
1724 struct btrfs_file_extent_item);
1725 num_bytes = btrfs_file_extent_num_bytes(leaf, fi) + end -
1726 offset;
1727 btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1728 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
1729 btrfs_set_file_extent_offset(leaf, fi, 0);
1730 btrfs_mark_buffer_dirty(leaf);
1731 goto out;
1732 }
1733 btrfs_release_path(path);
1734
1735 ret = btrfs_insert_file_extent(trans, root, btrfs_ino(inode), offset,
1736 0, 0, end - offset, 0, end - offset,
1737 0, 0, 0);
1738 if (ret)
1739 return ret;
1740
1741out:
1742 btrfs_release_path(path);
1743
1744 hole_em = alloc_extent_map();
1745 if (!hole_em) {
1746 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
1747 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1748 &BTRFS_I(inode)->runtime_flags);
1749 } else {
1750 hole_em->start = offset;
1751 hole_em->len = end - offset;
1752 hole_em->orig_start = offset;
1753
1754 hole_em->block_start = EXTENT_MAP_HOLE;
1755 hole_em->block_len = 0;
1756 hole_em->bdev = root->fs_info->fs_devices->latest_bdev;
1757 hole_em->compress_type = BTRFS_COMPRESS_NONE;
1758 hole_em->generation = trans->transid;
1759
1760 do {
1761 btrfs_drop_extent_cache(inode, offset, end - 1, 0);
1762 write_lock(&em_tree->lock);
1763 ret = add_extent_mapping(em_tree, hole_em);
1764 if (!ret)
1765 list_move(&hole_em->list,
1766 &em_tree->modified_extents);
1767 write_unlock(&em_tree->lock);
1768 } while (ret == -EEXIST);
1769 free_extent_map(hole_em);
1770 if (ret)
1771 set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
1772 &BTRFS_I(inode)->runtime_flags);
1773 }
1774
1775 return 0;
1776}
1777
1778static int btrfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
1779{
1780 struct btrfs_root *root = BTRFS_I(inode)->root;
1781 struct extent_state *cached_state = NULL;
1782 struct btrfs_path *path;
1783 struct btrfs_block_rsv *rsv;
1784 struct btrfs_trans_handle *trans;
1785 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
1786 u64 lockstart = (offset + mask) & ~mask;
1787 u64 lockend = ((offset + len) & ~mask) - 1;
1788 u64 cur_offset = lockstart;
1789 u64 min_size = btrfs_calc_trunc_metadata_size(root, 1);
1790 u64 drop_end;
1791 unsigned long nr;
1792 int ret = 0;
1793 int err = 0;
1794 bool same_page = (offset >> PAGE_CACHE_SHIFT) ==
1795 ((offset + len) >> PAGE_CACHE_SHIFT);
1796
1797 btrfs_wait_ordered_range(inode, offset, len);
1798
1799 mutex_lock(&inode->i_mutex);
1800 if (offset >= inode->i_size) {
1801 mutex_unlock(&inode->i_mutex);
1802 return 0;
1803 }
1804
1805 /*
1806 * Only do this if we are in the same page and we aren't doing the
1807 * entire page.
1808 */
1809 if (same_page && len < PAGE_CACHE_SIZE) {
1810 ret = btrfs_truncate_page(inode, offset, len, 0);
1811 mutex_unlock(&inode->i_mutex);
1812 return ret;
1813 }
1814
1815 /* zero back part of the first page */
1816 ret = btrfs_truncate_page(inode, offset, 0, 0);
1817 if (ret) {
1818 mutex_unlock(&inode->i_mutex);
1819 return ret;
1820 }
1821
1822 /* zero the front end of the last page */
1823 ret = btrfs_truncate_page(inode, offset + len, 0, 1);
1824 if (ret) {
1825 mutex_unlock(&inode->i_mutex);
1826 return ret;
1827 }
1828
1829 if (lockend < lockstart) {
1830 mutex_unlock(&inode->i_mutex);
1831 return 0;
1832 }
1833
1834 while (1) {
1835 struct btrfs_ordered_extent *ordered;
1836
1837 truncate_pagecache_range(inode, lockstart, lockend);
1838
1839 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
1840 0, &cached_state);
1841 ordered = btrfs_lookup_first_ordered_extent(inode, lockend);
1842
1843 /*
1844 * We need to make sure we have no ordered extents in this range
1845 * and nobody raced in and read a page in this range, if we did
1846 * we need to try again.
1847 */
1848 if ((!ordered ||
1849 (ordered->file_offset + ordered->len < lockstart ||
1850 ordered->file_offset > lockend)) &&
1851 !test_range_bit(&BTRFS_I(inode)->io_tree, lockstart,
1852 lockend, EXTENT_UPTODATE, 0,
1853 cached_state)) {
1854 if (ordered)
1855 btrfs_put_ordered_extent(ordered);
1856 break;
1857 }
1858 if (ordered)
1859 btrfs_put_ordered_extent(ordered);
1860 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart,
1861 lockend, &cached_state, GFP_NOFS);
1862 btrfs_wait_ordered_range(inode, lockstart,
1863 lockend - lockstart + 1);
1864 }
1865
1866 path = btrfs_alloc_path();
1867 if (!path) {
1868 ret = -ENOMEM;
1869 goto out;
1870 }
1871
1872 rsv = btrfs_alloc_block_rsv(root);
1873 if (!rsv) {
1874 ret = -ENOMEM;
1875 goto out_free;
1876 }
1877 rsv->size = btrfs_calc_trunc_metadata_size(root, 1);
1878 rsv->failfast = 1;
1879
1880 /*
1881 * 1 - update the inode
1882 * 1 - removing the extents in the range
1883 * 1 - adding the hole extent
1884 */
1885 trans = btrfs_start_transaction(root, 3);
1886 if (IS_ERR(trans)) {
1887 err = PTR_ERR(trans);
1888 goto out_free;
1889 }
1890
1891 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv, rsv,
1892 min_size);
1893 BUG_ON(ret);
1894 trans->block_rsv = rsv;
1895
1896 while (cur_offset < lockend) {
1897 ret = __btrfs_drop_extents(trans, root, inode, path,
1898 cur_offset, lockend + 1,
1899 &drop_end, 1);
1900 if (ret != -ENOSPC)
1901 break;
1902
1903 trans->block_rsv = &root->fs_info->trans_block_rsv;
1904
1905 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
1906 if (ret) {
1907 err = ret;
1908 break;
1909 }
1910
1911 cur_offset = drop_end;
1912
1913 ret = btrfs_update_inode(trans, root, inode);
1914 if (ret) {
1915 err = ret;
1916 break;
1917 }
1918
1919 nr = trans->blocks_used;
1920 btrfs_end_transaction(trans, root);
1921 btrfs_btree_balance_dirty(root, nr);
1922
1923 trans = btrfs_start_transaction(root, 3);
1924 if (IS_ERR(trans)) {
1925 ret = PTR_ERR(trans);
1926 trans = NULL;
1927 break;
1928 }
1929
1930 ret = btrfs_block_rsv_migrate(&root->fs_info->trans_block_rsv,
1931 rsv, min_size);
1932 BUG_ON(ret); /* shouldn't happen */
1933 trans->block_rsv = rsv;
1934 }
1935
1936 if (ret) {
1937 err = ret;
1938 goto out_trans;
1939 }
1940
1941 trans->block_rsv = &root->fs_info->trans_block_rsv;
1942 ret = fill_holes(trans, inode, path, cur_offset, drop_end);
1943 if (ret) {
1944 err = ret;
1945 goto out_trans;
1946 }
1947
1948out_trans:
1949 if (!trans)
1950 goto out_free;
1951
1952 trans->block_rsv = &root->fs_info->trans_block_rsv;
1953 ret = btrfs_update_inode(trans, root, inode);
1954 nr = trans->blocks_used;
1955 btrfs_end_transaction(trans, root);
1956 btrfs_btree_balance_dirty(root, nr);
1957out_free:
1958 btrfs_free_path(path);
1959 btrfs_free_block_rsv(root, rsv);
1960out:
1961 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
1962 &cached_state, GFP_NOFS);
1963 mutex_unlock(&inode->i_mutex);
1964 if (ret && !err)
1965 err = ret;
1966 return err;
1967}
1968
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001969static long btrfs_fallocate(struct file *file, int mode,
1970 loff_t offset, loff_t len)
1971{
1972 struct inode *inode = file->f_path.dentry->d_inode;
1973 struct extent_state *cached_state = NULL;
1974 u64 cur_offset;
1975 u64 last_byte;
1976 u64 alloc_start;
1977 u64 alloc_end;
1978 u64 alloc_hint = 0;
1979 u64 locked_end;
1980 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
1981 struct extent_map *em;
1982 int ret;
1983
1984 alloc_start = offset & ~mask;
1985 alloc_end = (offset + len + mask) & ~mask;
1986
Josef Bacik2aaa6652012-08-29 14:27:18 -04001987 /* Make sure we aren't being give some crap mode */
1988 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001989 return -EOPNOTSUPP;
1990
Josef Bacik2aaa6652012-08-29 14:27:18 -04001991 if (mode & FALLOC_FL_PUNCH_HOLE)
1992 return btrfs_punch_hole(inode, offset, len);
1993
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001994 /*
Chris Masond98456f2012-01-31 20:27:41 -05001995 * Make sure we have enough space before we do the
1996 * allocation.
1997 */
1998 ret = btrfs_check_data_free_space(inode, len);
1999 if (ret)
2000 return ret;
2001
2002 /*
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002003 * wait for ordered IO before we have any locks. We'll loop again
2004 * below with the locks held.
2005 */
2006 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
2007
2008 mutex_lock(&inode->i_mutex);
2009 ret = inode_newsize_ok(inode, alloc_end);
2010 if (ret)
2011 goto out;
2012
2013 if (alloc_start > inode->i_size) {
Josef Bacika41ad392011-01-31 15:30:16 -05002014 ret = btrfs_cont_expand(inode, i_size_read(inode),
2015 alloc_start);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002016 if (ret)
2017 goto out;
2018 }
2019
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002020 locked_end = alloc_end - 1;
2021 while (1) {
2022 struct btrfs_ordered_extent *ordered;
2023
2024 /* the extent lock is ordered inside the running
2025 * transaction
2026 */
2027 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002028 locked_end, 0, &cached_state);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002029 ordered = btrfs_lookup_first_ordered_extent(inode,
2030 alloc_end - 1);
2031 if (ordered &&
2032 ordered->file_offset + ordered->len > alloc_start &&
2033 ordered->file_offset < alloc_end) {
2034 btrfs_put_ordered_extent(ordered);
2035 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
2036 alloc_start, locked_end,
2037 &cached_state, GFP_NOFS);
2038 /*
2039 * we can't wait on the range with the transaction
2040 * running or with the extent lock held
2041 */
2042 btrfs_wait_ordered_range(inode, alloc_start,
2043 alloc_end - alloc_start);
2044 } else {
2045 if (ordered)
2046 btrfs_put_ordered_extent(ordered);
2047 break;
2048 }
2049 }
2050
2051 cur_offset = alloc_start;
2052 while (1) {
Josef Bacikf1e490a2011-08-18 10:36:39 -04002053 u64 actual_end;
2054
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002055 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
2056 alloc_end - cur_offset, 0);
Jeff Mahoney79787ea2012-03-12 16:03:00 +01002057 if (IS_ERR_OR_NULL(em)) {
2058 if (!em)
2059 ret = -ENOMEM;
2060 else
2061 ret = PTR_ERR(em);
2062 break;
2063 }
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002064 last_byte = min(extent_map_end(em), alloc_end);
Josef Bacikf1e490a2011-08-18 10:36:39 -04002065 actual_end = min_t(u64, extent_map_end(em), offset + len);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002066 last_byte = (last_byte + mask) & ~mask;
Josef Bacikf1e490a2011-08-18 10:36:39 -04002067
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002068 if (em->block_start == EXTENT_MAP_HOLE ||
2069 (cur_offset >= inode->i_size &&
2070 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
2071 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
2072 last_byte - cur_offset,
2073 1 << inode->i_blkbits,
2074 offset + len,
2075 &alloc_hint);
Josef Bacik1b9c3322011-08-17 10:19:52 -04002076
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002077 if (ret < 0) {
2078 free_extent_map(em);
2079 break;
2080 }
Josef Bacikf1e490a2011-08-18 10:36:39 -04002081 } else if (actual_end > inode->i_size &&
2082 !(mode & FALLOC_FL_KEEP_SIZE)) {
2083 /*
2084 * We didn't need to allocate any more space, but we
2085 * still extended the size of the file so we need to
2086 * update i_size.
2087 */
2088 inode->i_ctime = CURRENT_TIME;
2089 i_size_write(inode, actual_end);
2090 btrfs_ordered_update_i_size(inode, actual_end, NULL);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002091 }
2092 free_extent_map(em);
2093
2094 cur_offset = last_byte;
2095 if (cur_offset >= alloc_end) {
2096 ret = 0;
2097 break;
2098 }
2099 }
2100 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
2101 &cached_state, GFP_NOFS);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002102out:
2103 mutex_unlock(&inode->i_mutex);
Chris Masond98456f2012-01-31 20:27:41 -05002104 /* Let go of our reservation. */
2105 btrfs_free_reserved_data_space(inode, len);
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002106 return ret;
2107}
2108
Josef Bacikb2675152011-07-18 13:21:36 -04002109static int find_desired_extent(struct inode *inode, loff_t *offset, int origin)
2110{
2111 struct btrfs_root *root = BTRFS_I(inode)->root;
2112 struct extent_map *em;
2113 struct extent_state *cached_state = NULL;
2114 u64 lockstart = *offset;
2115 u64 lockend = i_size_read(inode);
2116 u64 start = *offset;
2117 u64 orig_start = *offset;
2118 u64 len = i_size_read(inode);
2119 u64 last_end = 0;
2120 int ret = 0;
2121
2122 lockend = max_t(u64, root->sectorsize, lockend);
2123 if (lockend <= lockstart)
2124 lockend = lockstart + root->sectorsize;
2125
2126 len = lockend - lockstart + 1;
2127
2128 len = max_t(u64, len, root->sectorsize);
2129 if (inode->i_size == 0)
2130 return -ENXIO;
2131
2132 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend, 0,
Jeff Mahoneyd0082372012-03-01 14:57:19 +01002133 &cached_state);
Josef Bacikb2675152011-07-18 13:21:36 -04002134
2135 /*
2136 * Delalloc is such a pain. If we have a hole and we have pending
2137 * delalloc for a portion of the hole we will get back a hole that
2138 * exists for the entire range since it hasn't been actually written
2139 * yet. So to take care of this case we need to look for an extent just
2140 * before the position we want in case there is outstanding delalloc
2141 * going on here.
2142 */
2143 if (origin == SEEK_HOLE && start != 0) {
2144 if (start <= root->sectorsize)
2145 em = btrfs_get_extent_fiemap(inode, NULL, 0, 0,
2146 root->sectorsize, 0);
2147 else
2148 em = btrfs_get_extent_fiemap(inode, NULL, 0,
2149 start - root->sectorsize,
2150 root->sectorsize, 0);
2151 if (IS_ERR(em)) {
Jeff Liu6af021d2012-02-09 14:25:50 +08002152 ret = PTR_ERR(em);
Josef Bacikb2675152011-07-18 13:21:36 -04002153 goto out;
2154 }
2155 last_end = em->start + em->len;
2156 if (em->block_start == EXTENT_MAP_DELALLOC)
2157 last_end = min_t(u64, last_end, inode->i_size);
2158 free_extent_map(em);
2159 }
2160
2161 while (1) {
2162 em = btrfs_get_extent_fiemap(inode, NULL, 0, start, len, 0);
2163 if (IS_ERR(em)) {
Jeff Liu6af021d2012-02-09 14:25:50 +08002164 ret = PTR_ERR(em);
Josef Bacikb2675152011-07-18 13:21:36 -04002165 break;
2166 }
2167
2168 if (em->block_start == EXTENT_MAP_HOLE) {
2169 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2170 if (last_end <= orig_start) {
2171 free_extent_map(em);
2172 ret = -ENXIO;
2173 break;
2174 }
2175 }
2176
2177 if (origin == SEEK_HOLE) {
2178 *offset = start;
2179 free_extent_map(em);
2180 break;
2181 }
2182 } else {
2183 if (origin == SEEK_DATA) {
2184 if (em->block_start == EXTENT_MAP_DELALLOC) {
2185 if (start >= inode->i_size) {
2186 free_extent_map(em);
2187 ret = -ENXIO;
2188 break;
2189 }
2190 }
2191
2192 *offset = start;
2193 free_extent_map(em);
2194 break;
2195 }
2196 }
2197
2198 start = em->start + em->len;
2199 last_end = em->start + em->len;
2200
2201 if (em->block_start == EXTENT_MAP_DELALLOC)
2202 last_end = min_t(u64, last_end, inode->i_size);
2203
2204 if (test_bit(EXTENT_FLAG_VACANCY, &em->flags)) {
2205 free_extent_map(em);
2206 ret = -ENXIO;
2207 break;
2208 }
2209 free_extent_map(em);
2210 cond_resched();
2211 }
2212 if (!ret)
2213 *offset = min(*offset, inode->i_size);
2214out:
2215 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
2216 &cached_state, GFP_NOFS);
2217 return ret;
2218}
2219
2220static loff_t btrfs_file_llseek(struct file *file, loff_t offset, int origin)
2221{
2222 struct inode *inode = file->f_mapping->host;
2223 int ret;
2224
2225 mutex_lock(&inode->i_mutex);
2226 switch (origin) {
2227 case SEEK_END:
2228 case SEEK_CUR:
Andi Kleenef3d0fd2011-09-15 16:06:48 -07002229 offset = generic_file_llseek(file, offset, origin);
Josef Bacikb2675152011-07-18 13:21:36 -04002230 goto out;
2231 case SEEK_DATA:
2232 case SEEK_HOLE:
Jeff Liu48802c82011-09-18 10:34:02 -04002233 if (offset >= i_size_read(inode)) {
2234 mutex_unlock(&inode->i_mutex);
2235 return -ENXIO;
2236 }
2237
Josef Bacikb2675152011-07-18 13:21:36 -04002238 ret = find_desired_extent(inode, &offset, origin);
2239 if (ret) {
2240 mutex_unlock(&inode->i_mutex);
2241 return ret;
2242 }
2243 }
2244
Dan Carpenter9a4327c2011-08-18 10:16:05 -04002245 if (offset < 0 && !(file->f_mode & FMODE_UNSIGNED_OFFSET)) {
Jeff Liu48802c82011-09-18 10:34:02 -04002246 offset = -EINVAL;
Dan Carpenter9a4327c2011-08-18 10:16:05 -04002247 goto out;
2248 }
2249 if (offset > inode->i_sb->s_maxbytes) {
Jeff Liu48802c82011-09-18 10:34:02 -04002250 offset = -EINVAL;
Dan Carpenter9a4327c2011-08-18 10:16:05 -04002251 goto out;
2252 }
Josef Bacikb2675152011-07-18 13:21:36 -04002253
2254 /* Special lock needed here? */
2255 if (offset != file->f_pos) {
2256 file->f_pos = offset;
2257 file->f_version = 0;
2258 }
2259out:
2260 mutex_unlock(&inode->i_mutex);
2261 return offset;
2262}
2263
Alexey Dobriyan828c0952009-10-01 15:43:56 -07002264const struct file_operations btrfs_file_operations = {
Josef Bacikb2675152011-07-18 13:21:36 -04002265 .llseek = btrfs_file_llseek,
Chris Mason39279cc2007-06-12 06:35:45 -04002266 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00002267 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04002268 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05002269 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04002270 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04002271 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04002272 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04002273 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04002274 .fsync = btrfs_sync_file,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01002275 .fallocate = btrfs_fallocate,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002276 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002277#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002278 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002279#endif
2280};