blob: f447b783bb84ce25af44cb2276b3d49684948166 [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"
Chris Mason39279cc2007-06-12 06:35:45 -040042
43
Chris Masond352ac62008-09-29 15:18:18 -040044/* simple helper to fault in pages and copy. This should go away
45 * and be replaced with calls into generic code.
46 */
Chris Masond3977122009-01-05 21:25:51 -050047static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
Chris Masona1b32a52008-09-05 16:09:51 -040048 int write_bytes,
49 struct page **prepared_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -040050 struct iov_iter *i)
Chris Mason39279cc2007-06-12 06:35:45 -040051{
Xin Zhong914ee292010-12-09 09:30:14 +000052 size_t copied = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -040053 int pg = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040054 int offset = pos & (PAGE_CACHE_SIZE - 1);
Xin Zhong914ee292010-12-09 09:30:14 +000055 int total_copied = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040056
Josef Bacik11c65dc2010-05-23 11:07:21 -040057 while (write_bytes > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -040058 size_t count = min_t(size_t,
59 PAGE_CACHE_SIZE - offset, write_bytes);
Josef Bacik11c65dc2010-05-23 11:07:21 -040060 struct page *page = prepared_pages[pg];
Xin Zhong914ee292010-12-09 09:30:14 +000061 /*
62 * Copy data from userspace to the current page
63 *
64 * Disable pagefault to avoid recursive lock since
65 * the pages are already locked
66 */
67 pagefault_disable();
68 copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
69 pagefault_enable();
Josef Bacik11c65dc2010-05-23 11:07:21 -040070
Chris Mason39279cc2007-06-12 06:35:45 -040071 /* Flush processor's dcache for this page */
72 flush_dcache_page(page);
Chris Mason31339ac2011-03-07 11:10:24 -050073
74 /*
75 * if we get a partial write, we can end up with
76 * partially up to date pages. These add
77 * a lot of complexity, so make sure they don't
78 * happen by forcing this copy to be retried.
79 *
80 * The rest of the btrfs_file_write code will fall
81 * back to page at a time copies after we return 0.
82 */
83 if (!PageUptodate(page) && copied < count)
84 copied = 0;
85
Josef Bacik11c65dc2010-05-23 11:07:21 -040086 iov_iter_advance(i, copied);
87 write_bytes -= copied;
Xin Zhong914ee292010-12-09 09:30:14 +000088 total_copied += copied;
Chris Mason39279cc2007-06-12 06:35:45 -040089
Xin Zhong914ee292010-12-09 09:30:14 +000090 /* Return to btrfs_file_aio_write to fault page */
Josef Bacik11c65dc2010-05-23 11:07:21 -040091 if (unlikely(copied == 0)) {
Xin Zhong914ee292010-12-09 09:30:14 +000092 break;
Josef Bacik11c65dc2010-05-23 11:07:21 -040093 }
94
95 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
96 offset += copied;
97 } else {
98 pg++;
99 offset = 0;
100 }
Chris Mason39279cc2007-06-12 06:35:45 -0400101 }
Xin Zhong914ee292010-12-09 09:30:14 +0000102 return total_copied;
Chris Mason39279cc2007-06-12 06:35:45 -0400103}
104
Chris Masond352ac62008-09-29 15:18:18 -0400105/*
106 * unlocks pages after btrfs_file_write is done with them
107 */
Chris Masond3977122009-01-05 21:25:51 -0500108static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -0400109{
110 size_t i;
111 for (i = 0; i < num_pages; i++) {
112 if (!pages[i])
113 break;
Chris Masond352ac62008-09-29 15:18:18 -0400114 /* page checked is some magic around finding pages that
115 * have been modified without going through btrfs_set_page_dirty
116 * clear it here
117 */
Chris Mason4a096752008-07-21 10:29:44 -0400118 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400119 unlock_page(pages[i]);
120 mark_page_accessed(pages[i]);
121 page_cache_release(pages[i]);
122 }
123}
124
Chris Masond352ac62008-09-29 15:18:18 -0400125/*
126 * after copy_from_user, pages need to be dirtied and we need to make
127 * sure holes are created between the current EOF and the start of
128 * any next extents (if required).
129 *
130 * this also makes the decision about creating an inline extent vs
131 * doing real data extents, marking pages dirty and delalloc as required.
132 */
Chris Masond3977122009-01-05 21:25:51 -0500133static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400134 struct btrfs_root *root,
135 struct file *file,
136 struct page **pages,
137 size_t num_pages,
138 loff_t pos,
139 size_t write_bytes)
140{
Chris Mason39279cc2007-06-12 06:35:45 -0400141 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400142 int i;
Chris Mason6da6aba2007-12-18 16:15:09 -0500143 struct inode *inode = fdentry(file)->d_inode;
Chris Masondb945352007-10-15 16:15:53 -0400144 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400145 u64 start_pos;
146 u64 end_of_last_block;
147 u64 end_pos = pos + write_bytes;
148 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400149
Chris Mason5f39d392007-10-15 16:14:19 -0400150 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400151 num_bytes = (write_bytes + pos - start_pos +
152 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400153
Chris Masondb945352007-10-15 16:15:53 -0400154 end_of_last_block = start_pos + num_bytes - 1;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000155 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
156 NULL);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400157 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400158
Chris Masonc8b97812008-10-29 14:49:59 -0400159 for (i = 0; i < num_pages; i++) {
160 struct page *p = pages[i];
161 SetPageUptodate(p);
162 ClearPageChecked(p);
163 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400164 }
165 if (end_pos > isize) {
166 i_size_write(inode, end_pos);
Chris Masonf597bb12009-06-27 21:06:22 -0400167 /* we've only changed i_size in ram, and we haven't updated
168 * the disk i_size. There is no need to log the inode
169 * at this time.
170 */
Chris Mason39279cc2007-06-12 06:35:45 -0400171 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400172 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400173}
174
Chris Masond352ac62008-09-29 15:18:18 -0400175/*
176 * this drops all the extents in the cache that intersect the range
177 * [start, end]. Existing extents are split as required.
178 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400179int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
180 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400181{
182 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400183 struct extent_map *split = NULL;
184 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400185 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500186 u64 len = end - start + 1;
Chris Mason3b951512008-04-17 11:29:12 -0400187 int ret;
188 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400189 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400190 int compressed = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400191
Chris Masone6dcd2d2008-07-17 12:53:50 -0400192 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400193 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500194 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400195 testend = 0;
196 }
Chris Masond3977122009-01-05 21:25:51 -0500197 while (1) {
Chris Mason3b951512008-04-17 11:29:12 -0400198 if (!split)
199 split = alloc_extent_map(GFP_NOFS);
200 if (!split2)
201 split2 = alloc_extent_map(GFP_NOFS);
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000202 BUG_ON(!split || !split2);
Chris Mason3b951512008-04-17 11:29:12 -0400203
Chris Mason890871b2009-09-02 16:24:52 -0400204 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500205 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500206 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400207 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400208 break;
Chris Masond1310b22008-01-24 16:13:08 -0500209 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400210 flags = em->flags;
211 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000212 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400213 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400214 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400215 break;
216 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000217 start = em->start + em->len;
218 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400219 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400220 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400221 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400222 continue;
223 }
Chris Masonc8b97812008-10-29 14:49:59 -0400224 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400225 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400226 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400227
228 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
229 em->start < start) {
230 split->start = em->start;
231 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500232 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400233 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400234
235 if (compressed)
236 split->block_len = em->block_len;
237 else
238 split->block_len = split->len;
239
Chris Mason3b951512008-04-17 11:29:12 -0400240 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400241 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800242 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400243 ret = add_extent_mapping(em_tree, split);
244 BUG_ON(ret);
245 free_extent_map(split);
246 split = split2;
247 split2 = NULL;
248 }
249 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
250 testend && em->start + em->len > start + len) {
251 u64 diff = start + len - em->start;
252
253 split->start = start + len;
254 split->len = em->start + em->len - (start + len);
255 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400256 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800257 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400258
Chris Masonc8b97812008-10-29 14:49:59 -0400259 if (compressed) {
260 split->block_len = em->block_len;
261 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500262 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400263 } else {
264 split->block_len = split->len;
265 split->block_start = em->block_start + diff;
Chris Mason445a6942008-11-10 11:53:33 -0500266 split->orig_start = split->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400267 }
Chris Mason3b951512008-04-17 11:29:12 -0400268
269 ret = add_extent_mapping(em_tree, split);
270 BUG_ON(ret);
271 free_extent_map(split);
272 split = NULL;
273 }
Chris Mason890871b2009-09-02 16:24:52 -0400274 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500275
Chris Masona52d9a82007-08-27 16:49:44 -0400276 /* once for us */
277 free_extent_map(em);
278 /* once for the tree*/
279 free_extent_map(em);
280 }
Chris Mason3b951512008-04-17 11:29:12 -0400281 if (split)
282 free_extent_map(split);
283 if (split2)
284 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400285 return 0;
286}
287
Chris Mason39279cc2007-06-12 06:35:45 -0400288/*
289 * this is very complex, but the basic idea is to drop all extents
290 * in the range start - end. hint_block is filled in with a block number
291 * that would be a good hint to the block allocator for this file.
292 *
293 * If an extent intersects the range but is not entirely inside the range
294 * it is either truncated or split. Anything entirely inside the range
295 * is deleted from the tree.
296 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000297int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
298 u64 start, u64 end, u64 *hint_byte, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400299{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000300 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason00f5c792007-11-30 10:09:33 -0500301 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000302 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500303 struct btrfs_path *path;
304 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000305 struct btrfs_key new_key;
306 u64 search_start = start;
307 u64 disk_bytenr = 0;
308 u64 num_bytes = 0;
309 u64 extent_offset = 0;
310 u64 extent_end = 0;
311 int del_nr = 0;
312 int del_slot = 0;
313 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400314 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500315 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400316
Chris Masona1ed8352009-09-11 12:27:37 -0400317 if (drop_cache)
318 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400319
Chris Mason39279cc2007-06-12 06:35:45 -0400320 path = btrfs_alloc_path();
321 if (!path)
322 return -ENOMEM;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000323
Chris Masond3977122009-01-05 21:25:51 -0500324 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400325 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400326 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
327 search_start, -1);
328 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000329 break;
330 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
331 leaf = path->nodes[0];
332 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
333 if (key.objectid == inode->i_ino &&
334 key.type == BTRFS_EXTENT_DATA_KEY)
335 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400336 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400337 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000338next_slot:
339 leaf = path->nodes[0];
340 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
341 BUG_ON(del_nr > 0);
342 ret = btrfs_next_leaf(root, path);
343 if (ret < 0)
344 break;
345 if (ret > 0) {
346 ret = 0;
347 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400348 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000349 leaf = path->nodes[0];
350 recow = 1;
351 }
352
353 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
354 if (key.objectid > inode->i_ino ||
355 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
356 break;
357
358 fi = btrfs_item_ptr(leaf, path->slots[0],
359 struct btrfs_file_extent_item);
360 extent_type = btrfs_file_extent_type(leaf, fi);
361
362 if (extent_type == BTRFS_FILE_EXTENT_REG ||
363 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
364 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
365 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
366 extent_offset = btrfs_file_extent_offset(leaf, fi);
367 extent_end = key.offset +
368 btrfs_file_extent_num_bytes(leaf, fi);
369 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
370 extent_end = key.offset +
371 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400372 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000373 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400374 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400375 }
376
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000377 if (extent_end <= search_start) {
378 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400379 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400380 }
381
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000382 search_start = max(key.offset, start);
383 if (recow) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400384 btrfs_release_path(root, path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000385 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400386 }
Chris Mason771ed682008-11-06 22:02:51 -0500387
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000388 /*
389 * | - range to drop - |
390 * | -------- extent -------- |
391 */
392 if (start > key.offset && end < extent_end) {
393 BUG_ON(del_nr > 0);
394 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
395
396 memcpy(&new_key, &key, sizeof(new_key));
397 new_key.offset = start;
398 ret = btrfs_duplicate_item(trans, root, path,
399 &new_key);
400 if (ret == -EAGAIN) {
401 btrfs_release_path(root, path);
402 continue;
403 }
404 if (ret < 0)
405 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400406
Chris Mason5f39d392007-10-15 16:14:19 -0400407 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000408 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
409 struct btrfs_file_extent_item);
410 btrfs_set_file_extent_num_bytes(leaf, fi,
411 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400412
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000413 fi = btrfs_item_ptr(leaf, path->slots[0],
414 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400415
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000416 extent_offset += start - key.offset;
417 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
418 btrfs_set_file_extent_num_bytes(leaf, fi,
419 extent_end - start);
420 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400421
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000422 if (disk_bytenr > 0) {
423 ret = btrfs_inc_extent_ref(trans, root,
424 disk_bytenr, num_bytes, 0,
425 root->root_key.objectid,
426 new_key.objectid,
427 start - extent_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -0400428 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000429 *hint_byte = disk_bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400430 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000431 key.offset = start;
432 }
433 /*
434 * | ---- range to drop ----- |
435 * | -------- extent -------- |
436 */
437 if (start <= key.offset && end < extent_end) {
438 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
439
440 memcpy(&new_key, &key, sizeof(new_key));
441 new_key.offset = end;
442 btrfs_set_item_key_safe(trans, root, path, &new_key);
443
444 extent_offset += end - key.offset;
445 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
446 btrfs_set_file_extent_num_bytes(leaf, fi,
447 extent_end - end);
448 btrfs_mark_buffer_dirty(leaf);
449 if (disk_bytenr > 0) {
450 inode_sub_bytes(inode, end - key.offset);
451 *hint_byte = disk_bytenr;
452 }
453 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400454 }
455
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000456 search_start = extent_end;
457 /*
458 * | ---- range to drop ----- |
459 * | -------- extent -------- |
460 */
461 if (start > key.offset && end >= extent_end) {
462 BUG_ON(del_nr > 0);
463 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
464
465 btrfs_set_file_extent_num_bytes(leaf, fi,
466 start - key.offset);
467 btrfs_mark_buffer_dirty(leaf);
468 if (disk_bytenr > 0) {
469 inode_sub_bytes(inode, extent_end - start);
470 *hint_byte = disk_bytenr;
471 }
472 if (end == extent_end)
473 break;
474
475 path->slots[0]++;
476 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400477 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000478
479 /*
480 * | ---- range to drop ----- |
481 * | ------ extent ------ |
482 */
483 if (start <= key.offset && end >= extent_end) {
484 if (del_nr == 0) {
485 del_slot = path->slots[0];
486 del_nr = 1;
487 } else {
488 BUG_ON(del_slot + del_nr != path->slots[0]);
489 del_nr++;
490 }
491
492 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
493 inode_sub_bytes(inode,
494 extent_end - key.offset);
495 extent_end = ALIGN(extent_end,
496 root->sectorsize);
497 } else if (disk_bytenr > 0) {
498 ret = btrfs_free_extent(trans, root,
499 disk_bytenr, num_bytes, 0,
500 root->root_key.objectid,
501 key.objectid, key.offset -
502 extent_offset);
503 BUG_ON(ret);
504 inode_sub_bytes(inode,
505 extent_end - key.offset);
506 *hint_byte = disk_bytenr;
507 }
508
509 if (end == extent_end)
510 break;
511
512 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
513 path->slots[0]++;
514 goto next_slot;
515 }
516
517 ret = btrfs_del_items(trans, root, path, del_slot,
518 del_nr);
519 BUG_ON(ret);
520
521 del_nr = 0;
522 del_slot = 0;
523
524 btrfs_release_path(root, path);
525 continue;
526 }
527
528 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400529 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000530
531 if (del_nr > 0) {
532 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
533 BUG_ON(ret);
534 }
535
Chris Mason39279cc2007-06-12 06:35:45 -0400536 btrfs_free_path(path);
537 return ret;
538}
539
Yan Zhengd899e052008-10-30 14:25:28 -0400540static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000541 u64 objectid, u64 bytenr, u64 orig_offset,
542 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400543{
544 struct btrfs_file_extent_item *fi;
545 struct btrfs_key key;
546 u64 extent_end;
547
548 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
549 return 0;
550
551 btrfs_item_key_to_cpu(leaf, &key, slot);
552 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
553 return 0;
554
555 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
556 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
557 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000558 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400559 btrfs_file_extent_compression(leaf, fi) ||
560 btrfs_file_extent_encryption(leaf, fi) ||
561 btrfs_file_extent_other_encoding(leaf, fi))
562 return 0;
563
564 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
565 if ((*start && *start != key.offset) || (*end && *end != extent_end))
566 return 0;
567
568 *start = key.offset;
569 *end = extent_end;
570 return 1;
571}
572
573/*
574 * Mark extent in the range start - end as written.
575 *
576 * This changes extent type from 'pre-allocated' to 'regular'. If only
577 * part of extent is marked as written, the extent will be split into
578 * two or three.
579 */
580int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400581 struct inode *inode, u64 start, u64 end)
582{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000583 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400584 struct extent_buffer *leaf;
585 struct btrfs_path *path;
586 struct btrfs_file_extent_item *fi;
587 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000588 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400589 u64 bytenr;
590 u64 num_bytes;
591 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400592 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400593 u64 other_start;
594 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000595 u64 split;
596 int del_nr = 0;
597 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000598 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400599 int ret;
600
601 btrfs_drop_extent_cache(inode, start, end - 1, 0);
602
603 path = btrfs_alloc_path();
604 BUG_ON(!path);
605again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000606 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000607 split = start;
Yan Zhengd899e052008-10-30 14:25:28 -0400608 key.objectid = inode->i_ino;
609 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000610 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -0400611
612 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
613 if (ret > 0 && path->slots[0] > 0)
614 path->slots[0]--;
615
616 leaf = path->nodes[0];
617 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
618 BUG_ON(key.objectid != inode->i_ino ||
619 key.type != BTRFS_EXTENT_DATA_KEY);
620 fi = btrfs_item_ptr(leaf, path->slots[0],
621 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000622 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
623 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -0400624 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
625 BUG_ON(key.offset > start || extent_end < end);
626
627 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
628 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400629 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000630 memcpy(&new_key, &key, sizeof(new_key));
631
632 if (start == key.offset && end < extent_end) {
633 other_start = 0;
634 other_end = start;
635 if (extent_mergeable(leaf, path->slots[0] - 1,
636 inode->i_ino, bytenr, orig_offset,
637 &other_start, &other_end)) {
638 new_key.offset = end;
639 btrfs_set_item_key_safe(trans, root, path, &new_key);
640 fi = btrfs_item_ptr(leaf, path->slots[0],
641 struct btrfs_file_extent_item);
642 btrfs_set_file_extent_num_bytes(leaf, fi,
643 extent_end - end);
644 btrfs_set_file_extent_offset(leaf, fi,
645 end - orig_offset);
646 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
647 struct btrfs_file_extent_item);
648 btrfs_set_file_extent_num_bytes(leaf, fi,
649 end - other_start);
650 btrfs_mark_buffer_dirty(leaf);
651 goto out;
652 }
653 }
654
655 if (start > key.offset && end == extent_end) {
656 other_start = end;
657 other_end = 0;
658 if (extent_mergeable(leaf, path->slots[0] + 1,
659 inode->i_ino, bytenr, orig_offset,
660 &other_start, &other_end)) {
661 fi = btrfs_item_ptr(leaf, path->slots[0],
662 struct btrfs_file_extent_item);
663 btrfs_set_file_extent_num_bytes(leaf, fi,
664 start - key.offset);
665 path->slots[0]++;
666 new_key.offset = start;
667 btrfs_set_item_key_safe(trans, root, path, &new_key);
668
669 fi = btrfs_item_ptr(leaf, path->slots[0],
670 struct btrfs_file_extent_item);
671 btrfs_set_file_extent_num_bytes(leaf, fi,
672 other_end - start);
673 btrfs_set_file_extent_offset(leaf, fi,
674 start - orig_offset);
675 btrfs_mark_buffer_dirty(leaf);
676 goto out;
677 }
678 }
Yan Zhengd899e052008-10-30 14:25:28 -0400679
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000680 while (start > key.offset || end < extent_end) {
681 if (key.offset == start)
682 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400683
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000684 new_key.offset = split;
685 ret = btrfs_duplicate_item(trans, root, path, &new_key);
686 if (ret == -EAGAIN) {
687 btrfs_release_path(root, path);
688 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -0400689 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000690 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -0400691
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000692 leaf = path->nodes[0];
693 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -0400694 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -0400695 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000696 split - key.offset);
697
698 fi = btrfs_item_ptr(leaf, path->slots[0],
699 struct btrfs_file_extent_item);
700
701 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
702 btrfs_set_file_extent_num_bytes(leaf, fi,
703 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -0400704 btrfs_mark_buffer_dirty(leaf);
705
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000706 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
707 root->root_key.objectid,
708 inode->i_ino, orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400709 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -0400710
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000711 if (split == start) {
712 key.offset = start;
713 } else {
714 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400715 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000716 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400717 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000718 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -0400719 }
720
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000721 other_start = end;
722 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000723 if (extent_mergeable(leaf, path->slots[0] + 1,
724 inode->i_ino, bytenr, orig_offset,
725 &other_start, &other_end)) {
726 if (recow) {
727 btrfs_release_path(root, path);
728 goto again;
729 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000730 extent_end = other_end;
731 del_slot = path->slots[0] + 1;
732 del_nr++;
733 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
734 0, root->root_key.objectid,
735 inode->i_ino, orig_offset);
736 BUG_ON(ret);
737 }
738 other_start = 0;
739 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000740 if (extent_mergeable(leaf, path->slots[0] - 1,
741 inode->i_ino, bytenr, orig_offset,
742 &other_start, &other_end)) {
743 if (recow) {
744 btrfs_release_path(root, path);
745 goto again;
746 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000747 key.offset = other_start;
748 del_slot = path->slots[0];
749 del_nr++;
750 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
751 0, root->root_key.objectid,
752 inode->i_ino, orig_offset);
753 BUG_ON(ret);
754 }
755 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000756 fi = btrfs_item_ptr(leaf, path->slots[0],
757 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000758 btrfs_set_file_extent_type(leaf, fi,
759 BTRFS_FILE_EXTENT_REG);
760 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000761 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000762 fi = btrfs_item_ptr(leaf, del_slot - 1,
763 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000764 btrfs_set_file_extent_type(leaf, fi,
765 BTRFS_FILE_EXTENT_REG);
766 btrfs_set_file_extent_num_bytes(leaf, fi,
767 extent_end - key.offset);
768 btrfs_mark_buffer_dirty(leaf);
769
770 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
771 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000772 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000773out:
Yan Zhengd899e052008-10-30 14:25:28 -0400774 btrfs_free_path(path);
775 return 0;
776}
777
Chris Mason39279cc2007-06-12 06:35:45 -0400778/*
Chris Masonb1bf8622011-02-28 09:52:08 -0500779 * on error we return an unlocked page and the error value
780 * on success we return a locked page and 0
781 */
782static int prepare_uptodate_page(struct page *page, u64 pos)
783{
784 int ret = 0;
785
786 if ((pos & (PAGE_CACHE_SIZE - 1)) && !PageUptodate(page)) {
787 ret = btrfs_readpage(NULL, page);
788 if (ret)
789 return ret;
790 lock_page(page);
791 if (!PageUptodate(page)) {
792 unlock_page(page);
793 return -EIO;
794 }
795 }
796 return 0;
797}
798
799/*
Chris Masond352ac62008-09-29 15:18:18 -0400800 * this gets pages into the page cache and locks them down, it also properly
801 * waits for data=ordered extents to finish before allowing the pages to be
802 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -0400803 */
Chris Masond3977122009-01-05 21:25:51 -0500804static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500805 struct page **pages, size_t num_pages,
806 loff_t pos, unsigned long first_index,
807 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400808{
Josef Bacik2ac55d42010-02-03 19:33:23 +0000809 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -0400810 int i;
811 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500812 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400813 int err = 0;
Chris Masonb1bf8622011-02-28 09:52:08 -0500814 int faili = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400815 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400816 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400817
Chris Mason5f39d392007-10-15 16:14:19 -0400818 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400819 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400820
Yan Zheng9036c102008-10-30 14:19:41 -0400821 if (start_pos > inode->i_size) {
822 err = btrfs_cont_expand(inode, start_pos);
823 if (err)
824 return err;
825 }
826
Chris Mason39279cc2007-06-12 06:35:45 -0400827 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400828again:
Chris Mason39279cc2007-06-12 06:35:45 -0400829 for (i = 0; i < num_pages; i++) {
830 pages[i] = grab_cache_page(inode->i_mapping, index + i);
831 if (!pages[i]) {
Chris Masonb1bf8622011-02-28 09:52:08 -0500832 faili = i - 1;
833 err = -ENOMEM;
834 goto fail;
835 }
836
837 if (i == 0)
838 err = prepare_uptodate_page(pages[i], pos);
839 if (i == num_pages - 1)
840 err = prepare_uptodate_page(pages[i],
841 pos + write_bytes);
842 if (err) {
843 page_cache_release(pages[i]);
844 faili = i - 1;
845 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -0400846 }
Chris Masonccd467d2007-06-28 15:57:36 -0400847 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400848 }
Chris Masonb1bf8622011-02-28 09:52:08 -0500849 err = 0;
Chris Mason07627042008-02-19 11:29:24 -0500850 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400851 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000852 lock_extent_bits(&BTRFS_I(inode)->io_tree,
853 start_pos, last_pos - 1, 0, &cached_state,
854 GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500855 ordered = btrfs_lookup_first_ordered_extent(inode,
856 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400857 if (ordered &&
858 ordered->file_offset + ordered->len > start_pos &&
859 ordered->file_offset < last_pos) {
860 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000861 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
862 start_pos, last_pos - 1,
863 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400864 for (i = 0; i < num_pages; i++) {
865 unlock_page(pages[i]);
866 page_cache_release(pages[i]);
867 }
868 btrfs_wait_ordered_range(inode, start_pos,
869 last_pos - start_pos);
870 goto again;
871 }
872 if (ordered)
873 btrfs_put_ordered_extent(ordered);
874
Josef Bacik2ac55d42010-02-03 19:33:23 +0000875 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -0400876 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +0000877 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
Chris Mason07627042008-02-19 11:29:24 -0500878 GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000879 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
880 start_pos, last_pos - 1, &cached_state,
881 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500882 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400883 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400884 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400885 set_page_extent_mapped(pages[i]);
886 WARN_ON(!PageLocked(pages[i]));
887 }
Chris Mason39279cc2007-06-12 06:35:45 -0400888 return 0;
Chris Masonb1bf8622011-02-28 09:52:08 -0500889fail:
890 while (faili >= 0) {
891 unlock_page(pages[faili]);
892 page_cache_release(pages[faili]);
893 faili--;
894 }
895 return err;
896
Chris Mason39279cc2007-06-12 06:35:45 -0400897}
898
Josef Bacik11c65dc2010-05-23 11:07:21 -0400899static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
900 const struct iovec *iov,
901 unsigned long nr_segs, loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400902{
Josef Bacik11c65dc2010-05-23 11:07:21 -0400903 struct file *file = iocb->ki_filp;
904 struct inode *inode = fdentry(file)->d_inode;
905 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400906 struct page **pages = NULL;
907 struct iov_iter i;
908 loff_t *ppos = &iocb->ki_pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400909 loff_t start_pos;
910 ssize_t num_written = 0;
911 ssize_t err = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400912 size_t count;
913 size_t ocount;
Chris Mason39279cc2007-06-12 06:35:45 -0400914 int ret = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400915 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400916 unsigned long first_index;
917 unsigned long last_index;
Chris Masoncb843a62008-10-03 12:30:02 -0400918 int will_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400919 int buffered = 0;
Xin Zhong914ee292010-12-09 09:30:14 +0000920 int copied = 0;
921 int dirty_pages = 0;
Chris Masoncb843a62008-10-03 12:30:02 -0400922
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100923 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
Chris Masoncb843a62008-10-03 12:30:02 -0400924 (file->f_flags & O_DIRECT));
Chris Mason8c2383c2007-06-18 09:57:58 -0400925
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400926 start_pos = pos;
927
Chris Mason39279cc2007-06-12 06:35:45 -0400928 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
Chris Mason2b1f55b2008-09-24 11:48:04 -0400929
Chris Masonab93dbe2009-10-01 12:29:10 -0400930 mutex_lock(&inode->i_mutex);
931
Josef Bacik11c65dc2010-05-23 11:07:21 -0400932 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
933 if (err)
934 goto out;
935 count = ocount;
936
Chris Mason1832a6d2007-12-21 16:27:21 -0500937 current->backing_dev_info = inode->i_mapping->backing_dev_info;
Chris Mason39279cc2007-06-12 06:35:45 -0400938 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
Chris Mason1832a6d2007-12-21 16:27:21 -0500939 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400940 goto out;
941
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -0400942 if (count == 0)
Chris Masonab93dbe2009-10-01 12:29:10 -0400943 goto out;
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400944
945 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400946 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400947 goto out;
948
liuboacce9522011-01-06 19:30:25 +0800949 /*
950 * If BTRFS flips readonly due to some impossible error
951 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
952 * although we have opened a file as writable, we have
953 * to stop this write operation to ensure FS consistency.
954 */
955 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
956 err = -EROFS;
957 goto out;
958 }
959
Chris Mason39279cc2007-06-12 06:35:45 -0400960 file_update_time(file);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400961 BTRFS_I(inode)->sequence++;
Chris Mason39279cc2007-06-12 06:35:45 -0400962
Josef Bacik4b46fce2010-05-23 11:00:55 -0400963 if (unlikely(file->f_flags & O_DIRECT)) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400964 num_written = generic_file_direct_write(iocb, iov, &nr_segs,
965 pos, ppos, count,
966 ocount);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400967 /*
968 * the generic O_DIRECT will update in-memory i_size after the
969 * DIOs are done. But our endio handlers that update the on
970 * disk i_size never update past the in memory i_size. So we
971 * need one more update here to catch any additions to the
972 * file
973 */
974 if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
975 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
976 mark_inode_dirty(inode);
977 }
978
979 if (num_written < 0) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400980 ret = num_written;
981 num_written = 0;
982 goto out;
983 } else if (num_written == count) {
984 /* pick up pos changes done by the generic code */
985 pos = *ppos;
986 goto out;
987 }
Josef Bacik4b46fce2010-05-23 11:00:55 -0400988 /*
989 * We are going to do buffered for the rest of the range, so we
990 * need to make sure to invalidate the buffered pages when we're
991 * done.
992 */
993 buffered = 1;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400994 pos += num_written;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400995 }
996
Josef Bacik11c65dc2010-05-23 11:07:21 -0400997 iov_iter_init(&i, iov, nr_segs, count, num_written);
998 nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
999 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
1000 (sizeof(struct page *)));
Chris Mason8c2383c2007-06-18 09:57:58 -04001001 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
liubo2a29edc2011-01-26 06:22:08 +00001002 if (!pages) {
1003 ret = -ENOMEM;
1004 goto out;
1005 }
Chris Mason39279cc2007-06-12 06:35:45 -04001006
Chris Masonab93dbe2009-10-01 12:29:10 -04001007 /* generic_write_checks can change our pos */
1008 start_pos = pos;
1009
Chris Mason39279cc2007-06-12 06:35:45 -04001010 first_index = pos >> PAGE_CACHE_SHIFT;
Josef Bacik11c65dc2010-05-23 11:07:21 -04001011 last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001012
Josef Bacik11c65dc2010-05-23 11:07:21 -04001013 while (iov_iter_count(&i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -04001014 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacik11c65dc2010-05-23 11:07:21 -04001015 size_t write_bytes = min(iov_iter_count(&i),
1016 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -04001017 offset);
Yan, Zheng3a909832011-01-18 13:34:40 +08001018 size_t num_pages = (write_bytes + offset +
1019 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001020
Chris Mason8c2383c2007-06-18 09:57:58 -04001021 WARN_ON(num_pages > nrptrs);
yanhai zhu9aead432009-01-05 15:49:11 -05001022 memset(pages, 0, sizeof(struct page *) * nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -05001023
Xin Zhong914ee292010-12-09 09:30:14 +00001024 /*
1025 * Fault pages before locking them in prepare_pages
1026 * to avoid recursive lock
1027 */
1028 if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) {
1029 ret = -EFAULT;
1030 goto out;
1031 }
1032
1033 ret = btrfs_delalloc_reserve_space(inode,
1034 num_pages << PAGE_CACHE_SHIFT);
Chris Mason1832a6d2007-12-21 16:27:21 -05001035 if (ret)
1036 goto out;
1037
Chris Mason39279cc2007-06-12 06:35:45 -04001038 ret = prepare_pages(root, file, pages, num_pages,
1039 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -04001040 write_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001041 if (ret) {
Xin Zhong914ee292010-12-09 09:30:14 +00001042 btrfs_delalloc_release_space(inode,
1043 num_pages << PAGE_CACHE_SHIFT);
Chris Mason54aa1f42007-06-22 14:16:25 -04001044 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -05001045 }
Chris Mason39279cc2007-06-12 06:35:45 -04001046
Xin Zhong914ee292010-12-09 09:30:14 +00001047 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001048 write_bytes, pages, &i);
Chris Masonb1bf8622011-02-28 09:52:08 -05001049
1050 /*
1051 * if we have trouble faulting in the pages, fall
1052 * back to one page at a time
1053 */
1054 if (copied < write_bytes)
1055 nrptrs = 1;
1056
1057 if (copied == 0)
1058 dirty_pages = 0;
1059 else
1060 dirty_pages = (copied + offset +
1061 PAGE_CACHE_SIZE - 1) >>
1062 PAGE_CACHE_SHIFT;
Xin Zhong914ee292010-12-09 09:30:14 +00001063
1064 if (num_pages > dirty_pages) {
1065 if (copied > 0)
1066 atomic_inc(
1067 &BTRFS_I(inode)->outstanding_extents);
1068 btrfs_delalloc_release_space(inode,
1069 (num_pages - dirty_pages) <<
1070 PAGE_CACHE_SHIFT);
1071 }
1072
1073 if (copied > 0) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001074 dirty_and_release_pages(NULL, root, file, pages,
Xin Zhong914ee292010-12-09 09:30:14 +00001075 dirty_pages, pos, copied);
Chris Mason54aa1f42007-06-22 14:16:25 -04001076 }
Chris Mason39279cc2007-06-12 06:35:45 -04001077
Chris Mason39279cc2007-06-12 06:35:45 -04001078 btrfs_drop_pages(pages, num_pages);
Xin Zhong914ee292010-12-09 09:30:14 +00001079
1080 if (copied > 0) {
1081 if (will_write) {
1082 filemap_fdatawrite_range(inode->i_mapping, pos,
1083 pos + copied - 1);
1084 } else {
1085 balance_dirty_pages_ratelimited_nr(
1086 inode->i_mapping,
1087 dirty_pages);
1088 if (dirty_pages <
1089 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1090 btrfs_btree_balance_dirty(root, 1);
1091 btrfs_throttle(root);
1092 }
Josef Bacik6a632092009-02-20 11:00:09 -05001093 }
Chris Mason39279cc2007-06-12 06:35:45 -04001094
Xin Zhong914ee292010-12-09 09:30:14 +00001095 pos += copied;
1096 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001097
Chris Mason39279cc2007-06-12 06:35:45 -04001098 cond_resched();
1099 }
Chris Mason39279cc2007-06-12 06:35:45 -04001100out:
Chris Mason1832a6d2007-12-21 16:27:21 -05001101 mutex_unlock(&inode->i_mutex);
Josef Bacik6a632092009-02-20 11:00:09 -05001102 if (ret)
1103 err = ret;
Chris Mason5b92ee72008-01-03 13:46:11 -05001104
Chris Mason8c2383c2007-06-18 09:57:58 -04001105 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -04001106 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001107
Chris Mason5a3f23d2009-03-31 13:27:11 -04001108 /*
1109 * we want to make sure fsync finds this change
1110 * but we haven't joined a transaction running right now.
1111 *
1112 * Later on, someone is sure to update the inode and get the
1113 * real transid recorded.
1114 *
1115 * We set last_trans now to the fs_info generation + 1,
1116 * this will either be one more than the running transaction
1117 * or the generation used for the next transaction if there isn't
1118 * one running right now.
1119 */
1120 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1121
Chris Masoncb843a62008-10-03 12:30:02 -04001122 if (num_written > 0 && will_write) {
Chris Masone02119d2008-09-05 16:13:11 -04001123 struct btrfs_trans_handle *trans;
1124
Chris Masoncb843a62008-10-03 12:30:02 -04001125 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1126 if (err)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001127 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -04001128
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001129 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001130 trans = btrfs_start_transaction(root, 0);
Josef Bacik495e8672010-11-19 20:36:10 +00001131 if (IS_ERR(trans)) {
1132 num_written = PTR_ERR(trans);
1133 goto done;
1134 }
1135 mutex_lock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001136 ret = btrfs_log_dentry_safe(trans, root,
1137 file->f_dentry);
Josef Bacik495e8672010-11-19 20:36:10 +00001138 mutex_unlock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001139 if (ret == 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001140 ret = btrfs_sync_log(trans, root);
1141 if (ret == 0)
1142 btrfs_end_transaction(trans, root);
1143 else
1144 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001145 } else if (ret != BTRFS_NO_LOG_SYNC) {
Chris Masoncb843a62008-10-03 12:30:02 -04001146 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001147 } else {
1148 btrfs_end_transaction(trans, root);
Chris Masoncb843a62008-10-03 12:30:02 -04001149 }
Chris Masone02119d2008-09-05 16:13:11 -04001150 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04001151 if (file->f_flags & O_DIRECT && buffered) {
Chris Masoncb843a62008-10-03 12:30:02 -04001152 invalidate_mapping_pages(inode->i_mapping,
1153 start_pos >> PAGE_CACHE_SHIFT,
1154 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1155 }
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001156 }
Josef Bacik495e8672010-11-19 20:36:10 +00001157done:
Chris Mason39279cc2007-06-12 06:35:45 -04001158 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001159 return num_written ? num_written : err;
1160}
1161
Chris Masond3977122009-01-05 21:25:51 -05001162int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001163{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001164 /*
1165 * ordered_data_close is set by settattr when we are about to truncate
1166 * a file from a non-zero size to a zero size. This tries to
1167 * flush down new bytes that may have been written if the
1168 * application were using truncate to replace a file in place.
1169 */
1170 if (BTRFS_I(inode)->ordered_data_close) {
1171 BTRFS_I(inode)->ordered_data_close = 0;
1172 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1173 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1174 filemap_flush(inode->i_mapping);
1175 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001176 if (filp->private_data)
1177 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001178 return 0;
1179}
1180
Chris Masond352ac62008-09-29 15:18:18 -04001181/*
1182 * fsync call for both files and directories. This logs the inode into
1183 * the tree log instead of forcing full commits whenever possible.
1184 *
1185 * It needs to call filemap_fdatawait so that all ordered extent updates are
1186 * in the metadata btree are up to date for copying to the log.
1187 *
1188 * It drops the inode mutex before doing the tree log commit. This is an
1189 * important optimization for directories because holding the mutex prevents
1190 * new operations on the dir while we write to disk.
1191 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001192int btrfs_sync_file(struct file *file, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001193{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001194 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001195 struct inode *inode = dentry->d_inode;
1196 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001197 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001198 struct btrfs_trans_handle *trans;
1199
Chris Mason257c62e2009-10-13 13:21:08 -04001200
1201 /* we wait first, since the writeback may change the inode */
1202 root->log_batch++;
1203 /* the VFS called filemap_fdatawrite for us */
1204 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1205 root->log_batch++;
1206
Chris Mason39279cc2007-06-12 06:35:45 -04001207 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001208 * check the transaction that last modified this inode
1209 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001210 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001211 if (!BTRFS_I(inode)->last_trans)
1212 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001213
Chris Mason257c62e2009-10-13 13:21:08 -04001214 /*
1215 * if the last transaction that changed this file was before
1216 * the current transaction, we can bail out now without any
1217 * syncing
1218 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001219 mutex_lock(&root->fs_info->trans_mutex);
1220 if (BTRFS_I(inode)->last_trans <=
1221 root->fs_info->last_trans_committed) {
1222 BTRFS_I(inode)->last_trans = 0;
1223 mutex_unlock(&root->fs_info->trans_mutex);
1224 goto out;
1225 }
1226 mutex_unlock(&root->fs_info->trans_mutex);
1227
1228 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001229 * ok we haven't committed the transaction yet, lets do a commit
1230 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001231 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001232 btrfs_ioctl_trans_end(file);
1233
Yan, Zhenga22285a2010-05-16 10:48:46 -04001234 trans = btrfs_start_transaction(root, 0);
1235 if (IS_ERR(trans)) {
1236 ret = PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04001237 goto out;
1238 }
Chris Masone02119d2008-09-05 16:13:11 -04001239
Chris Mason2cfbd502009-02-20 10:55:10 -05001240 ret = btrfs_log_dentry_safe(trans, root, dentry);
Chris Masond3977122009-01-05 21:25:51 -05001241 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04001242 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001243
1244 /* we've logged all the items and now have a consistent
1245 * version of the file in the log. It is possible that
1246 * someone will come in and modify the file, but that's
1247 * fine because the log is consistent on disk, and we
1248 * have references to all of the file's extents
1249 *
1250 * It is possible that someone will come in and log the
1251 * file again, but that will end up using the synchronization
1252 * inside btrfs_sync_log to keep things safe.
1253 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001254 mutex_unlock(&dentry->d_inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001255
Chris Mason257c62e2009-10-13 13:21:08 -04001256 if (ret != BTRFS_NO_LOG_SYNC) {
1257 if (ret > 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001258 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001259 } else {
1260 ret = btrfs_sync_log(trans, root);
1261 if (ret == 0)
1262 ret = btrfs_end_transaction(trans, root);
1263 else
1264 ret = btrfs_commit_transaction(trans, root);
1265 }
1266 } else {
1267 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001268 }
Chris Mason2cfbd502009-02-20 10:55:10 -05001269 mutex_lock(&dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001270out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001271 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001272}
1273
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001274static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001275 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001276 .page_mkwrite = btrfs_page_mkwrite,
1277};
1278
1279static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1280{
Miao Xie058a4572010-05-20 07:21:50 +00001281 struct address_space *mapping = filp->f_mapping;
1282
1283 if (!mapping->a_ops->readpage)
1284 return -ENOEXEC;
1285
Chris Mason9ebefb182007-06-15 13:50:00 -04001286 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00001287 vma->vm_ops = &btrfs_file_vm_ops;
1288 vma->vm_flags |= VM_CAN_NONLINEAR;
1289
Chris Mason9ebefb182007-06-15 13:50:00 -04001290 return 0;
1291}
1292
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001293static long btrfs_fallocate(struct file *file, int mode,
1294 loff_t offset, loff_t len)
1295{
1296 struct inode *inode = file->f_path.dentry->d_inode;
1297 struct extent_state *cached_state = NULL;
1298 u64 cur_offset;
1299 u64 last_byte;
1300 u64 alloc_start;
1301 u64 alloc_end;
1302 u64 alloc_hint = 0;
1303 u64 locked_end;
1304 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
1305 struct extent_map *em;
1306 int ret;
1307
1308 alloc_start = offset & ~mask;
1309 alloc_end = (offset + len + mask) & ~mask;
1310
1311 /* We only support the FALLOC_FL_KEEP_SIZE mode */
1312 if (mode & ~FALLOC_FL_KEEP_SIZE)
1313 return -EOPNOTSUPP;
1314
1315 /*
1316 * wait for ordered IO before we have any locks. We'll loop again
1317 * below with the locks held.
1318 */
1319 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
1320
1321 mutex_lock(&inode->i_mutex);
1322 ret = inode_newsize_ok(inode, alloc_end);
1323 if (ret)
1324 goto out;
1325
1326 if (alloc_start > inode->i_size) {
1327 ret = btrfs_cont_expand(inode, alloc_start);
1328 if (ret)
1329 goto out;
1330 }
1331
1332 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
1333 if (ret)
1334 goto out;
1335
1336 locked_end = alloc_end - 1;
1337 while (1) {
1338 struct btrfs_ordered_extent *ordered;
1339
1340 /* the extent lock is ordered inside the running
1341 * transaction
1342 */
1343 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
1344 locked_end, 0, &cached_state, GFP_NOFS);
1345 ordered = btrfs_lookup_first_ordered_extent(inode,
1346 alloc_end - 1);
1347 if (ordered &&
1348 ordered->file_offset + ordered->len > alloc_start &&
1349 ordered->file_offset < alloc_end) {
1350 btrfs_put_ordered_extent(ordered);
1351 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1352 alloc_start, locked_end,
1353 &cached_state, GFP_NOFS);
1354 /*
1355 * we can't wait on the range with the transaction
1356 * running or with the extent lock held
1357 */
1358 btrfs_wait_ordered_range(inode, alloc_start,
1359 alloc_end - alloc_start);
1360 } else {
1361 if (ordered)
1362 btrfs_put_ordered_extent(ordered);
1363 break;
1364 }
1365 }
1366
1367 cur_offset = alloc_start;
1368 while (1) {
1369 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
1370 alloc_end - cur_offset, 0);
1371 BUG_ON(IS_ERR(em) || !em);
1372 last_byte = min(extent_map_end(em), alloc_end);
1373 last_byte = (last_byte + mask) & ~mask;
1374 if (em->block_start == EXTENT_MAP_HOLE ||
1375 (cur_offset >= inode->i_size &&
1376 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
1377 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
1378 last_byte - cur_offset,
1379 1 << inode->i_blkbits,
1380 offset + len,
1381 &alloc_hint);
1382 if (ret < 0) {
1383 free_extent_map(em);
1384 break;
1385 }
1386 }
1387 free_extent_map(em);
1388
1389 cur_offset = last_byte;
1390 if (cur_offset >= alloc_end) {
1391 ret = 0;
1392 break;
1393 }
1394 }
1395 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
1396 &cached_state, GFP_NOFS);
1397
1398 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
1399out:
1400 mutex_unlock(&inode->i_mutex);
1401 return ret;
1402}
1403
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001404const struct file_operations btrfs_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04001405 .llseek = generic_file_llseek,
1406 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00001407 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001408 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001409 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001410 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001411 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001412 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001413 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001414 .fsync = btrfs_sync_file,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001415 .fallocate = btrfs_fallocate,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001416 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001417#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001418 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001419#endif
1420};