blob: 05df688c96f442311482a87e5d966494ccc0049b [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]) {
795 err = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -0400796 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400797 }
Chris Masonccd467d2007-06-28 15:57:36 -0400798 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400799 }
Chris Mason07627042008-02-19 11:29:24 -0500800 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400801 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000802 lock_extent_bits(&BTRFS_I(inode)->io_tree,
803 start_pos, last_pos - 1, 0, &cached_state,
804 GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500805 ordered = btrfs_lookup_first_ordered_extent(inode,
806 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400807 if (ordered &&
808 ordered->file_offset + ordered->len > start_pos &&
809 ordered->file_offset < last_pos) {
810 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000811 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
812 start_pos, last_pos - 1,
813 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400814 for (i = 0; i < num_pages; i++) {
815 unlock_page(pages[i]);
816 page_cache_release(pages[i]);
817 }
818 btrfs_wait_ordered_range(inode, start_pos,
819 last_pos - start_pos);
820 goto again;
821 }
822 if (ordered)
823 btrfs_put_ordered_extent(ordered);
824
Josef Bacik2ac55d42010-02-03 19:33:23 +0000825 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -0400826 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +0000827 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
Chris Mason07627042008-02-19 11:29:24 -0500828 GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000829 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
830 start_pos, last_pos - 1, &cached_state,
831 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500832 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400833 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400834 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400835 set_page_extent_mapped(pages[i]);
836 WARN_ON(!PageLocked(pages[i]));
837 }
Chris Mason39279cc2007-06-12 06:35:45 -0400838 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400839}
840
Josef Bacik11c65dc2010-05-23 11:07:21 -0400841static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
842 const struct iovec *iov,
843 unsigned long nr_segs, loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400844{
Josef Bacik11c65dc2010-05-23 11:07:21 -0400845 struct file *file = iocb->ki_filp;
846 struct inode *inode = fdentry(file)->d_inode;
847 struct btrfs_root *root = BTRFS_I(inode)->root;
848 struct page *pinned[2];
849 struct page **pages = NULL;
850 struct iov_iter i;
851 loff_t *ppos = &iocb->ki_pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400852 loff_t start_pos;
853 ssize_t num_written = 0;
854 ssize_t err = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400855 size_t count;
856 size_t ocount;
Chris Mason39279cc2007-06-12 06:35:45 -0400857 int ret = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400858 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400859 unsigned long first_index;
860 unsigned long last_index;
Chris Masoncb843a62008-10-03 12:30:02 -0400861 int will_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400862 int buffered = 0;
Xin Zhong914ee292010-12-09 09:30:14 +0000863 int copied = 0;
864 int dirty_pages = 0;
Chris Masoncb843a62008-10-03 12:30:02 -0400865
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100866 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
Chris Masoncb843a62008-10-03 12:30:02 -0400867 (file->f_flags & O_DIRECT));
Chris Mason8c2383c2007-06-18 09:57:58 -0400868
Chris Mason39279cc2007-06-12 06:35:45 -0400869 pinned[0] = NULL;
870 pinned[1] = NULL;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400871
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400872 start_pos = pos;
873
Chris Mason39279cc2007-06-12 06:35:45 -0400874 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
Chris Mason2b1f55b2008-09-24 11:48:04 -0400875
Chris Masonab93dbe2009-10-01 12:29:10 -0400876 mutex_lock(&inode->i_mutex);
877
Josef Bacik11c65dc2010-05-23 11:07:21 -0400878 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
879 if (err)
880 goto out;
881 count = ocount;
882
Chris Mason1832a6d2007-12-21 16:27:21 -0500883 current->backing_dev_info = inode->i_mapping->backing_dev_info;
Chris Mason39279cc2007-06-12 06:35:45 -0400884 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
Chris Mason1832a6d2007-12-21 16:27:21 -0500885 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400886 goto out;
887
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -0400888 if (count == 0)
Chris Masonab93dbe2009-10-01 12:29:10 -0400889 goto out;
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400890
891 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400892 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400893 goto out;
894
Chris Mason39279cc2007-06-12 06:35:45 -0400895 file_update_time(file);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400896 BTRFS_I(inode)->sequence++;
Chris Mason39279cc2007-06-12 06:35:45 -0400897
Josef Bacik4b46fce2010-05-23 11:00:55 -0400898 if (unlikely(file->f_flags & O_DIRECT)) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400899 num_written = generic_file_direct_write(iocb, iov, &nr_segs,
900 pos, ppos, count,
901 ocount);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400902 /*
903 * the generic O_DIRECT will update in-memory i_size after the
904 * DIOs are done. But our endio handlers that update the on
905 * disk i_size never update past the in memory i_size. So we
906 * need one more update here to catch any additions to the
907 * file
908 */
909 if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
910 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
911 mark_inode_dirty(inode);
912 }
913
914 if (num_written < 0) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400915 ret = num_written;
916 num_written = 0;
917 goto out;
918 } else if (num_written == count) {
919 /* pick up pos changes done by the generic code */
920 pos = *ppos;
921 goto out;
922 }
Josef Bacik4b46fce2010-05-23 11:00:55 -0400923 /*
924 * We are going to do buffered for the rest of the range, so we
925 * need to make sure to invalidate the buffered pages when we're
926 * done.
927 */
928 buffered = 1;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400929 pos += num_written;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400930 }
931
Josef Bacik11c65dc2010-05-23 11:07:21 -0400932 iov_iter_init(&i, iov, nr_segs, count, num_written);
933 nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
934 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
935 (sizeof(struct page *)));
Chris Mason8c2383c2007-06-18 09:57:58 -0400936 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -0400937
Chris Masonab93dbe2009-10-01 12:29:10 -0400938 /* generic_write_checks can change our pos */
939 start_pos = pos;
940
Chris Mason39279cc2007-06-12 06:35:45 -0400941 first_index = pos >> PAGE_CACHE_SHIFT;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400942 last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400943
944 /*
945 * there are lots of better ways to do this, but this code
946 * makes sure the first and last page in the file range are
947 * up to date and ready for cow
948 */
949 if ((pos & (PAGE_CACHE_SIZE - 1))) {
950 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
951 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400952 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400953 BUG_ON(ret);
954 wait_on_page_locked(pinned[0]);
955 } else {
956 unlock_page(pinned[0]);
957 }
958 }
Josef Bacik11c65dc2010-05-23 11:07:21 -0400959 if ((pos + iov_iter_count(&i)) & (PAGE_CACHE_SIZE - 1)) {
Chris Mason39279cc2007-06-12 06:35:45 -0400960 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
961 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400962 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400963 BUG_ON(ret);
964 wait_on_page_locked(pinned[1]);
965 } else {
966 unlock_page(pinned[1]);
967 }
968 }
969
Josef Bacik11c65dc2010-05-23 11:07:21 -0400970 while (iov_iter_count(&i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400971 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400972 size_t write_bytes = min(iov_iter_count(&i),
973 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400974 offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400975 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
976 PAGE_CACHE_SHIFT;
977
Chris Mason8c2383c2007-06-18 09:57:58 -0400978 WARN_ON(num_pages > nrptrs);
yanhai zhu9aead432009-01-05 15:49:11 -0500979 memset(pages, 0, sizeof(struct page *) * nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -0500980
Xin Zhong914ee292010-12-09 09:30:14 +0000981 /*
982 * Fault pages before locking them in prepare_pages
983 * to avoid recursive lock
984 */
985 if (unlikely(iov_iter_fault_in_readable(&i, write_bytes))) {
986 ret = -EFAULT;
987 goto out;
988 }
989
990 ret = btrfs_delalloc_reserve_space(inode,
991 num_pages << PAGE_CACHE_SHIFT);
Chris Mason1832a6d2007-12-21 16:27:21 -0500992 if (ret)
993 goto out;
994
Chris Mason39279cc2007-06-12 06:35:45 -0400995 ret = prepare_pages(root, file, pages, num_pages,
996 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400997 write_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500998 if (ret) {
Xin Zhong914ee292010-12-09 09:30:14 +0000999 btrfs_delalloc_release_space(inode,
1000 num_pages << PAGE_CACHE_SHIFT);
Chris Mason54aa1f42007-06-22 14:16:25 -04001001 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -05001002 }
Chris Mason39279cc2007-06-12 06:35:45 -04001003
Xin Zhong914ee292010-12-09 09:30:14 +00001004 copied = btrfs_copy_from_user(pos, num_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001005 write_bytes, pages, &i);
Xin Zhong914ee292010-12-09 09:30:14 +00001006 dirty_pages = (copied + PAGE_CACHE_SIZE - 1) >>
1007 PAGE_CACHE_SHIFT;
1008
1009 if (num_pages > dirty_pages) {
1010 if (copied > 0)
1011 atomic_inc(
1012 &BTRFS_I(inode)->outstanding_extents);
1013 btrfs_delalloc_release_space(inode,
1014 (num_pages - dirty_pages) <<
1015 PAGE_CACHE_SHIFT);
1016 }
1017
1018 if (copied > 0) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -04001019 dirty_and_release_pages(NULL, root, file, pages,
Xin Zhong914ee292010-12-09 09:30:14 +00001020 dirty_pages, pos, copied);
Chris Mason54aa1f42007-06-22 14:16:25 -04001021 }
Chris Mason39279cc2007-06-12 06:35:45 -04001022
Chris Mason39279cc2007-06-12 06:35:45 -04001023 btrfs_drop_pages(pages, num_pages);
Xin Zhong914ee292010-12-09 09:30:14 +00001024
1025 if (copied > 0) {
1026 if (will_write) {
1027 filemap_fdatawrite_range(inode->i_mapping, pos,
1028 pos + copied - 1);
1029 } else {
1030 balance_dirty_pages_ratelimited_nr(
1031 inode->i_mapping,
1032 dirty_pages);
1033 if (dirty_pages <
1034 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1035 btrfs_btree_balance_dirty(root, 1);
1036 btrfs_throttle(root);
1037 }
Josef Bacik6a632092009-02-20 11:00:09 -05001038 }
Chris Mason39279cc2007-06-12 06:35:45 -04001039
Xin Zhong914ee292010-12-09 09:30:14 +00001040 pos += copied;
1041 num_written += copied;
Chris Mason39279cc2007-06-12 06:35:45 -04001042
Chris Mason39279cc2007-06-12 06:35:45 -04001043 cond_resched();
1044 }
Chris Mason39279cc2007-06-12 06:35:45 -04001045out:
Chris Mason1832a6d2007-12-21 16:27:21 -05001046 mutex_unlock(&inode->i_mutex);
Josef Bacik6a632092009-02-20 11:00:09 -05001047 if (ret)
1048 err = ret;
Chris Mason5b92ee72008-01-03 13:46:11 -05001049
Chris Mason8c2383c2007-06-18 09:57:58 -04001050 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -04001051 if (pinned[0])
1052 page_cache_release(pinned[0]);
1053 if (pinned[1])
1054 page_cache_release(pinned[1]);
1055 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001056
Chris Mason5a3f23d2009-03-31 13:27:11 -04001057 /*
1058 * we want to make sure fsync finds this change
1059 * but we haven't joined a transaction running right now.
1060 *
1061 * Later on, someone is sure to update the inode and get the
1062 * real transid recorded.
1063 *
1064 * We set last_trans now to the fs_info generation + 1,
1065 * this will either be one more than the running transaction
1066 * or the generation used for the next transaction if there isn't
1067 * one running right now.
1068 */
1069 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1070
Chris Masoncb843a62008-10-03 12:30:02 -04001071 if (num_written > 0 && will_write) {
Chris Masone02119d2008-09-05 16:13:11 -04001072 struct btrfs_trans_handle *trans;
1073
Chris Masoncb843a62008-10-03 12:30:02 -04001074 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1075 if (err)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001076 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -04001077
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001078 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001079 trans = btrfs_start_transaction(root, 0);
Josef Bacik495e8672010-11-19 20:36:10 +00001080 if (IS_ERR(trans)) {
1081 num_written = PTR_ERR(trans);
1082 goto done;
1083 }
1084 mutex_lock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001085 ret = btrfs_log_dentry_safe(trans, root,
1086 file->f_dentry);
Josef Bacik495e8672010-11-19 20:36:10 +00001087 mutex_unlock(&inode->i_mutex);
Chris Masoncb843a62008-10-03 12:30:02 -04001088 if (ret == 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001089 ret = btrfs_sync_log(trans, root);
1090 if (ret == 0)
1091 btrfs_end_transaction(trans, root);
1092 else
1093 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001094 } else if (ret != BTRFS_NO_LOG_SYNC) {
Chris Masoncb843a62008-10-03 12:30:02 -04001095 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001096 } else {
1097 btrfs_end_transaction(trans, root);
Chris Masoncb843a62008-10-03 12:30:02 -04001098 }
Chris Masone02119d2008-09-05 16:13:11 -04001099 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04001100 if (file->f_flags & O_DIRECT && buffered) {
Chris Masoncb843a62008-10-03 12:30:02 -04001101 invalidate_mapping_pages(inode->i_mapping,
1102 start_pos >> PAGE_CACHE_SHIFT,
1103 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1104 }
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001105 }
Josef Bacik495e8672010-11-19 20:36:10 +00001106done:
Chris Mason39279cc2007-06-12 06:35:45 -04001107 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001108 return num_written ? num_written : err;
1109}
1110
Chris Masond3977122009-01-05 21:25:51 -05001111int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001112{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001113 /*
1114 * ordered_data_close is set by settattr when we are about to truncate
1115 * a file from a non-zero size to a zero size. This tries to
1116 * flush down new bytes that may have been written if the
1117 * application were using truncate to replace a file in place.
1118 */
1119 if (BTRFS_I(inode)->ordered_data_close) {
1120 BTRFS_I(inode)->ordered_data_close = 0;
1121 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1122 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1123 filemap_flush(inode->i_mapping);
1124 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001125 if (filp->private_data)
1126 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001127 return 0;
1128}
1129
Chris Masond352ac62008-09-29 15:18:18 -04001130/*
1131 * fsync call for both files and directories. This logs the inode into
1132 * the tree log instead of forcing full commits whenever possible.
1133 *
1134 * It needs to call filemap_fdatawait so that all ordered extent updates are
1135 * in the metadata btree are up to date for copying to the log.
1136 *
1137 * It drops the inode mutex before doing the tree log commit. This is an
1138 * important optimization for directories because holding the mutex prevents
1139 * new operations on the dir while we write to disk.
1140 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001141int btrfs_sync_file(struct file *file, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001142{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001143 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001144 struct inode *inode = dentry->d_inode;
1145 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001146 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001147 struct btrfs_trans_handle *trans;
1148
Chris Mason257c62e2009-10-13 13:21:08 -04001149
1150 /* we wait first, since the writeback may change the inode */
1151 root->log_batch++;
1152 /* the VFS called filemap_fdatawrite for us */
1153 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1154 root->log_batch++;
1155
Chris Mason39279cc2007-06-12 06:35:45 -04001156 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001157 * check the transaction that last modified this inode
1158 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001159 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001160 if (!BTRFS_I(inode)->last_trans)
1161 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001162
Chris Mason257c62e2009-10-13 13:21:08 -04001163 /*
1164 * if the last transaction that changed this file was before
1165 * the current transaction, we can bail out now without any
1166 * syncing
1167 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001168 mutex_lock(&root->fs_info->trans_mutex);
1169 if (BTRFS_I(inode)->last_trans <=
1170 root->fs_info->last_trans_committed) {
1171 BTRFS_I(inode)->last_trans = 0;
1172 mutex_unlock(&root->fs_info->trans_mutex);
1173 goto out;
1174 }
1175 mutex_unlock(&root->fs_info->trans_mutex);
1176
1177 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001178 * ok we haven't committed the transaction yet, lets do a commit
1179 */
Dan Carpenter6f902af2010-05-29 09:49:07 +00001180 if (file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001181 btrfs_ioctl_trans_end(file);
1182
Yan, Zhenga22285a2010-05-16 10:48:46 -04001183 trans = btrfs_start_transaction(root, 0);
1184 if (IS_ERR(trans)) {
1185 ret = PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04001186 goto out;
1187 }
Chris Masone02119d2008-09-05 16:13:11 -04001188
Chris Mason2cfbd502009-02-20 10:55:10 -05001189 ret = btrfs_log_dentry_safe(trans, root, dentry);
Chris Masond3977122009-01-05 21:25:51 -05001190 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04001191 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001192
1193 /* we've logged all the items and now have a consistent
1194 * version of the file in the log. It is possible that
1195 * someone will come in and modify the file, but that's
1196 * fine because the log is consistent on disk, and we
1197 * have references to all of the file's extents
1198 *
1199 * It is possible that someone will come in and log the
1200 * file again, but that will end up using the synchronization
1201 * inside btrfs_sync_log to keep things safe.
1202 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001203 mutex_unlock(&dentry->d_inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001204
Chris Mason257c62e2009-10-13 13:21:08 -04001205 if (ret != BTRFS_NO_LOG_SYNC) {
1206 if (ret > 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001207 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001208 } else {
1209 ret = btrfs_sync_log(trans, root);
1210 if (ret == 0)
1211 ret = btrfs_end_transaction(trans, root);
1212 else
1213 ret = btrfs_commit_transaction(trans, root);
1214 }
1215 } else {
1216 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001217 }
Chris Mason2cfbd502009-02-20 10:55:10 -05001218 mutex_lock(&dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001219out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001220 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001221}
1222
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001223static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001224 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001225 .page_mkwrite = btrfs_page_mkwrite,
1226};
1227
1228static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1229{
Miao Xie058a4572010-05-20 07:21:50 +00001230 struct address_space *mapping = filp->f_mapping;
1231
1232 if (!mapping->a_ops->readpage)
1233 return -ENOEXEC;
1234
Chris Mason9ebefb182007-06-15 13:50:00 -04001235 file_accessed(filp);
Miao Xie058a4572010-05-20 07:21:50 +00001236 vma->vm_ops = &btrfs_file_vm_ops;
1237 vma->vm_flags |= VM_CAN_NONLINEAR;
1238
Chris Mason9ebefb182007-06-15 13:50:00 -04001239 return 0;
1240}
1241
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001242const struct file_operations btrfs_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04001243 .llseek = generic_file_llseek,
1244 .read = do_sync_read,
Miao Xie4a0010712010-06-07 03:38:51 +00001245 .write = do_sync_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001246 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001247 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001248 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001249 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001250 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001251 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001252 .fsync = btrfs_sync_file,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001253 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001254#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001255 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001256#endif
1257};