blob: 7084140d5940e68f653ad8919a0084549d6b8e1b [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);
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000189 BUG_ON(!split || !split2);
Chris Mason3b951512008-04-17 11:29:12 -0400190
Chris Mason890871b2009-09-02 16:24:52 -0400191 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500192 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500193 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400194 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400195 break;
Chris Masond1310b22008-01-24 16:13:08 -0500196 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400197 flags = em->flags;
198 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000199 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400200 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400201 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400202 break;
203 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000204 start = em->start + em->len;
205 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400206 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400207 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400208 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400209 continue;
210 }
Chris Masonc8b97812008-10-29 14:49:59 -0400211 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400212 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400213 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400214
215 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
216 em->start < start) {
217 split->start = em->start;
218 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500219 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400220 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400221
222 if (compressed)
223 split->block_len = em->block_len;
224 else
225 split->block_len = split->len;
226
Chris Mason3b951512008-04-17 11:29:12 -0400227 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400228 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800229 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400230 ret = add_extent_mapping(em_tree, split);
231 BUG_ON(ret);
232 free_extent_map(split);
233 split = split2;
234 split2 = NULL;
235 }
236 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
237 testend && em->start + em->len > start + len) {
238 u64 diff = start + len - em->start;
239
240 split->start = start + len;
241 split->len = em->start + em->len - (start + len);
242 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400243 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800244 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400245
Chris Masonc8b97812008-10-29 14:49:59 -0400246 if (compressed) {
247 split->block_len = em->block_len;
248 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500249 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400250 } else {
251 split->block_len = split->len;
252 split->block_start = em->block_start + diff;
Chris Mason445a6942008-11-10 11:53:33 -0500253 split->orig_start = split->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400254 }
Chris Mason3b951512008-04-17 11:29:12 -0400255
256 ret = add_extent_mapping(em_tree, split);
257 BUG_ON(ret);
258 free_extent_map(split);
259 split = NULL;
260 }
Chris Mason890871b2009-09-02 16:24:52 -0400261 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500262
Chris Masona52d9a82007-08-27 16:49:44 -0400263 /* once for us */
264 free_extent_map(em);
265 /* once for the tree*/
266 free_extent_map(em);
267 }
Chris Mason3b951512008-04-17 11:29:12 -0400268 if (split)
269 free_extent_map(split);
270 if (split2)
271 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400272 return 0;
273}
274
Chris Mason39279cc2007-06-12 06:35:45 -0400275/*
276 * this is very complex, but the basic idea is to drop all extents
277 * in the range start - end. hint_block is filled in with a block number
278 * that would be a good hint to the block allocator for this file.
279 *
280 * If an extent intersects the range but is not entirely inside the range
281 * it is either truncated or split. Anything entirely inside the range
282 * is deleted from the tree.
283 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000284int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
285 u64 start, u64 end, u64 *hint_byte, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400286{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000287 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason00f5c792007-11-30 10:09:33 -0500288 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000289 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500290 struct btrfs_path *path;
291 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000292 struct btrfs_key new_key;
293 u64 search_start = start;
294 u64 disk_bytenr = 0;
295 u64 num_bytes = 0;
296 u64 extent_offset = 0;
297 u64 extent_end = 0;
298 int del_nr = 0;
299 int del_slot = 0;
300 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400301 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500302 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400303
Chris Masona1ed8352009-09-11 12:27:37 -0400304 if (drop_cache)
305 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400306
Chris Mason39279cc2007-06-12 06:35:45 -0400307 path = btrfs_alloc_path();
308 if (!path)
309 return -ENOMEM;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000310
Chris Masond3977122009-01-05 21:25:51 -0500311 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400312 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400313 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
314 search_start, -1);
315 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000316 break;
317 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
318 leaf = path->nodes[0];
319 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
320 if (key.objectid == inode->i_ino &&
321 key.type == BTRFS_EXTENT_DATA_KEY)
322 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400323 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400324 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000325next_slot:
326 leaf = path->nodes[0];
327 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
328 BUG_ON(del_nr > 0);
329 ret = btrfs_next_leaf(root, path);
330 if (ret < 0)
331 break;
332 if (ret > 0) {
333 ret = 0;
334 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400335 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000336 leaf = path->nodes[0];
337 recow = 1;
338 }
339
340 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
341 if (key.objectid > inode->i_ino ||
342 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
343 break;
344
345 fi = btrfs_item_ptr(leaf, path->slots[0],
346 struct btrfs_file_extent_item);
347 extent_type = btrfs_file_extent_type(leaf, fi);
348
349 if (extent_type == BTRFS_FILE_EXTENT_REG ||
350 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
351 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
352 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
353 extent_offset = btrfs_file_extent_offset(leaf, fi);
354 extent_end = key.offset +
355 btrfs_file_extent_num_bytes(leaf, fi);
356 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
357 extent_end = key.offset +
358 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400359 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000360 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400361 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400362 }
363
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000364 if (extent_end <= search_start) {
365 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400366 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400367 }
368
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000369 search_start = max(key.offset, start);
370 if (recow) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400371 btrfs_release_path(root, path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000372 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400373 }
Chris Mason771ed682008-11-06 22:02:51 -0500374
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000375 /*
376 * | - range to drop - |
377 * | -------- extent -------- |
378 */
379 if (start > key.offset && end < extent_end) {
380 BUG_ON(del_nr > 0);
381 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
382
383 memcpy(&new_key, &key, sizeof(new_key));
384 new_key.offset = start;
385 ret = btrfs_duplicate_item(trans, root, path,
386 &new_key);
387 if (ret == -EAGAIN) {
388 btrfs_release_path(root, path);
389 continue;
390 }
391 if (ret < 0)
392 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400393
Chris Mason5f39d392007-10-15 16:14:19 -0400394 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000395 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
396 struct btrfs_file_extent_item);
397 btrfs_set_file_extent_num_bytes(leaf, fi,
398 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400399
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000400 fi = btrfs_item_ptr(leaf, path->slots[0],
401 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400402
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000403 extent_offset += start - key.offset;
404 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
405 btrfs_set_file_extent_num_bytes(leaf, fi,
406 extent_end - start);
407 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400408
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000409 if (disk_bytenr > 0) {
410 ret = btrfs_inc_extent_ref(trans, root,
411 disk_bytenr, num_bytes, 0,
412 root->root_key.objectid,
413 new_key.objectid,
414 start - extent_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -0400415 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000416 *hint_byte = disk_bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400417 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000418 key.offset = start;
419 }
420 /*
421 * | ---- range to drop ----- |
422 * | -------- extent -------- |
423 */
424 if (start <= key.offset && end < extent_end) {
425 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
426
427 memcpy(&new_key, &key, sizeof(new_key));
428 new_key.offset = end;
429 btrfs_set_item_key_safe(trans, root, path, &new_key);
430
431 extent_offset += end - key.offset;
432 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
433 btrfs_set_file_extent_num_bytes(leaf, fi,
434 extent_end - end);
435 btrfs_mark_buffer_dirty(leaf);
436 if (disk_bytenr > 0) {
437 inode_sub_bytes(inode, end - key.offset);
438 *hint_byte = disk_bytenr;
439 }
440 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400441 }
442
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000443 search_start = extent_end;
444 /*
445 * | ---- range to drop ----- |
446 * | -------- extent -------- |
447 */
448 if (start > key.offset && end >= extent_end) {
449 BUG_ON(del_nr > 0);
450 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
451
452 btrfs_set_file_extent_num_bytes(leaf, fi,
453 start - key.offset);
454 btrfs_mark_buffer_dirty(leaf);
455 if (disk_bytenr > 0) {
456 inode_sub_bytes(inode, extent_end - start);
457 *hint_byte = disk_bytenr;
458 }
459 if (end == extent_end)
460 break;
461
462 path->slots[0]++;
463 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400464 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000465
466 /*
467 * | ---- range to drop ----- |
468 * | ------ extent ------ |
469 */
470 if (start <= key.offset && end >= extent_end) {
471 if (del_nr == 0) {
472 del_slot = path->slots[0];
473 del_nr = 1;
474 } else {
475 BUG_ON(del_slot + del_nr != path->slots[0]);
476 del_nr++;
477 }
478
479 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
480 inode_sub_bytes(inode,
481 extent_end - key.offset);
482 extent_end = ALIGN(extent_end,
483 root->sectorsize);
484 } else if (disk_bytenr > 0) {
485 ret = btrfs_free_extent(trans, root,
486 disk_bytenr, num_bytes, 0,
487 root->root_key.objectid,
488 key.objectid, key.offset -
489 extent_offset);
490 BUG_ON(ret);
491 inode_sub_bytes(inode,
492 extent_end - key.offset);
493 *hint_byte = disk_bytenr;
494 }
495
496 if (end == extent_end)
497 break;
498
499 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
500 path->slots[0]++;
501 goto next_slot;
502 }
503
504 ret = btrfs_del_items(trans, root, path, del_slot,
505 del_nr);
506 BUG_ON(ret);
507
508 del_nr = 0;
509 del_slot = 0;
510
511 btrfs_release_path(root, path);
512 continue;
513 }
514
515 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400516 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000517
518 if (del_nr > 0) {
519 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
520 BUG_ON(ret);
521 }
522
Chris Mason39279cc2007-06-12 06:35:45 -0400523 btrfs_free_path(path);
524 return ret;
525}
526
Yan Zhengd899e052008-10-30 14:25:28 -0400527static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000528 u64 objectid, u64 bytenr, u64 orig_offset,
529 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400530{
531 struct btrfs_file_extent_item *fi;
532 struct btrfs_key key;
533 u64 extent_end;
534
535 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
536 return 0;
537
538 btrfs_item_key_to_cpu(leaf, &key, slot);
539 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
540 return 0;
541
542 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
543 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
544 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000545 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400546 btrfs_file_extent_compression(leaf, fi) ||
547 btrfs_file_extent_encryption(leaf, fi) ||
548 btrfs_file_extent_other_encoding(leaf, fi))
549 return 0;
550
551 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
552 if ((*start && *start != key.offset) || (*end && *end != extent_end))
553 return 0;
554
555 *start = key.offset;
556 *end = extent_end;
557 return 1;
558}
559
560/*
561 * Mark extent in the range start - end as written.
562 *
563 * This changes extent type from 'pre-allocated' to 'regular'. If only
564 * part of extent is marked as written, the extent will be split into
565 * two or three.
566 */
567int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400568 struct inode *inode, u64 start, u64 end)
569{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000570 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400571 struct extent_buffer *leaf;
572 struct btrfs_path *path;
573 struct btrfs_file_extent_item *fi;
574 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000575 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400576 u64 bytenr;
577 u64 num_bytes;
578 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400579 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400580 u64 other_start;
581 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000582 u64 split;
583 int del_nr = 0;
584 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000585 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400586 int ret;
587
588 btrfs_drop_extent_cache(inode, start, end - 1, 0);
589
590 path = btrfs_alloc_path();
591 BUG_ON(!path);
592again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000593 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000594 split = start;
Yan Zhengd899e052008-10-30 14:25:28 -0400595 key.objectid = inode->i_ino;
596 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000597 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -0400598
599 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
600 if (ret > 0 && path->slots[0] > 0)
601 path->slots[0]--;
602
603 leaf = path->nodes[0];
604 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
605 BUG_ON(key.objectid != inode->i_ino ||
606 key.type != BTRFS_EXTENT_DATA_KEY);
607 fi = btrfs_item_ptr(leaf, path->slots[0],
608 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000609 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
610 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -0400611 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
612 BUG_ON(key.offset > start || extent_end < end);
613
614 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
615 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400616 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000617 memcpy(&new_key, &key, sizeof(new_key));
618
619 if (start == key.offset && end < extent_end) {
620 other_start = 0;
621 other_end = start;
622 if (extent_mergeable(leaf, path->slots[0] - 1,
623 inode->i_ino, bytenr, orig_offset,
624 &other_start, &other_end)) {
625 new_key.offset = end;
626 btrfs_set_item_key_safe(trans, root, path, &new_key);
627 fi = btrfs_item_ptr(leaf, path->slots[0],
628 struct btrfs_file_extent_item);
629 btrfs_set_file_extent_num_bytes(leaf, fi,
630 extent_end - end);
631 btrfs_set_file_extent_offset(leaf, fi,
632 end - orig_offset);
633 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
634 struct btrfs_file_extent_item);
635 btrfs_set_file_extent_num_bytes(leaf, fi,
636 end - other_start);
637 btrfs_mark_buffer_dirty(leaf);
638 goto out;
639 }
640 }
641
642 if (start > key.offset && end == extent_end) {
643 other_start = end;
644 other_end = 0;
645 if (extent_mergeable(leaf, path->slots[0] + 1,
646 inode->i_ino, bytenr, orig_offset,
647 &other_start, &other_end)) {
648 fi = btrfs_item_ptr(leaf, path->slots[0],
649 struct btrfs_file_extent_item);
650 btrfs_set_file_extent_num_bytes(leaf, fi,
651 start - key.offset);
652 path->slots[0]++;
653 new_key.offset = start;
654 btrfs_set_item_key_safe(trans, root, path, &new_key);
655
656 fi = btrfs_item_ptr(leaf, path->slots[0],
657 struct btrfs_file_extent_item);
658 btrfs_set_file_extent_num_bytes(leaf, fi,
659 other_end - start);
660 btrfs_set_file_extent_offset(leaf, fi,
661 start - orig_offset);
662 btrfs_mark_buffer_dirty(leaf);
663 goto out;
664 }
665 }
Yan Zhengd899e052008-10-30 14:25:28 -0400666
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000667 while (start > key.offset || end < extent_end) {
668 if (key.offset == start)
669 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400670
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000671 new_key.offset = split;
672 ret = btrfs_duplicate_item(trans, root, path, &new_key);
673 if (ret == -EAGAIN) {
674 btrfs_release_path(root, path);
675 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -0400676 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000677 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -0400678
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000679 leaf = path->nodes[0];
680 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -0400681 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -0400682 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000683 split - key.offset);
684
685 fi = btrfs_item_ptr(leaf, path->slots[0],
686 struct btrfs_file_extent_item);
687
688 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
689 btrfs_set_file_extent_num_bytes(leaf, fi,
690 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -0400691 btrfs_mark_buffer_dirty(leaf);
692
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000693 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
694 root->root_key.objectid,
695 inode->i_ino, orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400696 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -0400697
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000698 if (split == start) {
699 key.offset = start;
700 } else {
701 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400702 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000703 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400704 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000705 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -0400706 }
707
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000708 other_start = end;
709 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000710 if (extent_mergeable(leaf, path->slots[0] + 1,
711 inode->i_ino, bytenr, orig_offset,
712 &other_start, &other_end)) {
713 if (recow) {
714 btrfs_release_path(root, path);
715 goto again;
716 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000717 extent_end = other_end;
718 del_slot = path->slots[0] + 1;
719 del_nr++;
720 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
721 0, root->root_key.objectid,
722 inode->i_ino, orig_offset);
723 BUG_ON(ret);
724 }
725 other_start = 0;
726 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000727 if (extent_mergeable(leaf, path->slots[0] - 1,
728 inode->i_ino, bytenr, orig_offset,
729 &other_start, &other_end)) {
730 if (recow) {
731 btrfs_release_path(root, path);
732 goto again;
733 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000734 key.offset = other_start;
735 del_slot = path->slots[0];
736 del_nr++;
737 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
738 0, root->root_key.objectid,
739 inode->i_ino, orig_offset);
740 BUG_ON(ret);
741 }
742 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000743 fi = btrfs_item_ptr(leaf, path->slots[0],
744 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000745 btrfs_set_file_extent_type(leaf, fi,
746 BTRFS_FILE_EXTENT_REG);
747 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000748 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000749 fi = btrfs_item_ptr(leaf, del_slot - 1,
750 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000751 btrfs_set_file_extent_type(leaf, fi,
752 BTRFS_FILE_EXTENT_REG);
753 btrfs_set_file_extent_num_bytes(leaf, fi,
754 extent_end - key.offset);
755 btrfs_mark_buffer_dirty(leaf);
756
757 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
758 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000759 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000760out:
Yan Zhengd899e052008-10-30 14:25:28 -0400761 btrfs_free_path(path);
762 return 0;
763}
764
Chris Mason39279cc2007-06-12 06:35:45 -0400765/*
Chris Masond352ac62008-09-29 15:18:18 -0400766 * this gets pages into the page cache and locks them down, it also properly
767 * waits for data=ordered extents to finish before allowing the pages to be
768 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -0400769 */
Chris Masond3977122009-01-05 21:25:51 -0500770static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500771 struct page **pages, size_t num_pages,
772 loff_t pos, unsigned long first_index,
773 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400774{
Josef Bacik2ac55d42010-02-03 19:33:23 +0000775 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -0400776 int i;
777 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500778 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400779 int err = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400780 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400781 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400782
Chris Mason5f39d392007-10-15 16:14:19 -0400783 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400784 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400785
Yan Zheng9036c102008-10-30 14:19:41 -0400786 if (start_pos > inode->i_size) {
787 err = btrfs_cont_expand(inode, start_pos);
788 if (err)
789 return err;
790 }
791
Chris Mason39279cc2007-06-12 06:35:45 -0400792 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400793again:
Chris Mason39279cc2007-06-12 06:35:45 -0400794 for (i = 0; i < num_pages; i++) {
795 pages[i] = grab_cache_page(inode->i_mapping, index + i);
796 if (!pages[i]) {
Josef Bacik7adf5df2011-01-25 22:11:54 +0000797 int c;
798 for (c = i - 1; c >= 0; c--) {
799 unlock_page(pages[c]);
800 page_cache_release(pages[c]);
801 }
802 return -ENOMEM;
Chris Mason39279cc2007-06-12 06:35:45 -0400803 }
Chris Masonccd467d2007-06-28 15:57:36 -0400804 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400805 }
Chris Mason07627042008-02-19 11:29:24 -0500806 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400807 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000808 lock_extent_bits(&BTRFS_I(inode)->io_tree,
809 start_pos, last_pos - 1, 0, &cached_state,
810 GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500811 ordered = btrfs_lookup_first_ordered_extent(inode,
812 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400813 if (ordered &&
814 ordered->file_offset + ordered->len > start_pos &&
815 ordered->file_offset < last_pos) {
816 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000817 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
818 start_pos, last_pos - 1,
819 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400820 for (i = 0; i < num_pages; i++) {
821 unlock_page(pages[i]);
822 page_cache_release(pages[i]);
823 }
824 btrfs_wait_ordered_range(inode, start_pos,
825 last_pos - start_pos);
826 goto again;
827 }
828 if (ordered)
829 btrfs_put_ordered_extent(ordered);
830
Josef Bacik2ac55d42010-02-03 19:33:23 +0000831 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -0400832 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +0000833 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
Chris Mason07627042008-02-19 11:29:24 -0500834 GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000835 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
836 start_pos, last_pos - 1, &cached_state,
837 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500838 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400839 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400840 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400841 set_page_extent_mapped(pages[i]);
842 WARN_ON(!PageLocked(pages[i]));
843 }
Chris Mason39279cc2007-06-12 06:35:45 -0400844 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400845}
846
Josef Bacik11c65dc2010-05-23 11:07:21 -0400847static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
848 const struct iovec *iov,
849 unsigned long nr_segs, loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400850{
Josef Bacik11c65dc2010-05-23 11:07:21 -0400851 struct file *file = iocb->ki_filp;
852 struct inode *inode = fdentry(file)->d_inode;
853 struct btrfs_root *root = BTRFS_I(inode)->root;
854 struct page *pinned[2];
855 struct page **pages = NULL;
856 struct iov_iter i;
857 loff_t *ppos = &iocb->ki_pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400858 loff_t start_pos;
859 ssize_t num_written = 0;
860 ssize_t err = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400861 size_t count;
862 size_t ocount;
Chris Mason39279cc2007-06-12 06:35:45 -0400863 int ret = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400864 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400865 unsigned long first_index;
866 unsigned long last_index;
Chris Masoncb843a62008-10-03 12:30:02 -0400867 int will_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400868 int buffered = 0;
Xin Zhong914ee292010-12-09 09:30:14 +0000869 int copied = 0;
870 int dirty_pages = 0;
Chris Masoncb843a62008-10-03 12:30:02 -0400871
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100872 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
Chris Masoncb843a62008-10-03 12:30:02 -0400873 (file->f_flags & O_DIRECT));
Chris Mason8c2383c2007-06-18 09:57:58 -0400874
Chris Mason39279cc2007-06-12 06:35:45 -0400875 pinned[0] = NULL;
876 pinned[1] = NULL;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400877
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400878 start_pos = pos;
879
Chris Mason39279cc2007-06-12 06:35:45 -0400880 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
Chris Mason2b1f55b2008-09-24 11:48:04 -0400881
Chris Masonab93dbe2009-10-01 12:29:10 -0400882 mutex_lock(&inode->i_mutex);
883
Josef Bacik11c65dc2010-05-23 11:07:21 -0400884 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
885 if (err)
886 goto out;
887 count = ocount;
888
Chris Mason1832a6d2007-12-21 16:27:21 -0500889 current->backing_dev_info = inode->i_mapping->backing_dev_info;
Chris Mason39279cc2007-06-12 06:35:45 -0400890 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
Chris Mason1832a6d2007-12-21 16:27:21 -0500891 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400892 goto out;
893
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -0400894 if (count == 0)
Chris Masonab93dbe2009-10-01 12:29:10 -0400895 goto out;
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400896
897 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400898 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400899 goto out;
900
liuboacce9522011-01-06 19:30:25 +0800901 /*
902 * If BTRFS flips readonly due to some impossible error
903 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
904 * although we have opened a file as writable, we have
905 * to stop this write operation to ensure FS consistency.
906 */
907 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
908 err = -EROFS;
909 goto out;
910 }
911
Chris Mason39279cc2007-06-12 06:35:45 -0400912 file_update_time(file);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400913 BTRFS_I(inode)->sequence++;
Chris Mason39279cc2007-06-12 06:35:45 -0400914
Josef Bacik4b46fce2010-05-23 11:00:55 -0400915 if (unlikely(file->f_flags & O_DIRECT)) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400916 num_written = generic_file_direct_write(iocb, iov, &nr_segs,
917 pos, ppos, count,
918 ocount);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400919 /*
920 * the generic O_DIRECT will update in-memory i_size after the
921 * DIOs are done. But our endio handlers that update the on
922 * disk i_size never update past the in memory i_size. So we
923 * need one more update here to catch any additions to the
924 * file
925 */
926 if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
927 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
928 mark_inode_dirty(inode);
929 }
930
931 if (num_written < 0) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400932 ret = num_written;
933 num_written = 0;
934 goto out;
935 } else if (num_written == count) {
936 /* pick up pos changes done by the generic code */
937 pos = *ppos;
938 goto out;
939 }
Josef Bacik4b46fce2010-05-23 11:00:55 -0400940 /*
941 * We are going to do buffered for the rest of the range, so we
942 * need to make sure to invalidate the buffered pages when we're
943 * done.
944 */
945 buffered = 1;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400946 pos += num_written;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400947 }
948
Josef Bacik11c65dc2010-05-23 11:07:21 -0400949 iov_iter_init(&i, iov, nr_segs, count, num_written);
950 nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
951 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
952 (sizeof(struct page *)));
Chris Mason8c2383c2007-06-18 09:57:58 -0400953 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
liubo2a29edc2011-01-26 06:22:08 +0000954 if (!pages) {
955 ret = -ENOMEM;
956 goto out;
957 }
Chris Mason39279cc2007-06-12 06:35:45 -0400958
Chris Masonab93dbe2009-10-01 12:29:10 -0400959 /* generic_write_checks can change our pos */
960 start_pos = pos;
961
Chris Mason39279cc2007-06-12 06:35:45 -0400962 first_index = pos >> PAGE_CACHE_SHIFT;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400963 last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400964
965 /*
966 * there are lots of better ways to do this, but this code
967 * makes sure the first and last page in the file range are
968 * up to date and ready for cow
969 */
970 if ((pos & (PAGE_CACHE_SIZE - 1))) {
971 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
972 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400973 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400974 BUG_ON(ret);
975 wait_on_page_locked(pinned[0]);
976 } else {
977 unlock_page(pinned[0]);
978 }
979 }
Josef Bacik11c65dc2010-05-23 11:07:21 -0400980 if ((pos + iov_iter_count(&i)) & (PAGE_CACHE_SIZE - 1)) {
Chris Mason39279cc2007-06-12 06:35:45 -0400981 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
982 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400983 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400984 BUG_ON(ret);
985 wait_on_page_locked(pinned[1]);
986 } else {
987 unlock_page(pinned[1]);
988 }
989 }
990
Josef Bacik11c65dc2010-05-23 11:07:21 -0400991 while (iov_iter_count(&i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400992 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400993 size_t write_bytes = min(iov_iter_count(&i),
994 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400995 offset);
Yan, Zheng3a909832011-01-18 13:34:40 +0800996 size_t num_pages = (write_bytes + offset +
997 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400998
Chris Mason8c2383c2007-06-18 09:57:58 -0400999 WARN_ON(num_pages > nrptrs);
yanhai zhu9aead432009-01-05 15:49:11 -05001000 memset(pages, 0, sizeof(struct page *) * nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -05001001
Xin Zhong914ee292010-12-09 09:30:14 +00001002 /*
1003 * Fault pages before locking them in prepare_pages
1004 * to avoid recursive lock
1005 */
1006 if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) {
1007 ret = -EFAULT;
1008 goto out;
1009 }
1010
1011 ret = btrfs_delalloc_reserve_space(inode,
1012 num_pages << PAGE_CACHE_SHIFT);
Chris Mason1832a6d2007-12-21 16:27:21 -05001013 if (ret)
1014 goto out;
1015
Chris Mason39279cc2007-06-12 06:35:45 -04001016 ret = prepare_pages(root, file, pages, num_pages,
1017 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -04001018 write_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001019 if (ret) {
Xin Zhong914ee292010-12-09 09:30:14 +00001020 btrfs_delalloc_release_space(inode,
1021 num_pages << PAGE_CACHE_SHIFT);
Chris Mason54aa1f42007-06-22 14:16:25 -04001022 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -05001023 }
Chris Mason39279cc2007-06-12 06:35:45 -04001024
Xin Zhong914ee292010-12-09 09:30:14 +00001025 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001026 write_bytes, pages, &i);
Yan, Zheng3a909832011-01-18 13:34:40 +08001027 dirty_pages = (copied + offset + PAGE_CACHE_SIZE - 1) >>
1028 PAGE_CACHE_SHIFT;
Xin Zhong914ee292010-12-09 09:30:14 +00001029
1030 if (num_pages > dirty_pages) {
1031 if (copied > 0)
1032 atomic_inc(
1033 &BTRFS_I(inode)->outstanding_extents);
1034 btrfs_delalloc_release_space(inode,
1035 (num_pages - dirty_pages) <<
1036 PAGE_CACHE_SHIFT);
1037 }
1038
1039 if (copied > 0) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001040 dirty_and_release_pages(NULL, root, file, pages,
Xin Zhong914ee292010-12-09 09:30:14 +00001041 dirty_pages, pos, copied);
Chris Mason54aa1f42007-06-22 14:16:25 -04001042 }
Chris Mason39279cc2007-06-12 06:35:45 -04001043
Chris Mason39279cc2007-06-12 06:35:45 -04001044 btrfs_drop_pages(pages, num_pages);
Xin Zhong914ee292010-12-09 09:30:14 +00001045
1046 if (copied > 0) {
1047 if (will_write) {
1048 filemap_fdatawrite_range(inode->i_mapping, pos,
1049 pos + copied - 1);
1050 } else {
1051 balance_dirty_pages_ratelimited_nr(
1052 inode->i_mapping,
1053 dirty_pages);
1054 if (dirty_pages <
1055 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1056 btrfs_btree_balance_dirty(root, 1);
1057 btrfs_throttle(root);
1058 }
Josef Bacik6a632092009-02-20 11:00:09 -05001059 }
Chris Mason39279cc2007-06-12 06:35:45 -04001060
Xin Zhong914ee292010-12-09 09:30:14 +00001061 pos += copied;
1062 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001063
Chris Mason39279cc2007-06-12 06:35:45 -04001064 cond_resched();
1065 }
Chris Mason39279cc2007-06-12 06:35:45 -04001066out:
Chris Mason1832a6d2007-12-21 16:27:21 -05001067 mutex_unlock(&inode->i_mutex);
Josef Bacik6a632092009-02-20 11:00:09 -05001068 if (ret)
1069 err = ret;
Chris Mason5b92ee72008-01-03 13:46:11 -05001070
Chris Mason8c2383c2007-06-18 09:57:58 -04001071 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -04001072 if (pinned[0])
1073 page_cache_release(pinned[0]);
1074 if (pinned[1])
1075 page_cache_release(pinned[1]);
1076 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001077
Chris Mason5a3f23d2009-03-31 13:27:11 -04001078 /*
1079 * we want to make sure fsync finds this change
1080 * but we haven't joined a transaction running right now.
1081 *
1082 * Later on, someone is sure to update the inode and get the
1083 * real transid recorded.
1084 *
1085 * We set last_trans now to the fs_info generation + 1,
1086 * this will either be one more than the running transaction
1087 * or the generation used for the next transaction if there isn't
1088 * one running right now.
1089 */
1090 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1091
Chris Masoncb843a62008-10-03 12:30:02 -04001092 if (num_written > 0 && will_write) {
Chris Masone02119d2008-09-05 16:13:11 -04001093 struct btrfs_trans_handle *trans;
1094
Chris Masoncb843a62008-10-03 12:30:02 -04001095 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1096 if (err)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001097 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -04001098
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001099 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001100 trans = btrfs_start_transaction(root, 0);
Josef Bacik495e8672010-11-19 20:36:10 +00001101 if (IS_ERR(trans)) {
1102 num_written = PTR_ERR(trans);
1103 goto done;
1104 }
1105 mutex_lock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001106 ret = btrfs_log_dentry_safe(trans, root,
1107 file->f_dentry);
Josef Bacik495e8672010-11-19 20:36:10 +00001108 mutex_unlock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001109 if (ret == 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001110 ret = btrfs_sync_log(trans, root);
1111 if (ret == 0)
1112 btrfs_end_transaction(trans, root);
1113 else
1114 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001115 } else if (ret != BTRFS_NO_LOG_SYNC) {
Chris Masoncb843a62008-10-03 12:30:02 -04001116 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001117 } else {
1118 btrfs_end_transaction(trans, root);
Chris Masoncb843a62008-10-03 12:30:02 -04001119 }
Chris Masone02119d2008-09-05 16:13:11 -04001120 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04001121 if (file->f_flags & O_DIRECT && buffered) {
Chris Masoncb843a62008-10-03 12:30:02 -04001122 invalidate_mapping_pages(inode->i_mapping,
1123 start_pos >> PAGE_CACHE_SHIFT,
1124 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1125 }
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001126 }
Josef Bacik495e8672010-11-19 20:36:10 +00001127done:
Chris Mason39279cc2007-06-12 06:35:45 -04001128 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001129 return num_written ? num_written : err;
1130}
1131
Chris Masond3977122009-01-05 21:25:51 -05001132int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001133{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001134 /*
1135 * ordered_data_close is set by settattr when we are about to truncate
1136 * a file from a non-zero size to a zero size. This tries to
1137 * flush down new bytes that may have been written if the
1138 * application were using truncate to replace a file in place.
1139 */
1140 if (BTRFS_I(inode)->ordered_data_close) {
1141 BTRFS_I(inode)->ordered_data_close = 0;
1142 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1143 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1144 filemap_flush(inode->i_mapping);
1145 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001146 if (filp->private_data)
1147 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001148 return 0;
1149}
1150
Chris Masond352ac62008-09-29 15:18:18 -04001151/*
1152 * fsync call for both files and directories. This logs the inode into
1153 * the tree log instead of forcing full commits whenever possible.
1154 *
1155 * It needs to call filemap_fdatawait so that all ordered extent updates are
1156 * in the metadata btree are up to date for copying to the log.
1157 *
1158 * It drops the inode mutex before doing the tree log commit. This is an
1159 * important optimization for directories because holding the mutex prevents
1160 * new operations on the dir while we write to disk.
1161 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001162int btrfs_sync_file(struct file *file, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001163{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001164 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001165 struct inode *inode = dentry->d_inode;
1166 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001167 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001168 struct btrfs_trans_handle *trans;
1169
Chris Mason257c62e2009-10-13 13:21:08 -04001170
1171 /* we wait first, since the writeback may change the inode */
1172 root->log_batch++;
1173 /* the VFS called filemap_fdatawrite for us */
1174 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1175 root->log_batch++;
1176
Chris Mason39279cc2007-06-12 06:35:45 -04001177 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001178 * check the transaction that last modified this inode
1179 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001180 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001181 if (!BTRFS_I(inode)->last_trans)
1182 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001183
Chris Mason257c62e2009-10-13 13:21:08 -04001184 /*
1185 * if the last transaction that changed this file was before
1186 * the current transaction, we can bail out now without any
1187 * syncing
1188 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001189 mutex_lock(&root->fs_info->trans_mutex);
1190 if (BTRFS_I(inode)->last_trans <=
1191 root->fs_info->last_trans_committed) {
1192 BTRFS_I(inode)->last_trans = 0;
1193 mutex_unlock(&root->fs_info->trans_mutex);
1194 goto out;
1195 }
1196 mutex_unlock(&root->fs_info->trans_mutex);
1197
1198 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001199 * ok we haven't committed the transaction yet, lets do a commit
1200 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001201 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001202 btrfs_ioctl_trans_end(file);
1203
Yan, Zhenga22285a2010-05-16 10:48:46 -04001204 trans = btrfs_start_transaction(root, 0);
1205 if (IS_ERR(trans)) {
1206 ret = PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04001207 goto out;
1208 }
Chris Masone02119d2008-09-05 16:13:11 -04001209
Chris Mason2cfbd502009-02-20 10:55:10 -05001210 ret = btrfs_log_dentry_safe(trans, root, dentry);
Chris Masond3977122009-01-05 21:25:51 -05001211 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04001212 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001213
1214 /* we've logged all the items and now have a consistent
1215 * version of the file in the log. It is possible that
1216 * someone will come in and modify the file, but that's
1217 * fine because the log is consistent on disk, and we
1218 * have references to all of the file's extents
1219 *
1220 * It is possible that someone will come in and log the
1221 * file again, but that will end up using the synchronization
1222 * inside btrfs_sync_log to keep things safe.
1223 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001224 mutex_unlock(&dentry->d_inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001225
Chris Mason257c62e2009-10-13 13:21:08 -04001226 if (ret != BTRFS_NO_LOG_SYNC) {
1227 if (ret > 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001228 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001229 } else {
1230 ret = btrfs_sync_log(trans, root);
1231 if (ret == 0)
1232 ret = btrfs_end_transaction(trans, root);
1233 else
1234 ret = btrfs_commit_transaction(trans, root);
1235 }
1236 } else {
1237 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001238 }
Chris Mason2cfbd502009-02-20 10:55:10 -05001239 mutex_lock(&dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001240out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001241 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001242}
1243
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001244static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001245 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001246 .page_mkwrite = btrfs_page_mkwrite,
1247};
1248
1249static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1250{
Miao Xie058a4572010-05-20 07:21:50 +00001251 struct address_space *mapping = filp->f_mapping;
1252
1253 if (!mapping->a_ops->readpage)
1254 return -ENOEXEC;
1255
Chris Mason9ebefb182007-06-15 13:50:00 -04001256 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00001257 vma->vm_ops = &btrfs_file_vm_ops;
1258 vma->vm_flags |= VM_CAN_NONLINEAR;
1259
Chris Mason9ebefb182007-06-15 13:50:00 -04001260 return 0;
1261}
1262
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001263static long btrfs_fallocate(struct file *file, int mode,
1264 loff_t offset, loff_t len)
1265{
1266 struct inode *inode = file->f_path.dentry->d_inode;
1267 struct extent_state *cached_state = NULL;
1268 u64 cur_offset;
1269 u64 last_byte;
1270 u64 alloc_start;
1271 u64 alloc_end;
1272 u64 alloc_hint = 0;
1273 u64 locked_end;
1274 u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
1275 struct extent_map *em;
1276 int ret;
1277
1278 alloc_start = offset & ~mask;
1279 alloc_end = (offset + len + mask) & ~mask;
1280
1281 /* We only support the FALLOC_FL_KEEP_SIZE mode */
1282 if (mode & ~FALLOC_FL_KEEP_SIZE)
1283 return -EOPNOTSUPP;
1284
1285 /*
1286 * wait for ordered IO before we have any locks. We'll loop again
1287 * below with the locks held.
1288 */
1289 btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
1290
1291 mutex_lock(&inode->i_mutex);
1292 ret = inode_newsize_ok(inode, alloc_end);
1293 if (ret)
1294 goto out;
1295
1296 if (alloc_start > inode->i_size) {
1297 ret = btrfs_cont_expand(inode, alloc_start);
1298 if (ret)
1299 goto out;
1300 }
1301
1302 ret = btrfs_check_data_free_space(inode, alloc_end - alloc_start);
1303 if (ret)
1304 goto out;
1305
1306 locked_end = alloc_end - 1;
1307 while (1) {
1308 struct btrfs_ordered_extent *ordered;
1309
1310 /* the extent lock is ordered inside the running
1311 * transaction
1312 */
1313 lock_extent_bits(&BTRFS_I(inode)->io_tree, alloc_start,
1314 locked_end, 0, &cached_state, GFP_NOFS);
1315 ordered = btrfs_lookup_first_ordered_extent(inode,
1316 alloc_end - 1);
1317 if (ordered &&
1318 ordered->file_offset + ordered->len > alloc_start &&
1319 ordered->file_offset < alloc_end) {
1320 btrfs_put_ordered_extent(ordered);
1321 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
1322 alloc_start, locked_end,
1323 &cached_state, GFP_NOFS);
1324 /*
1325 * we can't wait on the range with the transaction
1326 * running or with the extent lock held
1327 */
1328 btrfs_wait_ordered_range(inode, alloc_start,
1329 alloc_end - alloc_start);
1330 } else {
1331 if (ordered)
1332 btrfs_put_ordered_extent(ordered);
1333 break;
1334 }
1335 }
1336
1337 cur_offset = alloc_start;
1338 while (1) {
1339 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
1340 alloc_end - cur_offset, 0);
1341 BUG_ON(IS_ERR(em) || !em);
1342 last_byte = min(extent_map_end(em), alloc_end);
1343 last_byte = (last_byte + mask) & ~mask;
1344 if (em->block_start == EXTENT_MAP_HOLE ||
1345 (cur_offset >= inode->i_size &&
1346 !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
1347 ret = btrfs_prealloc_file_range(inode, mode, cur_offset,
1348 last_byte - cur_offset,
1349 1 << inode->i_blkbits,
1350 offset + len,
1351 &alloc_hint);
1352 if (ret < 0) {
1353 free_extent_map(em);
1354 break;
1355 }
1356 }
1357 free_extent_map(em);
1358
1359 cur_offset = last_byte;
1360 if (cur_offset >= alloc_end) {
1361 ret = 0;
1362 break;
1363 }
1364 }
1365 unlock_extent_cached(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
1366 &cached_state, GFP_NOFS);
1367
1368 btrfs_free_reserved_data_space(inode, alloc_end - alloc_start);
1369out:
1370 mutex_unlock(&inode->i_mutex);
1371 return ret;
1372}
1373
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001374const struct file_operations btrfs_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04001375 .llseek = generic_file_llseek,
1376 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00001377 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001378 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001379 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001380 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001381 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001382 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001383 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001384 .fsync = btrfs_sync_file,
Christoph Hellwig2fe17c12011-01-14 13:07:43 +01001385 .fallocate = btrfs_fallocate,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001386 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001387#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001388 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001389#endif
1390};