blob: b0ff34b966078e0ef5221cac40678b2c725b026a [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;
Li Zefan261507a02010-12-17 14:21:50 +0800227 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400228 ret = add_extent_mapping(em_tree, split);
229 BUG_ON(ret);
230 free_extent_map(split);
231 split = split2;
232 split2 = NULL;
233 }
234 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
235 testend && em->start + em->len > start + len) {
236 u64 diff = start + len - em->start;
237
238 split->start = start + len;
239 split->len = em->start + em->len - (start + len);
240 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400241 split->flags = flags;
Li Zefan261507a02010-12-17 14:21:50 +0800242 split->compress_type = em->compress_type;
Chris Mason3b951512008-04-17 11:29:12 -0400243
Chris Masonc8b97812008-10-29 14:49:59 -0400244 if (compressed) {
245 split->block_len = em->block_len;
246 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500247 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400248 } else {
249 split->block_len = split->len;
250 split->block_start = em->block_start + diff;
Chris Mason445a6942008-11-10 11:53:33 -0500251 split->orig_start = split->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400252 }
Chris Mason3b951512008-04-17 11:29:12 -0400253
254 ret = add_extent_mapping(em_tree, split);
255 BUG_ON(ret);
256 free_extent_map(split);
257 split = NULL;
258 }
Chris Mason890871b2009-09-02 16:24:52 -0400259 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500260
Chris Masona52d9a82007-08-27 16:49:44 -0400261 /* once for us */
262 free_extent_map(em);
263 /* once for the tree*/
264 free_extent_map(em);
265 }
Chris Mason3b951512008-04-17 11:29:12 -0400266 if (split)
267 free_extent_map(split);
268 if (split2)
269 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400270 return 0;
271}
272
Chris Mason39279cc2007-06-12 06:35:45 -0400273/*
274 * this is very complex, but the basic idea is to drop all extents
275 * in the range start - end. hint_block is filled in with a block number
276 * that would be a good hint to the block allocator for this file.
277 *
278 * If an extent intersects the range but is not entirely inside the range
279 * it is either truncated or split. Anything entirely inside the range
280 * is deleted from the tree.
281 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000282int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
283 u64 start, u64 end, u64 *hint_byte, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400284{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000285 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason00f5c792007-11-30 10:09:33 -0500286 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000287 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500288 struct btrfs_path *path;
289 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000290 struct btrfs_key new_key;
291 u64 search_start = start;
292 u64 disk_bytenr = 0;
293 u64 num_bytes = 0;
294 u64 extent_offset = 0;
295 u64 extent_end = 0;
296 int del_nr = 0;
297 int del_slot = 0;
298 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400299 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500300 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400301
Chris Masona1ed8352009-09-11 12:27:37 -0400302 if (drop_cache)
303 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400304
Chris Mason39279cc2007-06-12 06:35:45 -0400305 path = btrfs_alloc_path();
306 if (!path)
307 return -ENOMEM;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000308
Chris Masond3977122009-01-05 21:25:51 -0500309 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400310 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400311 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
312 search_start, -1);
313 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000314 break;
315 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
316 leaf = path->nodes[0];
317 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
318 if (key.objectid == inode->i_ino &&
319 key.type == BTRFS_EXTENT_DATA_KEY)
320 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400321 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400322 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000323next_slot:
324 leaf = path->nodes[0];
325 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
326 BUG_ON(del_nr > 0);
327 ret = btrfs_next_leaf(root, path);
328 if (ret < 0)
329 break;
330 if (ret > 0) {
331 ret = 0;
332 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400333 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000334 leaf = path->nodes[0];
335 recow = 1;
336 }
337
338 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
339 if (key.objectid > inode->i_ino ||
340 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
341 break;
342
343 fi = btrfs_item_ptr(leaf, path->slots[0],
344 struct btrfs_file_extent_item);
345 extent_type = btrfs_file_extent_type(leaf, fi);
346
347 if (extent_type == BTRFS_FILE_EXTENT_REG ||
348 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
349 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
350 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
351 extent_offset = btrfs_file_extent_offset(leaf, fi);
352 extent_end = key.offset +
353 btrfs_file_extent_num_bytes(leaf, fi);
354 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
355 extent_end = key.offset +
356 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400357 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000358 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400359 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400360 }
361
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000362 if (extent_end <= search_start) {
363 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400364 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400365 }
366
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000367 search_start = max(key.offset, start);
368 if (recow) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400369 btrfs_release_path(root, path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000370 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400371 }
Chris Mason771ed682008-11-06 22:02:51 -0500372
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000373 /*
374 * | - range to drop - |
375 * | -------- extent -------- |
376 */
377 if (start > key.offset && end < extent_end) {
378 BUG_ON(del_nr > 0);
379 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
380
381 memcpy(&new_key, &key, sizeof(new_key));
382 new_key.offset = start;
383 ret = btrfs_duplicate_item(trans, root, path,
384 &new_key);
385 if (ret == -EAGAIN) {
386 btrfs_release_path(root, path);
387 continue;
388 }
389 if (ret < 0)
390 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400391
Chris Mason5f39d392007-10-15 16:14:19 -0400392 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000393 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
394 struct btrfs_file_extent_item);
395 btrfs_set_file_extent_num_bytes(leaf, fi,
396 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400397
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000398 fi = btrfs_item_ptr(leaf, path->slots[0],
399 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400400
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000401 extent_offset += start - key.offset;
402 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
403 btrfs_set_file_extent_num_bytes(leaf, fi,
404 extent_end - start);
405 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400406
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000407 if (disk_bytenr > 0) {
408 ret = btrfs_inc_extent_ref(trans, root,
409 disk_bytenr, num_bytes, 0,
410 root->root_key.objectid,
411 new_key.objectid,
412 start - extent_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -0400413 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000414 *hint_byte = disk_bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400415 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000416 key.offset = start;
417 }
418 /*
419 * | ---- range to drop ----- |
420 * | -------- extent -------- |
421 */
422 if (start <= key.offset && end < extent_end) {
423 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
424
425 memcpy(&new_key, &key, sizeof(new_key));
426 new_key.offset = end;
427 btrfs_set_item_key_safe(trans, root, path, &new_key);
428
429 extent_offset += end - key.offset;
430 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
431 btrfs_set_file_extent_num_bytes(leaf, fi,
432 extent_end - end);
433 btrfs_mark_buffer_dirty(leaf);
434 if (disk_bytenr > 0) {
435 inode_sub_bytes(inode, end - key.offset);
436 *hint_byte = disk_bytenr;
437 }
438 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400439 }
440
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000441 search_start = extent_end;
442 /*
443 * | ---- range to drop ----- |
444 * | -------- extent -------- |
445 */
446 if (start > key.offset && end >= extent_end) {
447 BUG_ON(del_nr > 0);
448 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
449
450 btrfs_set_file_extent_num_bytes(leaf, fi,
451 start - key.offset);
452 btrfs_mark_buffer_dirty(leaf);
453 if (disk_bytenr > 0) {
454 inode_sub_bytes(inode, extent_end - start);
455 *hint_byte = disk_bytenr;
456 }
457 if (end == extent_end)
458 break;
459
460 path->slots[0]++;
461 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400462 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000463
464 /*
465 * | ---- range to drop ----- |
466 * | ------ extent ------ |
467 */
468 if (start <= key.offset && end >= extent_end) {
469 if (del_nr == 0) {
470 del_slot = path->slots[0];
471 del_nr = 1;
472 } else {
473 BUG_ON(del_slot + del_nr != path->slots[0]);
474 del_nr++;
475 }
476
477 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
478 inode_sub_bytes(inode,
479 extent_end - key.offset);
480 extent_end = ALIGN(extent_end,
481 root->sectorsize);
482 } else if (disk_bytenr > 0) {
483 ret = btrfs_free_extent(trans, root,
484 disk_bytenr, num_bytes, 0,
485 root->root_key.objectid,
486 key.objectid, key.offset -
487 extent_offset);
488 BUG_ON(ret);
489 inode_sub_bytes(inode,
490 extent_end - key.offset);
491 *hint_byte = disk_bytenr;
492 }
493
494 if (end == extent_end)
495 break;
496
497 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
498 path->slots[0]++;
499 goto next_slot;
500 }
501
502 ret = btrfs_del_items(trans, root, path, del_slot,
503 del_nr);
504 BUG_ON(ret);
505
506 del_nr = 0;
507 del_slot = 0;
508
509 btrfs_release_path(root, path);
510 continue;
511 }
512
513 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400514 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000515
516 if (del_nr > 0) {
517 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
518 BUG_ON(ret);
519 }
520
Chris Mason39279cc2007-06-12 06:35:45 -0400521 btrfs_free_path(path);
522 return ret;
523}
524
Yan Zhengd899e052008-10-30 14:25:28 -0400525static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000526 u64 objectid, u64 bytenr, u64 orig_offset,
527 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400528{
529 struct btrfs_file_extent_item *fi;
530 struct btrfs_key key;
531 u64 extent_end;
532
533 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
534 return 0;
535
536 btrfs_item_key_to_cpu(leaf, &key, slot);
537 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
538 return 0;
539
540 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
541 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
542 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000543 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400544 btrfs_file_extent_compression(leaf, fi) ||
545 btrfs_file_extent_encryption(leaf, fi) ||
546 btrfs_file_extent_other_encoding(leaf, fi))
547 return 0;
548
549 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
550 if ((*start && *start != key.offset) || (*end && *end != extent_end))
551 return 0;
552
553 *start = key.offset;
554 *end = extent_end;
555 return 1;
556}
557
558/*
559 * Mark extent in the range start - end as written.
560 *
561 * This changes extent type from 'pre-allocated' to 'regular'. If only
562 * part of extent is marked as written, the extent will be split into
563 * two or three.
564 */
565int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400566 struct inode *inode, u64 start, u64 end)
567{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000568 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400569 struct extent_buffer *leaf;
570 struct btrfs_path *path;
571 struct btrfs_file_extent_item *fi;
572 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000573 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400574 u64 bytenr;
575 u64 num_bytes;
576 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400577 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400578 u64 other_start;
579 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000580 u64 split;
581 int del_nr = 0;
582 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000583 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400584 int ret;
585
586 btrfs_drop_extent_cache(inode, start, end - 1, 0);
587
588 path = btrfs_alloc_path();
589 BUG_ON(!path);
590again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000591 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000592 split = start;
Yan Zhengd899e052008-10-30 14:25:28 -0400593 key.objectid = inode->i_ino;
594 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000595 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -0400596
597 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
598 if (ret > 0 && path->slots[0] > 0)
599 path->slots[0]--;
600
601 leaf = path->nodes[0];
602 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
603 BUG_ON(key.objectid != inode->i_ino ||
604 key.type != BTRFS_EXTENT_DATA_KEY);
605 fi = btrfs_item_ptr(leaf, path->slots[0],
606 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000607 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
608 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -0400609 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
610 BUG_ON(key.offset > start || extent_end < end);
611
612 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
613 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400614 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000615 memcpy(&new_key, &key, sizeof(new_key));
616
617 if (start == key.offset && end < extent_end) {
618 other_start = 0;
619 other_end = start;
620 if (extent_mergeable(leaf, path->slots[0] - 1,
621 inode->i_ino, bytenr, orig_offset,
622 &other_start, &other_end)) {
623 new_key.offset = end;
624 btrfs_set_item_key_safe(trans, root, path, &new_key);
625 fi = btrfs_item_ptr(leaf, path->slots[0],
626 struct btrfs_file_extent_item);
627 btrfs_set_file_extent_num_bytes(leaf, fi,
628 extent_end - end);
629 btrfs_set_file_extent_offset(leaf, fi,
630 end - orig_offset);
631 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
632 struct btrfs_file_extent_item);
633 btrfs_set_file_extent_num_bytes(leaf, fi,
634 end - other_start);
635 btrfs_mark_buffer_dirty(leaf);
636 goto out;
637 }
638 }
639
640 if (start > key.offset && end == extent_end) {
641 other_start = end;
642 other_end = 0;
643 if (extent_mergeable(leaf, path->slots[0] + 1,
644 inode->i_ino, bytenr, orig_offset,
645 &other_start, &other_end)) {
646 fi = btrfs_item_ptr(leaf, path->slots[0],
647 struct btrfs_file_extent_item);
648 btrfs_set_file_extent_num_bytes(leaf, fi,
649 start - key.offset);
650 path->slots[0]++;
651 new_key.offset = start;
652 btrfs_set_item_key_safe(trans, root, path, &new_key);
653
654 fi = btrfs_item_ptr(leaf, path->slots[0],
655 struct btrfs_file_extent_item);
656 btrfs_set_file_extent_num_bytes(leaf, fi,
657 other_end - start);
658 btrfs_set_file_extent_offset(leaf, fi,
659 start - orig_offset);
660 btrfs_mark_buffer_dirty(leaf);
661 goto out;
662 }
663 }
Yan Zhengd899e052008-10-30 14:25:28 -0400664
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000665 while (start > key.offset || end < extent_end) {
666 if (key.offset == start)
667 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400668
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000669 new_key.offset = split;
670 ret = btrfs_duplicate_item(trans, root, path, &new_key);
671 if (ret == -EAGAIN) {
672 btrfs_release_path(root, path);
673 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -0400674 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000675 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -0400676
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000677 leaf = path->nodes[0];
678 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -0400679 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -0400680 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000681 split - key.offset);
682
683 fi = btrfs_item_ptr(leaf, path->slots[0],
684 struct btrfs_file_extent_item);
685
686 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
687 btrfs_set_file_extent_num_bytes(leaf, fi,
688 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -0400689 btrfs_mark_buffer_dirty(leaf);
690
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000691 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
692 root->root_key.objectid,
693 inode->i_ino, orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400694 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -0400695
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000696 if (split == start) {
697 key.offset = start;
698 } else {
699 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400700 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000701 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400702 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000703 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -0400704 }
705
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000706 other_start = end;
707 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000708 if (extent_mergeable(leaf, path->slots[0] + 1,
709 inode->i_ino, bytenr, orig_offset,
710 &other_start, &other_end)) {
711 if (recow) {
712 btrfs_release_path(root, path);
713 goto again;
714 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000715 extent_end = other_end;
716 del_slot = path->slots[0] + 1;
717 del_nr++;
718 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
719 0, root->root_key.objectid,
720 inode->i_ino, orig_offset);
721 BUG_ON(ret);
722 }
723 other_start = 0;
724 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000725 if (extent_mergeable(leaf, path->slots[0] - 1,
726 inode->i_ino, bytenr, orig_offset,
727 &other_start, &other_end)) {
728 if (recow) {
729 btrfs_release_path(root, path);
730 goto again;
731 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000732 key.offset = other_start;
733 del_slot = path->slots[0];
734 del_nr++;
735 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
736 0, root->root_key.objectid,
737 inode->i_ino, orig_offset);
738 BUG_ON(ret);
739 }
740 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000741 fi = btrfs_item_ptr(leaf, path->slots[0],
742 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000743 btrfs_set_file_extent_type(leaf, fi,
744 BTRFS_FILE_EXTENT_REG);
745 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000746 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000747 fi = btrfs_item_ptr(leaf, del_slot - 1,
748 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000749 btrfs_set_file_extent_type(leaf, fi,
750 BTRFS_FILE_EXTENT_REG);
751 btrfs_set_file_extent_num_bytes(leaf, fi,
752 extent_end - key.offset);
753 btrfs_mark_buffer_dirty(leaf);
754
755 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
756 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000757 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000758out:
Yan Zhengd899e052008-10-30 14:25:28 -0400759 btrfs_free_path(path);
760 return 0;
761}
762
Chris Mason39279cc2007-06-12 06:35:45 -0400763/*
Chris Masond352ac62008-09-29 15:18:18 -0400764 * this gets pages into the page cache and locks them down, it also properly
765 * waits for data=ordered extents to finish before allowing the pages to be
766 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -0400767 */
Chris Masond3977122009-01-05 21:25:51 -0500768static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500769 struct page **pages, size_t num_pages,
770 loff_t pos, unsigned long first_index,
771 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400772{
Josef Bacik2ac55d42010-02-03 19:33:23 +0000773 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -0400774 int i;
775 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500776 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400777 int err = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400778 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400779 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400780
Chris Mason5f39d392007-10-15 16:14:19 -0400781 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400782 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400783
Yan Zheng9036c102008-10-30 14:19:41 -0400784 if (start_pos > inode->i_size) {
785 err = btrfs_cont_expand(inode, start_pos);
786 if (err)
787 return err;
788 }
789
Chris Mason39279cc2007-06-12 06:35:45 -0400790 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400791again:
Chris Mason39279cc2007-06-12 06:35:45 -0400792 for (i = 0; i < num_pages; i++) {
793 pages[i] = grab_cache_page(inode->i_mapping, index + i);
794 if (!pages[i]) {
Josef Bacik7adf5df2011-01-25 22:11:54 +0000795 int c;
796 for (c = i - 1; c >= 0; c--) {
797 unlock_page(pages[c]);
798 page_cache_release(pages[c]);
799 }
800 return -ENOMEM;
Chris Mason39279cc2007-06-12 06:35:45 -0400801 }
Chris Masonccd467d2007-06-28 15:57:36 -0400802 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400803 }
Chris Mason07627042008-02-19 11:29:24 -0500804 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400805 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000806 lock_extent_bits(&BTRFS_I(inode)->io_tree,
807 start_pos, last_pos - 1, 0, &cached_state,
808 GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500809 ordered = btrfs_lookup_first_ordered_extent(inode,
810 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400811 if (ordered &&
812 ordered->file_offset + ordered->len > start_pos &&
813 ordered->file_offset < last_pos) {
814 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000815 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
816 start_pos, last_pos - 1,
817 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400818 for (i = 0; i < num_pages; i++) {
819 unlock_page(pages[i]);
820 page_cache_release(pages[i]);
821 }
822 btrfs_wait_ordered_range(inode, start_pos,
823 last_pos - start_pos);
824 goto again;
825 }
826 if (ordered)
827 btrfs_put_ordered_extent(ordered);
828
Josef Bacik2ac55d42010-02-03 19:33:23 +0000829 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -0400830 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +0000831 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
Chris Mason07627042008-02-19 11:29:24 -0500832 GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000833 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
834 start_pos, last_pos - 1, &cached_state,
835 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500836 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400837 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400838 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400839 set_page_extent_mapped(pages[i]);
840 WARN_ON(!PageLocked(pages[i]));
841 }
Chris Mason39279cc2007-06-12 06:35:45 -0400842 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400843}
844
Josef Bacik11c65dc2010-05-23 11:07:21 -0400845static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
846 const struct iovec *iov,
847 unsigned long nr_segs, loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400848{
Josef Bacik11c65dc2010-05-23 11:07:21 -0400849 struct file *file = iocb->ki_filp;
850 struct inode *inode = fdentry(file)->d_inode;
851 struct btrfs_root *root = BTRFS_I(inode)->root;
852 struct page *pinned[2];
853 struct page **pages = NULL;
854 struct iov_iter i;
855 loff_t *ppos = &iocb->ki_pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400856 loff_t start_pos;
857 ssize_t num_written = 0;
858 ssize_t err = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400859 size_t count;
860 size_t ocount;
Chris Mason39279cc2007-06-12 06:35:45 -0400861 int ret = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400862 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400863 unsigned long first_index;
864 unsigned long last_index;
Chris Masoncb843a62008-10-03 12:30:02 -0400865 int will_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400866 int buffered = 0;
Xin Zhong914ee292010-12-09 09:30:14 +0000867 int copied = 0;
868 int dirty_pages = 0;
Chris Masoncb843a62008-10-03 12:30:02 -0400869
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100870 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
Chris Masoncb843a62008-10-03 12:30:02 -0400871 (file->f_flags & O_DIRECT));
Chris Mason8c2383c2007-06-18 09:57:58 -0400872
Chris Mason39279cc2007-06-12 06:35:45 -0400873 pinned[0] = NULL;
874 pinned[1] = NULL;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400875
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400876 start_pos = pos;
877
Chris Mason39279cc2007-06-12 06:35:45 -0400878 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
Chris Mason2b1f55b2008-09-24 11:48:04 -0400879
Chris Masonab93dbe2009-10-01 12:29:10 -0400880 mutex_lock(&inode->i_mutex);
881
Josef Bacik11c65dc2010-05-23 11:07:21 -0400882 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
883 if (err)
884 goto out;
885 count = ocount;
886
Chris Mason1832a6d2007-12-21 16:27:21 -0500887 current->backing_dev_info = inode->i_mapping->backing_dev_info;
Chris Mason39279cc2007-06-12 06:35:45 -0400888 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
Chris Mason1832a6d2007-12-21 16:27:21 -0500889 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400890 goto out;
891
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -0400892 if (count == 0)
Chris Masonab93dbe2009-10-01 12:29:10 -0400893 goto out;
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400894
895 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400896 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400897 goto out;
898
liuboacce9522011-01-06 19:30:25 +0800899 /*
900 * If BTRFS flips readonly due to some impossible error
901 * (fs_info->fs_state now has BTRFS_SUPER_FLAG_ERROR),
902 * although we have opened a file as writable, we have
903 * to stop this write operation to ensure FS consistency.
904 */
905 if (root->fs_info->fs_state & BTRFS_SUPER_FLAG_ERROR) {
906 err = -EROFS;
907 goto out;
908 }
909
Chris Mason39279cc2007-06-12 06:35:45 -0400910 file_update_time(file);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400911 BTRFS_I(inode)->sequence++;
Chris Mason39279cc2007-06-12 06:35:45 -0400912
Josef Bacik4b46fce2010-05-23 11:00:55 -0400913 if (unlikely(file->f_flags & O_DIRECT)) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400914 num_written = generic_file_direct_write(iocb, iov, &nr_segs,
915 pos, ppos, count,
916 ocount);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400917 /*
918 * the generic O_DIRECT will update in-memory i_size after the
919 * DIOs are done. But our endio handlers that update the on
920 * disk i_size never update past the in memory i_size. So we
921 * need one more update here to catch any additions to the
922 * file
923 */
924 if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
925 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
926 mark_inode_dirty(inode);
927 }
928
929 if (num_written < 0) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400930 ret = num_written;
931 num_written = 0;
932 goto out;
933 } else if (num_written == count) {
934 /* pick up pos changes done by the generic code */
935 pos = *ppos;
936 goto out;
937 }
Josef Bacik4b46fce2010-05-23 11:00:55 -0400938 /*
939 * We are going to do buffered for the rest of the range, so we
940 * need to make sure to invalidate the buffered pages when we're
941 * done.
942 */
943 buffered = 1;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400944 pos += num_written;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400945 }
946
Josef Bacik11c65dc2010-05-23 11:07:21 -0400947 iov_iter_init(&i, iov, nr_segs, count, num_written);
948 nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
949 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
950 (sizeof(struct page *)));
Chris Mason8c2383c2007-06-18 09:57:58 -0400951 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
liubo2a29edc2011-01-26 06:22:08 +0000952 if (!pages) {
953 ret = -ENOMEM;
954 goto out;
955 }
Chris Mason39279cc2007-06-12 06:35:45 -0400956
Chris Masonab93dbe2009-10-01 12:29:10 -0400957 /* generic_write_checks can change our pos */
958 start_pos = pos;
959
Chris Mason39279cc2007-06-12 06:35:45 -0400960 first_index = pos >> PAGE_CACHE_SHIFT;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400961 last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400962
963 /*
964 * there are lots of better ways to do this, but this code
965 * makes sure the first and last page in the file range are
966 * up to date and ready for cow
967 */
968 if ((pos & (PAGE_CACHE_SIZE - 1))) {
969 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
970 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400971 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400972 BUG_ON(ret);
973 wait_on_page_locked(pinned[0]);
974 } else {
975 unlock_page(pinned[0]);
976 }
977 }
Josef Bacik11c65dc2010-05-23 11:07:21 -0400978 if ((pos + iov_iter_count(&i)) & (PAGE_CACHE_SIZE - 1)) {
Chris Mason39279cc2007-06-12 06:35:45 -0400979 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
980 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400981 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400982 BUG_ON(ret);
983 wait_on_page_locked(pinned[1]);
984 } else {
985 unlock_page(pinned[1]);
986 }
987 }
988
Josef Bacik11c65dc2010-05-23 11:07:21 -0400989 while (iov_iter_count(&i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400990 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400991 size_t write_bytes = min(iov_iter_count(&i),
992 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400993 offset);
Yan, Zheng3a909832011-01-18 13:34:40 +0800994 size_t num_pages = (write_bytes + offset +
995 PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400996
Chris Mason8c2383c2007-06-18 09:57:58 -0400997 WARN_ON(num_pages > nrptrs);
yanhai zhu9aead432009-01-05 15:49:11 -0500998 memset(pages, 0, sizeof(struct page *) * nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -0500999
Xin Zhong914ee292010-12-09 09:30:14 +00001000 /*
1001 * Fault pages before locking them in prepare_pages
1002 * to avoid recursive lock
1003 */
1004 if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) {
1005 ret = -EFAULT;
1006 goto out;
1007 }
1008
1009 ret = btrfs_delalloc_reserve_space(inode,
1010 num_pages << PAGE_CACHE_SHIFT);
Chris Mason1832a6d2007-12-21 16:27:21 -05001011 if (ret)
1012 goto out;
1013
Chris Mason39279cc2007-06-12 06:35:45 -04001014 ret = prepare_pages(root, file, pages, num_pages,
1015 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -04001016 write_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -05001017 if (ret) {
Xin Zhong914ee292010-12-09 09:30:14 +00001018 btrfs_delalloc_release_space(inode,
1019 num_pages << PAGE_CACHE_SHIFT);
Chris Mason54aa1f42007-06-22 14:16:25 -04001020 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -05001021 }
Chris Mason39279cc2007-06-12 06:35:45 -04001022
Xin Zhong914ee292010-12-09 09:30:14 +00001023 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001024 write_bytes, pages, &i);
Yan, Zheng3a909832011-01-18 13:34:40 +08001025 dirty_pages = (copied + offset + PAGE_CACHE_SIZE - 1) >>
1026 PAGE_CACHE_SHIFT;
Xin Zhong914ee292010-12-09 09:30:14 +00001027
1028 if (num_pages > dirty_pages) {
1029 if (copied > 0)
1030 atomic_inc(
1031 &BTRFS_I(inode)->outstanding_extents);
1032 btrfs_delalloc_release_space(inode,
1033 (num_pages - dirty_pages) <<
1034 PAGE_CACHE_SHIFT);
1035 }
1036
1037 if (copied > 0) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001038 dirty_and_release_pages(NULL, root, file, pages,
Xin Zhong914ee292010-12-09 09:30:14 +00001039 dirty_pages, pos, copied);
Chris Mason54aa1f42007-06-22 14:16:25 -04001040 }
Chris Mason39279cc2007-06-12 06:35:45 -04001041
Chris Mason39279cc2007-06-12 06:35:45 -04001042 btrfs_drop_pages(pages, num_pages);
Xin Zhong914ee292010-12-09 09:30:14 +00001043
1044 if (copied > 0) {
1045 if (will_write) {
1046 filemap_fdatawrite_range(inode->i_mapping, pos,
1047 pos + copied - 1);
1048 } else {
1049 balance_dirty_pages_ratelimited_nr(
1050 inode->i_mapping,
1051 dirty_pages);
1052 if (dirty_pages <
1053 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1054 btrfs_btree_balance_dirty(root, 1);
1055 btrfs_throttle(root);
1056 }
Josef Bacik6a632092009-02-20 11:00:09 -05001057 }
Chris Mason39279cc2007-06-12 06:35:45 -04001058
Xin Zhong914ee292010-12-09 09:30:14 +00001059 pos += copied;
1060 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001061
Chris Mason39279cc2007-06-12 06:35:45 -04001062 cond_resched();
1063 }
Chris Mason39279cc2007-06-12 06:35:45 -04001064out:
Chris Mason1832a6d2007-12-21 16:27:21 -05001065 mutex_unlock(&inode->i_mutex);
Josef Bacik6a632092009-02-20 11:00:09 -05001066 if (ret)
1067 err = ret;
Chris Mason5b92ee72008-01-03 13:46:11 -05001068
Chris Mason8c2383c2007-06-18 09:57:58 -04001069 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -04001070 if (pinned[0])
1071 page_cache_release(pinned[0]);
1072 if (pinned[1])
1073 page_cache_release(pinned[1]);
1074 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001075
Chris Mason5a3f23d2009-03-31 13:27:11 -04001076 /*
1077 * we want to make sure fsync finds this change
1078 * but we haven't joined a transaction running right now.
1079 *
1080 * Later on, someone is sure to update the inode and get the
1081 * real transid recorded.
1082 *
1083 * We set last_trans now to the fs_info generation + 1,
1084 * this will either be one more than the running transaction
1085 * or the generation used for the next transaction if there isn't
1086 * one running right now.
1087 */
1088 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1089
Chris Masoncb843a62008-10-03 12:30:02 -04001090 if (num_written > 0 && will_write) {
Chris Masone02119d2008-09-05 16:13:11 -04001091 struct btrfs_trans_handle *trans;
1092
Chris Masoncb843a62008-10-03 12:30:02 -04001093 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1094 if (err)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001095 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -04001096
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001097 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001098 trans = btrfs_start_transaction(root, 0);
Josef Bacik495e8672010-11-19 20:36:10 +00001099 if (IS_ERR(trans)) {
1100 num_written = PTR_ERR(trans);
1101 goto done;
1102 }
1103 mutex_lock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001104 ret = btrfs_log_dentry_safe(trans, root,
1105 file->f_dentry);
Josef Bacik495e8672010-11-19 20:36:10 +00001106 mutex_unlock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001107 if (ret == 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001108 ret = btrfs_sync_log(trans, root);
1109 if (ret == 0)
1110 btrfs_end_transaction(trans, root);
1111 else
1112 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001113 } else if (ret != BTRFS_NO_LOG_SYNC) {
Chris Masoncb843a62008-10-03 12:30:02 -04001114 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001115 } else {
1116 btrfs_end_transaction(trans, root);
Chris Masoncb843a62008-10-03 12:30:02 -04001117 }
Chris Masone02119d2008-09-05 16:13:11 -04001118 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04001119 if (file->f_flags & O_DIRECT && buffered) {
Chris Masoncb843a62008-10-03 12:30:02 -04001120 invalidate_mapping_pages(inode->i_mapping,
1121 start_pos >> PAGE_CACHE_SHIFT,
1122 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1123 }
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001124 }
Josef Bacik495e8672010-11-19 20:36:10 +00001125done:
Chris Mason39279cc2007-06-12 06:35:45 -04001126 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001127 return num_written ? num_written : err;
1128}
1129
Chris Masond3977122009-01-05 21:25:51 -05001130int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001131{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001132 /*
1133 * ordered_data_close is set by settattr when we are about to truncate
1134 * a file from a non-zero size to a zero size. This tries to
1135 * flush down new bytes that may have been written if the
1136 * application were using truncate to replace a file in place.
1137 */
1138 if (BTRFS_I(inode)->ordered_data_close) {
1139 BTRFS_I(inode)->ordered_data_close = 0;
1140 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1141 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1142 filemap_flush(inode->i_mapping);
1143 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001144 if (filp->private_data)
1145 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001146 return 0;
1147}
1148
Chris Masond352ac62008-09-29 15:18:18 -04001149/*
1150 * fsync call for both files and directories. This logs the inode into
1151 * the tree log instead of forcing full commits whenever possible.
1152 *
1153 * It needs to call filemap_fdatawait so that all ordered extent updates are
1154 * in the metadata btree are up to date for copying to the log.
1155 *
1156 * It drops the inode mutex before doing the tree log commit. This is an
1157 * important optimization for directories because holding the mutex prevents
1158 * new operations on the dir while we write to disk.
1159 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001160int btrfs_sync_file(struct file *file, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001161{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001162 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001163 struct inode *inode = dentry->d_inode;
1164 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001165 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001166 struct btrfs_trans_handle *trans;
1167
Chris Mason257c62e2009-10-13 13:21:08 -04001168
1169 /* we wait first, since the writeback may change the inode */
1170 root->log_batch++;
1171 /* the VFS called filemap_fdatawrite for us */
1172 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1173 root->log_batch++;
1174
Chris Mason39279cc2007-06-12 06:35:45 -04001175 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001176 * check the transaction that last modified this inode
1177 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001178 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001179 if (!BTRFS_I(inode)->last_trans)
1180 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001181
Chris Mason257c62e2009-10-13 13:21:08 -04001182 /*
1183 * if the last transaction that changed this file was before
1184 * the current transaction, we can bail out now without any
1185 * syncing
1186 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001187 mutex_lock(&root->fs_info->trans_mutex);
1188 if (BTRFS_I(inode)->last_trans <=
1189 root->fs_info->last_trans_committed) {
1190 BTRFS_I(inode)->last_trans = 0;
1191 mutex_unlock(&root->fs_info->trans_mutex);
1192 goto out;
1193 }
1194 mutex_unlock(&root->fs_info->trans_mutex);
1195
1196 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001197 * ok we haven't committed the transaction yet, lets do a commit
1198 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001199 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001200 btrfs_ioctl_trans_end(file);
1201
Yan, Zhenga22285a2010-05-16 10:48:46 -04001202 trans = btrfs_start_transaction(root, 0);
1203 if (IS_ERR(trans)) {
1204 ret = PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04001205 goto out;
1206 }
Chris Masone02119d2008-09-05 16:13:11 -04001207
Chris Mason2cfbd502009-02-20 10:55:10 -05001208 ret = btrfs_log_dentry_safe(trans, root, dentry);
Chris Masond3977122009-01-05 21:25:51 -05001209 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04001210 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001211
1212 /* we've logged all the items and now have a consistent
1213 * version of the file in the log. It is possible that
1214 * someone will come in and modify the file, but that's
1215 * fine because the log is consistent on disk, and we
1216 * have references to all of the file's extents
1217 *
1218 * It is possible that someone will come in and log the
1219 * file again, but that will end up using the synchronization
1220 * inside btrfs_sync_log to keep things safe.
1221 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001222 mutex_unlock(&dentry->d_inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001223
Chris Mason257c62e2009-10-13 13:21:08 -04001224 if (ret != BTRFS_NO_LOG_SYNC) {
1225 if (ret > 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001226 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001227 } else {
1228 ret = btrfs_sync_log(trans, root);
1229 if (ret == 0)
1230 ret = btrfs_end_transaction(trans, root);
1231 else
1232 ret = btrfs_commit_transaction(trans, root);
1233 }
1234 } else {
1235 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001236 }
Chris Mason2cfbd502009-02-20 10:55:10 -05001237 mutex_lock(&dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001238out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001239 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001240}
1241
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001242static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001243 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001244 .page_mkwrite = btrfs_page_mkwrite,
1245};
1246
1247static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1248{
Miao Xie058a4572010-05-20 07:21:50 +00001249 struct address_space *mapping = filp->f_mapping;
1250
1251 if (!mapping->a_ops->readpage)
1252 return -ENOEXEC;
1253
Chris Mason9ebefb182007-06-15 13:50:00 -04001254 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00001255 vma->vm_ops = &btrfs_file_vm_ops;
1256 vma->vm_flags |= VM_CAN_NONLINEAR;
1257
Chris Mason9ebefb182007-06-15 13:50:00 -04001258 return 0;
1259}
1260
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001261const struct file_operations btrfs_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04001262 .llseek = generic_file_llseek,
1263 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00001264 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001265 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001266 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001267 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001268 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001269 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001270 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001271 .fsync = btrfs_sync_file,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001272 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001273#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001274 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001275#endif
1276};