blob: 13664b315fe284c39dc7ffd502dc03c8f1ee9a58 [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);
Tsutomu Itohc26a9202011-02-14 00:45:29 +0000188 BUG_ON(!split || !split2);
Chris Mason3b951512008-04-17 11:29:12 -0400189
Chris Mason890871b2009-09-02 16:24:52 -0400190 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500191 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500192 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400193 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400194 break;
Chris Masond1310b22008-01-24 16:13:08 -0500195 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400196 flags = em->flags;
197 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000198 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400199 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400200 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400201 break;
202 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000203 start = em->start + em->len;
204 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400205 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400206 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400207 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400208 continue;
209 }
Chris Masonc8b97812008-10-29 14:49:59 -0400210 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400211 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400212 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400213
214 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
215 em->start < start) {
216 split->start = em->start;
217 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500218 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400219 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400220
221 if (compressed)
222 split->block_len = em->block_len;
223 else
224 split->block_len = split->len;
225
Chris Mason3b951512008-04-17 11:29:12 -0400226 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400227 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800228 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400229 ret = add_extent_mapping(em_tree, split);
230 BUG_ON(ret);
231 free_extent_map(split);
232 split = split2;
233 split2 = NULL;
234 }
235 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
236 testend && em->start + em->len > start + len) {
237 u64 diff = start + len - em->start;
238
239 split->start = start + len;
240 split->len = em->start + em->len - (start + len);
241 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400242 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800243 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400244
Chris Masonc8b97812008-10-29 14:49:59 -0400245 if (compressed) {
246 split->block_len = em->block_len;
247 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500248 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400249 } else {
250 split->block_len = split->len;
251 split->block_start = em->block_start + diff;
Chris Mason445a6942008-11-10 11:53:33 -0500252 split->orig_start = split->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400253 }
Chris Mason3b951512008-04-17 11:29:12 -0400254
255 ret = add_extent_mapping(em_tree, split);
256 BUG_ON(ret);
257 free_extent_map(split);
258 split = NULL;
259 }
Chris Mason890871b2009-09-02 16:24:52 -0400260 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500261
Chris Masona52d9a82007-08-27 16:49:44 -0400262 /* once for us */
263 free_extent_map(em);
264 /* once for the tree*/
265 free_extent_map(em);
266 }
Chris Mason3b951512008-04-17 11:29:12 -0400267 if (split)
268 free_extent_map(split);
269 if (split2)
270 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400271 return 0;
272}
273
Chris Mason39279cc2007-06-12 06:35:45 -0400274/*
275 * this is very complex, but the basic idea is to drop all extents
276 * in the range start - end. hint_block is filled in with a block number
277 * that would be a good hint to the block allocator for this file.
278 *
279 * If an extent intersects the range but is not entirely inside the range
280 * it is either truncated or split. Anything entirely inside the range
281 * is deleted from the tree.
282 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000283int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
284 u64 start, u64 end, u64 *hint_byte, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400285{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000286 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason00f5c792007-11-30 10:09:33 -0500287 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000288 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500289 struct btrfs_path *path;
290 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000291 struct btrfs_key new_key;
292 u64 search_start = start;
293 u64 disk_bytenr = 0;
294 u64 num_bytes = 0;
295 u64 extent_offset = 0;
296 u64 extent_end = 0;
297 int del_nr = 0;
298 int del_slot = 0;
299 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400300 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500301 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400302
Chris Masona1ed8352009-09-11 12:27:37 -0400303 if (drop_cache)
304 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400305
Chris Mason39279cc2007-06-12 06:35:45 -0400306 path = btrfs_alloc_path();
307 if (!path)
308 return -ENOMEM;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000309
Chris Masond3977122009-01-05 21:25:51 -0500310 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400311 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400312 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
313 search_start, -1);
314 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000315 break;
316 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
317 leaf = path->nodes[0];
318 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
319 if (key.objectid == inode->i_ino &&
320 key.type == BTRFS_EXTENT_DATA_KEY)
321 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400322 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400323 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000324next_slot:
325 leaf = path->nodes[0];
326 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
327 BUG_ON(del_nr > 0);
328 ret = btrfs_next_leaf(root, path);
329 if (ret < 0)
330 break;
331 if (ret > 0) {
332 ret = 0;
333 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400334 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000335 leaf = path->nodes[0];
336 recow = 1;
337 }
338
339 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
340 if (key.objectid > inode->i_ino ||
341 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
342 break;
343
344 fi = btrfs_item_ptr(leaf, path->slots[0],
345 struct btrfs_file_extent_item);
346 extent_type = btrfs_file_extent_type(leaf, fi);
347
348 if (extent_type == BTRFS_FILE_EXTENT_REG ||
349 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
350 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
351 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
352 extent_offset = btrfs_file_extent_offset(leaf, fi);
353 extent_end = key.offset +
354 btrfs_file_extent_num_bytes(leaf, fi);
355 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
356 extent_end = key.offset +
357 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400358 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000359 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400360 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400361 }
362
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000363 if (extent_end <= search_start) {
364 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400365 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400366 }
367
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000368 search_start = max(key.offset, start);
369 if (recow) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400370 btrfs_release_path(root, path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000371 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400372 }
Chris Mason771ed682008-11-06 22:02:51 -0500373
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000374 /*
375 * | - range to drop - |
376 * | -------- extent -------- |
377 */
378 if (start > key.offset && end < extent_end) {
379 BUG_ON(del_nr > 0);
380 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
381
382 memcpy(&new_key, &key, sizeof(new_key));
383 new_key.offset = start;
384 ret = btrfs_duplicate_item(trans, root, path,
385 &new_key);
386 if (ret == -EAGAIN) {
387 btrfs_release_path(root, path);
388 continue;
389 }
390 if (ret < 0)
391 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400392
Chris Mason5f39d392007-10-15 16:14:19 -0400393 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000394 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
395 struct btrfs_file_extent_item);
396 btrfs_set_file_extent_num_bytes(leaf, fi,
397 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400398
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000399 fi = btrfs_item_ptr(leaf, path->slots[0],
400 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400401
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000402 extent_offset += start - key.offset;
403 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
404 btrfs_set_file_extent_num_bytes(leaf, fi,
405 extent_end - start);
406 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400407
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000408 if (disk_bytenr > 0) {
409 ret = btrfs_inc_extent_ref(trans, root,
410 disk_bytenr, num_bytes, 0,
411 root->root_key.objectid,
412 new_key.objectid,
413 start - extent_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -0400414 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000415 *hint_byte = disk_bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400416 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000417 key.offset = start;
418 }
419 /*
420 * | ---- range to drop ----- |
421 * | -------- extent -------- |
422 */
423 if (start <= key.offset && end < extent_end) {
424 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
425
426 memcpy(&new_key, &key, sizeof(new_key));
427 new_key.offset = end;
428 btrfs_set_item_key_safe(trans, root, path, &new_key);
429
430 extent_offset += end - key.offset;
431 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
432 btrfs_set_file_extent_num_bytes(leaf, fi,
433 extent_end - end);
434 btrfs_mark_buffer_dirty(leaf);
435 if (disk_bytenr > 0) {
436 inode_sub_bytes(inode, end - key.offset);
437 *hint_byte = disk_bytenr;
438 }
439 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400440 }
441
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000442 search_start = extent_end;
443 /*
444 * | ---- range to drop ----- |
445 * | -------- extent -------- |
446 */
447 if (start > key.offset && end >= extent_end) {
448 BUG_ON(del_nr > 0);
449 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
450
451 btrfs_set_file_extent_num_bytes(leaf, fi,
452 start - key.offset);
453 btrfs_mark_buffer_dirty(leaf);
454 if (disk_bytenr > 0) {
455 inode_sub_bytes(inode, extent_end - start);
456 *hint_byte = disk_bytenr;
457 }
458 if (end == extent_end)
459 break;
460
461 path->slots[0]++;
462 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400463 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000464
465 /*
466 * | ---- range to drop ----- |
467 * | ------ extent ------ |
468 */
469 if (start <= key.offset && end >= extent_end) {
470 if (del_nr == 0) {
471 del_slot = path->slots[0];
472 del_nr = 1;
473 } else {
474 BUG_ON(del_slot + del_nr != path->slots[0]);
475 del_nr++;
476 }
477
478 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
479 inode_sub_bytes(inode,
480 extent_end - key.offset);
481 extent_end = ALIGN(extent_end,
482 root->sectorsize);
483 } else if (disk_bytenr > 0) {
484 ret = btrfs_free_extent(trans, root,
485 disk_bytenr, num_bytes, 0,
486 root->root_key.objectid,
487 key.objectid, key.offset -
488 extent_offset);
489 BUG_ON(ret);
490 inode_sub_bytes(inode,
491 extent_end - key.offset);
492 *hint_byte = disk_bytenr;
493 }
494
495 if (end == extent_end)
496 break;
497
498 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
499 path->slots[0]++;
500 goto next_slot;
501 }
502
503 ret = btrfs_del_items(trans, root, path, del_slot,
504 del_nr);
505 BUG_ON(ret);
506
507 del_nr = 0;
508 del_slot = 0;
509
510 btrfs_release_path(root, path);
511 continue;
512 }
513
514 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400515 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000516
517 if (del_nr > 0) {
518 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
519 BUG_ON(ret);
520 }
521
Chris Mason39279cc2007-06-12 06:35:45 -0400522 btrfs_free_path(path);
523 return ret;
524}
525
Yan Zhengd899e052008-10-30 14:25:28 -0400526static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000527 u64 objectid, u64 bytenr, u64 orig_offset,
528 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400529{
530 struct btrfs_file_extent_item *fi;
531 struct btrfs_key key;
532 u64 extent_end;
533
534 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
535 return 0;
536
537 btrfs_item_key_to_cpu(leaf, &key, slot);
538 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
539 return 0;
540
541 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
542 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
543 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000544 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400545 btrfs_file_extent_compression(leaf, fi) ||
546 btrfs_file_extent_encryption(leaf, fi) ||
547 btrfs_file_extent_other_encoding(leaf, fi))
548 return 0;
549
550 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
551 if ((*start && *start != key.offset) || (*end && *end != extent_end))
552 return 0;
553
554 *start = key.offset;
555 *end = extent_end;
556 return 1;
557}
558
559/*
560 * Mark extent in the range start - end as written.
561 *
562 * This changes extent type from 'pre-allocated' to 'regular'. If only
563 * part of extent is marked as written, the extent will be split into
564 * two or three.
565 */
566int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400567 struct inode *inode, u64 start, u64 end)
568{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000569 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400570 struct extent_buffer *leaf;
571 struct btrfs_path *path;
572 struct btrfs_file_extent_item *fi;
573 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000574 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400575 u64 bytenr;
576 u64 num_bytes;
577 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400578 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400579 u64 other_start;
580 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000581 u64 split;
582 int del_nr = 0;
583 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000584 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400585 int ret;
586
587 btrfs_drop_extent_cache(inode, start, end - 1, 0);
588
589 path = btrfs_alloc_path();
590 BUG_ON(!path);
591again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000592 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000593 split = start;
Yan Zhengd899e052008-10-30 14:25:28 -0400594 key.objectid = inode->i_ino;
595 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000596 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -0400597
598 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
599 if (ret > 0 && path->slots[0] > 0)
600 path->slots[0]--;
601
602 leaf = path->nodes[0];
603 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
604 BUG_ON(key.objectid != inode->i_ino ||
605 key.type != BTRFS_EXTENT_DATA_KEY);
606 fi = btrfs_item_ptr(leaf, path->slots[0],
607 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000608 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
609 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -0400610 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
611 BUG_ON(key.offset > start || extent_end < end);
612
613 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
614 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400615 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000616 memcpy(&new_key, &key, sizeof(new_key));
617
618 if (start == key.offset && end < extent_end) {
619 other_start = 0;
620 other_end = start;
621 if (extent_mergeable(leaf, path->slots[0] - 1,
622 inode->i_ino, bytenr, orig_offset,
623 &other_start, &other_end)) {
624 new_key.offset = end;
625 btrfs_set_item_key_safe(trans, root, path, &new_key);
626 fi = btrfs_item_ptr(leaf, path->slots[0],
627 struct btrfs_file_extent_item);
628 btrfs_set_file_extent_num_bytes(leaf, fi,
629 extent_end - end);
630 btrfs_set_file_extent_offset(leaf, fi,
631 end - orig_offset);
632 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
633 struct btrfs_file_extent_item);
634 btrfs_set_file_extent_num_bytes(leaf, fi,
635 end - other_start);
636 btrfs_mark_buffer_dirty(leaf);
637 goto out;
638 }
639 }
640
641 if (start > key.offset && end == extent_end) {
642 other_start = end;
643 other_end = 0;
644 if (extent_mergeable(leaf, path->slots[0] + 1,
645 inode->i_ino, bytenr, orig_offset,
646 &other_start, &other_end)) {
647 fi = btrfs_item_ptr(leaf, path->slots[0],
648 struct btrfs_file_extent_item);
649 btrfs_set_file_extent_num_bytes(leaf, fi,
650 start - key.offset);
651 path->slots[0]++;
652 new_key.offset = start;
653 btrfs_set_item_key_safe(trans, root, path, &new_key);
654
655 fi = btrfs_item_ptr(leaf, path->slots[0],
656 struct btrfs_file_extent_item);
657 btrfs_set_file_extent_num_bytes(leaf, fi,
658 other_end - start);
659 btrfs_set_file_extent_offset(leaf, fi,
660 start - orig_offset);
661 btrfs_mark_buffer_dirty(leaf);
662 goto out;
663 }
664 }
Yan Zhengd899e052008-10-30 14:25:28 -0400665
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000666 while (start > key.offset || end < extent_end) {
667 if (key.offset == start)
668 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400669
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000670 new_key.offset = split;
671 ret = btrfs_duplicate_item(trans, root, path, &new_key);
672 if (ret == -EAGAIN) {
673 btrfs_release_path(root, path);
674 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -0400675 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000676 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -0400677
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000678 leaf = path->nodes[0];
679 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -0400680 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -0400681 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000682 split - key.offset);
683
684 fi = btrfs_item_ptr(leaf, path->slots[0],
685 struct btrfs_file_extent_item);
686
687 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
688 btrfs_set_file_extent_num_bytes(leaf, fi,
689 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -0400690 btrfs_mark_buffer_dirty(leaf);
691
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000692 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
693 root->root_key.objectid,
694 inode->i_ino, orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400695 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -0400696
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000697 if (split == start) {
698 key.offset = start;
699 } else {
700 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400701 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000702 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400703 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000704 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -0400705 }
706
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000707 other_start = end;
708 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000709 if (extent_mergeable(leaf, path->slots[0] + 1,
710 inode->i_ino, bytenr, orig_offset,
711 &other_start, &other_end)) {
712 if (recow) {
713 btrfs_release_path(root, path);
714 goto again;
715 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000716 extent_end = other_end;
717 del_slot = path->slots[0] + 1;
718 del_nr++;
719 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
720 0, root->root_key.objectid,
721 inode->i_ino, orig_offset);
722 BUG_ON(ret);
723 }
724 other_start = 0;
725 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000726 if (extent_mergeable(leaf, path->slots[0] - 1,
727 inode->i_ino, bytenr, orig_offset,
728 &other_start, &other_end)) {
729 if (recow) {
730 btrfs_release_path(root, path);
731 goto again;
732 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000733 key.offset = other_start;
734 del_slot = path->slots[0];
735 del_nr++;
736 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
737 0, root->root_key.objectid,
738 inode->i_ino, orig_offset);
739 BUG_ON(ret);
740 }
741 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000742 fi = btrfs_item_ptr(leaf, path->slots[0],
743 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000744 btrfs_set_file_extent_type(leaf, fi,
745 BTRFS_FILE_EXTENT_REG);
746 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000747 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000748 fi = btrfs_item_ptr(leaf, del_slot - 1,
749 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000750 btrfs_set_file_extent_type(leaf, fi,
751 BTRFS_FILE_EXTENT_REG);
752 btrfs_set_file_extent_num_bytes(leaf, fi,
753 extent_end - key.offset);
754 btrfs_mark_buffer_dirty(leaf);
755
756 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
757 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000758 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000759out:
Yan Zhengd899e052008-10-30 14:25:28 -0400760 btrfs_free_path(path);
761 return 0;
762}
763
Chris Mason39279cc2007-06-12 06:35:45 -0400764/*
Chris Masonb1bf8622011-02-28 09:52:08 -0500765 * on error we return an unlocked page and the error value
766 * on success we return a locked page and 0
767 */
768static int prepare_uptodate_page(struct page *page, u64 pos)
769{
770 int ret = 0;
771
772 if ((pos & (PAGE_CACHE_SIZE - 1)) && !PageUptodate(page)) {
773 ret = btrfs_readpage(NULL, page);
774 if (ret)
775 return ret;
776 lock_page(page);
777 if (!PageUptodate(page)) {
778 unlock_page(page);
779 return -EIO;
780 }
781 }
782 return 0;
783}
784
785/*
Chris Masond352ac62008-09-29 15:18:18 -0400786 * this gets pages into the page cache and locks them down, it also properly
787 * waits for data=ordered extents to finish before allowing the pages to be
788 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -0400789 */
Chris Masond3977122009-01-05 21:25:51 -0500790static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500791 struct page **pages, size_t num_pages,
792 loff_t pos, unsigned long first_index,
793 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400794{
Josef Bacik2ac55d42010-02-03 19:33:23 +0000795 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -0400796 int i;
797 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500798 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400799 int err = 0;
Chris Masonb1bf8622011-02-28 09:52:08 -0500800 int faili = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400801 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400802 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400803
Chris Mason5f39d392007-10-15 16:14:19 -0400804 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400805 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400806
Yan Zheng9036c102008-10-30 14:19:41 -0400807 if (start_pos > inode->i_size) {
808 err = btrfs_cont_expand(inode, start_pos);
809 if (err)
810 return err;
811 }
812
Chris Mason39279cc2007-06-12 06:35:45 -0400813 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400814again:
Chris Mason39279cc2007-06-12 06:35:45 -0400815 for (i = 0; i < num_pages; i++) {
816 pages[i] = grab_cache_page(inode->i_mapping, index + i);
817 if (!pages[i]) {
Chris Masonb1bf8622011-02-28 09:52:08 -0500818 faili = i - 1;
819 err = -ENOMEM;
820 goto fail;
821 }
822
823 if (i == 0)
824 err = prepare_uptodate_page(pages[i], pos);
825 if (i == num_pages - 1)
826 err = prepare_uptodate_page(pages[i],
827 pos + write_bytes);
828 if (err) {
829 page_cache_release(pages[i]);
830 faili = i - 1;
831 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -0400832 }
Chris Masonccd467d2007-06-28 15:57:36 -0400833 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400834 }
Chris Masonb1bf8622011-02-28 09:52:08 -0500835 err = 0;
Chris Mason07627042008-02-19 11:29:24 -0500836 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400837 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000838 lock_extent_bits(&BTRFS_I(inode)->io_tree,
839 start_pos, last_pos - 1, 0, &cached_state,
840 GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500841 ordered = btrfs_lookup_first_ordered_extent(inode,
842 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400843 if (ordered &&
844 ordered->file_offset + ordered->len > start_pos &&
845 ordered->file_offset < last_pos) {
846 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000847 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
848 start_pos, last_pos - 1,
849 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400850 for (i = 0; i < num_pages; i++) {
851 unlock_page(pages[i]);
852 page_cache_release(pages[i]);
853 }
854 btrfs_wait_ordered_range(inode, start_pos,
855 last_pos - start_pos);
856 goto again;
857 }
858 if (ordered)
859 btrfs_put_ordered_extent(ordered);
860
Josef Bacik2ac55d42010-02-03 19:33:23 +0000861 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -0400862 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +0000863 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
Chris Mason07627042008-02-19 11:29:24 -0500864 GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000865 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
866 start_pos, last_pos - 1, &cached_state,
867 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500868 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400869 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400870 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400871 set_page_extent_mapped(pages[i]);
872 WARN_ON(!PageLocked(pages[i]));
873 }
Chris Mason39279cc2007-06-12 06:35:45 -0400874 return 0;
Chris Masonb1bf8622011-02-28 09:52:08 -0500875fail:
876 while (faili >= 0) {
877 unlock_page(pages[faili]);
878 page_cache_release(pages[faili]);
879 faili--;
880 }
881 return err;
882
Chris Mason39279cc2007-06-12 06:35:45 -0400883}
884
Josef Bacik11c65dc2010-05-23 11:07:21 -0400885static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
886 const struct iovec *iov,
887 unsigned long nr_segs, loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400888{
Josef Bacik11c65dc2010-05-23 11:07:21 -0400889 struct file *file = iocb->ki_filp;
890 struct inode *inode = fdentry(file)->d_inode;
891 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400892 struct page **pages = NULL;
893 struct iov_iter i;
894 loff_t *ppos = &iocb->ki_pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400895 loff_t start_pos;
896 ssize_t num_written = 0;
897 ssize_t err = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400898 size_t count;
899 size_t ocount;
Chris Mason39279cc2007-06-12 06:35:45 -0400900 int ret = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400901 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400902 unsigned long first_index;
903 unsigned long last_index;
Chris Masoncb843a62008-10-03 12:30:02 -0400904 int will_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400905 int buffered = 0;
Xin Zhong914ee292010-12-09 09:30:14 +0000906 int copied = 0;
907 int dirty_pages = 0;
Chris Masoncb843a62008-10-03 12:30:02 -0400908
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100909 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
Chris Masoncb843a62008-10-03 12:30:02 -0400910 (file->f_flags & O_DIRECT));
Chris Mason8c2383c2007-06-18 09:57:58 -0400911
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400912 start_pos = pos;
913
Chris Mason39279cc2007-06-12 06:35:45 -0400914 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
Chris Mason2b1f55b2008-09-24 11:48:04 -0400915
Chris Masonab93dbe2009-10-01 12:29:10 -0400916 mutex_lock(&inode->i_mutex);
917
Josef Bacik11c65dc2010-05-23 11:07:21 -0400918 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
919 if (err)
920 goto out;
921 count = ocount;
922
Chris Mason1832a6d2007-12-21 16:27:21 -0500923 current->backing_dev_info = inode->i_mapping->backing_dev_info;
Chris Mason39279cc2007-06-12 06:35:45 -0400924 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
Chris Mason1832a6d2007-12-21 16:27:21 -0500925 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400926 goto out;
927
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -0400928 if (count == 0)
Chris Masonab93dbe2009-10-01 12:29:10 -0400929 goto out;
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400930
931 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400932 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400933 goto out;
934
liuboacce9522011-01-06 19:30:25 +0800935 /*
936 * If BTRFS flips readonly due to some impossible error
937 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
938 * although we have opened a file as writable, we have
939 * to stop this write operation to ensure FS consistency.
940 */
941 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
942 err = -EROFS;
943 goto out;
944 }
945
Chris Mason39279cc2007-06-12 06:35:45 -0400946 file_update_time(file);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400947 BTRFS_I(inode)->sequence++;
Chris Mason39279cc2007-06-12 06:35:45 -0400948
Josef Bacik4b46fce2010-05-23 11:00:55 -0400949 if (unlikely(file->f_flags & O_DIRECT)) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400950 num_written = generic_file_direct_write(iocb, iov, &nr_segs,
951 pos, ppos, count,
952 ocount);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400953 /*
954 * the generic O_DIRECT will update in-memory i_size after the
955 * DIOs are done. But our endio handlers that update the on
956 * disk i_size never update past the in memory i_size. So we
957 * need one more update here to catch any additions to the
958 * file
959 */
960 if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
961 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
962 mark_inode_dirty(inode);
963 }
964
965 if (num_written < 0) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400966 ret = num_written;
967 num_written = 0;
968 goto out;
969 } else if (num_written == count) {
970 /* pick up pos changes done by the generic code */
971 pos = *ppos;
972 goto out;
973 }
Josef Bacik4b46fce2010-05-23 11:00:55 -0400974 /*
975 * We are going to do buffered for the rest of the range, so we
976 * need to make sure to invalidate the buffered pages when we're
977 * done.
978 */
979 buffered = 1;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400980 pos += num_written;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400981 }
982
Josef Bacik11c65dc2010-05-23 11:07:21 -0400983 iov_iter_init(&i, iov, nr_segs, count, num_written);
984 nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
985 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
986 (sizeof(struct page *)));
Chris Mason8c2383c2007-06-18 09:57:58 -0400987 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
liubo2a29edc2011-01-26 06:22:08 +0000988 if (!pages) {
989 ret = -ENOMEM;
990 goto out;
991 }
Chris Mason39279cc2007-06-12 06:35:45 -0400992
Chris Masonab93dbe2009-10-01 12:29:10 -0400993 /* generic_write_checks can change our pos */
994 start_pos = pos;
995
Chris Mason39279cc2007-06-12 06:35:45 -0400996 first_index = pos >> PAGE_CACHE_SHIFT;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400997 last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400998
Josef Bacik11c65dc2010-05-23 11:07:21 -0400999 while (iov_iter_count(&i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -04001000 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacik11c65dc2010-05-23 11:07:21 -04001001 size_t write_bytes = min(iov_iter_count(&i),
1002 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -04001003 offset);
Yan, Zheng3a909832011-01-18 13:34:40 +08001004 size_t num_pages = (write_bytes + offset +
1005 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -04001006
Chris Mason8c2383c2007-06-18 09:57:58 -04001007 WARN_ON(num_pages > nrptrs);
yanhai zhu9aead432009-01-05 15:49:11 -05001008 memset(pages, 0, sizeof(struct page *) * nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -05001009
Xin Zhong914ee292010-12-09 09:30:14 +00001010 /*
1011 * Fault pages before locking them in prepare_pages
1012 * to avoid recursive lock
1013 */
1014 if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) {
1015 ret = -EFAULT;
1016 goto out;
1017 }
1018
1019 ret = btrfs_delalloc_reserve_space(inode,
1020 num_pages << PAGE_CACHE_SHIFT);
Chris Mason1832a6d2007-12-21 16:27:21 -05001021 if (ret)
1022 goto out;
1023
Chris Mason39279cc2007-06-12 06:35:45 -04001024 ret = prepare_pages(root, file, pages, num_pages,
1025 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -04001026 write_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001027 if (ret) {
Xin Zhong914ee292010-12-09 09:30:14 +00001028 btrfs_delalloc_release_space(inode,
1029 num_pages << PAGE_CACHE_SHIFT);
Chris Mason54aa1f42007-06-22 14:16:25 -04001030 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -05001031 }
Chris Mason39279cc2007-06-12 06:35:45 -04001032
Xin Zhong914ee292010-12-09 09:30:14 +00001033 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001034 write_bytes, pages, &i);
Chris Masonb1bf8622011-02-28 09:52:08 -05001035
1036 /*
1037 * if we have trouble faulting in the pages, fall
1038 * back to one page at a time
1039 */
1040 if (copied < write_bytes)
1041 nrptrs = 1;
1042
1043 if (copied == 0)
1044 dirty_pages = 0;
1045 else
1046 dirty_pages = (copied + offset +
1047 PAGE_CACHE_SIZE - 1) >>
1048 PAGE_CACHE_SHIFT;
Xin Zhong914ee292010-12-09 09:30:14 +00001049
1050 if (num_pages > dirty_pages) {
1051 if (copied > 0)
1052 atomic_inc(
1053 &BTRFS_I(inode)->outstanding_extents);
1054 btrfs_delalloc_release_space(inode,
1055 (num_pages - dirty_pages) <<
1056 PAGE_CACHE_SHIFT);
1057 }
1058
1059 if (copied > 0) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001060 dirty_and_release_pages(NULL, root, file, pages,
Xin Zhong914ee292010-12-09 09:30:14 +00001061 dirty_pages, pos, copied);
Chris Mason54aa1f42007-06-22 14:16:25 -04001062 }
Chris Mason39279cc2007-06-12 06:35:45 -04001063
Chris Mason39279cc2007-06-12 06:35:45 -04001064 btrfs_drop_pages(pages, num_pages);
Xin Zhong914ee292010-12-09 09:30:14 +00001065
1066 if (copied > 0) {
1067 if (will_write) {
1068 filemap_fdatawrite_range(inode->i_mapping, pos,
1069 pos + copied - 1);
1070 } else {
1071 balance_dirty_pages_ratelimited_nr(
1072 inode->i_mapping,
1073 dirty_pages);
1074 if (dirty_pages <
1075 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1076 btrfs_btree_balance_dirty(root, 1);
1077 btrfs_throttle(root);
1078 }
Josef Bacik6a632092009-02-20 11:00:09 -05001079 }
Chris Mason39279cc2007-06-12 06:35:45 -04001080
Xin Zhong914ee292010-12-09 09:30:14 +00001081 pos += copied;
1082 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001083
Chris Mason39279cc2007-06-12 06:35:45 -04001084 cond_resched();
1085 }
Chris Mason39279cc2007-06-12 06:35:45 -04001086out:
Chris Mason1832a6d2007-12-21 16:27:21 -05001087 mutex_unlock(&inode->i_mutex);
Josef Bacik6a632092009-02-20 11:00:09 -05001088 if (ret)
1089 err = ret;
Chris Mason5b92ee72008-01-03 13:46:11 -05001090
Chris Mason8c2383c2007-06-18 09:57:58 -04001091 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -04001092 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001093
Chris Mason5a3f23d2009-03-31 13:27:11 -04001094 /*
1095 * we want to make sure fsync finds this change
1096 * but we haven't joined a transaction running right now.
1097 *
1098 * Later on, someone is sure to update the inode and get the
1099 * real transid recorded.
1100 *
1101 * We set last_trans now to the fs_info generation + 1,
1102 * this will either be one more than the running transaction
1103 * or the generation used for the next transaction if there isn't
1104 * one running right now.
1105 */
1106 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1107
Chris Masoncb843a62008-10-03 12:30:02 -04001108 if (num_written > 0 && will_write) {
Chris Masone02119d2008-09-05 16:13:11 -04001109 struct btrfs_trans_handle *trans;
1110
Chris Masoncb843a62008-10-03 12:30:02 -04001111 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1112 if (err)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001113 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -04001114
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001115 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001116 trans = btrfs_start_transaction(root, 0);
Josef Bacik495e8672010-11-19 20:36:10 +00001117 if (IS_ERR(trans)) {
1118 num_written = PTR_ERR(trans);
1119 goto done;
1120 }
1121 mutex_lock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001122 ret = btrfs_log_dentry_safe(trans, root,
1123 file->f_dentry);
Josef Bacik495e8672010-11-19 20:36:10 +00001124 mutex_unlock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001125 if (ret == 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001126 ret = btrfs_sync_log(trans, root);
1127 if (ret == 0)
1128 btrfs_end_transaction(trans, root);
1129 else
1130 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001131 } else if (ret != BTRFS_NO_LOG_SYNC) {
Chris Masoncb843a62008-10-03 12:30:02 -04001132 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001133 } else {
1134 btrfs_end_transaction(trans, root);
Chris Masoncb843a62008-10-03 12:30:02 -04001135 }
Chris Masone02119d2008-09-05 16:13:11 -04001136 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04001137 if (file->f_flags & O_DIRECT && buffered) {
Chris Masoncb843a62008-10-03 12:30:02 -04001138 invalidate_mapping_pages(inode->i_mapping,
1139 start_pos >> PAGE_CACHE_SHIFT,
1140 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1141 }
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001142 }
Josef Bacik495e8672010-11-19 20:36:10 +00001143done:
Chris Mason39279cc2007-06-12 06:35:45 -04001144 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001145 return num_written ? num_written : err;
1146}
1147
Chris Masond3977122009-01-05 21:25:51 -05001148int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001149{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001150 /*
1151 * ordered_data_close is set by settattr when we are about to truncate
1152 * a file from a non-zero size to a zero size. This tries to
1153 * flush down new bytes that may have been written if the
1154 * application were using truncate to replace a file in place.
1155 */
1156 if (BTRFS_I(inode)->ordered_data_close) {
1157 BTRFS_I(inode)->ordered_data_close = 0;
1158 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1159 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1160 filemap_flush(inode->i_mapping);
1161 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001162 if (filp->private_data)
1163 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001164 return 0;
1165}
1166
Chris Masond352ac62008-09-29 15:18:18 -04001167/*
1168 * fsync call for both files and directories. This logs the inode into
1169 * the tree log instead of forcing full commits whenever possible.
1170 *
1171 * It needs to call filemap_fdatawait so that all ordered extent updates are
1172 * in the metadata btree are up to date for copying to the log.
1173 *
1174 * It drops the inode mutex before doing the tree log commit. This is an
1175 * important optimization for directories because holding the mutex prevents
1176 * new operations on the dir while we write to disk.
1177 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001178int btrfs_sync_file(struct file *file, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001179{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001180 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001181 struct inode *inode = dentry->d_inode;
1182 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001183 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001184 struct btrfs_trans_handle *trans;
1185
Chris Mason257c62e2009-10-13 13:21:08 -04001186
1187 /* we wait first, since the writeback may change the inode */
1188 root->log_batch++;
1189 /* the VFS called filemap_fdatawrite for us */
1190 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1191 root->log_batch++;
1192
Chris Mason39279cc2007-06-12 06:35:45 -04001193 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001194 * check the transaction that last modified this inode
1195 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001196 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001197 if (!BTRFS_I(inode)->last_trans)
1198 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001199
Chris Mason257c62e2009-10-13 13:21:08 -04001200 /*
1201 * if the last transaction that changed this file was before
1202 * the current transaction, we can bail out now without any
1203 * syncing
1204 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001205 mutex_lock(&root->fs_info->trans_mutex);
1206 if (BTRFS_I(inode)->last_trans <=
1207 root->fs_info->last_trans_committed) {
1208 BTRFS_I(inode)->last_trans = 0;
1209 mutex_unlock(&root->fs_info->trans_mutex);
1210 goto out;
1211 }
1212 mutex_unlock(&root->fs_info->trans_mutex);
1213
1214 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001215 * ok we haven't committed the transaction yet, lets do a commit
1216 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001217 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001218 btrfs_ioctl_trans_end(file);
1219
Yan, Zhenga22285a2010-05-16 10:48:46 -04001220 trans = btrfs_start_transaction(root, 0);
1221 if (IS_ERR(trans)) {
1222 ret = PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04001223 goto out;
1224 }
Chris Masone02119d2008-09-05 16:13:11 -04001225
Chris Mason2cfbd502009-02-20 10:55:10 -05001226 ret = btrfs_log_dentry_safe(trans, root, dentry);
Chris Masond3977122009-01-05 21:25:51 -05001227 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04001228 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001229
1230 /* we've logged all the items and now have a consistent
1231 * version of the file in the log. It is possible that
1232 * someone will come in and modify the file, but that's
1233 * fine because the log is consistent on disk, and we
1234 * have references to all of the file's extents
1235 *
1236 * It is possible that someone will come in and log the
1237 * file again, but that will end up using the synchronization
1238 * inside btrfs_sync_log to keep things safe.
1239 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001240 mutex_unlock(&dentry->d_inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001241
Chris Mason257c62e2009-10-13 13:21:08 -04001242 if (ret != BTRFS_NO_LOG_SYNC) {
1243 if (ret > 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001244 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001245 } else {
1246 ret = btrfs_sync_log(trans, root);
1247 if (ret == 0)
1248 ret = btrfs_end_transaction(trans, root);
1249 else
1250 ret = btrfs_commit_transaction(trans, root);
1251 }
1252 } else {
1253 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001254 }
Chris Mason2cfbd502009-02-20 10:55:10 -05001255 mutex_lock(&dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001256out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001257 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001258}
1259
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001260static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001261 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001262 .page_mkwrite = btrfs_page_mkwrite,
1263};
1264
1265static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1266{
Miao Xie058a4572010-05-20 07:21:50 +00001267 struct address_space *mapping = filp->f_mapping;
1268
1269 if (!mapping->a_ops->readpage)
1270 return -ENOEXEC;
1271
Chris Mason9ebefb182007-06-15 13:50:00 -04001272 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00001273 vma->vm_ops = &btrfs_file_vm_ops;
1274 vma->vm_flags |= VM_CAN_NONLINEAR;
1275
Chris Mason9ebefb182007-06-15 13:50:00 -04001276 return 0;
1277}
1278
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001279const struct file_operations btrfs_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04001280 .llseek = generic_file_llseek,
1281 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00001282 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001283 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001284 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001285 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001286 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001287 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001288 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001289 .fsync = btrfs_sync_file,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001290 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001291#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001292 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001293#endif
1294};