blob: 787b50a16a146eea9bb4ec4202fcdab1f3f4bec0 [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{
Josef Bacik11c65dc2010-05-23 11:07:21 -040051 size_t copied;
52 int pg = 0;
Chris Mason39279cc2007-06-12 06:35:45 -040053 int offset = pos & (PAGE_CACHE_SIZE - 1);
54
Josef Bacik11c65dc2010-05-23 11:07:21 -040055 while (write_bytes > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -040056 size_t count = min_t(size_t,
57 PAGE_CACHE_SIZE - offset, write_bytes);
Josef Bacik11c65dc2010-05-23 11:07:21 -040058 struct page *page = prepared_pages[pg];
59again:
60 if (unlikely(iov_iter_fault_in_readable(i, count)))
61 return -EFAULT;
Chris Mason39279cc2007-06-12 06:35:45 -040062
63 /* Copy data from userspace to the current page */
Josef Bacik11c65dc2010-05-23 11:07:21 -040064 copied = iov_iter_copy_from_user(page, i, offset, count);
65
Chris Mason39279cc2007-06-12 06:35:45 -040066 /* Flush processor's dcache for this page */
67 flush_dcache_page(page);
Josef Bacik11c65dc2010-05-23 11:07:21 -040068 iov_iter_advance(i, copied);
69 write_bytes -= copied;
Chris Mason39279cc2007-06-12 06:35:45 -040070
Josef Bacik11c65dc2010-05-23 11:07:21 -040071 if (unlikely(copied == 0)) {
72 count = min_t(size_t, PAGE_CACHE_SIZE - offset,
73 iov_iter_single_seg_count(i));
74 goto again;
75 }
76
77 if (unlikely(copied < PAGE_CACHE_SIZE - offset)) {
78 offset += copied;
79 } else {
80 pg++;
81 offset = 0;
82 }
Chris Mason39279cc2007-06-12 06:35:45 -040083 }
Josef Bacik11c65dc2010-05-23 11:07:21 -040084 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -040085}
86
Chris Masond352ac62008-09-29 15:18:18 -040087/*
88 * unlocks pages after btrfs_file_write is done with them
89 */
Chris Masond3977122009-01-05 21:25:51 -050090static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -040091{
92 size_t i;
93 for (i = 0; i < num_pages; i++) {
94 if (!pages[i])
95 break;
Chris Masond352ac62008-09-29 15:18:18 -040096 /* page checked is some magic around finding pages that
97 * have been modified without going through btrfs_set_page_dirty
98 * clear it here
99 */
Chris Mason4a096752008-07-21 10:29:44 -0400100 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400101 unlock_page(pages[i]);
102 mark_page_accessed(pages[i]);
103 page_cache_release(pages[i]);
104 }
105}
106
Chris Masond352ac62008-09-29 15:18:18 -0400107/*
108 * after copy_from_user, pages need to be dirtied and we need to make
109 * sure holes are created between the current EOF and the start of
110 * any next extents (if required).
111 *
112 * this also makes the decision about creating an inline extent vs
113 * doing real data extents, marking pages dirty and delalloc as required.
114 */
Chris Masond3977122009-01-05 21:25:51 -0500115static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400116 struct btrfs_root *root,
117 struct file *file,
118 struct page **pages,
119 size_t num_pages,
120 loff_t pos,
121 size_t write_bytes)
122{
Chris Mason39279cc2007-06-12 06:35:45 -0400123 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400124 int i;
Chris Mason6da6aba2007-12-18 16:15:09 -0500125 struct inode *inode = fdentry(file)->d_inode;
Chris Masondb945352007-10-15 16:15:53 -0400126 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400127 u64 start_pos;
128 u64 end_of_last_block;
129 u64 end_pos = pos + write_bytes;
130 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400131
Chris Mason5f39d392007-10-15 16:14:19 -0400132 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400133 num_bytes = (write_bytes + pos - start_pos +
134 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400135
Chris Masondb945352007-10-15 16:15:53 -0400136 end_of_last_block = start_pos + num_bytes - 1;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000137 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
138 NULL);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400139 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400140
Chris Masonc8b97812008-10-29 14:49:59 -0400141 for (i = 0; i < num_pages; i++) {
142 struct page *p = pages[i];
143 SetPageUptodate(p);
144 ClearPageChecked(p);
145 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400146 }
147 if (end_pos > isize) {
148 i_size_write(inode, end_pos);
Chris Masonf597bb12009-06-27 21:06:22 -0400149 /* we've only changed i_size in ram, and we haven't updated
150 * the disk i_size. There is no need to log the inode
151 * at this time.
152 */
Chris Mason39279cc2007-06-12 06:35:45 -0400153 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400154 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400155}
156
Chris Masond352ac62008-09-29 15:18:18 -0400157/*
158 * this drops all the extents in the cache that intersect the range
159 * [start, end]. Existing extents are split as required.
160 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400161int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
162 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400163{
164 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400165 struct extent_map *split = NULL;
166 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400167 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500168 u64 len = end - start + 1;
Chris Mason3b951512008-04-17 11:29:12 -0400169 int ret;
170 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400171 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400172 int compressed = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400173
Chris Masone6dcd2d2008-07-17 12:53:50 -0400174 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400175 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500176 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400177 testend = 0;
178 }
Chris Masond3977122009-01-05 21:25:51 -0500179 while (1) {
Chris Mason3b951512008-04-17 11:29:12 -0400180 if (!split)
181 split = alloc_extent_map(GFP_NOFS);
182 if (!split2)
183 split2 = alloc_extent_map(GFP_NOFS);
184
Chris Mason890871b2009-09-02 16:24:52 -0400185 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500186 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500187 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400188 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400189 break;
Chris Masond1310b22008-01-24 16:13:08 -0500190 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400191 flags = em->flags;
192 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000193 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400194 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400195 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400196 break;
197 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000198 start = em->start + em->len;
199 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400200 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400201 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400202 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400203 continue;
204 }
Chris Masonc8b97812008-10-29 14:49:59 -0400205 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400206 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400207 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400208
209 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
210 em->start < start) {
211 split->start = em->start;
212 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500213 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400214 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400215
216 if (compressed)
217 split->block_len = em->block_len;
218 else
219 split->block_len = split->len;
220
Chris Mason3b951512008-04-17 11:29:12 -0400221 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400222 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400223 ret = add_extent_mapping(em_tree, split);
224 BUG_ON(ret);
225 free_extent_map(split);
226 split = split2;
227 split2 = NULL;
228 }
229 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
230 testend && em->start + em->len > start + len) {
231 u64 diff = start + len - em->start;
232
233 split->start = start + len;
234 split->len = em->start + em->len - (start + len);
235 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400236 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400237
Chris Masonc8b97812008-10-29 14:49:59 -0400238 if (compressed) {
239 split->block_len = em->block_len;
240 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500241 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400242 } else {
243 split->block_len = split->len;
244 split->block_start = em->block_start + diff;
Chris Mason445a6942008-11-10 11:53:33 -0500245 split->orig_start = split->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400246 }
Chris Mason3b951512008-04-17 11:29:12 -0400247
248 ret = add_extent_mapping(em_tree, split);
249 BUG_ON(ret);
250 free_extent_map(split);
251 split = NULL;
252 }
Chris Mason890871b2009-09-02 16:24:52 -0400253 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500254
Chris Masona52d9a82007-08-27 16:49:44 -0400255 /* once for us */
256 free_extent_map(em);
257 /* once for the tree*/
258 free_extent_map(em);
259 }
Chris Mason3b951512008-04-17 11:29:12 -0400260 if (split)
261 free_extent_map(split);
262 if (split2)
263 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400264 return 0;
265}
266
Chris Mason39279cc2007-06-12 06:35:45 -0400267/*
268 * this is very complex, but the basic idea is to drop all extents
269 * in the range start - end. hint_block is filled in with a block number
270 * that would be a good hint to the block allocator for this file.
271 *
272 * If an extent intersects the range but is not entirely inside the range
273 * it is either truncated or split. Anything entirely inside the range
274 * is deleted from the tree.
275 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000276int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
277 u64 start, u64 end, u64 *hint_byte, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400278{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000279 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason00f5c792007-11-30 10:09:33 -0500280 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000281 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500282 struct btrfs_path *path;
283 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000284 struct btrfs_key new_key;
285 u64 search_start = start;
286 u64 disk_bytenr = 0;
287 u64 num_bytes = 0;
288 u64 extent_offset = 0;
289 u64 extent_end = 0;
290 int del_nr = 0;
291 int del_slot = 0;
292 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400293 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500294 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400295
Chris Masona1ed8352009-09-11 12:27:37 -0400296 if (drop_cache)
297 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400298
Chris Mason39279cc2007-06-12 06:35:45 -0400299 path = btrfs_alloc_path();
300 if (!path)
301 return -ENOMEM;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000302
Chris Masond3977122009-01-05 21:25:51 -0500303 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400304 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400305 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
306 search_start, -1);
307 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000308 break;
309 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
310 leaf = path->nodes[0];
311 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
312 if (key.objectid == inode->i_ino &&
313 key.type == BTRFS_EXTENT_DATA_KEY)
314 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400315 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400316 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000317next_slot:
318 leaf = path->nodes[0];
319 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
320 BUG_ON(del_nr > 0);
321 ret = btrfs_next_leaf(root, path);
322 if (ret < 0)
323 break;
324 if (ret > 0) {
325 ret = 0;
326 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400327 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000328 leaf = path->nodes[0];
329 recow = 1;
330 }
331
332 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
333 if (key.objectid > inode->i_ino ||
334 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
335 break;
336
337 fi = btrfs_item_ptr(leaf, path->slots[0],
338 struct btrfs_file_extent_item);
339 extent_type = btrfs_file_extent_type(leaf, fi);
340
341 if (extent_type == BTRFS_FILE_EXTENT_REG ||
342 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
343 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
344 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
345 extent_offset = btrfs_file_extent_offset(leaf, fi);
346 extent_end = key.offset +
347 btrfs_file_extent_num_bytes(leaf, fi);
348 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
349 extent_end = key.offset +
350 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400351 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000352 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400353 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400354 }
355
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000356 if (extent_end <= search_start) {
357 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400358 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400359 }
360
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000361 search_start = max(key.offset, start);
362 if (recow) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400363 btrfs_release_path(root, path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000364 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400365 }
Chris Mason771ed682008-11-06 22:02:51 -0500366
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000367 /*
368 * | - range to drop - |
369 * | -------- extent -------- |
370 */
371 if (start > key.offset && end < extent_end) {
372 BUG_ON(del_nr > 0);
373 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
374
375 memcpy(&new_key, &key, sizeof(new_key));
376 new_key.offset = start;
377 ret = btrfs_duplicate_item(trans, root, path,
378 &new_key);
379 if (ret == -EAGAIN) {
380 btrfs_release_path(root, path);
381 continue;
382 }
383 if (ret < 0)
384 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400385
Chris Mason5f39d392007-10-15 16:14:19 -0400386 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000387 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
388 struct btrfs_file_extent_item);
389 btrfs_set_file_extent_num_bytes(leaf, fi,
390 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400391
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000392 fi = btrfs_item_ptr(leaf, path->slots[0],
393 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400394
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000395 extent_offset += start - key.offset;
396 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
397 btrfs_set_file_extent_num_bytes(leaf, fi,
398 extent_end - start);
399 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400400
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000401 if (disk_bytenr > 0) {
402 ret = btrfs_inc_extent_ref(trans, root,
403 disk_bytenr, num_bytes, 0,
404 root->root_key.objectid,
405 new_key.objectid,
406 start - extent_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -0400407 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000408 *hint_byte = disk_bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400409 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000410 key.offset = start;
411 }
412 /*
413 * | ---- range to drop ----- |
414 * | -------- extent -------- |
415 */
416 if (start <= key.offset && end < extent_end) {
417 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
418
419 memcpy(&new_key, &key, sizeof(new_key));
420 new_key.offset = end;
421 btrfs_set_item_key_safe(trans, root, path, &new_key);
422
423 extent_offset += end - key.offset;
424 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
425 btrfs_set_file_extent_num_bytes(leaf, fi,
426 extent_end - end);
427 btrfs_mark_buffer_dirty(leaf);
428 if (disk_bytenr > 0) {
429 inode_sub_bytes(inode, end - key.offset);
430 *hint_byte = disk_bytenr;
431 }
432 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400433 }
434
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000435 search_start = extent_end;
436 /*
437 * | ---- range to drop ----- |
438 * | -------- extent -------- |
439 */
440 if (start > key.offset && end >= extent_end) {
441 BUG_ON(del_nr > 0);
442 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
443
444 btrfs_set_file_extent_num_bytes(leaf, fi,
445 start - key.offset);
446 btrfs_mark_buffer_dirty(leaf);
447 if (disk_bytenr > 0) {
448 inode_sub_bytes(inode, extent_end - start);
449 *hint_byte = disk_bytenr;
450 }
451 if (end == extent_end)
452 break;
453
454 path->slots[0]++;
455 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400456 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000457
458 /*
459 * | ---- range to drop ----- |
460 * | ------ extent ------ |
461 */
462 if (start <= key.offset && end >= extent_end) {
463 if (del_nr == 0) {
464 del_slot = path->slots[0];
465 del_nr = 1;
466 } else {
467 BUG_ON(del_slot + del_nr != path->slots[0]);
468 del_nr++;
469 }
470
471 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
472 inode_sub_bytes(inode,
473 extent_end - key.offset);
474 extent_end = ALIGN(extent_end,
475 root->sectorsize);
476 } else if (disk_bytenr > 0) {
477 ret = btrfs_free_extent(trans, root,
478 disk_bytenr, num_bytes, 0,
479 root->root_key.objectid,
480 key.objectid, key.offset -
481 extent_offset);
482 BUG_ON(ret);
483 inode_sub_bytes(inode,
484 extent_end - key.offset);
485 *hint_byte = disk_bytenr;
486 }
487
488 if (end == extent_end)
489 break;
490
491 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
492 path->slots[0]++;
493 goto next_slot;
494 }
495
496 ret = btrfs_del_items(trans, root, path, del_slot,
497 del_nr);
498 BUG_ON(ret);
499
500 del_nr = 0;
501 del_slot = 0;
502
503 btrfs_release_path(root, path);
504 continue;
505 }
506
507 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400508 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000509
510 if (del_nr > 0) {
511 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
512 BUG_ON(ret);
513 }
514
Chris Mason39279cc2007-06-12 06:35:45 -0400515 btrfs_free_path(path);
516 return ret;
517}
518
Yan Zhengd899e052008-10-30 14:25:28 -0400519static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000520 u64 objectid, u64 bytenr, u64 orig_offset,
521 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400522{
523 struct btrfs_file_extent_item *fi;
524 struct btrfs_key key;
525 u64 extent_end;
526
527 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
528 return 0;
529
530 btrfs_item_key_to_cpu(leaf, &key, slot);
531 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
532 return 0;
533
534 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
535 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
536 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000537 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400538 btrfs_file_extent_compression(leaf, fi) ||
539 btrfs_file_extent_encryption(leaf, fi) ||
540 btrfs_file_extent_other_encoding(leaf, fi))
541 return 0;
542
543 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
544 if ((*start && *start != key.offset) || (*end && *end != extent_end))
545 return 0;
546
547 *start = key.offset;
548 *end = extent_end;
549 return 1;
550}
551
552/*
553 * Mark extent in the range start - end as written.
554 *
555 * This changes extent type from 'pre-allocated' to 'regular'. If only
556 * part of extent is marked as written, the extent will be split into
557 * two or three.
558 */
559int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400560 struct inode *inode, u64 start, u64 end)
561{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000562 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400563 struct extent_buffer *leaf;
564 struct btrfs_path *path;
565 struct btrfs_file_extent_item *fi;
566 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000567 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400568 u64 bytenr;
569 u64 num_bytes;
570 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400571 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400572 u64 other_start;
573 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000574 u64 split;
575 int del_nr = 0;
576 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000577 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400578 int ret;
579
580 btrfs_drop_extent_cache(inode, start, end - 1, 0);
581
582 path = btrfs_alloc_path();
583 BUG_ON(!path);
584again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000585 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000586 split = start;
Yan Zhengd899e052008-10-30 14:25:28 -0400587 key.objectid = inode->i_ino;
588 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000589 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -0400590
591 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
592 if (ret > 0 && path->slots[0] > 0)
593 path->slots[0]--;
594
595 leaf = path->nodes[0];
596 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
597 BUG_ON(key.objectid != inode->i_ino ||
598 key.type != BTRFS_EXTENT_DATA_KEY);
599 fi = btrfs_item_ptr(leaf, path->slots[0],
600 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000601 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
602 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -0400603 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
604 BUG_ON(key.offset > start || extent_end < end);
605
606 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
607 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400608 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000609 memcpy(&new_key, &key, sizeof(new_key));
610
611 if (start == key.offset && end < extent_end) {
612 other_start = 0;
613 other_end = start;
614 if (extent_mergeable(leaf, path->slots[0] - 1,
615 inode->i_ino, bytenr, orig_offset,
616 &other_start, &other_end)) {
617 new_key.offset = end;
618 btrfs_set_item_key_safe(trans, root, path, &new_key);
619 fi = btrfs_item_ptr(leaf, path->slots[0],
620 struct btrfs_file_extent_item);
621 btrfs_set_file_extent_num_bytes(leaf, fi,
622 extent_end - end);
623 btrfs_set_file_extent_offset(leaf, fi,
624 end - orig_offset);
625 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
626 struct btrfs_file_extent_item);
627 btrfs_set_file_extent_num_bytes(leaf, fi,
628 end - other_start);
629 btrfs_mark_buffer_dirty(leaf);
630 goto out;
631 }
632 }
633
634 if (start > key.offset && end == extent_end) {
635 other_start = end;
636 other_end = 0;
637 if (extent_mergeable(leaf, path->slots[0] + 1,
638 inode->i_ino, bytenr, orig_offset,
639 &other_start, &other_end)) {
640 fi = btrfs_item_ptr(leaf, path->slots[0],
641 struct btrfs_file_extent_item);
642 btrfs_set_file_extent_num_bytes(leaf, fi,
643 start - key.offset);
644 path->slots[0]++;
645 new_key.offset = start;
646 btrfs_set_item_key_safe(trans, root, path, &new_key);
647
648 fi = btrfs_item_ptr(leaf, path->slots[0],
649 struct btrfs_file_extent_item);
650 btrfs_set_file_extent_num_bytes(leaf, fi,
651 other_end - start);
652 btrfs_set_file_extent_offset(leaf, fi,
653 start - orig_offset);
654 btrfs_mark_buffer_dirty(leaf);
655 goto out;
656 }
657 }
Yan Zhengd899e052008-10-30 14:25:28 -0400658
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000659 while (start > key.offset || end < extent_end) {
660 if (key.offset == start)
661 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400662
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000663 new_key.offset = split;
664 ret = btrfs_duplicate_item(trans, root, path, &new_key);
665 if (ret == -EAGAIN) {
666 btrfs_release_path(root, path);
667 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -0400668 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000669 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -0400670
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000671 leaf = path->nodes[0];
672 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -0400673 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -0400674 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000675 split - key.offset);
676
677 fi = btrfs_item_ptr(leaf, path->slots[0],
678 struct btrfs_file_extent_item);
679
680 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
681 btrfs_set_file_extent_num_bytes(leaf, fi,
682 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -0400683 btrfs_mark_buffer_dirty(leaf);
684
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000685 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
686 root->root_key.objectid,
687 inode->i_ino, orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400688 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -0400689
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000690 if (split == start) {
691 key.offset = start;
692 } else {
693 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400694 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000695 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400696 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000697 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -0400698 }
699
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000700 other_start = end;
701 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000702 if (extent_mergeable(leaf, path->slots[0] + 1,
703 inode->i_ino, bytenr, orig_offset,
704 &other_start, &other_end)) {
705 if (recow) {
706 btrfs_release_path(root, path);
707 goto again;
708 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000709 extent_end = other_end;
710 del_slot = path->slots[0] + 1;
711 del_nr++;
712 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
713 0, root->root_key.objectid,
714 inode->i_ino, orig_offset);
715 BUG_ON(ret);
716 }
717 other_start = 0;
718 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000719 if (extent_mergeable(leaf, path->slots[0] - 1,
720 inode->i_ino, bytenr, orig_offset,
721 &other_start, &other_end)) {
722 if (recow) {
723 btrfs_release_path(root, path);
724 goto again;
725 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000726 key.offset = other_start;
727 del_slot = path->slots[0];
728 del_nr++;
729 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
730 0, root->root_key.objectid,
731 inode->i_ino, orig_offset);
732 BUG_ON(ret);
733 }
734 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000735 fi = btrfs_item_ptr(leaf, path->slots[0],
736 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000737 btrfs_set_file_extent_type(leaf, fi,
738 BTRFS_FILE_EXTENT_REG);
739 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000740 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000741 fi = btrfs_item_ptr(leaf, del_slot - 1,
742 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000743 btrfs_set_file_extent_type(leaf, fi,
744 BTRFS_FILE_EXTENT_REG);
745 btrfs_set_file_extent_num_bytes(leaf, fi,
746 extent_end - key.offset);
747 btrfs_mark_buffer_dirty(leaf);
748
749 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
750 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000751 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000752out:
Yan Zhengd899e052008-10-30 14:25:28 -0400753 btrfs_free_path(path);
754 return 0;
755}
756
Chris Mason39279cc2007-06-12 06:35:45 -0400757/*
Chris Masond352ac62008-09-29 15:18:18 -0400758 * this gets pages into the page cache and locks them down, it also properly
759 * waits for data=ordered extents to finish before allowing the pages to be
760 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -0400761 */
Chris Masond3977122009-01-05 21:25:51 -0500762static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500763 struct page **pages, size_t num_pages,
764 loff_t pos, unsigned long first_index,
765 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400766{
Josef Bacik2ac55d42010-02-03 19:33:23 +0000767 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -0400768 int i;
769 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500770 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400771 int err = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400772 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400773 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400774
Chris Mason5f39d392007-10-15 16:14:19 -0400775 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400776 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400777
Yan Zheng9036c102008-10-30 14:19:41 -0400778 if (start_pos > inode->i_size) {
779 err = btrfs_cont_expand(inode, start_pos);
780 if (err)
781 return err;
782 }
783
Chris Mason39279cc2007-06-12 06:35:45 -0400784 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400785again:
Chris Mason39279cc2007-06-12 06:35:45 -0400786 for (i = 0; i < num_pages; i++) {
787 pages[i] = grab_cache_page(inode->i_mapping, index + i);
788 if (!pages[i]) {
789 err = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -0400790 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400791 }
Chris Masonccd467d2007-06-28 15:57:36 -0400792 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400793 }
Chris Mason07627042008-02-19 11:29:24 -0500794 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400795 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000796 lock_extent_bits(&BTRFS_I(inode)->io_tree,
797 start_pos, last_pos - 1, 0, &cached_state,
798 GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500799 ordered = btrfs_lookup_first_ordered_extent(inode,
800 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400801 if (ordered &&
802 ordered->file_offset + ordered->len > start_pos &&
803 ordered->file_offset < last_pos) {
804 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000805 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
806 start_pos, last_pos - 1,
807 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400808 for (i = 0; i < num_pages; i++) {
809 unlock_page(pages[i]);
810 page_cache_release(pages[i]);
811 }
812 btrfs_wait_ordered_range(inode, start_pos,
813 last_pos - start_pos);
814 goto again;
815 }
816 if (ordered)
817 btrfs_put_ordered_extent(ordered);
818
Josef Bacik2ac55d42010-02-03 19:33:23 +0000819 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -0400820 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +0000821 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
Chris Mason07627042008-02-19 11:29:24 -0500822 GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000823 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
824 start_pos, last_pos - 1, &cached_state,
825 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500826 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400827 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400828 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400829 set_page_extent_mapped(pages[i]);
830 WARN_ON(!PageLocked(pages[i]));
831 }
Chris Mason39279cc2007-06-12 06:35:45 -0400832 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400833}
834
Josef Bacik11c65dc2010-05-23 11:07:21 -0400835static ssize_t btrfs_file_aio_write(struct kiocb *iocb,
836 const struct iovec *iov,
837 unsigned long nr_segs, loff_t pos)
Josef Bacik4b46fce2010-05-23 11:00:55 -0400838{
Josef Bacik11c65dc2010-05-23 11:07:21 -0400839 struct file *file = iocb->ki_filp;
840 struct inode *inode = fdentry(file)->d_inode;
841 struct btrfs_root *root = BTRFS_I(inode)->root;
842 struct page *pinned[2];
843 struct page **pages = NULL;
844 struct iov_iter i;
845 loff_t *ppos = &iocb->ki_pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400846 loff_t start_pos;
847 ssize_t num_written = 0;
848 ssize_t err = 0;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400849 size_t count;
850 size_t ocount;
Chris Mason39279cc2007-06-12 06:35:45 -0400851 int ret = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400852 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400853 unsigned long first_index;
854 unsigned long last_index;
Chris Masoncb843a62008-10-03 12:30:02 -0400855 int will_write;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400856 int buffered = 0;
Chris Masoncb843a62008-10-03 12:30:02 -0400857
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100858 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
Chris Masoncb843a62008-10-03 12:30:02 -0400859 (file->f_flags & O_DIRECT));
Chris Mason8c2383c2007-06-18 09:57:58 -0400860
Chris Mason39279cc2007-06-12 06:35:45 -0400861 pinned[0] = NULL;
862 pinned[1] = NULL;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400863
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400864 start_pos = pos;
865
Chris Mason39279cc2007-06-12 06:35:45 -0400866 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
Chris Mason2b1f55b2008-09-24 11:48:04 -0400867
Chris Masonab93dbe2009-10-01 12:29:10 -0400868 mutex_lock(&inode->i_mutex);
869
Josef Bacik11c65dc2010-05-23 11:07:21 -0400870 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
871 if (err)
872 goto out;
873 count = ocount;
874
Chris Mason1832a6d2007-12-21 16:27:21 -0500875 current->backing_dev_info = inode->i_mapping->backing_dev_info;
Chris Mason39279cc2007-06-12 06:35:45 -0400876 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
Chris Mason1832a6d2007-12-21 16:27:21 -0500877 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400878 goto out;
879
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -0400880 if (count == 0)
Chris Masonab93dbe2009-10-01 12:29:10 -0400881 goto out;
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400882
883 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400884 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400885 goto out;
886
Chris Mason39279cc2007-06-12 06:35:45 -0400887 file_update_time(file);
Josef Bacik4b46fce2010-05-23 11:00:55 -0400888 BTRFS_I(inode)->sequence++;
Chris Mason39279cc2007-06-12 06:35:45 -0400889
Josef Bacik4b46fce2010-05-23 11:00:55 -0400890 if (unlikely(file->f_flags & O_DIRECT)) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400891 num_written = generic_file_direct_write(iocb, iov, &nr_segs,
892 pos, ppos, count,
893 ocount);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400894 /*
895 * the generic O_DIRECT will update in-memory i_size after the
896 * DIOs are done. But our endio handlers that update the on
897 * disk i_size never update past the in memory i_size. So we
898 * need one more update here to catch any additions to the
899 * file
900 */
901 if (inode->i_size != BTRFS_I(inode)->disk_i_size) {
902 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
903 mark_inode_dirty(inode);
904 }
905
906 if (num_written < 0) {
Josef Bacik11c65dc2010-05-23 11:07:21 -0400907 ret = num_written;
908 num_written = 0;
909 goto out;
910 } else if (num_written == count) {
911 /* pick up pos changes done by the generic code */
912 pos = *ppos;
913 goto out;
914 }
Josef Bacik4b46fce2010-05-23 11:00:55 -0400915 /*
916 * We are going to do buffered for the rest of the range, so we
917 * need to make sure to invalidate the buffered pages when we're
918 * done.
919 */
920 buffered = 1;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400921 pos += num_written;
Josef Bacik4b46fce2010-05-23 11:00:55 -0400922 }
923
Josef Bacik11c65dc2010-05-23 11:07:21 -0400924 iov_iter_init(&i, iov, nr_segs, count, num_written);
925 nrptrs = min((iov_iter_count(&i) + PAGE_CACHE_SIZE - 1) /
926 PAGE_CACHE_SIZE, PAGE_CACHE_SIZE /
927 (sizeof(struct page *)));
Chris Mason8c2383c2007-06-18 09:57:58 -0400928 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -0400929
Chris Masonab93dbe2009-10-01 12:29:10 -0400930 /* generic_write_checks can change our pos */
931 start_pos = pos;
932
Chris Mason39279cc2007-06-12 06:35:45 -0400933 first_index = pos >> PAGE_CACHE_SHIFT;
Josef Bacik11c65dc2010-05-23 11:07:21 -0400934 last_index = (pos + iov_iter_count(&i)) >> PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400935
936 /*
937 * there are lots of better ways to do this, but this code
938 * makes sure the first and last page in the file range are
939 * up to date and ready for cow
940 */
941 if ((pos & (PAGE_CACHE_SIZE - 1))) {
942 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
943 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400944 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400945 BUG_ON(ret);
946 wait_on_page_locked(pinned[0]);
947 } else {
948 unlock_page(pinned[0]);
949 }
950 }
Josef Bacik11c65dc2010-05-23 11:07:21 -0400951 if ((pos + iov_iter_count(&i)) & (PAGE_CACHE_SIZE - 1)) {
Chris Mason39279cc2007-06-12 06:35:45 -0400952 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
953 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400954 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400955 BUG_ON(ret);
956 wait_on_page_locked(pinned[1]);
957 } else {
958 unlock_page(pinned[1]);
959 }
960 }
961
Josef Bacik11c65dc2010-05-23 11:07:21 -0400962 while (iov_iter_count(&i) > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400963 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Josef Bacik11c65dc2010-05-23 11:07:21 -0400964 size_t write_bytes = min(iov_iter_count(&i),
965 nrptrs * (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400966 offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400967 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
968 PAGE_CACHE_SHIFT;
969
Chris Mason8c2383c2007-06-18 09:57:58 -0400970 WARN_ON(num_pages > nrptrs);
yanhai zhu9aead432009-01-05 15:49:11 -0500971 memset(pages, 0, sizeof(struct page *) * nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -0500972
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400973 ret = btrfs_delalloc_reserve_space(inode, write_bytes);
Chris Mason1832a6d2007-12-21 16:27:21 -0500974 if (ret)
975 goto out;
976
Chris Mason39279cc2007-06-12 06:35:45 -0400977 ret = prepare_pages(root, file, pages, num_pages,
978 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400979 write_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500980 if (ret) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400981 btrfs_delalloc_release_space(inode, write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400982 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -0500983 }
Chris Mason39279cc2007-06-12 06:35:45 -0400984
Chris Mason39279cc2007-06-12 06:35:45 -0400985 ret = btrfs_copy_from_user(pos, num_pages,
Josef Bacik11c65dc2010-05-23 11:07:21 -0400986 write_bytes, pages, &i);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400987 if (ret == 0) {
988 dirty_and_release_pages(NULL, root, file, pages,
989 num_pages, pos, write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400990 }
Chris Mason39279cc2007-06-12 06:35:45 -0400991
Chris Mason39279cc2007-06-12 06:35:45 -0400992 btrfs_drop_pages(pages, num_pages);
Josef Bacik6a632092009-02-20 11:00:09 -0500993 if (ret) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400994 btrfs_delalloc_release_space(inode, write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400995 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -0500996 }
Chris Mason39279cc2007-06-12 06:35:45 -0400997
Chris Masoncb843a62008-10-03 12:30:02 -0400998 if (will_write) {
Christoph Hellwig8aa38c32009-10-01 12:58:30 -0400999 filemap_fdatawrite_range(inode->i_mapping, pos,
1000 pos + write_bytes - 1);
Chris Masoncb843a62008-10-03 12:30:02 -04001001 } else {
1002 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
1003 num_pages);
1004 if (num_pages <
1005 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1006 btrfs_btree_balance_dirty(root, 1);
1007 btrfs_throttle(root);
1008 }
1009
Chris Mason39279cc2007-06-12 06:35:45 -04001010 pos += write_bytes;
1011 num_written += write_bytes;
1012
Chris Mason39279cc2007-06-12 06:35:45 -04001013 cond_resched();
1014 }
Chris Mason39279cc2007-06-12 06:35:45 -04001015out:
Chris Mason1832a6d2007-12-21 16:27:21 -05001016 mutex_unlock(&inode->i_mutex);
Josef Bacik6a632092009-02-20 11:00:09 -05001017 if (ret)
1018 err = ret;
Chris Mason5b92ee72008-01-03 13:46:11 -05001019
Chris Mason8c2383c2007-06-18 09:57:58 -04001020 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -04001021 if (pinned[0])
1022 page_cache_release(pinned[0]);
1023 if (pinned[1])
1024 page_cache_release(pinned[1]);
1025 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001026
Chris Mason5a3f23d2009-03-31 13:27:11 -04001027 /*
1028 * we want to make sure fsync finds this change
1029 * but we haven't joined a transaction running right now.
1030 *
1031 * Later on, someone is sure to update the inode and get the
1032 * real transid recorded.
1033 *
1034 * We set last_trans now to the fs_info generation + 1,
1035 * this will either be one more than the running transaction
1036 * or the generation used for the next transaction if there isn't
1037 * one running right now.
1038 */
1039 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1040
Chris Masoncb843a62008-10-03 12:30:02 -04001041 if (num_written > 0 && will_write) {
Chris Masone02119d2008-09-05 16:13:11 -04001042 struct btrfs_trans_handle *trans;
1043
Chris Masoncb843a62008-10-03 12:30:02 -04001044 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1045 if (err)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001046 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -04001047
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +01001048 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -04001049 trans = btrfs_start_transaction(root, 0);
Chris Masoncb843a62008-10-03 12:30:02 -04001050 ret = btrfs_log_dentry_safe(trans, root,
1051 file->f_dentry);
1052 if (ret == 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001053 ret = btrfs_sync_log(trans, root);
1054 if (ret == 0)
1055 btrfs_end_transaction(trans, root);
1056 else
1057 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001058 } else if (ret != BTRFS_NO_LOG_SYNC) {
Chris Masoncb843a62008-10-03 12:30:02 -04001059 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001060 } else {
1061 btrfs_end_transaction(trans, root);
Chris Masoncb843a62008-10-03 12:30:02 -04001062 }
Chris Masone02119d2008-09-05 16:13:11 -04001063 }
Josef Bacik4b46fce2010-05-23 11:00:55 -04001064 if (file->f_flags & O_DIRECT && buffered) {
Chris Masoncb843a62008-10-03 12:30:02 -04001065 invalidate_mapping_pages(inode->i_mapping,
1066 start_pos >> PAGE_CACHE_SHIFT,
1067 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1068 }
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001069 }
Chris Mason39279cc2007-06-12 06:35:45 -04001070 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001071 return num_written ? num_written : err;
1072}
1073
Chris Masond3977122009-01-05 21:25:51 -05001074int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001075{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001076 /*
1077 * ordered_data_close is set by settattr when we are about to truncate
1078 * a file from a non-zero size to a zero size. This tries to
1079 * flush down new bytes that may have been written if the
1080 * application were using truncate to replace a file in place.
1081 */
1082 if (BTRFS_I(inode)->ordered_data_close) {
1083 BTRFS_I(inode)->ordered_data_close = 0;
1084 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1085 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1086 filemap_flush(inode->i_mapping);
1087 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001088 if (filp->private_data)
1089 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001090 return 0;
1091}
1092
Chris Masond352ac62008-09-29 15:18:18 -04001093/*
1094 * fsync call for both files and directories. This logs the inode into
1095 * the tree log instead of forcing full commits whenever possible.
1096 *
1097 * It needs to call filemap_fdatawait so that all ordered extent updates are
1098 * in the metadata btree are up to date for copying to the log.
1099 *
1100 * It drops the inode mutex before doing the tree log commit. This is an
1101 * important optimization for directories because holding the mutex prevents
1102 * new operations on the dir while we write to disk.
1103 */
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001104int btrfs_sync_file(struct file *file, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001105{
Christoph Hellwig7ea80852010-05-26 17:53:25 +02001106 struct dentry *dentry = file->f_path.dentry;
Chris Mason39279cc2007-06-12 06:35:45 -04001107 struct inode *inode = dentry->d_inode;
1108 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001109 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001110 struct btrfs_trans_handle *trans;
1111
Chris Mason257c62e2009-10-13 13:21:08 -04001112
1113 /* we wait first, since the writeback may change the inode */
1114 root->log_batch++;
1115 /* the VFS called filemap_fdatawrite for us */
1116 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1117 root->log_batch++;
1118
Chris Mason39279cc2007-06-12 06:35:45 -04001119 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001120 * check the transaction that last modified this inode
1121 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001122 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001123 if (!BTRFS_I(inode)->last_trans)
1124 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001125
Chris Mason257c62e2009-10-13 13:21:08 -04001126 /*
1127 * if the last transaction that changed this file was before
1128 * the current transaction, we can bail out now without any
1129 * syncing
1130 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001131 mutex_lock(&root->fs_info->trans_mutex);
1132 if (BTRFS_I(inode)->last_trans <=
1133 root->fs_info->last_trans_committed) {
1134 BTRFS_I(inode)->last_trans = 0;
1135 mutex_unlock(&root->fs_info->trans_mutex);
1136 goto out;
1137 }
1138 mutex_unlock(&root->fs_info->trans_mutex);
1139
1140 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001141 * ok we haven't committed the transaction yet, lets do a commit
1142 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001143 if (file && file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001144 btrfs_ioctl_trans_end(file);
1145
Yan, Zhenga22285a2010-05-16 10:48:46 -04001146 trans = btrfs_start_transaction(root, 0);
1147 if (IS_ERR(trans)) {
1148 ret = PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04001149 goto out;
1150 }
Chris Masone02119d2008-09-05 16:13:11 -04001151
Chris Mason2cfbd502009-02-20 10:55:10 -05001152 ret = btrfs_log_dentry_safe(trans, root, dentry);
Chris Masond3977122009-01-05 21:25:51 -05001153 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04001154 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001155
1156 /* we've logged all the items and now have a consistent
1157 * version of the file in the log. It is possible that
1158 * someone will come in and modify the file, but that's
1159 * fine because the log is consistent on disk, and we
1160 * have references to all of the file's extents
1161 *
1162 * It is possible that someone will come in and log the
1163 * file again, but that will end up using the synchronization
1164 * inside btrfs_sync_log to keep things safe.
1165 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001166 mutex_unlock(&dentry->d_inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001167
Chris Mason257c62e2009-10-13 13:21:08 -04001168 if (ret != BTRFS_NO_LOG_SYNC) {
1169 if (ret > 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001170 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001171 } else {
1172 ret = btrfs_sync_log(trans, root);
1173 if (ret == 0)
1174 ret = btrfs_end_transaction(trans, root);
1175 else
1176 ret = btrfs_commit_transaction(trans, root);
1177 }
1178 } else {
1179 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001180 }
Chris Mason2cfbd502009-02-20 10:55:10 -05001181 mutex_lock(&dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001182out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001183 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001184}
1185
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +04001186static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001187 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001188 .page_mkwrite = btrfs_page_mkwrite,
1189};
1190
1191static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1192{
1193 vma->vm_ops = &btrfs_file_vm_ops;
1194 file_accessed(filp);
1195 return 0;
1196}
1197
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001198const struct file_operations btrfs_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04001199 .llseek = generic_file_llseek,
1200 .read = do_sync_read,
Chris Mason9ebefb182007-06-15 13:50:00 -04001201 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001202 .splice_read = generic_file_splice_read,
Josef Bacik11c65dc2010-05-23 11:07:21 -04001203 .aio_write = btrfs_file_aio_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001204 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001205 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001206 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001207 .fsync = btrfs_sync_file,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001208 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001209#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001210 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001211#endif
1212};