blob: 4123db9d51410b9de5e68724af45b0069d3ef76a [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>
25#include <linux/smp_lock.h>
26#include <linux/backing-dev.h>
27#include <linux/mpage.h>
28#include <linux/swap.h>
29#include <linux/writeback.h>
30#include <linux/statfs.h>
31#include <linux/compat.h>
32#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;
Chris Masonc8b97812008-10-29 14:49:59 -0400127 btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block);
128 for (i = 0; i < num_pages; i++) {
129 struct page *p = pages[i];
130 SetPageUptodate(p);
131 ClearPageChecked(p);
132 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400133 }
134 if (end_pos > isize) {
135 i_size_write(inode, end_pos);
Chris Masonf597bb12009-06-27 21:06:22 -0400136 /* we've only changed i_size in ram, and we haven't updated
137 * the disk i_size. There is no need to log the inode
138 * at this time.
139 */
Chris Mason39279cc2007-06-12 06:35:45 -0400140 }
Chris Mason39279cc2007-06-12 06:35:45 -0400141 return err;
142}
143
Chris Masond352ac62008-09-29 15:18:18 -0400144/*
145 * this drops all the extents in the cache that intersect the range
146 * [start, end]. Existing extents are split as required.
147 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400148int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
149 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400150{
151 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400152 struct extent_map *split = NULL;
153 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400154 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500155 u64 len = end - start + 1;
Chris Mason3b951512008-04-17 11:29:12 -0400156 int ret;
157 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400158 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400159 int compressed = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400160
Chris Masone6dcd2d2008-07-17 12:53:50 -0400161 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400162 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500163 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400164 testend = 0;
165 }
Chris Masond3977122009-01-05 21:25:51 -0500166 while (1) {
Chris Mason3b951512008-04-17 11:29:12 -0400167 if (!split)
168 split = alloc_extent_map(GFP_NOFS);
169 if (!split2)
170 split2 = alloc_extent_map(GFP_NOFS);
171
Chris Mason890871b2009-09-02 16:24:52 -0400172 write_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500173 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500174 if (!em) {
Chris Mason890871b2009-09-02 16:24:52 -0400175 write_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400176 break;
Chris Masond1310b22008-01-24 16:13:08 -0500177 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400178 flags = em->flags;
179 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400180 if (em->start <= start &&
181 (!testend || em->start + em->len >= start + len)) {
182 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400183 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400184 break;
185 }
186 if (start < em->start) {
187 len = em->start - start;
188 } else {
189 len = start + len - (em->start + em->len);
190 start = em->start + em->len;
191 }
192 free_extent_map(em);
Chris Masona1ed8352009-09-11 12:27:37 -0400193 write_unlock(&em_tree->lock);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400194 continue;
195 }
Chris Masonc8b97812008-10-29 14:49:59 -0400196 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400197 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400198 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400199
200 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
201 em->start < start) {
202 split->start = em->start;
203 split->len = start - em->start;
Yan Zhengff5b7ee2008-11-10 07:34:43 -0500204 split->orig_start = em->orig_start;
Chris Mason3b951512008-04-17 11:29:12 -0400205 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400206
207 if (compressed)
208 split->block_len = em->block_len;
209 else
210 split->block_len = split->len;
211
Chris Mason3b951512008-04-17 11:29:12 -0400212 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400213 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400214 ret = add_extent_mapping(em_tree, split);
215 BUG_ON(ret);
216 free_extent_map(split);
217 split = split2;
218 split2 = NULL;
219 }
220 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
221 testend && em->start + em->len > start + len) {
222 u64 diff = start + len - em->start;
223
224 split->start = start + len;
225 split->len = em->start + em->len - (start + len);
226 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400227 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400228
Chris Masonc8b97812008-10-29 14:49:59 -0400229 if (compressed) {
230 split->block_len = em->block_len;
231 split->block_start = em->block_start;
Chris Mason445a6942008-11-10 11:53:33 -0500232 split->orig_start = em->orig_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400233 } else {
234 split->block_len = split->len;
235 split->block_start = em->block_start + diff;
Chris Mason445a6942008-11-10 11:53:33 -0500236 split->orig_start = split->start;
Chris Masonc8b97812008-10-29 14:49:59 -0400237 }
Chris Mason3b951512008-04-17 11:29:12 -0400238
239 ret = add_extent_mapping(em_tree, split);
240 BUG_ON(ret);
241 free_extent_map(split);
242 split = NULL;
243 }
Chris Mason890871b2009-09-02 16:24:52 -0400244 write_unlock(&em_tree->lock);
Chris Masond1310b22008-01-24 16:13:08 -0500245
Chris Masona52d9a82007-08-27 16:49:44 -0400246 /* once for us */
247 free_extent_map(em);
248 /* once for the tree*/
249 free_extent_map(em);
250 }
Chris Mason3b951512008-04-17 11:29:12 -0400251 if (split)
252 free_extent_map(split);
253 if (split2)
254 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400255 return 0;
256}
257
Chris Mason39279cc2007-06-12 06:35:45 -0400258/*
259 * this is very complex, but the basic idea is to drop all extents
260 * in the range start - end. hint_block is filled in with a block number
261 * that would be a good hint to the block allocator for this file.
262 *
263 * If an extent intersects the range but is not entirely inside the range
264 * it is either truncated or split. Anything entirely inside the range
265 * is deleted from the tree.
Chris Masond352ac62008-09-29 15:18:18 -0400266 *
267 * inline_limit is used to tell this code which offsets in the file to keep
268 * if they contain inline extents.
Chris Mason39279cc2007-06-12 06:35:45 -0400269 */
Chris Masond3977122009-01-05 21:25:51 -0500270noinline int btrfs_drop_extents(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400271 struct btrfs_root *root, struct inode *inode,
Chris Masone980b502009-04-24 14:39:24 -0400272 u64 start, u64 end, u64 locked_end,
Chris Masona1ed8352009-09-11 12:27:37 -0400273 u64 inline_limit, u64 *hint_byte, int drop_cache)
Chris Mason39279cc2007-06-12 06:35:45 -0400274{
Chris Mason39279cc2007-06-12 06:35:45 -0400275 u64 extent_end = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400276 u64 search_start = start;
Chris Masonc8b97812008-10-29 14:49:59 -0400277 u64 ram_bytes = 0;
Chris Mason771ed682008-11-06 22:02:51 -0500278 u64 disk_bytenr = 0;
Chris Masone980b502009-04-24 14:39:24 -0400279 u64 orig_locked_end = locked_end;
Chris Mason70b99e62008-10-31 12:46:39 -0400280 u8 compression;
281 u8 encryption;
Chris Masonc8b97812008-10-29 14:49:59 -0400282 u16 other_encoding = 0;
Chris Mason00f5c792007-11-30 10:09:33 -0500283 struct extent_buffer *leaf;
284 struct btrfs_file_extent_item *extent;
285 struct btrfs_path *path;
286 struct btrfs_key key;
287 struct btrfs_file_extent_item old;
288 int keep;
289 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400290 int bookend;
Yan Zhengd899e052008-10-30 14:25:28 -0400291 int found_type = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400292 int found_extent;
293 int found_inline;
Chris Masonccd467d2007-06-28 15:57:36 -0400294 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500295 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400296
Chris Masonc8b97812008-10-29 14:49:59 -0400297 inline_limit = 0;
Chris Masona1ed8352009-09-11 12:27:37 -0400298 if (drop_cache)
299 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400300
Chris Mason39279cc2007-06-12 06:35:45 -0400301 path = btrfs_alloc_path();
302 if (!path)
303 return -ENOMEM;
Chris Masond3977122009-01-05 21:25:51 -0500304 while (1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400305 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400306 btrfs_release_path(root, path);
307 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
308 search_start, -1);
309 if (ret < 0)
310 goto out;
311 if (ret > 0) {
312 if (path->slots[0] == 0) {
313 ret = 0;
314 goto out;
315 }
316 path->slots[0]--;
317 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400318next_slot:
Chris Mason39279cc2007-06-12 06:35:45 -0400319 keep = 0;
320 bookend = 0;
321 found_extent = 0;
322 found_inline = 0;
Chris Mason70b99e62008-10-31 12:46:39 -0400323 compression = 0;
324 encryption = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400325 extent = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400326 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -0400327 slot = path->slots[0];
Chris Mason8c2383c2007-06-18 09:57:58 -0400328 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400329 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan72610092008-02-05 15:40:36 -0500330 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
331 key.offset >= end) {
Chris Mason39279cc2007-06-12 06:35:45 -0400332 goto out;
333 }
Yan72610092008-02-05 15:40:36 -0500334 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
335 key.objectid != inode->i_ino) {
Chris Mason39279cc2007-06-12 06:35:45 -0400336 goto out;
337 }
Chris Masonccd467d2007-06-28 15:57:36 -0400338 if (recow) {
Yan Zheng8247b412008-11-11 09:33:29 -0500339 search_start = max(key.offset, start);
Chris Masonccd467d2007-06-28 15:57:36 -0400340 continue;
341 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400342 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
343 extent = btrfs_item_ptr(leaf, slot,
344 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400345 found_type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8b97812008-10-29 14:49:59 -0400346 compression = btrfs_file_extent_compression(leaf,
347 extent);
348 encryption = btrfs_file_extent_encryption(leaf,
349 extent);
350 other_encoding = btrfs_file_extent_other_encoding(leaf,
351 extent);
Yan Zhengd899e052008-10-30 14:25:28 -0400352 if (found_type == BTRFS_FILE_EXTENT_REG ||
353 found_type == BTRFS_FILE_EXTENT_PREALLOC) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500354 extent_end =
355 btrfs_file_extent_disk_bytenr(leaf,
356 extent);
357 if (extent_end)
358 *hint_byte = extent_end;
359
Chris Mason8c2383c2007-06-18 09:57:58 -0400360 extent_end = key.offset +
Chris Masondb945352007-10-15 16:15:53 -0400361 btrfs_file_extent_num_bytes(leaf, extent);
Chris Masonc8b97812008-10-29 14:49:59 -0400362 ram_bytes = btrfs_file_extent_ram_bytes(leaf,
363 extent);
Chris Mason8c2383c2007-06-18 09:57:58 -0400364 found_extent = 1;
365 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
366 found_inline = 1;
367 extent_end = key.offset +
Chris Masonc8b97812008-10-29 14:49:59 -0400368 btrfs_file_extent_inline_len(leaf, extent);
Chris Mason8c2383c2007-06-18 09:57:58 -0400369 }
370 } else {
371 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400372 }
373
374 /* we found nothing we can drop */
Chris Mason8c2383c2007-06-18 09:57:58 -0400375 if ((!found_extent && !found_inline) ||
376 search_start >= extent_end) {
377 int nextret;
378 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400379 nritems = btrfs_header_nritems(leaf);
Chris Mason8c2383c2007-06-18 09:57:58 -0400380 if (slot >= nritems - 1) {
381 nextret = btrfs_next_leaf(root, path);
382 if (nextret)
383 goto out;
Chris Masonccd467d2007-06-28 15:57:36 -0400384 recow = 1;
Chris Mason8c2383c2007-06-18 09:57:58 -0400385 } else {
386 path->slots[0]++;
387 }
388 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400389 }
390
Chris Masonc8b97812008-10-29 14:49:59 -0400391 if (end <= extent_end && start >= key.offset && found_inline)
Chris Mason179e29e2007-11-01 11:28:41 -0400392 *hint_byte = EXTENT_MAP_INLINE;
Zheng Yan31840ae2008-09-23 13:14:14 -0400393
394 if (found_extent) {
395 read_extent_buffer(leaf, &old, (unsigned long)extent,
396 sizeof(old));
Zheng Yan31840ae2008-09-23 13:14:14 -0400397 }
398
Chris Mason39279cc2007-06-12 06:35:45 -0400399 if (end < extent_end && end >= key.offset) {
Chris Mason179e29e2007-11-01 11:28:41 -0400400 bookend = 1;
Yan0181e582008-01-30 14:39:54 -0500401 if (found_inline && start <= key.offset)
Chris Mason179e29e2007-11-01 11:28:41 -0400402 keep = 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400403 }
Yan Zheng66435582008-10-30 14:19:50 -0400404
Chris Mason771ed682008-11-06 22:02:51 -0500405 if (bookend && found_extent) {
406 if (locked_end < extent_end) {
407 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
408 locked_end, extent_end - 1,
409 GFP_NOFS);
410 if (!ret) {
411 btrfs_release_path(root, path);
412 lock_extent(&BTRFS_I(inode)->io_tree,
413 locked_end, extent_end - 1,
414 GFP_NOFS);
415 locked_end = extent_end;
416 continue;
417 }
Yan Zheng66435582008-10-30 14:19:50 -0400418 locked_end = extent_end;
Yan Zheng66435582008-10-30 14:19:50 -0400419 }
Chris Mason771ed682008-11-06 22:02:51 -0500420 disk_bytenr = le64_to_cpu(old.disk_bytenr);
421 if (disk_bytenr != 0) {
422 ret = btrfs_inc_extent_ref(trans, root,
423 disk_bytenr,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400424 le64_to_cpu(old.disk_num_bytes), 0,
425 root->root_key.objectid,
426 key.objectid, key.offset -
427 le64_to_cpu(old.offset));
Chris Mason771ed682008-11-06 22:02:51 -0500428 BUG_ON(ret);
429 }
Yan Zheng66435582008-10-30 14:19:50 -0400430 }
431
432 if (found_inline) {
433 u64 mask = root->sectorsize - 1;
434 search_start = (extent_end + mask) & ~mask;
435 } else
436 search_start = extent_end;
437
Chris Mason39279cc2007-06-12 06:35:45 -0400438 /* truncate existing extent */
439 if (start > key.offset) {
440 u64 new_num;
441 u64 old_num;
442 keep = 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400443 WARN_ON(start & (root->sectorsize - 1));
Chris Mason39279cc2007-06-12 06:35:45 -0400444 if (found_extent) {
Chris Masondb945352007-10-15 16:15:53 -0400445 new_num = start - key.offset;
446 old_num = btrfs_file_extent_num_bytes(leaf,
447 extent);
448 *hint_byte =
449 btrfs_file_extent_disk_bytenr(leaf,
450 extent);
451 if (btrfs_file_extent_disk_bytenr(leaf,
452 extent)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400453 inode_sub_bytes(inode, old_num -
454 new_num);
Chris Mason39279cc2007-06-12 06:35:45 -0400455 }
Chris Mason771ed682008-11-06 22:02:51 -0500456 btrfs_set_file_extent_num_bytes(leaf,
457 extent, new_num);
Chris Mason5f39d392007-10-15 16:14:19 -0400458 btrfs_mark_buffer_dirty(leaf);
Chris Mason00f5c792007-11-30 10:09:33 -0500459 } else if (key.offset < inline_limit &&
460 (end > extent_end) &&
461 (inline_limit < extent_end)) {
Chris Mason3326d1b2007-10-15 16:18:25 -0400462 u32 new_size;
463 new_size = btrfs_file_extent_calc_inline_size(
Chris Mason00f5c792007-11-30 10:09:33 -0500464 inline_limit - key.offset);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400465 inode_sub_bytes(inode, extent_end -
466 inline_limit);
Chris Mason70b99e62008-10-31 12:46:39 -0400467 btrfs_set_file_extent_ram_bytes(leaf, extent,
468 new_size);
469 if (!compression && !encryption) {
470 btrfs_truncate_item(trans, root, path,
471 new_size, 1);
472 }
Chris Mason39279cc2007-06-12 06:35:45 -0400473 }
474 }
475 /* delete the entire extent */
476 if (!keep) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400477 if (found_inline)
478 inode_sub_bytes(inode, extent_end -
479 key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400480 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400481 /* TODO update progress marker and return */
Chris Mason39279cc2007-06-12 06:35:45 -0400482 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400483 extent = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400484 btrfs_release_path(root, path);
485 /* the extent will be freed later */
Chris Mason39279cc2007-06-12 06:35:45 -0400486 }
Yan0181e582008-01-30 14:39:54 -0500487 if (bookend && found_inline && start <= key.offset) {
Chris Mason179e29e2007-11-01 11:28:41 -0400488 u32 new_size;
489 new_size = btrfs_file_extent_calc_inline_size(
Yan0181e582008-01-30 14:39:54 -0500490 extent_end - end);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400491 inode_sub_bytes(inode, end - key.offset);
Chris Mason70b99e62008-10-31 12:46:39 -0400492 btrfs_set_file_extent_ram_bytes(leaf, extent,
493 new_size);
494 if (!compression && !encryption)
495 ret = btrfs_truncate_item(trans, root, path,
496 new_size, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400497 BUG_ON(ret);
Chris Mason179e29e2007-11-01 11:28:41 -0400498 }
Chris Mason39279cc2007-06-12 06:35:45 -0400499 /* create bookend, splitting the extent in two */
500 if (bookend && found_extent) {
501 struct btrfs_key ins;
502 ins.objectid = inode->i_ino;
503 ins.offset = end;
Chris Mason39279cc2007-06-12 06:35:45 -0400504 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
Chris Mason771ed682008-11-06 22:02:51 -0500505
Chris Mason39279cc2007-06-12 06:35:45 -0400506 btrfs_release_path(root, path);
Chris Masonb9473432009-03-13 11:00:37 -0400507 path->leave_spinning = 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400508 ret = btrfs_insert_empty_item(trans, root, path, &ins,
509 sizeof(*extent));
Zheng Yan31840ae2008-09-23 13:14:14 -0400510 BUG_ON(ret);
Chris Mason8c2383c2007-06-18 09:57:58 -0400511
Chris Mason5f39d392007-10-15 16:14:19 -0400512 leaf = path->nodes[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400513 extent = btrfs_item_ptr(leaf, path->slots[0],
514 struct btrfs_file_extent_item);
515 write_extent_buffer(leaf, &old,
516 (unsigned long)extent, sizeof(old));
Chris Mason39279cc2007-06-12 06:35:45 -0400517
Chris Masonc8b97812008-10-29 14:49:59 -0400518 btrfs_set_file_extent_compression(leaf, extent,
519 compression);
520 btrfs_set_file_extent_encryption(leaf, extent,
521 encryption);
522 btrfs_set_file_extent_other_encoding(leaf, extent,
523 other_encoding);
Chris Mason5f39d392007-10-15 16:14:19 -0400524 btrfs_set_file_extent_offset(leaf, extent,
Chris Masondb945352007-10-15 16:15:53 -0400525 le64_to_cpu(old.offset) + end - key.offset);
526 WARN_ON(le64_to_cpu(old.num_bytes) <
527 (extent_end - end));
528 btrfs_set_file_extent_num_bytes(leaf, extent,
529 extent_end - end);
Chris Masonc8b97812008-10-29 14:49:59 -0400530
531 /*
532 * set the ram bytes to the size of the full extent
533 * before splitting. This is a worst case flag,
534 * but its the best we can do because we don't know
535 * how splitting affects compression
536 */
537 btrfs_set_file_extent_ram_bytes(leaf, extent,
538 ram_bytes);
Yan Zhengd899e052008-10-30 14:25:28 -0400539 btrfs_set_file_extent_type(leaf, extent, found_type);
Chris Masondb945352007-10-15 16:15:53 -0400540
Chris Masonb9473432009-03-13 11:00:37 -0400541 btrfs_unlock_up_safe(path, 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400542 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masonb9473432009-03-13 11:00:37 -0400543 btrfs_set_lock_blocking(path->nodes[0]);
Zheng Yan31840ae2008-09-23 13:14:14 -0400544
Chris Masonb9473432009-03-13 11:00:37 -0400545 path->leave_spinning = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400546 btrfs_release_path(root, path);
Chris Masond3977122009-01-05 21:25:51 -0500547 if (disk_bytenr != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400548 inode_add_bytes(inode, extent_end - end);
Zheng Yan31840ae2008-09-23 13:14:14 -0400549 }
550
551 if (found_extent && !keep) {
Christoph Hellwig6e430f92008-12-02 06:36:09 -0500552 u64 old_disk_bytenr = le64_to_cpu(old.disk_bytenr);
Zheng Yan31840ae2008-09-23 13:14:14 -0400553
Christoph Hellwig6e430f92008-12-02 06:36:09 -0500554 if (old_disk_bytenr != 0) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400555 inode_sub_bytes(inode,
556 le64_to_cpu(old.num_bytes));
Zheng Yan31840ae2008-09-23 13:14:14 -0400557 ret = btrfs_free_extent(trans, root,
Christoph Hellwig6e430f92008-12-02 06:36:09 -0500558 old_disk_bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -0400559 le64_to_cpu(old.disk_num_bytes),
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400560 0, root->root_key.objectid,
561 key.objectid, key.offset -
562 le64_to_cpu(old.offset));
Zheng Yan31840ae2008-09-23 13:14:14 -0400563 BUG_ON(ret);
Christoph Hellwig6e430f92008-12-02 06:36:09 -0500564 *hint_byte = old_disk_bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400565 }
566 }
567
568 if (search_start >= end) {
Chris Mason39279cc2007-06-12 06:35:45 -0400569 ret = 0;
570 goto out;
571 }
572 }
573out:
574 btrfs_free_path(path);
Chris Masone980b502009-04-24 14:39:24 -0400575 if (locked_end > orig_locked_end) {
576 unlock_extent(&BTRFS_I(inode)->io_tree, orig_locked_end,
577 locked_end - 1, GFP_NOFS);
Yan Zheng66435582008-10-30 14:19:50 -0400578 }
Chris Mason39279cc2007-06-12 06:35:45 -0400579 return ret;
580}
581
Yan Zhengd899e052008-10-30 14:25:28 -0400582static int extent_mergeable(struct extent_buffer *leaf, int slot,
583 u64 objectid, u64 bytenr, u64 *start, u64 *end)
584{
585 struct btrfs_file_extent_item *fi;
586 struct btrfs_key key;
587 u64 extent_end;
588
589 if (slot < 0 || slot >= btrfs_header_nritems(leaf))
590 return 0;
591
592 btrfs_item_key_to_cpu(leaf, &key, slot);
593 if (key.objectid != objectid || key.type != BTRFS_EXTENT_DATA_KEY)
594 return 0;
595
596 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
597 if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG ||
598 btrfs_file_extent_disk_bytenr(leaf, fi) != bytenr ||
599 btrfs_file_extent_compression(leaf, fi) ||
600 btrfs_file_extent_encryption(leaf, fi) ||
601 btrfs_file_extent_other_encoding(leaf, fi))
602 return 0;
603
604 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
605 if ((*start && *start != key.offset) || (*end && *end != extent_end))
606 return 0;
607
608 *start = key.offset;
609 *end = extent_end;
610 return 1;
611}
612
613/*
614 * Mark extent in the range start - end as written.
615 *
616 * This changes extent type from 'pre-allocated' to 'regular'. If only
617 * part of extent is marked as written, the extent will be split into
618 * two or three.
619 */
620int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
621 struct btrfs_root *root,
622 struct inode *inode, u64 start, u64 end)
623{
624 struct extent_buffer *leaf;
625 struct btrfs_path *path;
626 struct btrfs_file_extent_item *fi;
627 struct btrfs_key key;
628 u64 bytenr;
629 u64 num_bytes;
630 u64 extent_end;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400631 u64 orig_offset;
Yan Zhengd899e052008-10-30 14:25:28 -0400632 u64 other_start;
633 u64 other_end;
634 u64 split = start;
635 u64 locked_end = end;
636 int extent_type;
637 int split_end = 1;
638 int ret;
639
640 btrfs_drop_extent_cache(inode, start, end - 1, 0);
641
642 path = btrfs_alloc_path();
643 BUG_ON(!path);
644again:
645 key.objectid = inode->i_ino;
646 key.type = BTRFS_EXTENT_DATA_KEY;
647 if (split == start)
648 key.offset = split;
649 else
650 key.offset = split - 1;
651
652 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
653 if (ret > 0 && path->slots[0] > 0)
654 path->slots[0]--;
655
656 leaf = path->nodes[0];
657 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
658 BUG_ON(key.objectid != inode->i_ino ||
659 key.type != BTRFS_EXTENT_DATA_KEY);
660 fi = btrfs_item_ptr(leaf, path->slots[0],
661 struct btrfs_file_extent_item);
662 extent_type = btrfs_file_extent_type(leaf, fi);
663 BUG_ON(extent_type != BTRFS_FILE_EXTENT_PREALLOC);
664 extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
665 BUG_ON(key.offset > start || extent_end < end);
666
667 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
668 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400669 orig_offset = key.offset - btrfs_file_extent_offset(leaf, fi);
Yan Zhengd899e052008-10-30 14:25:28 -0400670
671 if (key.offset == start)
672 split = end;
673
674 if (key.offset == start && extent_end == end) {
675 int del_nr = 0;
676 int del_slot = 0;
Yan Zhengd899e052008-10-30 14:25:28 -0400677 other_start = end;
678 other_end = 0;
679 if (extent_mergeable(leaf, path->slots[0] + 1, inode->i_ino,
680 bytenr, &other_start, &other_end)) {
681 extent_end = other_end;
682 del_slot = path->slots[0] + 1;
683 del_nr++;
684 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400685 0, root->root_key.objectid,
686 inode->i_ino, orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400687 BUG_ON(ret);
688 }
689 other_start = 0;
690 other_end = start;
691 if (extent_mergeable(leaf, path->slots[0] - 1, inode->i_ino,
692 bytenr, &other_start, &other_end)) {
693 key.offset = other_start;
694 del_slot = path->slots[0];
695 del_nr++;
696 ret = btrfs_free_extent(trans, root, bytenr, num_bytes,
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400697 0, root->root_key.objectid,
698 inode->i_ino, orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400699 BUG_ON(ret);
700 }
701 split_end = 0;
702 if (del_nr == 0) {
703 btrfs_set_file_extent_type(leaf, fi,
704 BTRFS_FILE_EXTENT_REG);
705 goto done;
706 }
707
708 fi = btrfs_item_ptr(leaf, del_slot - 1,
709 struct btrfs_file_extent_item);
710 btrfs_set_file_extent_type(leaf, fi, BTRFS_FILE_EXTENT_REG);
711 btrfs_set_file_extent_num_bytes(leaf, fi,
712 extent_end - key.offset);
713 btrfs_mark_buffer_dirty(leaf);
714
715 ret = btrfs_del_items(trans, root, path, del_slot, del_nr);
716 BUG_ON(ret);
Chris Mason546888d2009-04-21 11:53:38 -0400717 goto release;
Yan Zhengd899e052008-10-30 14:25:28 -0400718 } else if (split == start) {
719 if (locked_end < extent_end) {
720 ret = try_lock_extent(&BTRFS_I(inode)->io_tree,
721 locked_end, extent_end - 1, GFP_NOFS);
722 if (!ret) {
723 btrfs_release_path(root, path);
724 lock_extent(&BTRFS_I(inode)->io_tree,
725 locked_end, extent_end - 1, GFP_NOFS);
726 locked_end = extent_end;
727 goto again;
728 }
729 locked_end = extent_end;
730 }
731 btrfs_set_file_extent_num_bytes(leaf, fi, split - key.offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400732 } else {
733 BUG_ON(key.offset != start);
Yan Zhengd899e052008-10-30 14:25:28 -0400734 key.offset = split;
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400735 btrfs_set_file_extent_offset(leaf, fi, key.offset -
736 orig_offset);
737 btrfs_set_file_extent_num_bytes(leaf, fi, extent_end - split);
Yan Zhengd899e052008-10-30 14:25:28 -0400738 btrfs_set_item_key_safe(trans, root, path, &key);
739 extent_end = split;
740 }
741
742 if (extent_end == end) {
743 split_end = 0;
744 extent_type = BTRFS_FILE_EXTENT_REG;
745 }
746 if (extent_end == end && split == start) {
747 other_start = end;
748 other_end = 0;
749 if (extent_mergeable(leaf, path->slots[0] + 1, inode->i_ino,
750 bytenr, &other_start, &other_end)) {
751 path->slots[0]++;
752 fi = btrfs_item_ptr(leaf, path->slots[0],
753 struct btrfs_file_extent_item);
754 key.offset = split;
755 btrfs_set_item_key_safe(trans, root, path, &key);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400756 btrfs_set_file_extent_offset(leaf, fi, key.offset -
757 orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400758 btrfs_set_file_extent_num_bytes(leaf, fi,
759 other_end - split);
760 goto done;
761 }
762 }
763 if (extent_end == end && split == end) {
764 other_start = 0;
765 other_end = start;
766 if (extent_mergeable(leaf, path->slots[0] - 1 , inode->i_ino,
767 bytenr, &other_start, &other_end)) {
768 path->slots[0]--;
769 fi = btrfs_item_ptr(leaf, path->slots[0],
770 struct btrfs_file_extent_item);
771 btrfs_set_file_extent_num_bytes(leaf, fi, extent_end -
772 other_start);
773 goto done;
774 }
775 }
776
777 btrfs_mark_buffer_dirty(leaf);
Yan Zhengc36047d2008-11-12 14:19:50 -0500778
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400779 ret = btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0,
780 root->root_key.objectid,
781 inode->i_ino, orig_offset);
Yan Zhengc36047d2008-11-12 14:19:50 -0500782 BUG_ON(ret);
Yan Zhengd899e052008-10-30 14:25:28 -0400783 btrfs_release_path(root, path);
784
785 key.offset = start;
786 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*fi));
787 BUG_ON(ret);
788
789 leaf = path->nodes[0];
790 fi = btrfs_item_ptr(leaf, path->slots[0],
791 struct btrfs_file_extent_item);
792 btrfs_set_file_extent_generation(leaf, fi, trans->transid);
793 btrfs_set_file_extent_type(leaf, fi, extent_type);
794 btrfs_set_file_extent_disk_bytenr(leaf, fi, bytenr);
795 btrfs_set_file_extent_disk_num_bytes(leaf, fi, num_bytes);
Yan Zheng5d4f98a2009-06-10 10:45:14 -0400796 btrfs_set_file_extent_offset(leaf, fi, key.offset - orig_offset);
Yan Zhengd899e052008-10-30 14:25:28 -0400797 btrfs_set_file_extent_num_bytes(leaf, fi, extent_end - key.offset);
798 btrfs_set_file_extent_ram_bytes(leaf, fi, num_bytes);
799 btrfs_set_file_extent_compression(leaf, fi, 0);
800 btrfs_set_file_extent_encryption(leaf, fi, 0);
801 btrfs_set_file_extent_other_encoding(leaf, fi, 0);
Yan Zhengd899e052008-10-30 14:25:28 -0400802done:
803 btrfs_mark_buffer_dirty(leaf);
Chris Mason546888d2009-04-21 11:53:38 -0400804
805release:
Yan Zhengd899e052008-10-30 14:25:28 -0400806 btrfs_release_path(root, path);
807 if (split_end && split == start) {
808 split = end;
809 goto again;
810 }
811 if (locked_end > end) {
812 unlock_extent(&BTRFS_I(inode)->io_tree, end, locked_end - 1,
813 GFP_NOFS);
814 }
815 btrfs_free_path(path);
816 return 0;
817}
818
Chris Mason39279cc2007-06-12 06:35:45 -0400819/*
Chris Masond352ac62008-09-29 15:18:18 -0400820 * this gets pages into the page cache and locks them down, it also properly
821 * waits for data=ordered extents to finish before allowing the pages to be
822 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -0400823 */
Chris Masond3977122009-01-05 21:25:51 -0500824static noinline int prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500825 struct page **pages, size_t num_pages,
826 loff_t pos, unsigned long first_index,
827 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400828{
829 int i;
830 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500831 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400832 int err = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400833 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400834 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400835
Chris Mason5f39d392007-10-15 16:14:19 -0400836 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400837 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400838
Yan Zheng9036c102008-10-30 14:19:41 -0400839 if (start_pos > inode->i_size) {
840 err = btrfs_cont_expand(inode, start_pos);
841 if (err)
842 return err;
843 }
844
Chris Mason39279cc2007-06-12 06:35:45 -0400845 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400846again:
Chris Mason39279cc2007-06-12 06:35:45 -0400847 for (i = 0; i < num_pages; i++) {
848 pages[i] = grab_cache_page(inode->i_mapping, index + i);
849 if (!pages[i]) {
850 err = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -0400851 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400852 }
Chris Masonccd467d2007-06-28 15:57:36 -0400853 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400854 }
Chris Mason07627042008-02-19 11:29:24 -0500855 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400856 struct btrfs_ordered_extent *ordered;
Chris Masond99cb302008-02-19 12:55:05 -0500857 lock_extent(&BTRFS_I(inode)->io_tree,
858 start_pos, last_pos - 1, GFP_NOFS);
Chris Masond3977122009-01-05 21:25:51 -0500859 ordered = btrfs_lookup_first_ordered_extent(inode,
860 last_pos - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400861 if (ordered &&
862 ordered->file_offset + ordered->len > start_pos &&
863 ordered->file_offset < last_pos) {
864 btrfs_put_ordered_extent(ordered);
865 unlock_extent(&BTRFS_I(inode)->io_tree,
866 start_pos, last_pos - 1, GFP_NOFS);
867 for (i = 0; i < num_pages; i++) {
868 unlock_page(pages[i]);
869 page_cache_release(pages[i]);
870 }
871 btrfs_wait_ordered_range(inode, start_pos,
872 last_pos - start_pos);
873 goto again;
874 }
875 if (ordered)
876 btrfs_put_ordered_extent(ordered);
877
Chris Mason07627042008-02-19 11:29:24 -0500878 clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
879 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
880 GFP_NOFS);
Chris Masond99cb302008-02-19 12:55:05 -0500881 unlock_extent(&BTRFS_I(inode)->io_tree,
882 start_pos, last_pos - 1, GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500883 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400884 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400885 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400886 set_page_extent_mapped(pages[i]);
887 WARN_ON(!PageLocked(pages[i]));
888 }
Chris Mason39279cc2007-06-12 06:35:45 -0400889 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400890}
891
892static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
893 size_t count, loff_t *ppos)
894{
895 loff_t pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400896 loff_t start_pos;
897 ssize_t num_written = 0;
898 ssize_t err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400899 int ret = 0;
Chris Mason6da6aba2007-12-18 16:15:09 -0500900 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400901 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8c2383c2007-06-18 09:57:58 -0400902 struct page **pages = NULL;
903 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400904 struct page *pinned[2];
905 unsigned long first_index;
906 unsigned long last_index;
Chris Masoncb843a62008-10-03 12:30:02 -0400907 int will_write;
908
909 will_write = ((file->f_flags & O_SYNC) || IS_SYNC(inode) ||
910 (file->f_flags & O_DIRECT));
Chris Mason8c2383c2007-06-18 09:57:58 -0400911
912 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
913 PAGE_CACHE_SIZE / (sizeof(struct page *)));
Chris Mason39279cc2007-06-12 06:35:45 -0400914 pinned[0] = NULL;
915 pinned[1] = NULL;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400916
Chris Mason39279cc2007-06-12 06:35:45 -0400917 pos = *ppos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400918 start_pos = pos;
919
Chris Mason39279cc2007-06-12 06:35:45 -0400920 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
921 current->backing_dev_info = inode->i_mapping->backing_dev_info;
922 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
923 if (err)
Chris Mason1832a6d2007-12-21 16:27:21 -0500924 goto out_nolock;
Chris Mason39279cc2007-06-12 06:35:45 -0400925 if (count == 0)
Chris Mason1832a6d2007-12-21 16:27:21 -0500926 goto out_nolock;
Chris Mason2b1f55b2008-09-24 11:48:04 -0400927
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400928 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400929 if (err)
Chris Mason1832a6d2007-12-21 16:27:21 -0500930 goto out_nolock;
Chris Mason39279cc2007-06-12 06:35:45 -0400931 file_update_time(file);
932
Chris Mason8c2383c2007-06-18 09:57:58 -0400933 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -0400934
935 mutex_lock(&inode->i_mutex);
Chris Masonc3027eb2008-12-08 16:40:21 -0500936 BTRFS_I(inode)->sequence++;
Chris Mason39279cc2007-06-12 06:35:45 -0400937 first_index = pos >> PAGE_CACHE_SHIFT;
938 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
939
940 /*
941 * there are lots of better ways to do this, but this code
942 * makes sure the first and last page in the file range are
943 * up to date and ready for cow
944 */
945 if ((pos & (PAGE_CACHE_SIZE - 1))) {
946 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
947 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400948 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400949 BUG_ON(ret);
950 wait_on_page_locked(pinned[0]);
951 } else {
952 unlock_page(pinned[0]);
953 }
954 }
955 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
956 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
957 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400958 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400959 BUG_ON(ret);
960 wait_on_page_locked(pinned[1]);
961 } else {
962 unlock_page(pinned[1]);
963 }
964 }
965
Chris Masond3977122009-01-05 21:25:51 -0500966 while (count > 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400967 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Chris Mason11bd1432007-06-22 14:16:24 -0400968 size_t write_bytes = min(count, nrptrs *
969 (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400970 offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400971 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
972 PAGE_CACHE_SHIFT;
973
Chris Mason8c2383c2007-06-18 09:57:58 -0400974 WARN_ON(num_pages > nrptrs);
yanhai zhu9aead432009-01-05 15:49:11 -0500975 memset(pages, 0, sizeof(struct page *) * nrptrs);
Chris Mason1832a6d2007-12-21 16:27:21 -0500976
Josef Bacik6a632092009-02-20 11:00:09 -0500977 ret = btrfs_check_data_free_space(root, inode, write_bytes);
Chris Mason1832a6d2007-12-21 16:27:21 -0500978 if (ret)
979 goto out;
980
Chris Mason39279cc2007-06-12 06:35:45 -0400981 ret = prepare_pages(root, file, pages, num_pages,
982 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400983 write_bytes);
Josef Bacik6a632092009-02-20 11:00:09 -0500984 if (ret) {
985 btrfs_free_reserved_data_space(root, inode,
986 write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400987 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -0500988 }
Chris Mason39279cc2007-06-12 06:35:45 -0400989
Chris Mason39279cc2007-06-12 06:35:45 -0400990 ret = btrfs_copy_from_user(pos, num_pages,
991 write_bytes, pages, buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400992 if (ret) {
Josef Bacik6a632092009-02-20 11:00:09 -0500993 btrfs_free_reserved_data_space(root, inode,
994 write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400995 btrfs_drop_pages(pages, num_pages);
996 goto out;
997 }
Chris Mason39279cc2007-06-12 06:35:45 -0400998
999 ret = dirty_and_release_pages(NULL, root, file, pages,
1000 num_pages, pos, write_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -04001001 btrfs_drop_pages(pages, num_pages);
Josef Bacik6a632092009-02-20 11:00:09 -05001002 if (ret) {
1003 btrfs_free_reserved_data_space(root, inode,
1004 write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -04001005 goto out;
Josef Bacik6a632092009-02-20 11:00:09 -05001006 }
Chris Mason39279cc2007-06-12 06:35:45 -04001007
Chris Masoncb843a62008-10-03 12:30:02 -04001008 if (will_write) {
1009 btrfs_fdatawrite_range(inode->i_mapping, pos,
1010 pos + write_bytes - 1,
Chris Masond313d7a2009-04-20 15:50:09 -04001011 WB_SYNC_ALL);
Chris Masoncb843a62008-10-03 12:30:02 -04001012 } else {
1013 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
1014 num_pages);
1015 if (num_pages <
1016 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
1017 btrfs_btree_balance_dirty(root, 1);
1018 btrfs_throttle(root);
1019 }
1020
Chris Mason39279cc2007-06-12 06:35:45 -04001021 buf += write_bytes;
1022 count -= write_bytes;
1023 pos += write_bytes;
1024 num_written += write_bytes;
1025
Chris Mason39279cc2007-06-12 06:35:45 -04001026 cond_resched();
1027 }
Chris Mason39279cc2007-06-12 06:35:45 -04001028out:
Chris Mason1832a6d2007-12-21 16:27:21 -05001029 mutex_unlock(&inode->i_mutex);
Josef Bacik6a632092009-02-20 11:00:09 -05001030 if (ret)
1031 err = ret;
Chris Mason5b92ee72008-01-03 13:46:11 -05001032
Chris Mason1832a6d2007-12-21 16:27:21 -05001033out_nolock:
Chris Mason8c2383c2007-06-18 09:57:58 -04001034 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -04001035 if (pinned[0])
1036 page_cache_release(pinned[0]);
1037 if (pinned[1])
1038 page_cache_release(pinned[1]);
1039 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001040
Chris Mason5a3f23d2009-03-31 13:27:11 -04001041 /*
1042 * we want to make sure fsync finds this change
1043 * but we haven't joined a transaction running right now.
1044 *
1045 * Later on, someone is sure to update the inode and get the
1046 * real transid recorded.
1047 *
1048 * We set last_trans now to the fs_info generation + 1,
1049 * this will either be one more than the running transaction
1050 * or the generation used for the next transaction if there isn't
1051 * one running right now.
1052 */
1053 BTRFS_I(inode)->last_trans = root->fs_info->generation + 1;
1054
Chris Masoncb843a62008-10-03 12:30:02 -04001055 if (num_written > 0 && will_write) {
Chris Masone02119d2008-09-05 16:13:11 -04001056 struct btrfs_trans_handle *trans;
1057
Chris Masoncb843a62008-10-03 12:30:02 -04001058 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
1059 if (err)
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001060 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -04001061
Chris Masoncb843a62008-10-03 12:30:02 -04001062 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
1063 trans = btrfs_start_transaction(root, 1);
1064 ret = btrfs_log_dentry_safe(trans, root,
1065 file->f_dentry);
1066 if (ret == 0) {
Chris Mason12fcfd22009-03-24 10:24:20 -04001067 ret = btrfs_sync_log(trans, root);
1068 if (ret == 0)
1069 btrfs_end_transaction(trans, root);
1070 else
1071 btrfs_commit_transaction(trans, root);
Chris Masoncb843a62008-10-03 12:30:02 -04001072 } else {
1073 btrfs_commit_transaction(trans, root);
1074 }
Chris Masone02119d2008-09-05 16:13:11 -04001075 }
Chris Masoncb843a62008-10-03 12:30:02 -04001076 if (file->f_flags & O_DIRECT) {
1077 invalidate_mapping_pages(inode->i_mapping,
1078 start_pos >> PAGE_CACHE_SHIFT,
1079 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
1080 }
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001081 }
Chris Mason39279cc2007-06-12 06:35:45 -04001082 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001083 return num_written ? num_written : err;
1084}
1085
Chris Masond3977122009-01-05 21:25:51 -05001086int btrfs_release_file(struct inode *inode, struct file *filp)
Mingminge1b81e62008-05-27 10:55:43 -04001087{
Chris Mason5a3f23d2009-03-31 13:27:11 -04001088 /*
1089 * ordered_data_close is set by settattr when we are about to truncate
1090 * a file from a non-zero size to a zero size. This tries to
1091 * flush down new bytes that may have been written if the
1092 * application were using truncate to replace a file in place.
1093 */
1094 if (BTRFS_I(inode)->ordered_data_close) {
1095 BTRFS_I(inode)->ordered_data_close = 0;
1096 btrfs_add_ordered_operation(NULL, BTRFS_I(inode)->root, inode);
1097 if (inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
1098 filemap_flush(inode->i_mapping);
1099 }
Sage Weil6bf13c02008-06-10 10:07:39 -04001100 if (filp->private_data)
1101 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001102 return 0;
1103}
1104
Chris Masond352ac62008-09-29 15:18:18 -04001105/*
1106 * fsync call for both files and directories. This logs the inode into
1107 * the tree log instead of forcing full commits whenever possible.
1108 *
1109 * It needs to call filemap_fdatawait so that all ordered extent updates are
1110 * in the metadata btree are up to date for copying to the log.
1111 *
1112 * It drops the inode mutex before doing the tree log commit. This is an
1113 * important optimization for directories because holding the mutex prevents
1114 * new operations on the dir while we write to disk.
1115 */
Chris Masone02119d2008-09-05 16:13:11 -04001116int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001117{
1118 struct inode *inode = dentry->d_inode;
1119 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001120 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001121 struct btrfs_trans_handle *trans;
1122
1123 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001124 * check the transaction that last modified this inode
1125 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001126 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001127 if (!BTRFS_I(inode)->last_trans)
1128 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001129
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001130 mutex_lock(&root->fs_info->trans_mutex);
1131 if (BTRFS_I(inode)->last_trans <=
1132 root->fs_info->last_trans_committed) {
1133 BTRFS_I(inode)->last_trans = 0;
1134 mutex_unlock(&root->fs_info->trans_mutex);
1135 goto out;
1136 }
1137 mutex_unlock(&root->fs_info->trans_mutex);
1138
Yan Zheng7237f182009-01-21 12:54:03 -05001139 root->log_batch++;
Chris Mason580afd72008-12-08 19:15:39 -05001140 filemap_fdatawrite(inode->i_mapping);
1141 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Yan Zheng7237f182009-01-21 12:54:03 -05001142 root->log_batch++;
Chris Masone02119d2008-09-05 16:13:11 -04001143
Hisashi Hifumi524724e2009-06-10 11:13:17 -04001144 if (datasync && !(inode->i_state & I_DIRTY_PAGES))
1145 goto out;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001146 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001147 * ok we haven't committed the transaction yet, lets do a commit
1148 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001149 if (file && file->private_data)
Sage Weil6bf13c02008-06-10 10:07:39 -04001150 btrfs_ioctl_trans_end(file);
1151
Chris Mason39279cc2007-06-12 06:35:45 -04001152 trans = btrfs_start_transaction(root, 1);
1153 if (!trans) {
1154 ret = -ENOMEM;
1155 goto out;
1156 }
Chris Masone02119d2008-09-05 16:13:11 -04001157
Chris Mason2cfbd502009-02-20 10:55:10 -05001158 ret = btrfs_log_dentry_safe(trans, root, dentry);
Chris Masond3977122009-01-05 21:25:51 -05001159 if (ret < 0)
Chris Masone02119d2008-09-05 16:13:11 -04001160 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001161
1162 /* we've logged all the items and now have a consistent
1163 * version of the file in the log. It is possible that
1164 * someone will come in and modify the file, but that's
1165 * fine because the log is consistent on disk, and we
1166 * have references to all of the file's extents
1167 *
1168 * It is possible that someone will come in and log the
1169 * file again, but that will end up using the synchronization
1170 * inside btrfs_sync_log to keep things safe.
1171 */
Chris Mason2cfbd502009-02-20 10:55:10 -05001172 mutex_unlock(&dentry->d_inode->i_mutex);
Chris Mason49eb7e42008-09-11 15:53:12 -04001173
Chris Masone02119d2008-09-05 16:13:11 -04001174 if (ret > 0) {
1175 ret = btrfs_commit_transaction(trans, root);
1176 } else {
Chris Mason12fcfd22009-03-24 10:24:20 -04001177 ret = btrfs_sync_log(trans, root);
1178 if (ret == 0)
1179 ret = btrfs_end_transaction(trans, root);
1180 else
1181 ret = btrfs_commit_transaction(trans, root);
Chris Masone02119d2008-09-05 16:13:11 -04001182 }
Chris Mason2cfbd502009-02-20 10:55:10 -05001183 mutex_lock(&dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001184out:
1185 return ret > 0 ? EIO : ret;
1186}
1187
Chris Mason9ebefb182007-06-15 13:50:00 -04001188static struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001189 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001190 .page_mkwrite = btrfs_page_mkwrite,
1191};
1192
1193static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1194{
1195 vma->vm_ops = &btrfs_file_vm_ops;
1196 file_accessed(filp);
1197 return 0;
1198}
1199
Chris Mason39279cc2007-06-12 06:35:45 -04001200struct file_operations btrfs_file_operations = {
1201 .llseek = generic_file_llseek,
1202 .read = do_sync_read,
Chris Mason9ebefb182007-06-15 13:50:00 -04001203 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001204 .splice_read = generic_file_splice_read,
Chris Mason39279cc2007-06-12 06:35:45 -04001205 .write = btrfs_file_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001206 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001207 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001208 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001209 .fsync = btrfs_sync_file,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001210 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001211#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001212 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001213#endif
1214};