blob: a9e0a4eaf3d91b322184019df694b2399bcbeba1 [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);
Josef Bacik11c65dc2010-05-23 11:07:21 -040073 iov_iter_advance(i, copied);
74 write_bytes -= copied;
Xin Zhong914ee292010-12-09 09:30:14 +000075 total_copied += copied;
Chris Mason39279cc2007-06-12 06:35:45 -040076
Xin Zhong914ee292010-12-09 09:30:14 +000077 /* Return to btrfs_file_aio_write to fault page */
Josef Bacik11c65dc2010-05-23 11:07:21 -040078 if (unlikely(copied == 0)) {
Xin Zhong914ee292010-12-09 09:30:14 +000079 break;
Josef Bacik11c65dc2010-05-23 11:07:21 -040080 }
81
82 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
83 offset += copied;
84 } else {
85 pg++;
86 offset = 0;
87 }
Chris Mason39279cc2007-06-12 06:35:45 -040088 }
Xin Zhong914ee292010-12-09 09:30:14 +000089 return total_copied;
Chris Mason39279cc2007-06-12 06:35:45 -040090}
91
Chris Masond352ac62008-09-29 15:18:18 -040092/*
93 * unlocks pages after btrfs_file_write is done with them
94 */
Chris Masond3977122009-01-05 21:25:51 -050095static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -040096{
97 size_t i;
98 for (i = 0; i < num_pages; i++) {
99 if (!pages[i])
100 break;
Chris Masond352ac62008-09-29 15:18:18 -0400101 /* page checked is some magic around finding pages that
102 * have been modified without going through btrfs_set_page_dirty
103 * clear it here
104 */
Chris Mason4a096752008-07-21 10:29:44 -0400105 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400106 unlock_page(pages[i]);
107 mark_page_accessed(pages[i]);
108 page_cache_release(pages[i]);
109 }
110}
111
Chris Masond352ac62008-09-29 15:18:18 -0400112/*
113 * after copy_from_user, pages need to be dirtied and we need to make
114 * sure holes are created between the current EOF and the start of
115 * any next extents (if required).
116 *
117 * this also makes the decision about creating an inline extent vs
118 * doing real data extents, marking pages dirty and delalloc as required.
119 */
Chris Masond3977122009-01-05 21:25:51 -0500120static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400121 struct btrfs_root *root,
122 struct file *file,
123 struct page **pages,
124 size_t num_pages,
125 loff_t pos,
126 size_t write_bytes)
127{
Chris Mason39279cc2007-06-12 06:35:45 -0400128 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400129 int i;
Chris Mason6da6aba2007-12-18 16:15:09 -0500130 struct inode *inode = fdentry(file)->d_inode;
Chris Masondb945352007-10-15 16:15:53 -0400131 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400132 u64 start_pos;
133 u64 end_of_last_block;
134 u64 end_pos = pos + write_bytes;
135 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400136
Chris Mason5f39d392007-10-15 16:14:19 -0400137 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400138 num_bytes = (write_bytes + pos - start_pos +
139 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400140
Chris Masondb945352007-10-15 16:15:53 -0400141 end_of_last_block = start_pos + num_bytes - 1;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000142 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
143 NULL);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400144 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400145
Chris Masonc8b97812008-10-29 14:49:59 -0400146 for (i = 0; i < num_pages; i++) {
147 struct page *p = pages[i];
148 SetPageUptodate(p);
149 ClearPageChecked(p);
150 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400151 }
152 if (end_pos > isize) {
153 i_size_write(inode, end_pos);
Chris Masonf597bb12009-06-27 21:06:22 -0400154 /* we've only changed i_size in ram, and we haven't updated
155 * the disk i_size. There is no need to log the inode
156 * at this time.
157 */
Chris Mason39279cc2007-06-12 06:35:45 -0400158 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400159 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400160}
161
Chris Masond352ac62008-09-29 15:18:18 -0400162/*
163 * this drops all the extents in the cache that intersect the range
164 * [start, end]. Existing extents are split as required.
165 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400166int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
167 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400168{
169 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400170 struct extent_map *split = NULL;
171 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400172 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500173 u64 len = end - start + 1;
Chris Mason3b951512008-04-17 11:29:12 -0400174 int ret;
175 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400176 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400177 int compressed = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400178
Chris Masone6dcd2d2008-07-17 12:53:50 -0400179 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400180 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500181 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400182 testend = 0;
183 }
Chris Masond3977122009-01-05 21:25:51 -0500184 while (1) {
Chris Mason3b951512008-04-17 11:29:12 -0400185 if (!split)
186 split = alloc_extent_map(GFP_NOFS);
187 if (!split2)
188 split2 = alloc_extent_map(GFP_NOFS);
189
Chris Mason890871b2009-09-02 16:24:52 -0400190 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500191 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500192 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400193 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400194 break;
Chris Masond1310b22008-01-24 16:13:08 -0500195 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400196 flags = em->flags;
197 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000198 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400199 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400200 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400201 break;
202 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000203 start = em->start + em->len;
204 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400205 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400206 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400207 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400208 continue;
209 }
Chris Masonc8b97812008-10-29 14:49:59 -0400210 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400211 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400212 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400213
214 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
215 em->start < start) {
216 split->start = em->start;
217 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500218 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400219 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400220
221 if (compressed)
222 split->block_len = em->block_len;
223 else
224 split->block_len = split->len;
225
Chris Mason3b951512008-04-17 11:29:12 -0400226 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400227 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400228 ret = add_extent_mapping(em_tree, split);
229 BUG_ON(ret);
230 free_extent_map(split);
231 split = split2;
232 split2 = NULL;
233 }
234 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
235 testend && em->start + em->len > start + len) {
236 u64 diff = start + len - em->start;
237
238 split->start = start + len;
239 split->len = em->start + em->len - (start + len);
240 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400241 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400242
Chris Masonc8b97812008-10-29 14:49:59 -0400243 if (compressed) {
244 split->block_len = em->block_len;
245 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500246 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400247 } else {
248 split->block_len = split->len;
249 split->block_start = em->block_start + diff;
Chris Mason445a6942008-11-10 11:53:33 -0500250 split->orig_start = split->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400251 }
Chris Mason3b951512008-04-17 11:29:12 -0400252
253 ret = add_extent_mapping(em_tree, split);
254 BUG_ON(ret);
255 free_extent_map(split);
256 split = NULL;
257 }
Chris Mason890871b2009-09-02 16:24:52 -0400258 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500259
Chris Masona52d9a82007-08-27 16:49:44 -0400260 /* once for us */
261 free_extent_map(em);
262 /* once for the tree*/
263 free_extent_map(em);
264 }
Chris Mason3b951512008-04-17 11:29:12 -0400265 if (split)
266 free_extent_map(split);
267 if (split2)
268 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400269 return 0;
270}
271
Chris Mason39279cc2007-06-12 06:35:45 -0400272/*
273 * this is very complex, but the basic idea is to drop all extents
274 * in the range start - end. hint_block is filled in with a block number
275 * that would be a good hint to the block allocator for this file.
276 *
277 * If an extent intersects the range but is not entirely inside the range
278 * it is either truncated or split. Anything entirely inside the range
279 * is deleted from the tree.
280 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000281int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
282 u64 start, u64 end, u64 *hint_byte, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400283{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000284 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason00f5c792007-11-30 10:09:33 -0500285 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000286 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500287 struct btrfs_path *path;
288 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000289 struct btrfs_key new_key;
290 u64 search_start = start;
291 u64 disk_bytenr = 0;
292 u64 num_bytes = 0;
293 u64 extent_offset = 0;
294 u64 extent_end = 0;
295 int del_nr = 0;
296 int del_slot = 0;
297 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400298 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500299 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400300
Chris Masona1ed8352009-09-11 12:27:37 -0400301 if (drop_cache)
302 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400303
Chris Mason39279cc2007-06-12 06:35:45 -0400304 path = btrfs_alloc_path();
305 if (!path)
306 return -ENOMEM;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000307
Chris Masond3977122009-01-05 21:25:51 -0500308 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400309 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400310 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
311 search_start, -1);
312 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000313 break;
314 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
315 leaf = path->nodes[0];
316 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
317 if (key.objectid == inode->i_ino &&
318 key.type == BTRFS_EXTENT_DATA_KEY)
319 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400320 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400321 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000322next_slot:
323 leaf = path->nodes[0];
324 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
325 BUG_ON(del_nr > 0);
326 ret = btrfs_next_leaf(root, path);
327 if (ret < 0)
328 break;
329 if (ret > 0) {
330 ret = 0;
331 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400332 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000333 leaf = path->nodes[0];
334 recow = 1;
335 }
336
337 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
338 if (key.objectid > inode->i_ino ||
339 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
340 break;
341
342 fi = btrfs_item_ptr(leaf, path->slots[0],
343 struct btrfs_file_extent_item);
344 extent_type = btrfs_file_extent_type(leaf, fi);
345
346 if (extent_type == BTRFS_FILE_EXTENT_REG ||
347 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
348 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
349 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
350 extent_offset = btrfs_file_extent_offset(leaf, fi);
351 extent_end = key.offset +
352 btrfs_file_extent_num_bytes(leaf, fi);
353 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
354 extent_end = key.offset +
355 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400356 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000357 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400358 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400359 }
360
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000361 if (extent_end <= search_start) {
362 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400363 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400364 }
365
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000366 search_start = max(key.offset, start);
367 if (recow) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400368 btrfs_release_path(root, path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000369 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400370 }
Chris Mason771ed682008-11-06 22:02:51 -0500371
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000372 /*
373 * | - range to drop - |
374 * | -------- extent -------- |
375 */
376 if (start > key.offset && end < extent_end) {
377 BUG_ON(del_nr > 0);
378 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
379
380 memcpy(&new_key, &key, sizeof(new_key));
381 new_key.offset = start;
382 ret = btrfs_duplicate_item(trans, root, path,
383 &new_key);
384 if (ret == -EAGAIN) {
385 btrfs_release_path(root, path);
386 continue;
387 }
388 if (ret < 0)
389 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400390
Chris Mason5f39d392007-10-15 16:14:19 -0400391 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000392 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
393 struct btrfs_file_extent_item);
394 btrfs_set_file_extent_num_bytes(leaf, fi,
395 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400396
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000397 fi = btrfs_item_ptr(leaf, path->slots[0],
398 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400399
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000400 extent_offset += start - key.offset;
401 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
402 btrfs_set_file_extent_num_bytes(leaf, fi,
403 extent_end - start);
404 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400405
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000406 if (disk_bytenr > 0) {
407 ret = btrfs_inc_extent_ref(trans, root,
408 disk_bytenr, num_bytes, 0,
409 root->root_key.objectid,
410 new_key.objectid,
411 start - extent_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -0400412 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000413 *hint_byte = disk_bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400414 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000415 key.offset = start;
416 }
417 /*
418 * | ---- range to drop ----- |
419 * | -------- extent -------- |
420 */
421 if (start <= key.offset && end < extent_end) {
422 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
423
424 memcpy(&new_key, &key, sizeof(new_key));
425 new_key.offset = end;
426 btrfs_set_item_key_safe(trans, root, path, &new_key);
427
428 extent_offset += end - key.offset;
429 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
430 btrfs_set_file_extent_num_bytes(leaf, fi,
431 extent_end - end);
432 btrfs_mark_buffer_dirty(leaf);
433 if (disk_bytenr > 0) {
434 inode_sub_bytes(inode, end - key.offset);
435 *hint_byte = disk_bytenr;
436 }
437 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400438 }
439
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000440 search_start = extent_end;
441 /*
442 * | ---- range to drop ----- |
443 * | -------- extent -------- |
444 */
445 if (start > key.offset && end >= extent_end) {
446 BUG_ON(del_nr > 0);
447 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
448
449 btrfs_set_file_extent_num_bytes(leaf, fi,
450 start - key.offset);
451 btrfs_mark_buffer_dirty(leaf);
452 if (disk_bytenr > 0) {
453 inode_sub_bytes(inode, extent_end - start);
454 *hint_byte = disk_bytenr;
455 }
456 if (end == extent_end)
457 break;
458
459 path->slots[0]++;
460 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400461 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000462
463 /*
464 * | ---- range to drop ----- |
465 * | ------ extent ------ |
466 */
467 if (start <= key.offset && end >= extent_end) {
468 if (del_nr == 0) {
469 del_slot = path->slots[0];
470 del_nr = 1;
471 } else {
472 BUG_ON(del_slot + del_nr != path->slots[0]);
473 del_nr++;
474 }
475
476 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
477 inode_sub_bytes(inode,
478 extent_end - key.offset);
479 extent_end = ALIGN(extent_end,
480 root->sectorsize);
481 } else if (disk_bytenr > 0) {
482 ret = btrfs_free_extent(trans, root,
483 disk_bytenr, num_bytes, 0,
484 root->root_key.objectid,
485 key.objectid, key.offset -
486 extent_offset);
487 BUG_ON(ret);
488 inode_sub_bytes(inode,
489 extent_end - key.offset);
490 *hint_byte = disk_bytenr;
491 }
492
493 if (end == extent_end)
494 break;
495
496 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
497 path->slots[0]++;
498 goto next_slot;
499 }
500
501 ret = btrfs_del_items(trans, root, path, del_slot,
502 del_nr);
503 BUG_ON(ret);
504
505 del_nr = 0;
506 del_slot = 0;
507
508 btrfs_release_path(root, path);
509 continue;
510 }
511
512 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400513 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000514
515 if (del_nr > 0) {
516 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
517 BUG_ON(ret);
518 }
519
Chris Mason39279cc2007-06-12 06:35:45 -0400520 btrfs_free_path(path);
521 return ret;
522}
523
Yan Zhengd899e052008-10-30 14:25:28 -0400524static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000525 u64 objectid, u64 bytenr, u64 orig_offset,
526 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400527{
528 struct btrfs_file_extent_item *fi;
529 struct btrfs_key key;
530 u64 extent_end;
531
532 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
533 return 0;
534
535 btrfs_item_key_to_cpu(leaf, &key, slot);
536 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
537 return 0;
538
539 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
540 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
541 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000542 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400543 btrfs_file_extent_compression(leaf, fi) ||
544 btrfs_file_extent_encryption(leaf, fi) ||
545 btrfs_file_extent_other_encoding(leaf, fi))
546 return 0;
547
548 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
549 if ((*start && *start != key.offset) || (*end && *end != extent_end))
550 return 0;
551
552 *start = key.offset;
553 *end = extent_end;
554 return 1;
555}
556
557/*
558 * Mark extent in the range start - end as written.
559 *
560 * This changes extent type from 'pre-allocated' to 'regular'. If only
561 * part of extent is marked as written, the extent will be split into
562 * two or three.
563 */
564int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400565 struct inode *inode, u64 start, u64 end)
566{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000567 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400568 struct extent_buffer *leaf;
569 struct btrfs_path *path;
570 struct btrfs_file_extent_item *fi;
571 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000572 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400573 u64 bytenr;
574 u64 num_bytes;
575 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400576 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400577 u64 other_start;
578 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000579 u64 split;
580 int del_nr = 0;
581 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000582 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400583 int ret;
584
585 btrfs_drop_extent_cache(inode, start, end - 1, 0);
586
587 path = btrfs_alloc_path();
588 BUG_ON(!path);
589again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000590 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000591 split = start;
Yan Zhengd899e052008-10-30 14:25:28 -0400592 key.objectid = inode->i_ino;
593 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000594 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -0400595
596 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
597 if (ret > 0 && path->slots[0] > 0)
598 path->slots[0]--;
599
600 leaf = path->nodes[0];
601 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
602 BUG_ON(key.objectid != inode->i_ino ||
603 key.type != BTRFS_EXTENT_DATA_KEY);
604 fi = btrfs_item_ptr(leaf, path->slots[0],
605 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000606 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
607 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -0400608 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
609 BUG_ON(key.offset > start || extent_end < end);
610
611 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
612 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400613 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000614 memcpy(&new_key, &key, sizeof(new_key));
615
616 if (start == key.offset && end < extent_end) {
617 other_start = 0;
618 other_end = start;
619 if (extent_mergeable(leaf, path->slots[0] - 1,
620 inode->i_ino, bytenr, orig_offset,
621 &other_start, &other_end)) {
622 new_key.offset = end;
623 btrfs_set_item_key_safe(trans, root, path, &new_key);
624 fi = btrfs_item_ptr(leaf, path->slots[0],
625 struct btrfs_file_extent_item);
626 btrfs_set_file_extent_num_bytes(leaf, fi,
627 extent_end - end);
628 btrfs_set_file_extent_offset(leaf, fi,
629 end - orig_offset);
630 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
631 struct btrfs_file_extent_item);
632 btrfs_set_file_extent_num_bytes(leaf, fi,
633 end - other_start);
634 btrfs_mark_buffer_dirty(leaf);
635 goto out;
636 }
637 }
638
639 if (start > key.offset && end == extent_end) {
640 other_start = end;
641 other_end = 0;
642 if (extent_mergeable(leaf, path->slots[0] + 1,
643 inode->i_ino, bytenr, orig_offset,
644 &other_start, &other_end)) {
645 fi = btrfs_item_ptr(leaf, path->slots[0],
646 struct btrfs_file_extent_item);
647 btrfs_set_file_extent_num_bytes(leaf, fi,
648 start - key.offset);
649 path->slots[0]++;
650 new_key.offset = start;
651 btrfs_set_item_key_safe(trans, root, path, &new_key);
652
653 fi = btrfs_item_ptr(leaf, path->slots[0],
654 struct btrfs_file_extent_item);
655 btrfs_set_file_extent_num_bytes(leaf, fi,
656 other_end - start);
657 btrfs_set_file_extent_offset(leaf, fi,
658 start - orig_offset);
659 btrfs_mark_buffer_dirty(leaf);
660 goto out;
661 }
662 }
Yan Zhengd899e052008-10-30 14:25:28 -0400663
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000664 while (start > key.offset || end < extent_end) {
665 if (key.offset == start)
666 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400667
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000668 new_key.offset = split;
669 ret = btrfs_duplicate_item(trans, root, path, &new_key);
670 if (ret == -EAGAIN) {
671 btrfs_release_path(root, path);
672 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -0400673 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000674 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -0400675
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000676 leaf = path->nodes[0];
677 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -0400678 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -0400679 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000680 split - key.offset);
681
682 fi = btrfs_item_ptr(leaf, path->slots[0],
683 struct btrfs_file_extent_item);
684
685 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
686 btrfs_set_file_extent_num_bytes(leaf, fi,
687 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -0400688 btrfs_mark_buffer_dirty(leaf);
689
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000690 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
691 root->root_key.objectid,
692 inode->i_ino, orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400693 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -0400694
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000695 if (split == start) {
696 key.offset = start;
697 } else {
698 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400699 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000700 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400701 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000702 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -0400703 }
704
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000705 other_start = end;
706 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000707 if (extent_mergeable(leaf, path->slots[0] + 1,
708 inode->i_ino, bytenr, orig_offset,
709 &other_start, &other_end)) {
710 if (recow) {
711 btrfs_release_path(root, path);
712 goto again;
713 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000714 extent_end = other_end;
715 del_slot = path->slots[0] + 1;
716 del_nr++;
717 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
718 0, root->root_key.objectid,
719 inode->i_ino, orig_offset);
720 BUG_ON(ret);
721 }
722 other_start = 0;
723 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000724 if (extent_mergeable(leaf, path->slots[0] - 1,
725 inode->i_ino, bytenr, orig_offset,
726 &other_start, &other_end)) {
727 if (recow) {
728 btrfs_release_path(root, path);
729 goto again;
730 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000731 key.offset = other_start;
732 del_slot = path->slots[0];
733 del_nr++;
734 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
735 0, root->root_key.objectid,
736 inode->i_ino, orig_offset);
737 BUG_ON(ret);
738 }
739 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000740 fi = btrfs_item_ptr(leaf, path->slots[0],
741 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000742 btrfs_set_file_extent_type(leaf, fi,
743 BTRFS_FILE_EXTENT_REG);
744 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000745 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000746 fi = btrfs_item_ptr(leaf, del_slot - 1,
747 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000748 btrfs_set_file_extent_type(leaf, fi,
749 BTRFS_FILE_EXTENT_REG);
750 btrfs_set_file_extent_num_bytes(leaf, fi,
751 extent_end - key.offset);
752 btrfs_mark_buffer_dirty(leaf);
753
754 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
755 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000756 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000757out:
Yan Zhengd899e052008-10-30 14:25:28 -0400758 btrfs_free_path(path);
759 return 0;
760}
761
Chris Mason39279cc2007-06-12 06:35:45 -0400762/*
Chris Masond352ac62008-09-29 15:18:18 -0400763 * this gets pages into the page cache and locks them down, it also properly
764 * waits for data=ordered extents to finish before allowing the pages to be
765 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -0400766 */
Chris Masond3977122009-01-05 21:25:51 -0500767static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500768 struct page **pages, size_t num_pages,
769 loff_t pos, unsigned long first_index,
770 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400771{
Josef Bacik2ac55d42010-02-03 19:33:23 +0000772 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -0400773 int i;
774 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500775 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400776 int err = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400777 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400778 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400779
Chris Mason5f39d392007-10-15 16:14:19 -0400780 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400781 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400782
Yan Zheng9036c102008-10-30 14:19:41 -0400783 if (start_pos > inode->i_size) {
784 err = btrfs_cont_expand(inode, start_pos);
785 if (err)
786 return err;
787 }
788
Chris Mason39279cc2007-06-12 06:35:45 -0400789 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400790again:
Chris Mason39279cc2007-06-12 06:35:45 -0400791 for (i = 0; i < num_pages; i++) {
792 pages[i] = grab_cache_page(inode->i_mapping, index + i);
793 if (!pages[i]) {
794 err = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -0400795 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400796 }
Chris Masonccd467d2007-06-28 15:57:36 -0400797 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400798 }
Chris Mason07627042008-02-19 11:29:24 -0500799 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400800 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000801 lock_extent_bits(&BTRFS_I(inode)->io_tree,
802 start_pos, last_pos - 1, 0, &cached_state,
803 GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500804 ordered = btrfs_lookup_first_ordered_extent(inode,
805 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400806 if (ordered &&
807 ordered->file_offset + ordered->len > start_pos &&
808 ordered->file_offset < last_pos) {
809 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000810 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
811 start_pos, last_pos - 1,
812 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400813 for (i = 0; i < num_pages; i++) {
814 unlock_page(pages[i]);
815 page_cache_release(pages[i]);
816 }
817 btrfs_wait_ordered_range(inode, start_pos,
818 last_pos - start_pos);
819 goto again;
820 }
821 if (ordered)
822 btrfs_put_ordered_extent(ordered);
823
Josef Bacik2ac55d42010-02-03 19:33:23 +0000824 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -0400825 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +0000826 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
Chris Mason07627042008-02-19 11:29:24 -0500827 GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000828 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
829 start_pos, last_pos - 1, &cached_state,
830 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500831 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400832 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400833 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400834 set_page_extent_mapped(pages[i]);
835 WARN_ON(!PageLocked(pages[i]));
836 }
Chris Mason39279cc2007-06-12 06:35:45 -0400837 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400838}
839
Josef Bacik11c65dc2010-05-23 11:07:21 -0400840static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
841 const struct iovec *iov,
842 unsigned long nr_segs, loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400843{
Josef Bacik11c65dc2010-05-23 11:07:21 -0400844 struct file *file = iocb->ki_filp;
845 struct inode *inode = fdentry(file)->d_inode;
846 struct btrfs_root *root = BTRFS_I(inode)->root;
847 struct page *pinned[2];
848 struct page **pages = NULL;
849 struct iov_iter i;
850 loff_t *ppos = &iocb->ki_pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400851 loff_t start_pos;
852 ssize_t num_written = 0;
853 ssize_t err = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400854 size_t count;
855 size_t ocount;
Chris Mason39279cc2007-06-12 06:35:45 -0400856 int ret = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400857 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400858 unsigned long first_index;
859 unsigned long last_index;
Chris Masoncb843a62008-10-03 12:30:02 -0400860 int will_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400861 int buffered = 0;
Xin Zhong914ee292010-12-09 09:30:14 +0000862 int copied = 0;
863 int dirty_pages = 0;
Chris Masoncb843a62008-10-03 12:30:02 -0400864
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100865 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
Chris Masoncb843a62008-10-03 12:30:02 -0400866 (file->f_flags & O_DIRECT));
Chris Mason8c2383c2007-06-18 09:57:58 -0400867
Chris Mason39279cc2007-06-12 06:35:45 -0400868 pinned[0] = NULL;
869 pinned[1] = NULL;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400870
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400871 start_pos = pos;
872
Chris Mason39279cc2007-06-12 06:35:45 -0400873 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
Chris Mason2b1f55b2008-09-24 11:48:04 -0400874
Chris Masonab93dbe2009-10-01 12:29:10 -0400875 mutex_lock(&inode->i_mutex);
876
Josef Bacik11c65dc2010-05-23 11:07:21 -0400877 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
878 if (err)
879 goto out;
880 count = ocount;
881
Chris Mason1832a6d2007-12-21 16:27:21 -0500882 current->backing_dev_info = inode->i_mapping->backing_dev_info;
Chris Mason39279cc2007-06-12 06:35:45 -0400883 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
Chris Mason1832a6d2007-12-21 16:27:21 -0500884 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400885 goto out;
886
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -0400887 if (count == 0)
Chris Masonab93dbe2009-10-01 12:29:10 -0400888 goto out;
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400889
890 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400891 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400892 goto out;
893
Chris Mason39279cc2007-06-12 06:35:45 -0400894 file_update_time(file);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400895 BTRFS_I(inode)->sequence++;
Chris Mason39279cc2007-06-12 06:35:45 -0400896
Josef Bacik4b46fce2010-05-23 11:00:55 -0400897 if (unlikely(file->f_flags & O_DIRECT)) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400898 num_written = generic_file_direct_write(iocb, iov, &nr_segs,
899 pos, ppos, count,
900 ocount);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400901 /*
902 * the generic O_DIRECT will update in-memory i_size after the
903 * DIOs are done. But our endio handlers that update the on
904 * disk i_size never update past the in memory i_size. So we
905 * need one more update here to catch any additions to the
906 * file
907 */
908 if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
909 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
910 mark_inode_dirty(inode);
911 }
912
913 if (num_written < 0) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400914 ret = num_written;
915 num_written = 0;
916 goto out;
917 } else if (num_written == count) {
918 /* pick up pos changes done by the generic code */
919 pos = *ppos;
920 goto out;
921 }
Josef Bacik4b46fce2010-05-23 11:00:55 -0400922 /*
923 * We are going to do buffered for the rest of the range, so we
924 * need to make sure to invalidate the buffered pages when we're
925 * done.
926 */
927 buffered = 1;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400928 pos += num_written;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400929 }
930
Josef Bacik11c65dc2010-05-23 11:07:21 -0400931 iov_iter_init(&i, iov, nr_segs, count, num_written);
932 nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
933 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
934 (sizeof(struct page *)));
Chris Mason8c2383c2007-06-18 09:57:58 -0400935 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -0400936
Chris Masonab93dbe2009-10-01 12:29:10 -0400937 /* generic_write_checks can change our pos */
938 start_pos = pos;
939
Chris Mason39279cc2007-06-12 06:35:45 -0400940 first_index = pos >> PAGE_CACHE_SHIFT;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400941 last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400942
943 /*
944 * there are lots of better ways to do this, but this code
945 * makes sure the first and last page in the file range are
946 * up to date and ready for cow
947 */
948 if ((pos & (PAGE_CACHE_SIZE - 1))) {
949 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
950 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400951 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400952 BUG_ON(ret);
953 wait_on_page_locked(pinned[0]);
954 } else {
955 unlock_page(pinned[0]);
956 }
957 }
Josef Bacik11c65dc2010-05-23 11:07:21 -0400958 if ((pos + iov_iter_count(&i)) & (PAGE_CACHE_SIZE - 1)) {
Chris Mason39279cc2007-06-12 06:35:45 -0400959 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
960 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400961 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400962 BUG_ON(ret);
963 wait_on_page_locked(pinned[1]);
964 } else {
965 unlock_page(pinned[1]);
966 }
967 }
968
Josef Bacik11c65dc2010-05-23 11:07:21 -0400969 while (iov_iter_count(&i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400970 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400971 size_t write_bytes = min(iov_iter_count(&i),
972 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400973 offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400974 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
975 PAGE_CACHE_SHIFT;
976
Chris Mason8c2383c2007-06-18 09:57:58 -0400977 WARN_ON(num_pages > nrptrs);
yanhai zhu9aead432009-01-05 15:49:11 -0500978 memset(pages, 0, sizeof(struct page *) * nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -0500979
Xin Zhong914ee292010-12-09 09:30:14 +0000980 /*
981 * Fault pages before locking them in prepare_pages
982 * to avoid recursive lock
983 */
984 if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) {
985 ret = -EFAULT;
986 goto out;
987 }
988
989 ret = btrfs_delalloc_reserve_space(inode,
990 num_pages << PAGE_CACHE_SHIFT);
Chris Mason1832a6d2007-12-21 16:27:21 -0500991 if (ret)
992 goto out;
993
Chris Mason39279cc2007-06-12 06:35:45 -0400994 ret = prepare_pages(root, file, pages, num_pages,
995 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400996 write_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500997 if (ret) {
Xin Zhong914ee292010-12-09 09:30:14 +0000998 btrfs_delalloc_release_space(inode,
999 num_pages << PAGE_CACHE_SHIFT);
Chris Mason54aa1f42007-06-22 14:16:25 -04001000 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -05001001 }
Chris Mason39279cc2007-06-12 06:35:45 -04001002
Xin Zhong914ee292010-12-09 09:30:14 +00001003 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001004 write_bytes, pages, &i);
Xin Zhong914ee292010-12-09 09:30:14 +00001005 dirty_pages = (copied + PAGE_CACHE_SIZE - 1) >>
1006 PAGE_CACHE_SHIFT;
1007
1008 if (num_pages > dirty_pages) {
1009 if (copied > 0)
1010 atomic_inc(
1011 &BTRFS_I(inode)->outstanding_extents);
1012 btrfs_delalloc_release_space(inode,
1013 (num_pages - dirty_pages) <<
1014 PAGE_CACHE_SHIFT);
1015 }
1016
1017 if (copied > 0) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001018 dirty_and_release_pages(NULL, root, file, pages,
Xin Zhong914ee292010-12-09 09:30:14 +00001019 dirty_pages, pos, copied);
Chris Mason54aa1f42007-06-22 14:16:25 -04001020 }
Chris Mason39279cc2007-06-12 06:35:45 -04001021
Chris Mason39279cc2007-06-12 06:35:45 -04001022 btrfs_drop_pages(pages, num_pages);
Xin Zhong914ee292010-12-09 09:30:14 +00001023
1024 if (copied > 0) {
1025 if (will_write) {
1026 filemap_fdatawrite_range(inode->i_mapping, pos,
1027 pos + copied - 1);
1028 } else {
1029 balance_dirty_pages_ratelimited_nr(
1030 inode->i_mapping,
1031 dirty_pages);
1032 if (dirty_pages <
1033 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1034 btrfs_btree_balance_dirty(root, 1);
1035 btrfs_throttle(root);
1036 }
Josef Bacik6a632092009-02-20 11:00:09 -05001037 }
Chris Mason39279cc2007-06-12 06:35:45 -04001038
Xin Zhong914ee292010-12-09 09:30:14 +00001039 pos += copied;
1040 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001041
Chris Mason39279cc2007-06-12 06:35:45 -04001042 cond_resched();
1043 }
Chris Mason39279cc2007-06-12 06:35:45 -04001044out:
Chris Mason1832a6d2007-12-21 16:27:21 -05001045 mutex_unlock(&inode->i_mutex);
Josef Bacik6a632092009-02-20 11:00:09 -05001046 if (ret)
1047 err = ret;
Chris Mason5b92ee72008-01-03 13:46:11 -05001048
Chris Mason8c2383c2007-06-18 09:57:58 -04001049 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -04001050 if (pinned[0])
1051 page_cache_release(pinned[0]);
1052 if (pinned[1])
1053 page_cache_release(pinned[1]);
1054 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001055
Chris Mason5a3f23d2009-03-31 13:27:11 -04001056 /*
1057 * we want to make sure fsync finds this change
1058 * but we haven't joined a transaction running right now.
1059 *
1060 * Later on, someone is sure to update the inode and get the
1061 * real transid recorded.
1062 *
1063 * We set last_trans now to the fs_info generation + 1,
1064 * this will either be one more than the running transaction
1065 * or the generation used for the next transaction if there isn't
1066 * one running right now.
1067 */
1068 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1069
Chris Masoncb843a62008-10-03 12:30:02 -04001070 if (num_written > 0 && will_write) {
Chris Masone02119d2008-09-05 16:13:11 -04001071 struct btrfs_trans_handle *trans;
1072
Chris Masoncb843a62008-10-03 12:30:02 -04001073 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1074 if (err)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001075 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -04001076
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001077 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001078 trans = btrfs_start_transaction(root, 0);
Josef Bacik495e8672010-11-19 20:36:10 +00001079 if (IS_ERR(trans)) {
1080 num_written = PTR_ERR(trans);
1081 goto done;
1082 }
1083 mutex_lock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001084 ret = btrfs_log_dentry_safe(trans, root,
1085 file->f_dentry);
Josef Bacik495e8672010-11-19 20:36:10 +00001086 mutex_unlock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001087 if (ret == 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001088 ret = btrfs_sync_log(trans, root);
1089 if (ret == 0)
1090 btrfs_end_transaction(trans, root);
1091 else
1092 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001093 } else if (ret != BTRFS_NO_LOG_SYNC) {
Chris Masoncb843a62008-10-03 12:30:02 -04001094 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001095 } else {
1096 btrfs_end_transaction(trans, root);
Chris Masoncb843a62008-10-03 12:30:02 -04001097 }
Chris Masone02119d2008-09-05 16:13:11 -04001098 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04001099 if (file->f_flags & O_DIRECT && buffered) {
Chris Masoncb843a62008-10-03 12:30:02 -04001100 invalidate_mapping_pages(inode->i_mapping,
1101 start_pos >> PAGE_CACHE_SHIFT,
1102 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1103 }
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001104 }
Josef Bacik495e8672010-11-19 20:36:10 +00001105done:
Chris Mason39279cc2007-06-12 06:35:45 -04001106 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001107 return num_written ? num_written : err;
1108}
1109
Chris Masond3977122009-01-05 21:25:51 -05001110int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001111{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001112 /*
1113 * ordered_data_close is set by settattr when we are about to truncate
1114 * a file from a non-zero size to a zero size. This tries to
1115 * flush down new bytes that may have been written if the
1116 * application were using truncate to replace a file in place.
1117 */
1118 if (BTRFS_I(inode)->ordered_data_close) {
1119 BTRFS_I(inode)->ordered_data_close = 0;
1120 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1121 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1122 filemap_flush(inode->i_mapping);
1123 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001124 if (filp->private_data)
1125 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001126 return 0;
1127}
1128
Chris Masond352ac62008-09-29 15:18:18 -04001129/*
1130 * fsync call for both files and directories. This logs the inode into
1131 * the tree log instead of forcing full commits whenever possible.
1132 *
1133 * It needs to call filemap_fdatawait so that all ordered extent updates are
1134 * in the metadata btree are up to date for copying to the log.
1135 *
1136 * It drops the inode mutex before doing the tree log commit. This is an
1137 * important optimization for directories because holding the mutex prevents
1138 * new operations on the dir while we write to disk.
1139 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001140int btrfs_sync_file(struct file *file, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001141{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001142 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001143 struct inode *inode = dentry->d_inode;
1144 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001145 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001146 struct btrfs_trans_handle *trans;
1147
Chris Mason257c62e2009-10-13 13:21:08 -04001148
1149 /* we wait first, since the writeback may change the inode */
1150 root->log_batch++;
1151 /* the VFS called filemap_fdatawrite for us */
1152 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1153 root->log_batch++;
1154
Chris Mason39279cc2007-06-12 06:35:45 -04001155 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001156 * check the transaction that last modified this inode
1157 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001158 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001159 if (!BTRFS_I(inode)->last_trans)
1160 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001161
Chris Mason257c62e2009-10-13 13:21:08 -04001162 /*
1163 * if the last transaction that changed this file was before
1164 * the current transaction, we can bail out now without any
1165 * syncing
1166 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001167 mutex_lock(&root->fs_info->trans_mutex);
1168 if (BTRFS_I(inode)->last_trans <=
1169 root->fs_info->last_trans_committed) {
1170 BTRFS_I(inode)->last_trans = 0;
1171 mutex_unlock(&root->fs_info->trans_mutex);
1172 goto out;
1173 }
1174 mutex_unlock(&root->fs_info->trans_mutex);
1175
1176 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001177 * ok we haven't committed the transaction yet, lets do a commit
1178 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001179 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001180 btrfs_ioctl_trans_end(file);
1181
Yan, Zhenga22285a2010-05-16 10:48:46 -04001182 trans = btrfs_start_transaction(root, 0);
1183 if (IS_ERR(trans)) {
1184 ret = PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04001185 goto out;
1186 }
Chris Masone02119d2008-09-05 16:13:11 -04001187
Chris Mason2cfbd502009-02-20 10:55:10 -05001188 ret = btrfs_log_dentry_safe(trans, root, dentry);
Chris Masond3977122009-01-05 21:25:51 -05001189 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04001190 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001191
1192 /* we've logged all the items and now have a consistent
1193 * version of the file in the log. It is possible that
1194 * someone will come in and modify the file, but that's
1195 * fine because the log is consistent on disk, and we
1196 * have references to all of the file's extents
1197 *
1198 * It is possible that someone will come in and log the
1199 * file again, but that will end up using the synchronization
1200 * inside btrfs_sync_log to keep things safe.
1201 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001202 mutex_unlock(&dentry->d_inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001203
Chris Mason257c62e2009-10-13 13:21:08 -04001204 if (ret != BTRFS_NO_LOG_SYNC) {
1205 if (ret > 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001206 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001207 } else {
1208 ret = btrfs_sync_log(trans, root);
1209 if (ret == 0)
1210 ret = btrfs_end_transaction(trans, root);
1211 else
1212 ret = btrfs_commit_transaction(trans, root);
1213 }
1214 } else {
1215 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001216 }
Chris Mason2cfbd502009-02-20 10:55:10 -05001217 mutex_lock(&dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001218out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001219 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001220}
1221
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001222static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001223 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001224 .page_mkwrite = btrfs_page_mkwrite,
1225};
1226
1227static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1228{
Miao Xie058a4572010-05-20 07:21:50 +00001229 struct address_space *mapping = filp->f_mapping;
1230
1231 if (!mapping->a_ops->readpage)
1232 return -ENOEXEC;
1233
Chris Mason9ebefb182007-06-15 13:50:00 -04001234 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00001235 vma->vm_ops = &btrfs_file_vm_ops;
1236 vma->vm_flags |= VM_CAN_NONLINEAR;
1237
Chris Mason9ebefb182007-06-15 13:50:00 -04001238 return 0;
1239}
1240
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001241static long btrfs_fallocate(struct file *file, int mode,
1242 loff_t offset, loff_t len)
1243{
1244 struct inode *inode = file->f_path.dentry->d_inode;
1245 struct extent_state *cached_state = NULL;
1246 u64 cur_offset;
1247 u64 last_byte;
1248 u64 alloc_start;
1249 u64 alloc_end;
1250 u64 alloc_hint = 0;
1251 u64 locked_end;
1252 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
1253 struct extent_map *em;
1254 int ret;
1255
1256 alloc_start = offset & ~mask;
1257 alloc_end = (offset + len + mask) & ~mask;
1258
1259 /* We only support the FALLOC_FL_KEEP_SIZE mode */
1260 if (mode & ~FALLOC_FL_KEEP_SIZE)
1261 return -EOPNOTSUPP;
1262
1263 /*
1264 * wait for ordered IO before we have any locks. We'll loop again
1265 * below with the locks held.
1266 */
1267 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
1268
1269 mutex_lock(&inode->i_mutex);
1270 ret = inode_newsize_ok(inode, alloc_end);
1271 if (ret)
1272 goto out;
1273
1274 if (alloc_start > inode->i_size) {
1275 ret = btrfs_cont_expand(inode, alloc_start);
1276 if (ret)
1277 goto out;
1278 }
1279
1280 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
1281 if (ret)
1282 goto out;
1283
1284 locked_end = alloc_end - 1;
1285 while (1) {
1286 struct btrfs_ordered_extent *ordered;
1287
1288 /* the extent lock is ordered inside the running
1289 * transaction
1290 */
1291 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
1292 locked_end, 0, &cached_state, GFP_NOFS);
1293 ordered = btrfs_lookup_first_ordered_extent(inode,
1294 alloc_end - 1);
1295 if (ordered &&
1296 ordered->file_offset + ordered->len > alloc_start &&
1297 ordered->file_offset < alloc_end) {
1298 btrfs_put_ordered_extent(ordered);
1299 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1300 alloc_start, locked_end,
1301 &cached_state, GFP_NOFS);
1302 /*
1303 * we can't wait on the range with the transaction
1304 * running or with the extent lock held
1305 */
1306 btrfs_wait_ordered_range(inode, alloc_start,
1307 alloc_end - alloc_start);
1308 } else {
1309 if (ordered)
1310 btrfs_put_ordered_extent(ordered);
1311 break;
1312 }
1313 }
1314
1315 cur_offset = alloc_start;
1316 while (1) {
1317 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
1318 alloc_end - cur_offset, 0);
1319 BUG_ON(IS_ERR(em) || !em);
1320 last_byte = min(extent_map_end(em), alloc_end);
1321 last_byte = (last_byte + mask) & ~mask;
1322 if (em->block_start == EXTENT_MAP_HOLE ||
1323 (cur_offset >= inode->i_size &&
1324 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
1325 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
1326 last_byte - cur_offset,
1327 1 << inode->i_blkbits,
1328 offset + len,
1329 &alloc_hint);
1330 if (ret < 0) {
1331 free_extent_map(em);
1332 break;
1333 }
1334 }
1335 free_extent_map(em);
1336
1337 cur_offset = last_byte;
1338 if (cur_offset >= alloc_end) {
1339 ret = 0;
1340 break;
1341 }
1342 }
1343 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
1344 &cached_state, GFP_NOFS);
1345
1346 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
1347out:
1348 mutex_unlock(&inode->i_mutex);
1349 return ret;
1350}
1351
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001352const struct file_operations btrfs_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04001353 .llseek = generic_file_llseek,
1354 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00001355 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001356 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001357 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001358 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001359 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001360 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001361 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001362 .fsync = btrfs_sync_file,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001363 .fallocate = btrfs_fallocate,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001364 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001365#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001366 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001367#endif
1368};