blob: 6d8f817eadb5013cab3b4c53b167dfefe4d1f8d9 [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,
Chris Masond3977122009-01-05 21:25:51 -050049 const char __user *buf)
Chris Mason39279cc2007-06-12 06:35:45 -040050{
51 long page_fault = 0;
52 int i;
53 int offset = pos & (PAGE_CACHE_SIZE - 1);
54
55 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
56 size_t count = min_t(size_t,
57 PAGE_CACHE_SIZE - offset, write_bytes);
58 struct page *page = prepared_pages[i];
59 fault_in_pages_readable(buf, count);
60
61 /* Copy data from userspace to the current page */
62 kmap(page);
63 page_fault = __copy_from_user(page_address(page) + offset,
64 buf, count);
65 /* Flush processor's dcache for this page */
66 flush_dcache_page(page);
67 kunmap(page);
68 buf += count;
69 write_bytes -= count;
70
71 if (page_fault)
72 break;
73 }
74 return page_fault ? -EFAULT : 0;
75}
76
Chris Masond352ac62008-09-29 15:18:18 -040077/*
78 * unlocks pages after btrfs_file_write is done with them
79 */
Chris Masond3977122009-01-05 21:25:51 -050080static noinline void btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -040081{
82 size_t i;
83 for (i = 0; i < num_pages; i++) {
84 if (!pages[i])
85 break;
Chris Masond352ac62008-09-29 15:18:18 -040086 /* page checked is some magic around finding pages that
87 * have been modified without going through btrfs_set_page_dirty
88 * clear it here
89 */
Chris Mason4a096752008-07-21 10:29:44 -040090 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -040091 unlock_page(pages[i]);
92 mark_page_accessed(pages[i]);
93 page_cache_release(pages[i]);
94 }
95}
96
Chris Masond352ac62008-09-29 15:18:18 -040097/*
98 * after copy_from_user, pages need to be dirtied and we need to make
99 * sure holes are created between the current EOF and the start of
100 * any next extents (if required).
101 *
102 * this also makes the decision about creating an inline extent vs
103 * doing real data extents, marking pages dirty and delalloc as required.
104 */
Chris Masond3977122009-01-05 21:25:51 -0500105static noinline int dirty_and_release_pages(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400106 struct btrfs_root *root,
107 struct file *file,
108 struct page **pages,
109 size_t num_pages,
110 loff_t pos,
111 size_t write_bytes)
112{
Chris Mason39279cc2007-06-12 06:35:45 -0400113 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400114 int i;
Chris Mason6da6aba2007-12-18 16:15:09 -0500115 struct inode *inode = fdentry(file)->d_inode;
Chris Masondb945352007-10-15 16:15:53 -0400116 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400117 u64 start_pos;
118 u64 end_of_last_block;
119 u64 end_pos = pos + write_bytes;
120 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400121
Chris Mason5f39d392007-10-15 16:14:19 -0400122 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400123 num_bytes = (write_bytes + pos - start_pos +
124 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400125
Chris Masondb945352007-10-15 16:15:53 -0400126 end_of_last_block = start_pos + num_bytes - 1;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000127 err = btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block,
128 NULL);
Yan, Zhenga22285a2010-05-16 10:48:46 -0400129 BUG_ON(err);
Josef Bacik9ed74f22009-09-11 16:12:44 -0400130
Chris Masonc8b97812008-10-29 14:49:59 -0400131 for (i = 0; i < num_pages; i++) {
132 struct page *p = pages[i];
133 SetPageUptodate(p);
134 ClearPageChecked(p);
135 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400136 }
137 if (end_pos > isize) {
138 i_size_write(inode, end_pos);
Chris Masonf597bb12009-06-27 21:06:22 -0400139 /* we've only changed i_size in ram, and we haven't updated
140 * the disk i_size. There is no need to log the inode
141 * at this time.
142 */
Chris Mason39279cc2007-06-12 06:35:45 -0400143 }
Yan, Zhenga22285a2010-05-16 10:48:46 -0400144 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400145}
146
Chris Masond352ac62008-09-29 15:18:18 -0400147/*
148 * this drops all the extents in the cache that intersect the range
149 * [start, end]. Existing extents are split as required.
150 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400151int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
152 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400153{
154 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400155 struct extent_map *split = NULL;
156 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400157 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500158 u64 len = end - start + 1;
Chris Mason3b951512008-04-17 11:29:12 -0400159 int ret;
160 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400161 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400162 int compressed = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400163
Chris Masone6dcd2d2008-07-17 12:53:50 -0400164 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400165 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500166 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400167 testend = 0;
168 }
Chris Masond3977122009-01-05 21:25:51 -0500169 while (1) {
Chris Mason3b951512008-04-17 11:29:12 -0400170 if (!split)
171 split = alloc_extent_map(GFP_NOFS);
172 if (!split2)
173 split2 = alloc_extent_map(GFP_NOFS);
174
Chris Mason890871b2009-09-02 16:24:52 -0400175 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500176 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500177 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400178 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400179 break;
Chris Masond1310b22008-01-24 16:13:08 -0500180 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400181 flags = em->flags;
182 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Yan, Zheng55ef6892009-11-12 09:36:44 +0000183 if (testend && em->start + em->len >= start + len) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400184 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400185 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400186 break;
187 }
Yan, Zheng55ef6892009-11-12 09:36:44 +0000188 start = em->start + em->len;
189 if (testend)
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400190 len = start + len - (em->start + em->len);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400191 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400192 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400193 continue;
194 }
Chris Masonc8b97812008-10-29 14:49:59 -0400195 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400196 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400197 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400198
199 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
200 em->start < start) {
201 split->start = em->start;
202 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500203 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400204 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400205
206 if (compressed)
207 split->block_len = em->block_len;
208 else
209 split->block_len = split->len;
210
Chris Mason3b951512008-04-17 11:29:12 -0400211 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400212 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400213 ret = add_extent_mapping(em_tree, split);
214 BUG_ON(ret);
215 free_extent_map(split);
216 split = split2;
217 split2 = NULL;
218 }
219 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
220 testend && em->start + em->len > start + len) {
221 u64 diff = start + len - em->start;
222
223 split->start = start + len;
224 split->len = em->start + em->len - (start + len);
225 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400226 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400227
Chris Masonc8b97812008-10-29 14:49:59 -0400228 if (compressed) {
229 split->block_len = em->block_len;
230 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500231 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400232 } else {
233 split->block_len = split->len;
234 split->block_start = em->block_start + diff;
Chris Mason445a6942008-11-10 11:53:33 -0500235 split->orig_start = split->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400236 }
Chris Mason3b951512008-04-17 11:29:12 -0400237
238 ret = add_extent_mapping(em_tree, split);
239 BUG_ON(ret);
240 free_extent_map(split);
241 split = NULL;
242 }
Chris Mason890871b2009-09-02 16:24:52 -0400243 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500244
Chris Masona52d9a82007-08-27 16:49:44 -0400245 /* once for us */
246 free_extent_map(em);
247 /* once for the tree*/
248 free_extent_map(em);
249 }
Chris Mason3b951512008-04-17 11:29:12 -0400250 if (split)
251 free_extent_map(split);
252 if (split2)
253 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400254 return 0;
255}
256
Chris Mason39279cc2007-06-12 06:35:45 -0400257/*
258 * this is very complex, but the basic idea is to drop all extents
259 * in the range start - end. hint_block is filled in with a block number
260 * that would be a good hint to the block allocator for this file.
261 *
262 * If an extent intersects the range but is not entirely inside the range
263 * it is either truncated or split. Anything entirely inside the range
264 * is deleted from the tree.
265 */
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000266int btrfs_drop_extents(struct btrfs_trans_handle *trans, struct inode *inode,
267 u64 start, u64 end, u64 *hint_byte, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400268{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000269 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason00f5c792007-11-30 10:09:33 -0500270 struct extent_buffer *leaf;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000271 struct btrfs_file_extent_item *fi;
Chris Mason00f5c792007-11-30 10:09:33 -0500272 struct btrfs_path *path;
273 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000274 struct btrfs_key new_key;
275 u64 search_start = start;
276 u64 disk_bytenr = 0;
277 u64 num_bytes = 0;
278 u64 extent_offset = 0;
279 u64 extent_end = 0;
280 int del_nr = 0;
281 int del_slot = 0;
282 int extent_type;
Chris Masonccd467d2007-06-28 15:57:36 -0400283 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500284 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400285
Chris Masona1ed8352009-09-11 12:27:37 -0400286 if (drop_cache)
287 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400288
Chris Mason39279cc2007-06-12 06:35:45 -0400289 path = btrfs_alloc_path();
290 if (!path)
291 return -ENOMEM;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000292
Chris Masond3977122009-01-05 21:25:51 -0500293 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400294 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400295 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
296 search_start, -1);
297 if (ret < 0)
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000298 break;
299 if (ret > 0 && path->slots[0] > 0 && search_start == start) {
300 leaf = path->nodes[0];
301 btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1);
302 if (key.objectid == inode->i_ino &&
303 key.type == BTRFS_EXTENT_DATA_KEY)
304 path->slots[0]--;
Chris Mason39279cc2007-06-12 06:35:45 -0400305 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400306 ret = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000307next_slot:
308 leaf = path->nodes[0];
309 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
310 BUG_ON(del_nr > 0);
311 ret = btrfs_next_leaf(root, path);
312 if (ret < 0)
313 break;
314 if (ret > 0) {
315 ret = 0;
316 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400317 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000318 leaf = path->nodes[0];
319 recow = 1;
320 }
321
322 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
323 if (key.objectid > inode->i_ino ||
324 key.type > BTRFS_EXTENT_DATA_KEY || key.offset >= end)
325 break;
326
327 fi = btrfs_item_ptr(leaf, path->slots[0],
328 struct btrfs_file_extent_item);
329 extent_type = btrfs_file_extent_type(leaf, fi);
330
331 if (extent_type == BTRFS_FILE_EXTENT_REG ||
332 extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
333 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
334 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
335 extent_offset = btrfs_file_extent_offset(leaf, fi);
336 extent_end = key.offset +
337 btrfs_file_extent_num_bytes(leaf, fi);
338 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
339 extent_end = key.offset +
340 btrfs_file_extent_inline_len(leaf, fi);
Chris Mason8c2383c2007-06-18 09:57:58 -0400341 } else {
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000342 WARN_ON(1);
Chris Mason8c2383c2007-06-18 09:57:58 -0400343 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400344 }
345
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000346 if (extent_end <= search_start) {
347 path->slots[0]++;
Chris Mason8c2383c2007-06-18 09:57:58 -0400348 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400349 }
350
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000351 search_start = max(key.offset, start);
352 if (recow) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400353 btrfs_release_path(root, path);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000354 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400355 }
Chris Mason771ed682008-11-06 22:02:51 -0500356
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000357 /*
358 * | - range to drop - |
359 * | -------- extent -------- |
360 */
361 if (start > key.offset && end < extent_end) {
362 BUG_ON(del_nr > 0);
363 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
364
365 memcpy(&new_key, &key, sizeof(new_key));
366 new_key.offset = start;
367 ret = btrfs_duplicate_item(trans, root, path,
368 &new_key);
369 if (ret == -EAGAIN) {
370 btrfs_release_path(root, path);
371 continue;
372 }
373 if (ret < 0)
374 break;
Chris Mason8c2383c2007-06-18 09:57:58 -0400375
Chris Mason5f39d392007-10-15 16:14:19 -0400376 leaf = path->nodes[0];
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000377 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
378 struct btrfs_file_extent_item);
379 btrfs_set_file_extent_num_bytes(leaf, fi,
380 start - key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400381
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000382 fi = btrfs_item_ptr(leaf, path->slots[0],
383 struct btrfs_file_extent_item);
Chris Masonc8b97812008-10-29 14:49:59 -0400384
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000385 extent_offset += start - key.offset;
386 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
387 btrfs_set_file_extent_num_bytes(leaf, fi,
388 extent_end - start);
389 btrfs_mark_buffer_dirty(leaf);
Chris Masondb945352007-10-15 16:15:53 -0400390
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000391 if (disk_bytenr > 0) {
392 ret = btrfs_inc_extent_ref(trans, root,
393 disk_bytenr, num_bytes, 0,
394 root->root_key.objectid,
395 new_key.objectid,
396 start - extent_offset);
Zheng Yan31840ae2008-09-23 13:14:14 -0400397 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000398 *hint_byte = disk_bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400399 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000400 key.offset = start;
401 }
402 /*
403 * | ---- range to drop ----- |
404 * | -------- extent -------- |
405 */
406 if (start <= key.offset && end < extent_end) {
407 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
408
409 memcpy(&new_key, &key, sizeof(new_key));
410 new_key.offset = end;
411 btrfs_set_item_key_safe(trans, root, path, &new_key);
412
413 extent_offset += end - key.offset;
414 btrfs_set_file_extent_offset(leaf, fi, extent_offset);
415 btrfs_set_file_extent_num_bytes(leaf, fi,
416 extent_end - end);
417 btrfs_mark_buffer_dirty(leaf);
418 if (disk_bytenr > 0) {
419 inode_sub_bytes(inode, end - key.offset);
420 *hint_byte = disk_bytenr;
421 }
422 break;
Zheng Yan31840ae2008-09-23 13:14:14 -0400423 }
424
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000425 search_start = extent_end;
426 /*
427 * | ---- range to drop ----- |
428 * | -------- extent -------- |
429 */
430 if (start > key.offset && end >= extent_end) {
431 BUG_ON(del_nr > 0);
432 BUG_ON(extent_type == BTRFS_FILE_EXTENT_INLINE);
433
434 btrfs_set_file_extent_num_bytes(leaf, fi,
435 start - key.offset);
436 btrfs_mark_buffer_dirty(leaf);
437 if (disk_bytenr > 0) {
438 inode_sub_bytes(inode, extent_end - start);
439 *hint_byte = disk_bytenr;
440 }
441 if (end == extent_end)
442 break;
443
444 path->slots[0]++;
445 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400446 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000447
448 /*
449 * | ---- range to drop ----- |
450 * | ------ extent ------ |
451 */
452 if (start <= key.offset && end >= extent_end) {
453 if (del_nr == 0) {
454 del_slot = path->slots[0];
455 del_nr = 1;
456 } else {
457 BUG_ON(del_slot + del_nr != path->slots[0]);
458 del_nr++;
459 }
460
461 if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
462 inode_sub_bytes(inode,
463 extent_end - key.offset);
464 extent_end = ALIGN(extent_end,
465 root->sectorsize);
466 } else if (disk_bytenr > 0) {
467 ret = btrfs_free_extent(trans, root,
468 disk_bytenr, num_bytes, 0,
469 root->root_key.objectid,
470 key.objectid, key.offset -
471 extent_offset);
472 BUG_ON(ret);
473 inode_sub_bytes(inode,
474 extent_end - key.offset);
475 *hint_byte = disk_bytenr;
476 }
477
478 if (end == extent_end)
479 break;
480
481 if (path->slots[0] + 1 < btrfs_header_nritems(leaf)) {
482 path->slots[0]++;
483 goto next_slot;
484 }
485
486 ret = btrfs_del_items(trans, root, path, del_slot,
487 del_nr);
488 BUG_ON(ret);
489
490 del_nr = 0;
491 del_slot = 0;
492
493 btrfs_release_path(root, path);
494 continue;
495 }
496
497 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400498 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000499
500 if (del_nr > 0) {
501 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
502 BUG_ON(ret);
503 }
504
Chris Mason39279cc2007-06-12 06:35:45 -0400505 btrfs_free_path(path);
506 return ret;
507}
508
Yan Zhengd899e052008-10-30 14:25:28 -0400509static int extent_mergeable(struct extent_buffer *leaf, int slot,
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000510 u64 objectid, u64 bytenr, u64 orig_offset,
511 u64 *start, u64 *end)
Yan Zhengd899e052008-10-30 14:25:28 -0400512{
513 struct btrfs_file_extent_item *fi;
514 struct btrfs_key key;
515 u64 extent_end;
516
517 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
518 return 0;
519
520 btrfs_item_key_to_cpu(leaf, &key, slot);
521 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
522 return 0;
523
524 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
525 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
526 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000527 btrfs_file_extent_offset(leaf, fi) != key.offset - orig_offset ||
Yan Zhengd899e052008-10-30 14:25:28 -0400528 btrfs_file_extent_compression(leaf, fi) ||
529 btrfs_file_extent_encryption(leaf, fi) ||
530 btrfs_file_extent_other_encoding(leaf, fi))
531 return 0;
532
533 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
534 if ((*start && *start != key.offset) || (*end && *end != extent_end))
535 return 0;
536
537 *start = key.offset;
538 *end = extent_end;
539 return 1;
540}
541
542/*
543 * Mark extent in the range start - end as written.
544 *
545 * This changes extent type from 'pre-allocated' to 'regular'. If only
546 * part of extent is marked as written, the extent will be split into
547 * two or three.
548 */
549int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
Yan Zhengd899e052008-10-30 14:25:28 -0400550 struct inode *inode, u64 start, u64 end)
551{
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000552 struct btrfs_root *root = BTRFS_I(inode)->root;
Yan Zhengd899e052008-10-30 14:25:28 -0400553 struct extent_buffer *leaf;
554 struct btrfs_path *path;
555 struct btrfs_file_extent_item *fi;
556 struct btrfs_key key;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000557 struct btrfs_key new_key;
Yan Zhengd899e052008-10-30 14:25:28 -0400558 u64 bytenr;
559 u64 num_bytes;
560 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400561 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400562 u64 other_start;
563 u64 other_end;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000564 u64 split;
565 int del_nr = 0;
566 int del_slot = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000567 int recow;
Yan Zhengd899e052008-10-30 14:25:28 -0400568 int ret;
569
570 btrfs_drop_extent_cache(inode, start, end - 1, 0);
571
572 path = btrfs_alloc_path();
573 BUG_ON(!path);
574again:
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000575 recow = 0;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000576 split = start;
Yan Zhengd899e052008-10-30 14:25:28 -0400577 key.objectid = inode->i_ino;
578 key.type = BTRFS_EXTENT_DATA_KEY;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000579 key.offset = split;
Yan Zhengd899e052008-10-30 14:25:28 -0400580
581 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
582 if (ret > 0 && path->slots[0] > 0)
583 path->slots[0]--;
584
585 leaf = path->nodes[0];
586 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
587 BUG_ON(key.objectid != inode->i_ino ||
588 key.type != BTRFS_EXTENT_DATA_KEY);
589 fi = btrfs_item_ptr(leaf, path->slots[0],
590 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000591 BUG_ON(btrfs_file_extent_type(leaf, fi) !=
592 BTRFS_FILE_EXTENT_PREALLOC);
Yan Zhengd899e052008-10-30 14:25:28 -0400593 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
594 BUG_ON(key.offset > start || extent_end < end);
595
596 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
597 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400598 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000599 memcpy(&new_key, &key, sizeof(new_key));
600
601 if (start == key.offset && end < extent_end) {
602 other_start = 0;
603 other_end = start;
604 if (extent_mergeable(leaf, path->slots[0] - 1,
605 inode->i_ino, bytenr, orig_offset,
606 &other_start, &other_end)) {
607 new_key.offset = end;
608 btrfs_set_item_key_safe(trans, root, path, &new_key);
609 fi = btrfs_item_ptr(leaf, path->slots[0],
610 struct btrfs_file_extent_item);
611 btrfs_set_file_extent_num_bytes(leaf, fi,
612 extent_end - end);
613 btrfs_set_file_extent_offset(leaf, fi,
614 end - orig_offset);
615 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
616 struct btrfs_file_extent_item);
617 btrfs_set_file_extent_num_bytes(leaf, fi,
618 end - other_start);
619 btrfs_mark_buffer_dirty(leaf);
620 goto out;
621 }
622 }
623
624 if (start > key.offset && end == extent_end) {
625 other_start = end;
626 other_end = 0;
627 if (extent_mergeable(leaf, path->slots[0] + 1,
628 inode->i_ino, bytenr, orig_offset,
629 &other_start, &other_end)) {
630 fi = btrfs_item_ptr(leaf, path->slots[0],
631 struct btrfs_file_extent_item);
632 btrfs_set_file_extent_num_bytes(leaf, fi,
633 start - key.offset);
634 path->slots[0]++;
635 new_key.offset = start;
636 btrfs_set_item_key_safe(trans, root, path, &new_key);
637
638 fi = btrfs_item_ptr(leaf, path->slots[0],
639 struct btrfs_file_extent_item);
640 btrfs_set_file_extent_num_bytes(leaf, fi,
641 other_end - start);
642 btrfs_set_file_extent_offset(leaf, fi,
643 start - orig_offset);
644 btrfs_mark_buffer_dirty(leaf);
645 goto out;
646 }
647 }
Yan Zhengd899e052008-10-30 14:25:28 -0400648
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000649 while (start > key.offset || end < extent_end) {
650 if (key.offset == start)
651 split = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400652
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000653 new_key.offset = split;
654 ret = btrfs_duplicate_item(trans, root, path, &new_key);
655 if (ret == -EAGAIN) {
656 btrfs_release_path(root, path);
657 goto again;
Yan Zhengd899e052008-10-30 14:25:28 -0400658 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000659 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -0400660
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000661 leaf = path->nodes[0];
662 fi = btrfs_item_ptr(leaf, path->slots[0] - 1,
Yan Zhengd899e052008-10-30 14:25:28 -0400663 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -0400664 btrfs_set_file_extent_num_bytes(leaf, fi,
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000665 split - key.offset);
666
667 fi = btrfs_item_ptr(leaf, path->slots[0],
668 struct btrfs_file_extent_item);
669
670 btrfs_set_file_extent_offset(leaf, fi, split - orig_offset);
671 btrfs_set_file_extent_num_bytes(leaf, fi,
672 extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -0400673 btrfs_mark_buffer_dirty(leaf);
674
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000675 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
676 root->root_key.objectid,
677 inode->i_ino, orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400678 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -0400679
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000680 if (split == start) {
681 key.offset = start;
682 } else {
683 BUG_ON(start != key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400684 path->slots[0]--;
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000685 extent_end = end;
Yan Zhengd899e052008-10-30 14:25:28 -0400686 }
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000687 recow = 1;
Yan Zhengd899e052008-10-30 14:25:28 -0400688 }
689
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000690 other_start = end;
691 other_end = 0;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000692 if (extent_mergeable(leaf, path->slots[0] + 1,
693 inode->i_ino, bytenr, orig_offset,
694 &other_start, &other_end)) {
695 if (recow) {
696 btrfs_release_path(root, path);
697 goto again;
698 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000699 extent_end = other_end;
700 del_slot = path->slots[0] + 1;
701 del_nr++;
702 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
703 0, root->root_key.objectid,
704 inode->i_ino, orig_offset);
705 BUG_ON(ret);
706 }
707 other_start = 0;
708 other_end = start;
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000709 if (extent_mergeable(leaf, path->slots[0] - 1,
710 inode->i_ino, bytenr, orig_offset,
711 &other_start, &other_end)) {
712 if (recow) {
713 btrfs_release_path(root, path);
714 goto again;
715 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000716 key.offset = other_start;
717 del_slot = path->slots[0];
718 del_nr++;
719 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
720 0, root->root_key.objectid,
721 inode->i_ino, orig_offset);
722 BUG_ON(ret);
723 }
724 if (del_nr == 0) {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000725 fi = btrfs_item_ptr(leaf, path->slots[0],
726 struct btrfs_file_extent_item);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000727 btrfs_set_file_extent_type(leaf, fi,
728 BTRFS_FILE_EXTENT_REG);
729 btrfs_mark_buffer_dirty(leaf);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000730 } else {
Shaohua Li3f6fae92010-02-11 07:43:00 +0000731 fi = btrfs_item_ptr(leaf, del_slot - 1,
732 struct btrfs_file_extent_item);
Yan, Zheng6c7d54a2010-01-15 08:43:09 +0000733 btrfs_set_file_extent_type(leaf, fi,
734 BTRFS_FILE_EXTENT_REG);
735 btrfs_set_file_extent_num_bytes(leaf, fi,
736 extent_end - key.offset);
737 btrfs_mark_buffer_dirty(leaf);
738
739 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
740 BUG_ON(ret);
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000741 }
Yan, Zheng920bbbf2009-11-12 09:34:08 +0000742out:
Yan Zhengd899e052008-10-30 14:25:28 -0400743 btrfs_free_path(path);
744 return 0;
745}
746
Chris Mason39279cc2007-06-12 06:35:45 -0400747/*
Chris Masond352ac62008-09-29 15:18:18 -0400748 * this gets pages into the page cache and locks them down, it also properly
749 * waits for data=ordered extents to finish before allowing the pages to be
750 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -0400751 */
Chris Masond3977122009-01-05 21:25:51 -0500752static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500753 struct page **pages, size_t num_pages,
754 loff_t pos, unsigned long first_index,
755 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400756{
Josef Bacik2ac55d42010-02-03 19:33:23 +0000757 struct extent_state *cached_state = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -0400758 int i;
759 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500760 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400761 int err = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400762 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400763 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400764
Chris Mason5f39d392007-10-15 16:14:19 -0400765 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400766 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400767
Yan Zheng9036c102008-10-30 14:19:41 -0400768 if (start_pos > inode->i_size) {
769 err = btrfs_cont_expand(inode, start_pos);
770 if (err)
771 return err;
772 }
773
Chris Mason39279cc2007-06-12 06:35:45 -0400774 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400775again:
Chris Mason39279cc2007-06-12 06:35:45 -0400776 for (i = 0; i < num_pages; i++) {
777 pages[i] = grab_cache_page(inode->i_mapping, index + i);
778 if (!pages[i]) {
779 err = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -0400780 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400781 }
Chris Masonccd467d2007-06-28 15:57:36 -0400782 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400783 }
Chris Mason07627042008-02-19 11:29:24 -0500784 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400785 struct btrfs_ordered_extent *ordered;
Josef Bacik2ac55d42010-02-03 19:33:23 +0000786 lock_extent_bits(&BTRFS_I(inode)->io_tree,
787 start_pos, last_pos - 1, 0, &cached_state,
788 GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500789 ordered = btrfs_lookup_first_ordered_extent(inode,
790 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400791 if (ordered &&
792 ordered->file_offset + ordered->len > start_pos &&
793 ordered->file_offset < last_pos) {
794 btrfs_put_ordered_extent(ordered);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000795 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
796 start_pos, last_pos - 1,
797 &cached_state, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400798 for (i = 0; i < num_pages; i++) {
799 unlock_page(pages[i]);
800 page_cache_release(pages[i]);
801 }
802 btrfs_wait_ordered_range(inode, start_pos,
803 last_pos - start_pos);
804 goto again;
805 }
806 if (ordered)
807 btrfs_put_ordered_extent(ordered);
808
Josef Bacik2ac55d42010-02-03 19:33:23 +0000809 clear_extent_bit(&BTRFS_I(inode)->io_tree, start_pos,
Josef Bacik32c00af2009-10-08 13:34:05 -0400810 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC |
Josef Bacik2ac55d42010-02-03 19:33:23 +0000811 EXTENT_DO_ACCOUNTING, 0, 0, &cached_state,
Chris Mason07627042008-02-19 11:29:24 -0500812 GFP_NOFS);
Josef Bacik2ac55d42010-02-03 19:33:23 +0000813 unlock_extent_cached(&BTRFS_I(inode)->io_tree,
814 start_pos, last_pos - 1, &cached_state,
815 GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500816 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400817 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400818 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400819 set_page_extent_mapped(pages[i]);
820 WARN_ON(!PageLocked(pages[i]));
821 }
Chris Mason39279cc2007-06-12 06:35:45 -0400822 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400823}
824
825static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
826 size_t count, loff_t *ppos)
827{
828 loff_t pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400829 loff_t start_pos;
830 ssize_t num_written = 0;
831 ssize_t err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400832 int ret = 0;
Chris Mason6da6aba2007-12-18 16:15:09 -0500833 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400834 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8c2383c2007-06-18 09:57:58 -0400835 struct page **pages = NULL;
836 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400837 struct page *pinned[2];
838 unsigned long first_index;
839 unsigned long last_index;
Chris Masoncb843a62008-10-03 12:30:02 -0400840 int will_write;
841
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100842 will_write = ((file->f_flags & O_DSYNC) || IS_SYNC(inode) ||
Chris Masoncb843a62008-10-03 12:30:02 -0400843 (file->f_flags & O_DIRECT));
Chris Mason8c2383c2007-06-18 09:57:58 -0400844
845 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
846 PAGE_CACHE_SIZE / (sizeof(struct page *)));
Chris Mason39279cc2007-06-12 06:35:45 -0400847 pinned[0] = NULL;
848 pinned[1] = NULL;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400849
Chris Mason39279cc2007-06-12 06:35:45 -0400850 pos = *ppos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400851 start_pos = pos;
852
Chris Mason39279cc2007-06-12 06:35:45 -0400853 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
Chris Mason2b1f55b2008-09-24 11:48:04 -0400854
Chris Masonab93dbe2009-10-01 12:29:10 -0400855 mutex_lock(&inode->i_mutex);
856
Chris Mason1832a6d2007-12-21 16:27:21 -0500857 current->backing_dev_info = inode->i_mapping->backing_dev_info;
Chris Mason39279cc2007-06-12 06:35:45 -0400858 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
Chris Mason1832a6d2007-12-21 16:27:21 -0500859 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400860 goto out;
861
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -0400862 if (count == 0)
Chris Masonab93dbe2009-10-01 12:29:10 -0400863 goto out;
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400864
865 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400866 if (err)
Chris Masonab93dbe2009-10-01 12:29:10 -0400867 goto out;
868
Chris Mason39279cc2007-06-12 06:35:45 -0400869 file_update_time(file);
870
Chris Mason8c2383c2007-06-18 09:57:58 -0400871 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -0400872
Chris Masonab93dbe2009-10-01 12:29:10 -0400873 /* generic_write_checks can change our pos */
874 start_pos = pos;
875
Chris Masonc3027eb2008-12-08 16:40:21 -0500876 BTRFS_I(inode)->sequence++;
Chris Mason39279cc2007-06-12 06:35:45 -0400877 first_index = pos >> PAGE_CACHE_SHIFT;
878 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
879
880 /*
881 * there are lots of better ways to do this, but this code
882 * makes sure the first and last page in the file range are
883 * up to date and ready for cow
884 */
885 if ((pos & (PAGE_CACHE_SIZE - 1))) {
886 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
887 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400888 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400889 BUG_ON(ret);
890 wait_on_page_locked(pinned[0]);
891 } else {
892 unlock_page(pinned[0]);
893 }
894 }
895 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
896 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
897 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400898 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400899 BUG_ON(ret);
900 wait_on_page_locked(pinned[1]);
901 } else {
902 unlock_page(pinned[1]);
903 }
904 }
905
Chris Masond3977122009-01-05 21:25:51 -0500906 while (count > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400907 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Chris Mason11bd1432007-06-22 14:16:24 -0400908 size_t write_bytes = min(count, nrptrs *
909 (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400910 offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400911 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
912 PAGE_CACHE_SHIFT;
913
Chris Mason8c2383c2007-06-18 09:57:58 -0400914 WARN_ON(num_pages > nrptrs);
yanhai zhu9aead432009-01-05 15:49:11 -0500915 memset(pages, 0, sizeof(struct page *) * nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -0500916
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400917 ret = btrfs_delalloc_reserve_space(inode, write_bytes);
Chris Mason1832a6d2007-12-21 16:27:21 -0500918 if (ret)
919 goto out;
920
Chris Mason39279cc2007-06-12 06:35:45 -0400921 ret = prepare_pages(root, file, pages, num_pages,
922 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400923 write_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500924 if (ret) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400925 btrfs_delalloc_release_space(inode, write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400926 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -0500927 }
Chris Mason39279cc2007-06-12 06:35:45 -0400928
Chris Mason39279cc2007-06-12 06:35:45 -0400929 ret = btrfs_copy_from_user(pos, num_pages,
930 write_bytes, pages, buf);
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400931 if (ret == 0) {
932 dirty_and_release_pages(NULL, root, file, pages,
933 num_pages, pos, write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400934 }
Chris Mason39279cc2007-06-12 06:35:45 -0400935
Chris Mason39279cc2007-06-12 06:35:45 -0400936 btrfs_drop_pages(pages, num_pages);
Josef Bacik6a632092009-02-20 11:00:09 -0500937 if (ret) {
Yan, Zheng0ca1f7c2010-05-16 10:48:47 -0400938 btrfs_delalloc_release_space(inode, write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400939 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -0500940 }
Chris Mason39279cc2007-06-12 06:35:45 -0400941
Chris Masoncb843a62008-10-03 12:30:02 -0400942 if (will_write) {
Christoph Hellwig8aa38c32009-10-01 12:58:30 -0400943 filemap_fdatawrite_range(inode->i_mapping, pos,
944 pos + write_bytes - 1);
Chris Masoncb843a62008-10-03 12:30:02 -0400945 } else {
946 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
947 num_pages);
948 if (num_pages <
949 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
950 btrfs_btree_balance_dirty(root, 1);
951 btrfs_throttle(root);
952 }
953
Chris Mason39279cc2007-06-12 06:35:45 -0400954 buf += write_bytes;
955 count -= write_bytes;
956 pos += write_bytes;
957 num_written += write_bytes;
958
Chris Mason39279cc2007-06-12 06:35:45 -0400959 cond_resched();
960 }
Chris Mason39279cc2007-06-12 06:35:45 -0400961out:
Chris Mason1832a6d2007-12-21 16:27:21 -0500962 mutex_unlock(&inode->i_mutex);
Josef Bacik6a632092009-02-20 11:00:09 -0500963 if (ret)
964 err = ret;
Chris Mason5b92ee72008-01-03 13:46:11 -0500965
Chris Mason8c2383c2007-06-18 09:57:58 -0400966 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -0400967 if (pinned[0])
968 page_cache_release(pinned[0]);
969 if (pinned[1])
970 page_cache_release(pinned[1]);
971 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400972
Chris Mason5a3f23d2009-03-31 13:27:11 -0400973 /*
974 * we want to make sure fsync finds this change
975 * but we haven't joined a transaction running right now.
976 *
977 * Later on, someone is sure to update the inode and get the
978 * real transid recorded.
979 *
980 * We set last_trans now to the fs_info generation + 1,
981 * this will either be one more than the running transaction
982 * or the generation used for the next transaction if there isn't
983 * one running right now.
984 */
985 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
986
Chris Masoncb843a62008-10-03 12:30:02 -0400987 if (num_written > 0 && will_write) {
Chris Masone02119d2008-09-05 16:13:11 -0400988 struct btrfs_trans_handle *trans;
989
Chris Masoncb843a62008-10-03 12:30:02 -0400990 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
991 if (err)
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400992 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -0400993
Christoph Hellwig6b2f3d12009-10-27 11:05:28 +0100994 if ((file->f_flags & O_DSYNC) || IS_SYNC(inode)) {
Yan, Zhenga22285a2010-05-16 10:48:46 -0400995 trans = btrfs_start_transaction(root, 0);
Chris Masoncb843a62008-10-03 12:30:02 -0400996 ret = btrfs_log_dentry_safe(trans, root,
997 file->f_dentry);
998 if (ret == 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -0400999 ret = btrfs_sync_log(trans, root);
1000 if (ret == 0)
1001 btrfs_end_transaction(trans, root);
1002 else
1003 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001004 } else if (ret != BTRFS_NO_LOG_SYNC) {
Chris Masoncb843a62008-10-03 12:30:02 -04001005 btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001006 } else {
1007 btrfs_end_transaction(trans, root);
Chris Masoncb843a62008-10-03 12:30:02 -04001008 }
Chris Masone02119d2008-09-05 16:13:11 -04001009 }
Chris Masoncb843a62008-10-03 12:30:02 -04001010 if (file->f_flags & O_DIRECT) {
1011 invalidate_mapping_pages(inode->i_mapping,
1012 start_pos >> PAGE_CACHE_SHIFT,
1013 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1014 }
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001015 }
Chris Mason39279cc2007-06-12 06:35:45 -04001016 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001017 return num_written ? num_written : err;
1018}
1019
Chris Masond3977122009-01-05 21:25:51 -05001020int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001021{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001022 /*
1023 * ordered_data_close is set by settattr when we are about to truncate
1024 * a file from a non-zero size to a zero size. This tries to
1025 * flush down new bytes that may have been written if the
1026 * application were using truncate to replace a file in place.
1027 */
1028 if (BTRFS_I(inode)->ordered_data_close) {
1029 BTRFS_I(inode)->ordered_data_close = 0;
1030 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1031 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1032 filemap_flush(inode->i_mapping);
1033 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001034 if (filp->private_data)
1035 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001036 return 0;
1037}
1038
Chris Masond352ac62008-09-29 15:18:18 -04001039/*
1040 * fsync call for both files and directories. This logs the inode into
1041 * the tree log instead of forcing full commits whenever possible.
1042 *
1043 * It needs to call filemap_fdatawait so that all ordered extent updates are
1044 * in the metadata btree are up to date for copying to the log.
1045 *
1046 * It drops the inode mutex before doing the tree log commit. This is an
1047 * important optimization for directories because holding the mutex prevents
1048 * new operations on the dir while we write to disk.
1049 */
Chris Masone02119d2008-09-05 16:13:11 -04001050int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001051{
1052 struct inode *inode = dentry->d_inode;
1053 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001054 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001055 struct btrfs_trans_handle *trans;
1056
Chris Mason257c62e2009-10-13 13:21:08 -04001057
1058 /* we wait first, since the writeback may change the inode */
1059 root->log_batch++;
1060 /* the VFS called filemap_fdatawrite for us */
1061 btrfs_wait_ordered_range(inode, 0, (u64)-1);
1062 root->log_batch++;
1063
Chris Mason39279cc2007-06-12 06:35:45 -04001064 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001065 * check the transaction that last modified this inode
1066 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001067 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001068 if (!BTRFS_I(inode)->last_trans)
1069 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001070
Chris Mason257c62e2009-10-13 13:21:08 -04001071 /*
1072 * if the last transaction that changed this file was before
1073 * the current transaction, we can bail out now without any
1074 * syncing
1075 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001076 mutex_lock(&root->fs_info->trans_mutex);
1077 if (BTRFS_I(inode)->last_trans <=
1078 root->fs_info->last_trans_committed) {
1079 BTRFS_I(inode)->last_trans = 0;
1080 mutex_unlock(&root->fs_info->trans_mutex);
1081 goto out;
1082 }
1083 mutex_unlock(&root->fs_info->trans_mutex);
1084
1085 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001086 * ok we haven't committed the transaction yet, lets do a commit
1087 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001088 if (file && file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001089 btrfs_ioctl_trans_end(file);
1090
Yan, Zhenga22285a2010-05-16 10:48:46 -04001091 trans = btrfs_start_transaction(root, 0);
1092 if (IS_ERR(trans)) {
1093 ret = PTR_ERR(trans);
Chris Mason39279cc2007-06-12 06:35:45 -04001094 goto out;
1095 }
Chris Masone02119d2008-09-05 16:13:11 -04001096
Chris Mason2cfbd502009-02-20 10:55:10 -05001097 ret = btrfs_log_dentry_safe(trans, root, dentry);
Chris Masond3977122009-01-05 21:25:51 -05001098 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04001099 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001100
1101 /* we've logged all the items and now have a consistent
1102 * version of the file in the log. It is possible that
1103 * someone will come in and modify the file, but that's
1104 * fine because the log is consistent on disk, and we
1105 * have references to all of the file's extents
1106 *
1107 * It is possible that someone will come in and log the
1108 * file again, but that will end up using the synchronization
1109 * inside btrfs_sync_log to keep things safe.
1110 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001111 mutex_unlock(&dentry->d_inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001112
Chris Mason257c62e2009-10-13 13:21:08 -04001113 if (ret != BTRFS_NO_LOG_SYNC) {
1114 if (ret > 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001115 ret = btrfs_commit_transaction(trans, root);
Chris Mason257c62e2009-10-13 13:21:08 -04001116 } else {
1117 ret = btrfs_sync_log(trans, root);
1118 if (ret == 0)
1119 ret = btrfs_end_transaction(trans, root);
1120 else
1121 ret = btrfs_commit_transaction(trans, root);
1122 }
1123 } else {
1124 ret = btrfs_end_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001125 }
Chris Mason2cfbd502009-02-20 10:55:10 -05001126 mutex_lock(&dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001127out:
Roel Kluin014e4ac2010-01-29 10:42:11 +00001128 return ret > 0 ? -EIO : ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001129}
1130
Alexey Dobriyanf0f37e2f2009-09-27 22:29:37 +04001131static const struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001132 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001133 .page_mkwrite = btrfs_page_mkwrite,
1134};
1135
1136static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1137{
1138 vma->vm_ops = &btrfs_file_vm_ops;
1139 file_accessed(filp);
1140 return 0;
1141}
1142
Alexey Dobriyan828c0952009-10-01 15:43:56 -07001143const struct file_operations btrfs_file_operations = {
Chris Mason39279cc2007-06-12 06:35:45 -04001144 .llseek = generic_file_llseek,
1145 .read = do_sync_read,
Chris Mason9ebefb182007-06-15 13:50:00 -04001146 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001147 .splice_read = generic_file_splice_read,
Chris Mason39279cc2007-06-12 06:35:45 -04001148 .write = btrfs_file_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001149 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001150 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001151 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001152 .fsync = btrfs_sync_file,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001153 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001154#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001155 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001156#endif
1157};