blob: 66836d85763bbfd2dafe3821a1be30f173784fdc [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>
27#include <linux/swap.h>
28#include <linux/writeback.h>
29#include <linux/statfs.h>
30#include <linux/compat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Chris Mason39279cc2007-06-12 06:35:45 -040032#include "ctree.h"
33#include "disk-io.h"
34#include "transaction.h"
35#include "btrfs_inode.h"
36#include "ioctl.h"
37#include "print-tree.h"
Chris Masone02119d2008-09-05 16:13:11 -040038#include "tree-log.h"
39#include "locking.h"
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -040040#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040041
42
Chris Masond352ac62008-09-29 15:18:18 -040043/* simple helper to fault in pages and copy. This should go away
44 * and be replaced with calls into generic code.
45 */
Chris Masond3977122009-01-05 21:25:51 -050046static noinline int btrfs_copy_from_user(loff_t pos, int num_pages,
Chris Masona1b32a52008-09-05 16:09:51 -040047 int write_bytes,
48 struct page **prepared_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -040049 struct iov_iter *i)
Chris Mason39279cc2007-06-12 06:35:45 -040050{
Xin Zhong914ee292010-12-09 09:30:14 +000051 size_t copied = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -040052 int pg = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040053 int offset = pos & (PAGE_CACHE_SIZE - 1);
Xin Zhong914ee292010-12-09 09:30:14 +000054 int total_copied = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040055
Josef Bacik11c65dc2010-05-23 11:07:21 -040056 while (write_bytes > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -040057 size_t count = min_t(size_t,
58 PAGE_CACHE_SIZE - offset, write_bytes);
Josef Bacik11c65dc2010-05-23 11:07:21 -040059 struct page *page = prepared_pages[pg];
Xin Zhong914ee292010-12-09 09:30:14 +000060 /*
61 * Copy data from userspace to the current page
62 *
63 * Disable pagefault to avoid recursive lock since
64 * the pages are already locked
65 */
66 pagefault_disable();
67 copied = iov_iter_copy_from_user_atomic(page, i, offset, count);
68 pagefault_enable();
Josef Bacik11c65dc2010-05-23 11:07:21 -040069
Chris Mason39279cc2007-06-12 06:35:45 -040070 /* Flush processor's dcache for this page */
71 flush_dcache_page(page);
Josef Bacik11c65dc2010-05-23 11:07:21 -040072 iov_iter_advance(i, copied);
73 write_bytes -= copied;
Xin Zhong914ee292010-12-09 09:30:14 +000074 total_copied += copied;
Chris Mason39279cc2007-06-12 06:35:45 -040075
Xin Zhong914ee292010-12-09 09:30:14 +000076 /* Return to btrfs_file_aio_write to fault page */
Josef Bacik11c65dc2010-05-23 11:07:21 -040077 if (unlikely(copied == 0)) {
Xin Zhong914ee292010-12-09 09:30:14 +000078 break;
Josef Bacik11c65dc2010-05-23 11:07:21 -040079 }
80
81 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
82 offset += copied;
83 } else {
84 pg++;
85 offset = 0;
86 }
Chris Mason39279cc2007-06-12 06:35:45 -040087 }
Xin Zhong914ee292010-12-09 09:30:14 +000088 return total_copied;
Chris Mason39279cc2007-06-12 06:35:45 -040089}
90
Chris Masond352ac62008-09-29 15:18:18 -040091/*
92 * unlocks pages after btrfs_file_write is done with them
93 */
Chris Masond3977122009-01-05 21:25:51 -050094static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -040095{
96 size_t i;
97 for (i = 0; i < num_pages; i++) {
98 if (!pages[i])
99 break;
Chris Masond352ac62008-09-29 15:18:18 -0400100 /* page checked is some magic around finding pages that
101 * have been modified without going through btrfs_set_page_dirty
102 * clear it here
103 */
Chris Mason4a096752008-07-21 10:29:44 -0400104 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400105 unlock_page(pages[i]);
106 mark_page_accessed(pages[i]);
107 page_cache_release(pages[i]);
108 }
109}
110
Chris Masond352ac62008-09-29 15:18:18 -0400111/*
112 * after copy_from_user, pages need to be dirtied and we need to make
113 * sure holes are created between the current EOF and the start of
114 * any next extents (if required).
115 *
116 * this also makes the decision about creating an inline extent vs
117 * doing real data extents, marking pages dirty and delalloc as required.
118 */
Chris Masond3977122009-01-05 21:25:51 -0500119static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400120 struct btrfs_root *root,
121 struct file *file,
122 struct page **pages,
123 size_t num_pages,
124 loff_t pos,
125 size_t write_bytes)
126{
Chris Mason39279cc2007-06-12 06:35:45 -0400127 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400128 int i;
Chris Mason6da6aba2007-12-18 16:15:09 -0500129 struct inode *inode = fdentry(file)->d_inode;
Chris Masondb945352007-10-15 16:15:53 -0400130 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400131 u64 start_pos;
132 u64 end_of_last_block;
133 u64 end_pos = pos + write_bytes;
134 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400135
Chris Mason5f39d392007-10-15 16:14:19 -0400136 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400137 num_bytes = (write_bytes + pos - start_pos +
138 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400139
Chris Masondb945352007-10-15 16:15:53 -0400140 end_of_last_block = start_pos + num_bytes - 1;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000141 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
142 NULL);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400143 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400144
Chris Masonc8b97812008-10-29 14:49:59 -0400145 for (i = 0; i < num_pages; i++) {
146 struct page *p = pages[i];
147 SetPageUptodate(p);
148 ClearPageChecked(p);
149 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400150 }
151 if (end_pos > isize) {
152 i_size_write(inode, end_pos);
Chris Masonf597bb12009-06-27 21:06:22 -0400153 /* we've only changed i_size in ram, and we haven't updated
154 * the disk i_size. There is no need to log the inode
155 * at this time.
156 */
Chris Mason39279cc2007-06-12 06:35:45 -0400157 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400158 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400159}
160
Chris Masond352ac62008-09-29 15:18:18 -0400161/*
162 * this drops all the extents in the cache that intersect the range
163 * [start, end]. Existing extents are split as required.
164 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400165int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
166 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400167{
168 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400169 struct extent_map *split = NULL;
170 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400171 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500172 u64 len = end - start + 1;
Chris Mason3b951512008-04-17 11:29:12 -0400173 int ret;
174 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400175 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400176 int compressed = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400177
Chris Masone6dcd2d2008-07-17 12:53:50 -0400178 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400179 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500180 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400181 testend = 0;
182 }
Chris Masond3977122009-01-05 21:25:51 -0500183 while (1) {
Chris Mason3b951512008-04-17 11:29:12 -0400184 if (!split)
185 split = alloc_extent_map(GFP_NOFS);
186 if (!split2)
187 split2 = alloc_extent_map(GFP_NOFS);
188
Chris Mason890871b2009-09-02 16:24:52 -0400189 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500190 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500191 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400192 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400193 break;
Chris Masond1310b22008-01-24 16:13:08 -0500194 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400195 flags = em->flags;
196 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000197 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400198 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400199 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400200 break;
201 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000202 start = em->start + em->len;
203 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400204 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400205 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400206 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400207 continue;
208 }
Chris Masonc8b97812008-10-29 14:49:59 -0400209 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400210 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400211 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400212
213 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
214 em->start < start) {
215 split->start = em->start;
216 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500217 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400218 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400219
220 if (compressed)
221 split->block_len = em->block_len;
222 else
223 split->block_len = split->len;
224
Chris Mason3b951512008-04-17 11:29:12 -0400225 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400226 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400227 ret = add_extent_mapping(em_tree, split);
228 BUG_ON(ret);
229 free_extent_map(split);
230 split = split2;
231 split2 = NULL;
232 }
233 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
234 testend && em->start + em->len > start + len) {
235 u64 diff = start + len - em->start;
236
237 split->start = start + len;
238 split->len = em->start + em->len - (start + len);
239 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400240 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400241
Chris Masonc8b97812008-10-29 14:49:59 -0400242 if (compressed) {
243 split->block_len = em->block_len;
244 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500245 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400246 } else {
247 split->block_len = split->len;
248 split->block_start = em->block_start + diff;
Chris Mason445a6942008-11-10 11:53:33 -0500249 split->orig_start = split->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400250 }
Chris Mason3b951512008-04-17 11:29:12 -0400251
252 ret = add_extent_mapping(em_tree, split);
253 BUG_ON(ret);
254 free_extent_map(split);
255 split = NULL;
256 }
Chris Mason890871b2009-09-02 16:24:52 -0400257 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500258
Chris Masona52d9a82007-08-27 16:49:44 -0400259 /* once for us */
260 free_extent_map(em);
261 /* once for the tree*/
262 free_extent_map(em);
263 }
Chris Mason3b951512008-04-17 11:29:12 -0400264 if (split)
265 free_extent_map(split);
266 if (split2)
267 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400268 return 0;
269}
270
Chris Mason39279cc2007-06-12 06:35:45 -0400271/*
272 * this is very complex, but the basic idea is to drop all extents
273 * in the range start - end. hint_block is filled in with a block number
274 * that would be a good hint to the block allocator for this file.
275 *
276 * If an extent intersects the range but is not entirely inside the range
277 * it is either truncated or split. Anything entirely inside the range
278 * is deleted from the tree.
279 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000280int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
281 u64 start, u64 end, u64 *hint_byte, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400282{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000283 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason00f5c792007-11-30 10:09:33 -0500284 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000285 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500286 struct btrfs_path *path;
287 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000288 struct btrfs_key new_key;
289 u64 search_start = start;
290 u64 disk_bytenr = 0;
291 u64 num_bytes = 0;
292 u64 extent_offset = 0;
293 u64 extent_end = 0;
294 int del_nr = 0;
295 int del_slot = 0;
296 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400297 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500298 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400299
Chris Masona1ed8352009-09-11 12:27:37 -0400300 if (drop_cache)
301 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400302
Chris Mason39279cc2007-06-12 06:35:45 -0400303 path = btrfs_alloc_path();
304 if (!path)
305 return -ENOMEM;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000306
Chris Masond3977122009-01-05 21:25:51 -0500307 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400308 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400309 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
310 search_start, -1);
311 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000312 break;
313 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
314 leaf = path->nodes[0];
315 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
316 if (key.objectid == inode->i_ino &&
317 key.type == BTRFS_EXTENT_DATA_KEY)
318 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400319 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400320 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000321next_slot:
322 leaf = path->nodes[0];
323 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
324 BUG_ON(del_nr > 0);
325 ret = btrfs_next_leaf(root, path);
326 if (ret < 0)
327 break;
328 if (ret > 0) {
329 ret = 0;
330 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400331 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000332 leaf = path->nodes[0];
333 recow = 1;
334 }
335
336 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
337 if (key.objectid > inode->i_ino ||
338 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
339 break;
340
341 fi = btrfs_item_ptr(leaf, path->slots[0],
342 struct btrfs_file_extent_item);
343 extent_type = btrfs_file_extent_type(leaf, fi);
344
345 if (extent_type == BTRFS_FILE_EXTENT_REG ||
346 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
347 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
348 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
349 extent_offset = btrfs_file_extent_offset(leaf, fi);
350 extent_end = key.offset +
351 btrfs_file_extent_num_bytes(leaf, fi);
352 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
353 extent_end = key.offset +
354 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400355 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000356 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400357 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400358 }
359
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000360 if (extent_end <= search_start) {
361 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400362 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400363 }
364
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000365 search_start = max(key.offset, start);
366 if (recow) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400367 btrfs_release_path(root, path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000368 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400369 }
Chris Mason771ed682008-11-06 22:02:51 -0500370
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000371 /*
372 * | - range to drop - |
373 * | -------- extent -------- |
374 */
375 if (start > key.offset && end < extent_end) {
376 BUG_ON(del_nr > 0);
377 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
378
379 memcpy(&new_key, &key, sizeof(new_key));
380 new_key.offset = start;
381 ret = btrfs_duplicate_item(trans, root, path,
382 &new_key);
383 if (ret == -EAGAIN) {
384 btrfs_release_path(root, path);
385 continue;
386 }
387 if (ret < 0)
388 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400389
Chris Mason5f39d392007-10-15 16:14:19 -0400390 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000391 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
392 struct btrfs_file_extent_item);
393 btrfs_set_file_extent_num_bytes(leaf, fi,
394 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400395
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000396 fi = btrfs_item_ptr(leaf, path->slots[0],
397 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400398
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000399 extent_offset += start - key.offset;
400 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
401 btrfs_set_file_extent_num_bytes(leaf, fi,
402 extent_end - start);
403 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400404
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000405 if (disk_bytenr > 0) {
406 ret = btrfs_inc_extent_ref(trans, root,
407 disk_bytenr, num_bytes, 0,
408 root->root_key.objectid,
409 new_key.objectid,
410 start - extent_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -0400411 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000412 *hint_byte = disk_bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400413 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000414 key.offset = start;
415 }
416 /*
417 * | ---- range to drop ----- |
418 * | -------- extent -------- |
419 */
420 if (start <= key.offset && end < extent_end) {
421 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
422
423 memcpy(&new_key, &key, sizeof(new_key));
424 new_key.offset = end;
425 btrfs_set_item_key_safe(trans, root, path, &new_key);
426
427 extent_offset += end - key.offset;
428 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
429 btrfs_set_file_extent_num_bytes(leaf, fi,
430 extent_end - end);
431 btrfs_mark_buffer_dirty(leaf);
432 if (disk_bytenr > 0) {
433 inode_sub_bytes(inode, end - key.offset);
434 *hint_byte = disk_bytenr;
435 }
436 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400437 }
438
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000439 search_start = extent_end;
440 /*
441 * | ---- range to drop ----- |
442 * | -------- extent -------- |
443 */
444 if (start > key.offset && end >= extent_end) {
445 BUG_ON(del_nr > 0);
446 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
447
448 btrfs_set_file_extent_num_bytes(leaf, fi,
449 start - key.offset);
450 btrfs_mark_buffer_dirty(leaf);
451 if (disk_bytenr > 0) {
452 inode_sub_bytes(inode, extent_end - start);
453 *hint_byte = disk_bytenr;
454 }
455 if (end == extent_end)
456 break;
457
458 path->slots[0]++;
459 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400460 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000461
462 /*
463 * | ---- range to drop ----- |
464 * | ------ extent ------ |
465 */
466 if (start <= key.offset && end >= extent_end) {
467 if (del_nr == 0) {
468 del_slot = path->slots[0];
469 del_nr = 1;
470 } else {
471 BUG_ON(del_slot + del_nr != path->slots[0]);
472 del_nr++;
473 }
474
475 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
476 inode_sub_bytes(inode,
477 extent_end - key.offset);
478 extent_end = ALIGN(extent_end,
479 root->sectorsize);
480 } else if (disk_bytenr > 0) {
481 ret = btrfs_free_extent(trans, root,
482 disk_bytenr, num_bytes, 0,
483 root->root_key.objectid,
484 key.objectid, key.offset -
485 extent_offset);
486 BUG_ON(ret);
487 inode_sub_bytes(inode,
488 extent_end - key.offset);
489 *hint_byte = disk_bytenr;
490 }
491
492 if (end == extent_end)
493 break;
494
495 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
496 path->slots[0]++;
497 goto next_slot;
498 }
499
500 ret = btrfs_del_items(trans, root, path, del_slot,
501 del_nr);
502 BUG_ON(ret);
503
504 del_nr = 0;
505 del_slot = 0;
506
507 btrfs_release_path(root, path);
508 continue;
509 }
510
511 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400512 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000513
514 if (del_nr > 0) {
515 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
516 BUG_ON(ret);
517 }
518
Chris Mason39279cc2007-06-12 06:35:45 -0400519 btrfs_free_path(path);
520 return ret;
521}
522
Yan Zhengd899e052008-10-30 14:25:28 -0400523static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000524 u64 objectid, u64 bytenr, u64 orig_offset,
525 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400526{
527 struct btrfs_file_extent_item *fi;
528 struct btrfs_key key;
529 u64 extent_end;
530
531 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
532 return 0;
533
534 btrfs_item_key_to_cpu(leaf, &key, slot);
535 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
536 return 0;
537
538 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
539 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
540 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000541 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400542 btrfs_file_extent_compression(leaf, fi) ||
543 btrfs_file_extent_encryption(leaf, fi) ||
544 btrfs_file_extent_other_encoding(leaf, fi))
545 return 0;
546
547 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
548 if ((*start && *start != key.offset) || (*end && *end != extent_end))
549 return 0;
550
551 *start = key.offset;
552 *end = extent_end;
553 return 1;
554}
555
556/*
557 * Mark extent in the range start - end as written.
558 *
559 * This changes extent type from 'pre-allocated' to 'regular'. If only
560 * part of extent is marked as written, the extent will be split into
561 * two or three.
562 */
563int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400564 struct inode *inode, u64 start, u64 end)
565{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000566 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400567 struct extent_buffer *leaf;
568 struct btrfs_path *path;
569 struct btrfs_file_extent_item *fi;
570 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000571 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400572 u64 bytenr;
573 u64 num_bytes;
574 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400575 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400576 u64 other_start;
577 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000578 u64 split;
579 int del_nr = 0;
580 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000581 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400582 int ret;
583
584 btrfs_drop_extent_cache(inode, start, end - 1, 0);
585
586 path = btrfs_alloc_path();
587 BUG_ON(!path);
588again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000589 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000590 split = start;
Yan Zhengd899e052008-10-30 14:25:28 -0400591 key.objectid = inode->i_ino;
592 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000593 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -0400594
595 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
596 if (ret > 0 && path->slots[0] > 0)
597 path->slots[0]--;
598
599 leaf = path->nodes[0];
600 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
601 BUG_ON(key.objectid != inode->i_ino ||
602 key.type != BTRFS_EXTENT_DATA_KEY);
603 fi = btrfs_item_ptr(leaf, path->slots[0],
604 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000605 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
606 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -0400607 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
608 BUG_ON(key.offset > start || extent_end < end);
609
610 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
611 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400612 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000613 memcpy(&new_key, &key, sizeof(new_key));
614
615 if (start == key.offset && end < extent_end) {
616 other_start = 0;
617 other_end = start;
618 if (extent_mergeable(leaf, path->slots[0] - 1,
619 inode->i_ino, bytenr, orig_offset,
620 &other_start, &other_end)) {
621 new_key.offset = end;
622 btrfs_set_item_key_safe(trans, root, path, &new_key);
623 fi = btrfs_item_ptr(leaf, path->slots[0],
624 struct btrfs_file_extent_item);
625 btrfs_set_file_extent_num_bytes(leaf, fi,
626 extent_end - end);
627 btrfs_set_file_extent_offset(leaf, fi,
628 end - orig_offset);
629 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
630 struct btrfs_file_extent_item);
631 btrfs_set_file_extent_num_bytes(leaf, fi,
632 end - other_start);
633 btrfs_mark_buffer_dirty(leaf);
634 goto out;
635 }
636 }
637
638 if (start > key.offset && end == extent_end) {
639 other_start = end;
640 other_end = 0;
641 if (extent_mergeable(leaf, path->slots[0] + 1,
642 inode->i_ino, bytenr, orig_offset,
643 &other_start, &other_end)) {
644 fi = btrfs_item_ptr(leaf, path->slots[0],
645 struct btrfs_file_extent_item);
646 btrfs_set_file_extent_num_bytes(leaf, fi,
647 start - key.offset);
648 path->slots[0]++;
649 new_key.offset = start;
650 btrfs_set_item_key_safe(trans, root, path, &new_key);
651
652 fi = btrfs_item_ptr(leaf, path->slots[0],
653 struct btrfs_file_extent_item);
654 btrfs_set_file_extent_num_bytes(leaf, fi,
655 other_end - start);
656 btrfs_set_file_extent_offset(leaf, fi,
657 start - orig_offset);
658 btrfs_mark_buffer_dirty(leaf);
659 goto out;
660 }
661 }
Yan Zhengd899e052008-10-30 14:25:28 -0400662
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000663 while (start > key.offset || end < extent_end) {
664 if (key.offset == start)
665 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400666
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000667 new_key.offset = split;
668 ret = btrfs_duplicate_item(trans, root, path, &new_key);
669 if (ret == -EAGAIN) {
670 btrfs_release_path(root, path);
671 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -0400672 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000673 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -0400674
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000675 leaf = path->nodes[0];
676 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -0400677 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -0400678 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000679 split - key.offset);
680
681 fi = btrfs_item_ptr(leaf, path->slots[0],
682 struct btrfs_file_extent_item);
683
684 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
685 btrfs_set_file_extent_num_bytes(leaf, fi,
686 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -0400687 btrfs_mark_buffer_dirty(leaf);
688
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000689 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
690 root->root_key.objectid,
691 inode->i_ino, orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400692 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -0400693
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000694 if (split == start) {
695 key.offset = start;
696 } else {
697 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400698 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000699 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400700 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000701 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -0400702 }
703
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000704 other_start = end;
705 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000706 if (extent_mergeable(leaf, path->slots[0] + 1,
707 inode->i_ino, bytenr, orig_offset,
708 &other_start, &other_end)) {
709 if (recow) {
710 btrfs_release_path(root, path);
711 goto again;
712 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000713 extent_end = other_end;
714 del_slot = path->slots[0] + 1;
715 del_nr++;
716 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
717 0, root->root_key.objectid,
718 inode->i_ino, orig_offset);
719 BUG_ON(ret);
720 }
721 other_start = 0;
722 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000723 if (extent_mergeable(leaf, path->slots[0] - 1,
724 inode->i_ino, bytenr, orig_offset,
725 &other_start, &other_end)) {
726 if (recow) {
727 btrfs_release_path(root, path);
728 goto again;
729 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000730 key.offset = other_start;
731 del_slot = path->slots[0];
732 del_nr++;
733 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
734 0, root->root_key.objectid,
735 inode->i_ino, orig_offset);
736 BUG_ON(ret);
737 }
738 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000739 fi = btrfs_item_ptr(leaf, path->slots[0],
740 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000741 btrfs_set_file_extent_type(leaf, fi,
742 BTRFS_FILE_EXTENT_REG);
743 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000744 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000745 fi = btrfs_item_ptr(leaf, del_slot - 1,
746 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000747 btrfs_set_file_extent_type(leaf, fi,
748 BTRFS_FILE_EXTENT_REG);
749 btrfs_set_file_extent_num_bytes(leaf, fi,
750 extent_end - key.offset);
751 btrfs_mark_buffer_dirty(leaf);
752
753 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
754 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000755 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000756out:
Yan Zhengd899e052008-10-30 14:25:28 -0400757 btrfs_free_path(path);
758 return 0;
759}
760
Chris Mason39279cc2007-06-12 06:35:45 -0400761/*
Chris Masond352ac62008-09-29 15:18:18 -0400762 * this gets pages into the page cache and locks them down, it also properly
763 * waits for data=ordered extents to finish before allowing the pages to be
764 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -0400765 */
Chris Masond3977122009-01-05 21:25:51 -0500766static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500767 struct page **pages, size_t num_pages,
768 loff_t pos, unsigned long first_index,
769 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400770{
Josef Bacik2ac55d42010-02-03 19:33:23 +0000771 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -0400772 int i;
773 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500774 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400775 int err = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400776 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400777 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400778
Chris Mason5f39d392007-10-15 16:14:19 -0400779 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400780 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400781
Yan Zheng9036c102008-10-30 14:19:41 -0400782 if (start_pos > inode->i_size) {
783 err = btrfs_cont_expand(inode, start_pos);
784 if (err)
785 return err;
786 }
787
Chris Mason39279cc2007-06-12 06:35:45 -0400788 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400789again:
Chris Mason39279cc2007-06-12 06:35:45 -0400790 for (i = 0; i < num_pages; i++) {
791 pages[i] = grab_cache_page(inode->i_mapping, index + i);
792 if (!pages[i]) {
793 err = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -0400794 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400795 }
Chris Masonccd467d2007-06-28 15:57:36 -0400796 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400797 }
Chris Mason07627042008-02-19 11:29:24 -0500798 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400799 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000800 lock_extent_bits(&BTRFS_I(inode)->io_tree,
801 start_pos, last_pos - 1, 0, &cached_state,
802 GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500803 ordered = btrfs_lookup_first_ordered_extent(inode,
804 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400805 if (ordered &&
806 ordered->file_offset + ordered->len > start_pos &&
807 ordered->file_offset < last_pos) {
808 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000809 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
810 start_pos, last_pos - 1,
811 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400812 for (i = 0; i < num_pages; i++) {
813 unlock_page(pages[i]);
814 page_cache_release(pages[i]);
815 }
816 btrfs_wait_ordered_range(inode, start_pos,
817 last_pos - start_pos);
818 goto again;
819 }
820 if (ordered)
821 btrfs_put_ordered_extent(ordered);
822
Josef Bacik2ac55d42010-02-03 19:33:23 +0000823 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -0400824 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +0000825 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
Chris Mason07627042008-02-19 11:29:24 -0500826 GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000827 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
828 start_pos, last_pos - 1, &cached_state,
829 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500830 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400831 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400832 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400833 set_page_extent_mapped(pages[i]);
834 WARN_ON(!PageLocked(pages[i]));
835 }
Chris Mason39279cc2007-06-12 06:35:45 -0400836 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400837}
838
Josef Bacik11c65dc2010-05-23 11:07:21 -0400839static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
840 const struct iovec *iov,
841 unsigned long nr_segs, loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400842{
Josef Bacik11c65dc2010-05-23 11:07:21 -0400843 struct file *file = iocb->ki_filp;
844 struct inode *inode = fdentry(file)->d_inode;
845 struct btrfs_root *root = BTRFS_I(inode)->root;
846 struct page *pinned[2];
847 struct page **pages = NULL;
848 struct iov_iter i;
849 loff_t *ppos = &iocb->ki_pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400850 loff_t start_pos;
851 ssize_t num_written = 0;
852 ssize_t err = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400853 size_t count;
854 size_t ocount;
Chris Mason39279cc2007-06-12 06:35:45 -0400855 int ret = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400856 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400857 unsigned long first_index;
858 unsigned long last_index;
Chris Masoncb843a62008-10-03 12:30:02 -0400859 int will_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400860 int buffered = 0;
Xin Zhong914ee292010-12-09 09:30:14 +0000861 int copied = 0;
862 int dirty_pages = 0;
Chris Masoncb843a62008-10-03 12:30:02 -0400863
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100864 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
Chris Masoncb843a62008-10-03 12:30:02 -0400865 (file->f_flags & O_DIRECT));
Chris Mason8c2383c2007-06-18 09:57:58 -0400866
Chris Mason39279cc2007-06-12 06:35:45 -0400867 pinned[0] = NULL;
868 pinned[1] = NULL;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400869
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400870 start_pos = pos;
871
Chris Mason39279cc2007-06-12 06:35:45 -0400872 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
Chris Mason2b1f55b2008-09-24 11:48:04 -0400873
Chris Masonab93dbe2009-10-01 12:29:10 -0400874 mutex_lock(&inode->i_mutex);
875
Josef Bacik11c65dc2010-05-23 11:07:21 -0400876 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
877 if (err)
878 goto out;
879 count = ocount;
880
Chris Mason1832a6d2007-12-21 16:27:21 -0500881 current->backing_dev_info = inode->i_mapping->backing_dev_info;
Chris Mason39279cc2007-06-12 06:35:45 -0400882 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
Chris Mason1832a6d2007-12-21 16:27:21 -0500883 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400884 goto out;
885
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -0400886 if (count == 0)
Chris Masonab93dbe2009-10-01 12:29:10 -0400887 goto out;
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400888
889 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400890 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400891 goto out;
892
Chris Mason39279cc2007-06-12 06:35:45 -0400893 file_update_time(file);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400894 BTRFS_I(inode)->sequence++;
Chris Mason39279cc2007-06-12 06:35:45 -0400895
Josef Bacik4b46fce2010-05-23 11:00:55 -0400896 if (unlikely(file->f_flags & O_DIRECT)) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400897 num_written = generic_file_direct_write(iocb, iov, &nr_segs,
898 pos, ppos, count,
899 ocount);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400900 /*
901 * the generic O_DIRECT will update in-memory i_size after the
902 * DIOs are done. But our endio handlers that update the on
903 * disk i_size never update past the in memory i_size. So we
904 * need one more update here to catch any additions to the
905 * file
906 */
907 if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
908 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
909 mark_inode_dirty(inode);
910 }
911
912 if (num_written < 0) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400913 ret = num_written;
914 num_written = 0;
915 goto out;
916 } else if (num_written == count) {
917 /* pick up pos changes done by the generic code */
918 pos = *ppos;
919 goto out;
920 }
Josef Bacik4b46fce2010-05-23 11:00:55 -0400921 /*
922 * We are going to do buffered for the rest of the range, so we
923 * need to make sure to invalidate the buffered pages when we're
924 * done.
925 */
926 buffered = 1;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400927 pos += num_written;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400928 }
929
Josef Bacik11c65dc2010-05-23 11:07:21 -0400930 iov_iter_init(&i, iov, nr_segs, count, num_written);
931 nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
932 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
933 (sizeof(struct page *)));
Chris Mason8c2383c2007-06-18 09:57:58 -0400934 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -0400935
Chris Masonab93dbe2009-10-01 12:29:10 -0400936 /* generic_write_checks can change our pos */
937 start_pos = pos;
938
Chris Mason39279cc2007-06-12 06:35:45 -0400939 first_index = pos >> PAGE_CACHE_SHIFT;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400940 last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400941
942 /*
943 * there are lots of better ways to do this, but this code
944 * makes sure the first and last page in the file range are
945 * up to date and ready for cow
946 */
947 if ((pos & (PAGE_CACHE_SIZE - 1))) {
948 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
949 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400950 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400951 BUG_ON(ret);
952 wait_on_page_locked(pinned[0]);
953 } else {
954 unlock_page(pinned[0]);
955 }
956 }
Josef Bacik11c65dc2010-05-23 11:07:21 -0400957 if ((pos + iov_iter_count(&i)) & (PAGE_CACHE_SIZE - 1)) {
Chris Mason39279cc2007-06-12 06:35:45 -0400958 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
959 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400960 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400961 BUG_ON(ret);
962 wait_on_page_locked(pinned[1]);
963 } else {
964 unlock_page(pinned[1]);
965 }
966 }
967
Josef Bacik11c65dc2010-05-23 11:07:21 -0400968 while (iov_iter_count(&i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400969 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400970 size_t write_bytes = min(iov_iter_count(&i),
971 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400972 offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400973 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
974 PAGE_CACHE_SHIFT;
975
Chris Mason8c2383c2007-06-18 09:57:58 -0400976 WARN_ON(num_pages > nrptrs);
yanhai zhu9aead432009-01-05 15:49:11 -0500977 memset(pages, 0, sizeof(struct page *) * nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -0500978
Xin Zhong914ee292010-12-09 09:30:14 +0000979 /*
980 * Fault pages before locking them in prepare_pages
981 * to avoid recursive lock
982 */
983 if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) {
984 ret = -EFAULT;
985 goto out;
986 }
987
988 ret = btrfs_delalloc_reserve_space(inode,
989 num_pages << PAGE_CACHE_SHIFT);
Chris Mason1832a6d2007-12-21 16:27:21 -0500990 if (ret)
991 goto out;
992
Chris Mason39279cc2007-06-12 06:35:45 -0400993 ret = prepare_pages(root, file, pages, num_pages,
994 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400995 write_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500996 if (ret) {
Xin Zhong914ee292010-12-09 09:30:14 +0000997 btrfs_delalloc_release_space(inode,
998 num_pages << PAGE_CACHE_SHIFT);
Chris Mason54aa1f42007-06-22 14:16:25 -0400999 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -05001000 }
Chris Mason39279cc2007-06-12 06:35:45 -04001001
Xin Zhong914ee292010-12-09 09:30:14 +00001002 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001003 write_bytes, pages, &i);
Xin Zhong914ee292010-12-09 09:30:14 +00001004 dirty_pages = (copied + PAGE_CACHE_SIZE - 1) >>
1005 PAGE_CACHE_SHIFT;
1006
1007 if (num_pages > dirty_pages) {
1008 if (copied > 0)
1009 atomic_inc(
1010 &BTRFS_I(inode)->outstanding_extents);
1011 btrfs_delalloc_release_space(inode,
1012 (num_pages - dirty_pages) <<
1013 PAGE_CACHE_SHIFT);
1014 }
1015
1016 if (copied > 0) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001017 dirty_and_release_pages(NULL, root, file, pages,
Xin Zhong914ee292010-12-09 09:30:14 +00001018 dirty_pages, pos, copied);
Chris Mason54aa1f42007-06-22 14:16:25 -04001019 }
Chris Mason39279cc2007-06-12 06:35:45 -04001020
Chris Mason39279cc2007-06-12 06:35:45 -04001021 btrfs_drop_pages(pages, num_pages);
Xin Zhong914ee292010-12-09 09:30:14 +00001022
1023 if (copied > 0) {
1024 if (will_write) {
1025 filemap_fdatawrite_range(inode->i_mapping, pos,
1026 pos + copied - 1);
1027 } else {
1028 balance_dirty_pages_ratelimited_nr(
1029 inode->i_mapping,
1030 dirty_pages);
1031 if (dirty_pages <
1032 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1033 btrfs_btree_balance_dirty(root, 1);
1034 btrfs_throttle(root);
1035 }
Josef Bacik6a632092009-02-20 11:00:09 -05001036 }
Chris Mason39279cc2007-06-12 06:35:45 -04001037
Xin Zhong914ee292010-12-09 09:30:14 +00001038 pos += copied;
1039 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001040
Chris Mason39279cc2007-06-12 06:35:45 -04001041 cond_resched();
1042 }
Chris Mason39279cc2007-06-12 06:35:45 -04001043out:
Chris Mason1832a6d2007-12-21 16:27:21 -05001044 mutex_unlock(&inode->i_mutex);
Josef Bacik6a632092009-02-20 11:00:09 -05001045 if (ret)
1046 err = ret;
Chris Mason5b92ee72008-01-03 13:46:11 -05001047
Chris Mason8c2383c2007-06-18 09:57:58 -04001048 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -04001049 if (pinned[0])
1050 page_cache_release(pinned[0]);
1051 if (pinned[1])
1052 page_cache_release(pinned[1]);
1053 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001054
Chris Mason5a3f23d2009-03-31 13:27:11 -04001055 /*
1056 * we want to make sure fsync finds this change
1057 * but we haven't joined a transaction running right now.
1058 *
1059 * Later on, someone is sure to update the inode and get the
1060 * real transid recorded.
1061 *
1062 * We set last_trans now to the fs_info generation + 1,
1063 * this will either be one more than the running transaction
1064 * or the generation used for the next transaction if there isn't
1065 * one running right now.
1066 */
1067 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1068
Chris Masoncb843a62008-10-03 12:30:02 -04001069 if (num_written > 0 && will_write) {
Chris Masone02119d2008-09-05 16:13:11 -04001070 struct btrfs_trans_handle *trans;
1071
Chris Masoncb843a62008-10-03 12:30:02 -04001072 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1073 if (err)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001074 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -04001075
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001076 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001077 trans = btrfs_start_transaction(root, 0);
Josef Bacik495e8672010-11-19 20:36:10 +00001078 if (IS_ERR(trans)) {
1079 num_written = PTR_ERR(trans);
1080 goto done;
1081 }
1082 mutex_lock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001083 ret = btrfs_log_dentry_safe(trans, root,
1084 file->f_dentry);
Josef Bacik495e8672010-11-19 20:36:10 +00001085 mutex_unlock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001086 if (ret == 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001087 ret = btrfs_sync_log(trans, root);
1088 if (ret == 0)
1089 btrfs_end_transaction(trans, root);
1090 else
1091 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001092 } else if (ret != BTRFS_NO_LOG_SYNC) {
Chris Masoncb843a62008-10-03 12:30:02 -04001093 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001094 } else {
1095 btrfs_end_transaction(trans, root);
Chris Masoncb843a62008-10-03 12:30:02 -04001096 }
Chris Masone02119d2008-09-05 16:13:11 -04001097 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04001098 if (file->f_flags & O_DIRECT && buffered) {
Chris Masoncb843a62008-10-03 12:30:02 -04001099 invalidate_mapping_pages(inode->i_mapping,
1100 start_pos >> PAGE_CACHE_SHIFT,
1101 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1102 }
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001103 }
Josef Bacik495e8672010-11-19 20:36:10 +00001104done:
Chris Mason39279cc2007-06-12 06:35:45 -04001105 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001106 return num_written ? num_written : err;
1107}
1108
Chris Masond3977122009-01-05 21:25:51 -05001109int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001110{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001111 /*
1112 * ordered_data_close is set by settattr when we are about to truncate
1113 * a file from a non-zero size to a zero size. This tries to
1114 * flush down new bytes that may have been written if the
1115 * application were using truncate to replace a file in place.
1116 */
1117 if (BTRFS_I(inode)->ordered_data_close) {
1118 BTRFS_I(inode)->ordered_data_close = 0;
1119 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1120 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1121 filemap_flush(inode->i_mapping);
1122 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001123 if (filp->private_data)
1124 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001125 return 0;
1126}
1127
Chris Masond352ac62008-09-29 15:18:18 -04001128/*
1129 * fsync call for both files and directories. This logs the inode into
1130 * the tree log instead of forcing full commits whenever possible.
1131 *
1132 * It needs to call filemap_fdatawait so that all ordered extent updates are
1133 * in the metadata btree are up to date for copying to the log.
1134 *
1135 * It drops the inode mutex before doing the tree log commit. This is an
1136 * important optimization for directories because holding the mutex prevents
1137 * new operations on the dir while we write to disk.
1138 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001139int btrfs_sync_file(struct file *file, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001140{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001141 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001142 struct inode *inode = dentry->d_inode;
1143 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001144 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001145 struct btrfs_trans_handle *trans;
1146
Chris Mason257c62e2009-10-13 13:21:08 -04001147
1148 /* we wait first, since the writeback may change the inode */
1149 root->log_batch++;
1150 /* the VFS called filemap_fdatawrite for us */
1151 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1152 root->log_batch++;
1153
Chris Mason39279cc2007-06-12 06:35:45 -04001154 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001155 * check the transaction that last modified this inode
1156 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001157 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001158 if (!BTRFS_I(inode)->last_trans)
1159 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001160
Chris Mason257c62e2009-10-13 13:21:08 -04001161 /*
1162 * if the last transaction that changed this file was before
1163 * the current transaction, we can bail out now without any
1164 * syncing
1165 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001166 mutex_lock(&root->fs_info->trans_mutex);
1167 if (BTRFS_I(inode)->last_trans <=
1168 root->fs_info->last_trans_committed) {
1169 BTRFS_I(inode)->last_trans = 0;
1170 mutex_unlock(&root->fs_info->trans_mutex);
1171 goto out;
1172 }
1173 mutex_unlock(&root->fs_info->trans_mutex);
1174
1175 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001176 * ok we haven't committed the transaction yet, lets do a commit
1177 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001178 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001179 btrfs_ioctl_trans_end(file);
1180
Yan, Zhenga22285a2010-05-16 10:48:46 -04001181 trans = btrfs_start_transaction(root, 0);
1182 if (IS_ERR(trans)) {
1183 ret = PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04001184 goto out;
1185 }
Chris Masone02119d2008-09-05 16:13:11 -04001186
Chris Mason2cfbd502009-02-20 10:55:10 -05001187 ret = btrfs_log_dentry_safe(trans, root, dentry);
Chris Masond3977122009-01-05 21:25:51 -05001188 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04001189 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001190
1191 /* we've logged all the items and now have a consistent
1192 * version of the file in the log. It is possible that
1193 * someone will come in and modify the file, but that's
1194 * fine because the log is consistent on disk, and we
1195 * have references to all of the file's extents
1196 *
1197 * It is possible that someone will come in and log the
1198 * file again, but that will end up using the synchronization
1199 * inside btrfs_sync_log to keep things safe.
1200 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001201 mutex_unlock(&dentry->d_inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001202
Chris Mason257c62e2009-10-13 13:21:08 -04001203 if (ret != BTRFS_NO_LOG_SYNC) {
1204 if (ret > 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001205 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001206 } else {
1207 ret = btrfs_sync_log(trans, root);
1208 if (ret == 0)
1209 ret = btrfs_end_transaction(trans, root);
1210 else
1211 ret = btrfs_commit_transaction(trans, root);
1212 }
1213 } else {
1214 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001215 }
Chris Mason2cfbd502009-02-20 10:55:10 -05001216 mutex_lock(&dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001217out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001218 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001219}
1220
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001221static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001222 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001223 .page_mkwrite = btrfs_page_mkwrite,
1224};
1225
1226static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1227{
Miao Xie058a4572010-05-20 07:21:50 +00001228 struct address_space *mapping = filp->f_mapping;
1229
1230 if (!mapping->a_ops->readpage)
1231 return -ENOEXEC;
1232
Chris Mason9ebefb182007-06-15 13:50:00 -04001233 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00001234 vma->vm_ops = &btrfs_file_vm_ops;
1235 vma->vm_flags |= VM_CAN_NONLINEAR;
1236
Chris Mason9ebefb182007-06-15 13:50:00 -04001237 return 0;
1238}
1239
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001240const struct file_operations btrfs_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04001241 .llseek = generic_file_llseek,
1242 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00001243 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001244 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001245 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001246 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001247 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001248 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001249 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001250 .fsync = btrfs_sync_file,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001251 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001252#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001253 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001254#endif
1255};