blob: 0aa15436590e46bdc05396e28871d4aa3e12c072 [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>
Chris Mason92fee662007-07-25 12:31:35 -040032#include <linux/version.h>
Chris Mason39279cc2007-06-12 06:35:45 -040033#include "ctree.h"
34#include "disk-io.h"
35#include "transaction.h"
36#include "btrfs_inode.h"
37#include "ioctl.h"
38#include "print-tree.h"
Chris Masone02119d2008-09-05 16:13:11 -040039#include "tree-log.h"
40#include "locking.h"
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -040041#include "compat.h"
Chris Mason39279cc2007-06-12 06:35:45 -040042
43
Chris Masond352ac62008-09-29 15:18:18 -040044/* simple helper to fault in pages and copy. This should go away
45 * and be replaced with calls into generic code.
46 */
Chris Masona1b32a52008-09-05 16:09:51 -040047static int noinline btrfs_copy_from_user(loff_t pos, int num_pages,
48 int write_bytes,
49 struct page **prepared_pages,
50 const char __user * buf)
Chris Mason39279cc2007-06-12 06:35:45 -040051{
52 long page_fault = 0;
53 int i;
54 int offset = pos & (PAGE_CACHE_SIZE - 1);
55
56 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
57 size_t count = min_t(size_t,
58 PAGE_CACHE_SIZE - offset, write_bytes);
59 struct page *page = prepared_pages[i];
60 fault_in_pages_readable(buf, count);
61
62 /* Copy data from userspace to the current page */
63 kmap(page);
64 page_fault = __copy_from_user(page_address(page) + offset,
65 buf, count);
66 /* Flush processor's dcache for this page */
67 flush_dcache_page(page);
68 kunmap(page);
69 buf += count;
70 write_bytes -= count;
71
72 if (page_fault)
73 break;
74 }
75 return page_fault ? -EFAULT : 0;
76}
77
Chris Masond352ac62008-09-29 15:18:18 -040078/*
79 * unlocks pages after btrfs_file_write is done with them
80 */
Chris Masona1b32a52008-09-05 16:09:51 -040081static void noinline btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -040082{
83 size_t i;
84 for (i = 0; i < num_pages; i++) {
85 if (!pages[i])
86 break;
Chris Masond352ac62008-09-29 15:18:18 -040087 /* page checked is some magic around finding pages that
88 * have been modified without going through btrfs_set_page_dirty
89 * clear it here
90 */
Chris Mason4a096752008-07-21 10:29:44 -040091 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -040092 unlock_page(pages[i]);
93 mark_page_accessed(pages[i]);
94 page_cache_release(pages[i]);
95 }
96}
97
Chris Masond352ac62008-09-29 15:18:18 -040098/*
99 * after copy_from_user, pages need to be dirtied and we need to make
100 * sure holes are created between the current EOF and the start of
101 * any next extents (if required).
102 *
103 * this also makes the decision about creating an inline extent vs
104 * doing real data extents, marking pages dirty and delalloc as required.
105 */
Chris Mason98ed5172008-01-03 10:01:48 -0500106static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400107 struct btrfs_root *root,
108 struct file *file,
109 struct page **pages,
110 size_t num_pages,
111 loff_t pos,
112 size_t write_bytes)
113{
Chris Mason39279cc2007-06-12 06:35:45 -0400114 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400115 int i;
Chris Mason6da6aba2007-12-18 16:15:09 -0500116 struct inode *inode = fdentry(file)->d_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500117 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masondb945352007-10-15 16:15:53 -0400118 u64 hint_byte;
119 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400120 u64 start_pos;
121 u64 end_of_last_block;
122 u64 end_pos = pos + write_bytes;
123 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400124
Chris Mason5f39d392007-10-15 16:14:19 -0400125 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400126 num_bytes = (write_bytes + pos - start_pos +
127 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400128
Chris Masondb945352007-10-15 16:15:53 -0400129 end_of_last_block = start_pos + num_bytes - 1;
130
Chris Masond1310b22008-01-24 16:13:08 -0500131 lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
Chris Mason37d1aee2008-07-31 10:48:37 -0400132 trans = btrfs_join_transaction(root, 1);
Chris Masona52d9a82007-08-27 16:49:44 -0400133 if (!trans) {
134 err = -ENOMEM;
135 goto out_unlock;
136 }
137 btrfs_set_trans_block_group(trans, inode);
Chris Masondb945352007-10-15 16:15:53 -0400138 hint_byte = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400139
140 if ((end_of_last_block & 4095) == 0) {
Christoph Hellwig94330632007-09-10 20:02:22 -0400141 printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
Chris Masona52d9a82007-08-27 16:49:44 -0400142 }
Chris Masond1310b22008-01-24 16:13:08 -0500143 set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400144
145 /* FIXME...EIEIO, ENOSPC and more */
Chris Masona52d9a82007-08-27 16:49:44 -0400146 /* insert any holes we need to create */
Chris Mason1b1e2132008-06-25 16:01:31 -0400147 if (isize < start_pos) {
Chris Masona52d9a82007-08-27 16:49:44 -0400148 u64 last_pos_in_file;
149 u64 hole_size;
Chris Mason5f39d392007-10-15 16:14:19 -0400150 u64 mask = root->sectorsize - 1;
Chris Masona52d9a82007-08-27 16:49:44 -0400151 last_pos_in_file = (isize + mask) & ~mask;
Chris Mason1b1e2132008-06-25 16:01:31 -0400152 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400153 if (hole_size > 0) {
154 btrfs_wait_ordered_range(inode, last_pos_in_file,
155 last_pos_in_file + hole_size);
Chris Masonee6e6502008-07-17 12:54:40 -0400156 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400157 err = btrfs_drop_extents(trans, root, inode,
158 last_pos_in_file,
159 last_pos_in_file + hole_size,
Chris Mason3326d1b2007-10-15 16:18:25 -0400160 last_pos_in_file,
Chris Masondb945352007-10-15 16:15:53 -0400161 &hint_byte);
Chris Mason2bf5a722007-08-30 11:54:02 -0400162 if (err)
163 goto failed;
164
Chris Masona52d9a82007-08-27 16:49:44 -0400165 err = btrfs_insert_file_extent(trans, root,
166 inode->i_ino,
167 last_pos_in_file,
Chris Masonc8b97812008-10-29 14:49:59 -0400168 0, 0, hole_size, 0,
169 hole_size, 0, 0, 0);
Chris Masond1310b22008-01-24 16:13:08 -0500170 btrfs_drop_extent_cache(inode, last_pos_in_file,
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400171 last_pos_in_file + hole_size - 1, 0);
Chris Masonee6e6502008-07-17 12:54:40 -0400172 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason5f564062008-01-22 16:47:59 -0500173 btrfs_check_file(root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400174 }
Chris Masona52d9a82007-08-27 16:49:44 -0400175 if (err)
176 goto failed;
177 }
178
Chris Masonc8b97812008-10-29 14:49:59 -0400179 /* check for reserved extents on each page, we don't want
180 * to reset the delalloc bit on things that already have
181 * extents reserved.
Chris Masona52d9a82007-08-27 16:49:44 -0400182 */
Chris Masonc8b97812008-10-29 14:49:59 -0400183 btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block);
184 for (i = 0; i < num_pages; i++) {
185 struct page *p = pages[i];
186 SetPageUptodate(p);
187 ClearPageChecked(p);
188 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400189 }
190 if (end_pos > isize) {
191 i_size_write(inode, end_pos);
192 btrfs_update_inode(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400193 }
194failed:
Chris Mason017e5362008-07-28 15:32:51 -0400195 err = btrfs_end_transaction(trans, root);
Chris Masona52d9a82007-08-27 16:49:44 -0400196out_unlock:
Chris Masond1310b22008-01-24 16:13:08 -0500197 unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400198 return err;
199}
200
Chris Masond352ac62008-09-29 15:18:18 -0400201/*
202 * this drops all the extents in the cache that intersect the range
203 * [start, end]. Existing extents are split as required.
204 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400205int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
206 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400207{
208 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400209 struct extent_map *split = NULL;
210 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400211 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500212 u64 len = end - start + 1;
Chris Mason3b951512008-04-17 11:29:12 -0400213 int ret;
214 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400215 unsigned long flags;
Chris Masonc8b97812008-10-29 14:49:59 -0400216 int compressed = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400217
Chris Masone6dcd2d2008-07-17 12:53:50 -0400218 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400219 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500220 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400221 testend = 0;
222 }
Chris Masona52d9a82007-08-27 16:49:44 -0400223 while(1) {
Chris Mason3b951512008-04-17 11:29:12 -0400224 if (!split)
225 split = alloc_extent_map(GFP_NOFS);
226 if (!split2)
227 split2 = alloc_extent_map(GFP_NOFS);
228
Chris Masond1310b22008-01-24 16:13:08 -0500229 spin_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500230 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500231 if (!em) {
232 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400233 break;
Chris Masond1310b22008-01-24 16:13:08 -0500234 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400235 flags = em->flags;
236 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
237 spin_unlock(&em_tree->lock);
238 if (em->start <= start &&
239 (!testend || em->start + em->len >= start + len)) {
240 free_extent_map(em);
241 break;
242 }
243 if (start < em->start) {
244 len = em->start - start;
245 } else {
246 len = start + len - (em->start + em->len);
247 start = em->start + em->len;
248 }
249 free_extent_map(em);
250 continue;
251 }
Chris Masonc8b97812008-10-29 14:49:59 -0400252 compressed = test_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
Chris Mason3ce7e672008-07-31 15:42:54 -0400253 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400254 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400255
256 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
257 em->start < start) {
258 split->start = em->start;
259 split->len = start - em->start;
260 split->block_start = em->block_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400261
262 if (compressed)
263 split->block_len = em->block_len;
264 else
265 split->block_len = split->len;
266
Chris Mason3b951512008-04-17 11:29:12 -0400267 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400268 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400269 ret = add_extent_mapping(em_tree, split);
270 BUG_ON(ret);
271 free_extent_map(split);
272 split = split2;
273 split2 = NULL;
274 }
275 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
276 testend && em->start + em->len > start + len) {
277 u64 diff = start + len - em->start;
278
279 split->start = start + len;
280 split->len = em->start + em->len - (start + len);
281 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400282 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400283
Chris Masonc8b97812008-10-29 14:49:59 -0400284 if (compressed) {
285 split->block_len = em->block_len;
286 split->block_start = em->block_start;
287 } else {
288 split->block_len = split->len;
289 split->block_start = em->block_start + diff;
290 }
Chris Mason3b951512008-04-17 11:29:12 -0400291
292 ret = add_extent_mapping(em_tree, split);
293 BUG_ON(ret);
294 free_extent_map(split);
295 split = NULL;
296 }
Chris Masond1310b22008-01-24 16:13:08 -0500297 spin_unlock(&em_tree->lock);
298
Chris Masona52d9a82007-08-27 16:49:44 -0400299 /* once for us */
300 free_extent_map(em);
301 /* once for the tree*/
302 free_extent_map(em);
303 }
Chris Mason3b951512008-04-17 11:29:12 -0400304 if (split)
305 free_extent_map(split);
306 if (split2)
307 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400308 return 0;
309}
310
Chris Mason5f564062008-01-22 16:47:59 -0500311int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
312{
313 return 0;
314#if 0
315 struct btrfs_path *path;
316 struct btrfs_key found_key;
317 struct extent_buffer *leaf;
318 struct btrfs_file_extent_item *extent;
319 u64 last_offset = 0;
320 int nritems;
321 int slot;
322 int found_type;
323 int ret;
324 int err = 0;
325 u64 extent_end = 0;
326
327 path = btrfs_alloc_path();
328 ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
329 last_offset, 0);
330 while(1) {
331 nritems = btrfs_header_nritems(path->nodes[0]);
332 if (path->slots[0] >= nritems) {
333 ret = btrfs_next_leaf(root, path);
334 if (ret)
335 goto out;
336 nritems = btrfs_header_nritems(path->nodes[0]);
337 }
338 slot = path->slots[0];
339 leaf = path->nodes[0];
340 btrfs_item_key_to_cpu(leaf, &found_key, slot);
341 if (found_key.objectid != inode->i_ino)
342 break;
343 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
344 goto out;
345
Chris Mason90692182008-02-08 13:49:28 -0500346 if (found_key.offset < last_offset) {
Chris Mason5f564062008-01-22 16:47:59 -0500347 WARN_ON(1);
348 btrfs_print_leaf(root, leaf);
349 printk("inode %lu found offset %Lu expected %Lu\n",
350 inode->i_ino, found_key.offset, last_offset);
351 err = 1;
352 goto out;
353 }
354 extent = btrfs_item_ptr(leaf, slot,
355 struct btrfs_file_extent_item);
356 found_type = btrfs_file_extent_type(leaf, extent);
357 if (found_type == BTRFS_FILE_EXTENT_REG) {
358 extent_end = found_key.offset +
359 btrfs_file_extent_num_bytes(leaf, extent);
360 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
361 struct btrfs_item *item;
362 item = btrfs_item_nr(leaf, slot);
363 extent_end = found_key.offset +
Chris Masonc8b97812008-10-29 14:49:59 -0400364 btrfs_file_extent_inline_len(leaf, extent);
Chris Mason5f564062008-01-22 16:47:59 -0500365 extent_end = (extent_end + root->sectorsize - 1) &
366 ~((u64)root->sectorsize -1 );
367 }
368 last_offset = extent_end;
369 path->slots[0]++;
370 }
Chris Mason90692182008-02-08 13:49:28 -0500371 if (0 && last_offset < inode->i_size) {
Chris Mason5f564062008-01-22 16:47:59 -0500372 WARN_ON(1);
373 btrfs_print_leaf(root, leaf);
374 printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
375 last_offset, inode->i_size);
376 err = 1;
377
378 }
379out:
380 btrfs_free_path(path);
381 return err;
382#endif
383}
384
Chris Mason39279cc2007-06-12 06:35:45 -0400385/*
386 * this is very complex, but the basic idea is to drop all extents
387 * in the range start - end. hint_block is filled in with a block number
388 * that would be a good hint to the block allocator for this file.
389 *
390 * If an extent intersects the range but is not entirely inside the range
391 * it is either truncated or split. Anything entirely inside the range
392 * is deleted from the tree.
Chris Masond352ac62008-09-29 15:18:18 -0400393 *
394 * inline_limit is used to tell this code which offsets in the file to keep
395 * if they contain inline extents.
Chris Mason39279cc2007-06-12 06:35:45 -0400396 */
Chris Masona1b32a52008-09-05 16:09:51 -0400397int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400398 struct btrfs_root *root, struct inode *inode,
Chris Mason00f5c792007-11-30 10:09:33 -0500399 u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
Chris Mason39279cc2007-06-12 06:35:45 -0400400{
Chris Mason39279cc2007-06-12 06:35:45 -0400401 u64 extent_end = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400402 u64 search_start = start;
Zheng Yan31840ae2008-09-23 13:14:14 -0400403 u64 leaf_start;
Chris Masonc8b97812008-10-29 14:49:59 -0400404 u64 ram_bytes = 0;
405 u8 compression = 0;
406 u8 encryption = 0;
407 u16 other_encoding = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400408 u64 root_gen;
409 u64 root_owner;
Chris Mason00f5c792007-11-30 10:09:33 -0500410 struct extent_buffer *leaf;
411 struct btrfs_file_extent_item *extent;
412 struct btrfs_path *path;
413 struct btrfs_key key;
414 struct btrfs_file_extent_item old;
415 int keep;
416 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400417 int bookend;
418 int found_type;
419 int found_extent;
420 int found_inline;
Chris Masonccd467d2007-06-28 15:57:36 -0400421 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500422 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400423
Chris Masonc8b97812008-10-29 14:49:59 -0400424 inline_limit = 0;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400425 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400426
Chris Mason39279cc2007-06-12 06:35:45 -0400427 path = btrfs_alloc_path();
428 if (!path)
429 return -ENOMEM;
430 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400431 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400432 btrfs_release_path(root, path);
433 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
434 search_start, -1);
435 if (ret < 0)
436 goto out;
437 if (ret > 0) {
438 if (path->slots[0] == 0) {
439 ret = 0;
440 goto out;
441 }
442 path->slots[0]--;
443 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400444next_slot:
Chris Mason39279cc2007-06-12 06:35:45 -0400445 keep = 0;
446 bookend = 0;
447 found_extent = 0;
448 found_inline = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400449 leaf_start = 0;
450 root_gen = 0;
451 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400452 extent = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400453 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -0400454 slot = path->slots[0];
Chris Mason8c2383c2007-06-18 09:57:58 -0400455 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400456 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan72610092008-02-05 15:40:36 -0500457 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
458 key.offset >= end) {
Chris Mason39279cc2007-06-12 06:35:45 -0400459 goto out;
460 }
Yan72610092008-02-05 15:40:36 -0500461 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
462 key.objectid != inode->i_ino) {
Chris Mason39279cc2007-06-12 06:35:45 -0400463 goto out;
464 }
Chris Masonccd467d2007-06-28 15:57:36 -0400465 if (recow) {
466 search_start = key.offset;
467 continue;
468 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400469 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
470 extent = btrfs_item_ptr(leaf, slot,
471 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400472 found_type = btrfs_file_extent_type(leaf, extent);
Chris Masonc8b97812008-10-29 14:49:59 -0400473 compression = btrfs_file_extent_compression(leaf,
474 extent);
475 encryption = btrfs_file_extent_encryption(leaf,
476 extent);
477 other_encoding = btrfs_file_extent_other_encoding(leaf,
478 extent);
Chris Mason8c2383c2007-06-18 09:57:58 -0400479 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500480 extent_end =
481 btrfs_file_extent_disk_bytenr(leaf,
482 extent);
483 if (extent_end)
484 *hint_byte = extent_end;
485
Chris Mason8c2383c2007-06-18 09:57:58 -0400486 extent_end = key.offset +
Chris Masondb945352007-10-15 16:15:53 -0400487 btrfs_file_extent_num_bytes(leaf, extent);
Chris Masonc8b97812008-10-29 14:49:59 -0400488 ram_bytes = btrfs_file_extent_ram_bytes(leaf,
489 extent);
Chris Mason8c2383c2007-06-18 09:57:58 -0400490 found_extent = 1;
491 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
492 found_inline = 1;
493 extent_end = key.offset +
Chris Masonc8b97812008-10-29 14:49:59 -0400494 btrfs_file_extent_inline_len(leaf, extent);
Chris Mason8c2383c2007-06-18 09:57:58 -0400495 }
496 } else {
497 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400498 }
499
500 /* we found nothing we can drop */
Chris Mason8c2383c2007-06-18 09:57:58 -0400501 if ((!found_extent && !found_inline) ||
502 search_start >= extent_end) {
503 int nextret;
504 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400505 nritems = btrfs_header_nritems(leaf);
Chris Mason8c2383c2007-06-18 09:57:58 -0400506 if (slot >= nritems - 1) {
507 nextret = btrfs_next_leaf(root, path);
508 if (nextret)
509 goto out;
Chris Masonccd467d2007-06-28 15:57:36 -0400510 recow = 1;
Chris Mason8c2383c2007-06-18 09:57:58 -0400511 } else {
512 path->slots[0]++;
513 }
514 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400515 }
516
Chris Mason39279cc2007-06-12 06:35:45 -0400517 if (found_inline) {
Chris Mason5f39d392007-10-15 16:14:19 -0400518 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400519 search_start = (extent_end + mask) & ~mask;
520 } else
521 search_start = extent_end;
Chris Masonc8b97812008-10-29 14:49:59 -0400522
523 if (end <= extent_end && start >= key.offset && found_inline)
Chris Mason179e29e2007-11-01 11:28:41 -0400524 *hint_byte = EXTENT_MAP_INLINE;
Zheng Yan31840ae2008-09-23 13:14:14 -0400525
526 if (found_extent) {
527 read_extent_buffer(leaf, &old, (unsigned long)extent,
528 sizeof(old));
529 root_gen = btrfs_header_generation(leaf);
530 root_owner = btrfs_header_owner(leaf);
531 leaf_start = leaf->start;
532 }
533
Chris Mason39279cc2007-06-12 06:35:45 -0400534 if (end < extent_end && end >= key.offset) {
Chris Mason179e29e2007-11-01 11:28:41 -0400535 bookend = 1;
Yan0181e582008-01-30 14:39:54 -0500536 if (found_inline && start <= key.offset)
Chris Mason179e29e2007-11-01 11:28:41 -0400537 keep = 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400538 }
Chris Mason39279cc2007-06-12 06:35:45 -0400539 /* truncate existing extent */
540 if (start > key.offset) {
541 u64 new_num;
542 u64 old_num;
543 keep = 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400544 WARN_ON(start & (root->sectorsize - 1));
Chris Mason39279cc2007-06-12 06:35:45 -0400545 if (found_extent) {
Chris Masondb945352007-10-15 16:15:53 -0400546 new_num = start - key.offset;
547 old_num = btrfs_file_extent_num_bytes(leaf,
548 extent);
549 *hint_byte =
550 btrfs_file_extent_disk_bytenr(leaf,
551 extent);
552 if (btrfs_file_extent_disk_bytenr(leaf,
553 extent)) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400554 inode_sub_bytes(inode, old_num -
555 new_num);
Chris Mason39279cc2007-06-12 06:35:45 -0400556 }
Chris Masondb945352007-10-15 16:15:53 -0400557 btrfs_set_file_extent_num_bytes(leaf, extent,
558 new_num);
Chris Mason5f39d392007-10-15 16:14:19 -0400559 btrfs_mark_buffer_dirty(leaf);
Chris Mason00f5c792007-11-30 10:09:33 -0500560 } else if (key.offset < inline_limit &&
561 (end > extent_end) &&
562 (inline_limit < extent_end)) {
Chris Mason3326d1b2007-10-15 16:18:25 -0400563 u32 new_size;
564 new_size = btrfs_file_extent_calc_inline_size(
Chris Mason00f5c792007-11-30 10:09:33 -0500565 inline_limit - key.offset);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400566 inode_sub_bytes(inode, extent_end -
567 inline_limit);
Chris Mason3326d1b2007-10-15 16:18:25 -0400568 btrfs_truncate_item(trans, root, path,
Chris Mason179e29e2007-11-01 11:28:41 -0400569 new_size, 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400570 }
571 }
572 /* delete the entire extent */
573 if (!keep) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400574 if (found_inline)
575 inode_sub_bytes(inode, extent_end -
576 key.offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400577 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400578 /* TODO update progress marker and return */
Chris Mason39279cc2007-06-12 06:35:45 -0400579 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400580 extent = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400581 btrfs_release_path(root, path);
582 /* the extent will be freed later */
Chris Mason39279cc2007-06-12 06:35:45 -0400583 }
Yan0181e582008-01-30 14:39:54 -0500584 if (bookend && found_inline && start <= key.offset) {
Chris Mason179e29e2007-11-01 11:28:41 -0400585 u32 new_size;
586 new_size = btrfs_file_extent_calc_inline_size(
Yan0181e582008-01-30 14:39:54 -0500587 extent_end - end);
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400588 inode_sub_bytes(inode, end - key.offset);
Zheng Yan31840ae2008-09-23 13:14:14 -0400589 ret = btrfs_truncate_item(trans, root, path,
590 new_size, 0);
591 BUG_ON(ret);
Chris Mason179e29e2007-11-01 11:28:41 -0400592 }
Chris Mason39279cc2007-06-12 06:35:45 -0400593 /* create bookend, splitting the extent in two */
594 if (bookend && found_extent) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400595 u64 disk_bytenr;
Chris Mason39279cc2007-06-12 06:35:45 -0400596 struct btrfs_key ins;
597 ins.objectid = inode->i_ino;
598 ins.offset = end;
Chris Mason39279cc2007-06-12 06:35:45 -0400599 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -0400600 btrfs_release_path(root, path);
601 ret = btrfs_insert_empty_item(trans, root, path, &ins,
602 sizeof(*extent));
Zheng Yan31840ae2008-09-23 13:14:14 -0400603 BUG_ON(ret);
Chris Mason8c2383c2007-06-18 09:57:58 -0400604
Chris Mason5f39d392007-10-15 16:14:19 -0400605 leaf = path->nodes[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400606 extent = btrfs_item_ptr(leaf, path->slots[0],
607 struct btrfs_file_extent_item);
608 write_extent_buffer(leaf, &old,
609 (unsigned long)extent, sizeof(old));
Chris Mason39279cc2007-06-12 06:35:45 -0400610
Chris Masonc8b97812008-10-29 14:49:59 -0400611 btrfs_set_file_extent_compression(leaf, extent,
612 compression);
613 btrfs_set_file_extent_encryption(leaf, extent,
614 encryption);
615 btrfs_set_file_extent_other_encoding(leaf, extent,
616 other_encoding);
Chris Mason5f39d392007-10-15 16:14:19 -0400617 btrfs_set_file_extent_offset(leaf, extent,
Chris Masondb945352007-10-15 16:15:53 -0400618 le64_to_cpu(old.offset) + end - key.offset);
619 WARN_ON(le64_to_cpu(old.num_bytes) <
620 (extent_end - end));
621 btrfs_set_file_extent_num_bytes(leaf, extent,
622 extent_end - end);
Chris Masonc8b97812008-10-29 14:49:59 -0400623
624 /*
625 * set the ram bytes to the size of the full extent
626 * before splitting. This is a worst case flag,
627 * but its the best we can do because we don't know
628 * how splitting affects compression
629 */
630 btrfs_set_file_extent_ram_bytes(leaf, extent,
631 ram_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -0400632 btrfs_set_file_extent_type(leaf, extent,
Chris Mason39279cc2007-06-12 06:35:45 -0400633 BTRFS_FILE_EXTENT_REG);
Chris Masondb945352007-10-15 16:15:53 -0400634
Chris Mason39279cc2007-06-12 06:35:45 -0400635 btrfs_mark_buffer_dirty(path->nodes[0]);
Zheng Yan31840ae2008-09-23 13:14:14 -0400636
637 disk_bytenr = le64_to_cpu(old.disk_bytenr);
638 if (disk_bytenr != 0) {
639 ret = btrfs_inc_extent_ref(trans, root,
640 disk_bytenr,
641 le64_to_cpu(old.disk_num_bytes),
642 leaf->start,
643 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400644 trans->transid, ins.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400645 BUG_ON(ret);
646 }
647 btrfs_release_path(root, path);
648 if (disk_bytenr != 0) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400649 inode_add_bytes(inode, extent_end - end);
Chris Mason39279cc2007-06-12 06:35:45 -0400650 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400651 }
652
653 if (found_extent && !keep) {
654 u64 disk_bytenr = le64_to_cpu(old.disk_bytenr);
655
656 if (disk_bytenr != 0) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400657 inode_sub_bytes(inode,
658 le64_to_cpu(old.num_bytes));
Zheng Yan31840ae2008-09-23 13:14:14 -0400659 ret = btrfs_free_extent(trans, root,
660 disk_bytenr,
661 le64_to_cpu(old.disk_num_bytes),
662 leaf_start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400663 root_gen, key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400664 BUG_ON(ret);
665 *hint_byte = disk_bytenr;
666 }
667 }
668
669 if (search_start >= end) {
Chris Mason39279cc2007-06-12 06:35:45 -0400670 ret = 0;
671 goto out;
672 }
673 }
674out:
675 btrfs_free_path(path);
Chris Mason90692182008-02-08 13:49:28 -0500676 btrfs_check_file(root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400677 return ret;
678}
679
680/*
Chris Masond352ac62008-09-29 15:18:18 -0400681 * this gets pages into the page cache and locks them down, it also properly
682 * waits for data=ordered extents to finish before allowing the pages to be
683 * modified.
Chris Mason39279cc2007-06-12 06:35:45 -0400684 */
Chris Masona1b32a52008-09-05 16:09:51 -0400685static int noinline prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500686 struct page **pages, size_t num_pages,
687 loff_t pos, unsigned long first_index,
688 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400689{
690 int i;
691 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500692 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400693 int err = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400694 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400695 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400696
Chris Mason5f39d392007-10-15 16:14:19 -0400697 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400698 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400699
700 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400701again:
Chris Mason39279cc2007-06-12 06:35:45 -0400702 for (i = 0; i < num_pages; i++) {
703 pages[i] = grab_cache_page(inode->i_mapping, index + i);
704 if (!pages[i]) {
705 err = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -0400706 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400707 }
Chris Masonccd467d2007-06-28 15:57:36 -0400708 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400709 }
Chris Mason07627042008-02-19 11:29:24 -0500710 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400711 struct btrfs_ordered_extent *ordered;
Chris Masond99cb302008-02-19 12:55:05 -0500712 lock_extent(&BTRFS_I(inode)->io_tree,
713 start_pos, last_pos - 1, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400714 ordered = btrfs_lookup_first_ordered_extent(inode, last_pos -1);
715 if (ordered &&
716 ordered->file_offset + ordered->len > start_pos &&
717 ordered->file_offset < last_pos) {
718 btrfs_put_ordered_extent(ordered);
719 unlock_extent(&BTRFS_I(inode)->io_tree,
720 start_pos, last_pos - 1, GFP_NOFS);
721 for (i = 0; i < num_pages; i++) {
722 unlock_page(pages[i]);
723 page_cache_release(pages[i]);
724 }
725 btrfs_wait_ordered_range(inode, start_pos,
726 last_pos - start_pos);
727 goto again;
728 }
729 if (ordered)
730 btrfs_put_ordered_extent(ordered);
731
Chris Mason07627042008-02-19 11:29:24 -0500732 clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
733 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
734 GFP_NOFS);
Chris Masond99cb302008-02-19 12:55:05 -0500735 unlock_extent(&BTRFS_I(inode)->io_tree,
736 start_pos, last_pos - 1, GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500737 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400738 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400739 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400740 set_page_extent_mapped(pages[i]);
741 WARN_ON(!PageLocked(pages[i]));
742 }
Chris Mason39279cc2007-06-12 06:35:45 -0400743 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400744}
745
746static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
747 size_t count, loff_t *ppos)
748{
749 loff_t pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400750 loff_t start_pos;
751 ssize_t num_written = 0;
752 ssize_t err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400753 int ret = 0;
Chris Mason6da6aba2007-12-18 16:15:09 -0500754 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400755 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8c2383c2007-06-18 09:57:58 -0400756 struct page **pages = NULL;
757 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400758 struct page *pinned[2];
759 unsigned long first_index;
760 unsigned long last_index;
Chris Masoncb843a62008-10-03 12:30:02 -0400761 int will_write;
762
763 will_write = ((file->f_flags & O_SYNC) || IS_SYNC(inode) ||
764 (file->f_flags & O_DIRECT));
Chris Mason8c2383c2007-06-18 09:57:58 -0400765
766 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
767 PAGE_CACHE_SIZE / (sizeof(struct page *)));
Chris Mason39279cc2007-06-12 06:35:45 -0400768 pinned[0] = NULL;
769 pinned[1] = NULL;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400770
Chris Mason39279cc2007-06-12 06:35:45 -0400771 pos = *ppos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400772 start_pos = pos;
773
Chris Mason39279cc2007-06-12 06:35:45 -0400774 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
775 current->backing_dev_info = inode->i_mapping->backing_dev_info;
776 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
777 if (err)
Chris Mason1832a6d2007-12-21 16:27:21 -0500778 goto out_nolock;
Chris Mason39279cc2007-06-12 06:35:45 -0400779 if (count == 0)
Chris Mason1832a6d2007-12-21 16:27:21 -0500780 goto out_nolock;
Chris Mason2b1f55b2008-09-24 11:48:04 -0400781
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400782 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400783 if (err)
Chris Mason1832a6d2007-12-21 16:27:21 -0500784 goto out_nolock;
Chris Mason39279cc2007-06-12 06:35:45 -0400785 file_update_time(file);
786
Chris Mason8c2383c2007-06-18 09:57:58 -0400787 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -0400788
789 mutex_lock(&inode->i_mutex);
790 first_index = pos >> PAGE_CACHE_SHIFT;
791 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
792
793 /*
Chris Mason409c6112008-04-22 09:24:20 -0400794 * if this is a nodatasum mount, force summing off for the inode
795 * all the time. That way a later mount with summing on won't
796 * get confused
797 */
798 if (btrfs_test_opt(root, NODATASUM))
799 btrfs_set_flag(inode, NODATASUM);
800
801 /*
Chris Mason39279cc2007-06-12 06:35:45 -0400802 * there are lots of better ways to do this, but this code
803 * makes sure the first and last page in the file range are
804 * up to date and ready for cow
805 */
806 if ((pos & (PAGE_CACHE_SIZE - 1))) {
807 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
808 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400809 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400810 BUG_ON(ret);
811 wait_on_page_locked(pinned[0]);
812 } else {
813 unlock_page(pinned[0]);
814 }
815 }
816 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
817 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
818 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400819 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400820 BUG_ON(ret);
821 wait_on_page_locked(pinned[1]);
822 } else {
823 unlock_page(pinned[1]);
824 }
825 }
826
Chris Mason39279cc2007-06-12 06:35:45 -0400827 while(count > 0) {
828 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Chris Mason11bd1432007-06-22 14:16:24 -0400829 size_t write_bytes = min(count, nrptrs *
830 (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400831 offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400832 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
833 PAGE_CACHE_SHIFT;
834
Chris Mason8c2383c2007-06-18 09:57:58 -0400835 WARN_ON(num_pages > nrptrs);
Chris Mason39279cc2007-06-12 06:35:45 -0400836 memset(pages, 0, sizeof(pages));
Chris Mason1832a6d2007-12-21 16:27:21 -0500837
Chris Mason1832a6d2007-12-21 16:27:21 -0500838 ret = btrfs_check_free_space(root, write_bytes, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -0500839 if (ret)
840 goto out;
841
Chris Mason39279cc2007-06-12 06:35:45 -0400842 ret = prepare_pages(root, file, pages, num_pages,
843 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400844 write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400845 if (ret)
846 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -0400847
Chris Mason39279cc2007-06-12 06:35:45 -0400848 ret = btrfs_copy_from_user(pos, num_pages,
849 write_bytes, pages, buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400850 if (ret) {
851 btrfs_drop_pages(pages, num_pages);
852 goto out;
853 }
Chris Mason39279cc2007-06-12 06:35:45 -0400854
855 ret = dirty_and_release_pages(NULL, root, file, pages,
856 num_pages, pos, write_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -0400857 btrfs_drop_pages(pages, num_pages);
Chris Mason54aa1f42007-06-22 14:16:25 -0400858 if (ret)
859 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -0400860
Chris Masoncb843a62008-10-03 12:30:02 -0400861 if (will_write) {
862 btrfs_fdatawrite_range(inode->i_mapping, pos,
863 pos + write_bytes - 1,
864 WB_SYNC_NONE);
865 } else {
866 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
867 num_pages);
868 if (num_pages <
869 (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
870 btrfs_btree_balance_dirty(root, 1);
871 btrfs_throttle(root);
872 }
873
Chris Mason39279cc2007-06-12 06:35:45 -0400874 buf += write_bytes;
875 count -= write_bytes;
876 pos += write_bytes;
877 num_written += write_bytes;
878
Chris Mason39279cc2007-06-12 06:35:45 -0400879 cond_resched();
880 }
Chris Mason39279cc2007-06-12 06:35:45 -0400881out:
Chris Mason1832a6d2007-12-21 16:27:21 -0500882 mutex_unlock(&inode->i_mutex);
Chris Mason5b92ee72008-01-03 13:46:11 -0500883
Chris Mason1832a6d2007-12-21 16:27:21 -0500884out_nolock:
Chris Mason8c2383c2007-06-18 09:57:58 -0400885 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -0400886 if (pinned[0])
887 page_cache_release(pinned[0]);
888 if (pinned[1])
889 page_cache_release(pinned[1]);
890 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400891
Chris Masoncb843a62008-10-03 12:30:02 -0400892 if (num_written > 0 && will_write) {
Chris Masone02119d2008-09-05 16:13:11 -0400893 struct btrfs_trans_handle *trans;
894
Chris Masoncb843a62008-10-03 12:30:02 -0400895 err = btrfs_wait_ordered_range(inode, start_pos, num_written);
896 if (err)
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400897 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -0400898
Chris Masoncb843a62008-10-03 12:30:02 -0400899 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) {
900 trans = btrfs_start_transaction(root, 1);
901 ret = btrfs_log_dentry_safe(trans, root,
902 file->f_dentry);
903 if (ret == 0) {
904 btrfs_sync_log(trans, root);
905 btrfs_end_transaction(trans, root);
906 } else {
907 btrfs_commit_transaction(trans, root);
908 }
Chris Masone02119d2008-09-05 16:13:11 -0400909 }
Chris Masoncb843a62008-10-03 12:30:02 -0400910 if (file->f_flags & O_DIRECT) {
911 invalidate_mapping_pages(inode->i_mapping,
912 start_pos >> PAGE_CACHE_SHIFT,
913 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
914 }
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400915 }
Chris Mason39279cc2007-06-12 06:35:45 -0400916 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -0400917 return num_written ? num_written : err;
918}
919
Sage Weil6bf13c02008-06-10 10:07:39 -0400920int btrfs_release_file(struct inode * inode, struct file * filp)
Mingminge1b81e62008-05-27 10:55:43 -0400921{
Sage Weil6bf13c02008-06-10 10:07:39 -0400922 if (filp->private_data)
923 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -0400924 return 0;
925}
926
Chris Masond352ac62008-09-29 15:18:18 -0400927/*
928 * fsync call for both files and directories. This logs the inode into
929 * the tree log instead of forcing full commits whenever possible.
930 *
931 * It needs to call filemap_fdatawait so that all ordered extent updates are
932 * in the metadata btree are up to date for copying to the log.
933 *
934 * It drops the inode mutex before doing the tree log commit. This is an
935 * important optimization for directories because holding the mutex prevents
936 * new operations on the dir while we write to disk.
937 */
Chris Masone02119d2008-09-05 16:13:11 -0400938int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -0400939{
940 struct inode *inode = dentry->d_inode;
941 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400942 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400943 struct btrfs_trans_handle *trans;
944
945 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400946 * check the transaction that last modified this inode
947 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -0400948 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400949 if (!BTRFS_I(inode)->last_trans)
950 goto out;
Chris Masona2135012008-06-25 16:01:30 -0400951
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400952 mutex_lock(&root->fs_info->trans_mutex);
953 if (BTRFS_I(inode)->last_trans <=
954 root->fs_info->last_trans_committed) {
955 BTRFS_I(inode)->last_trans = 0;
956 mutex_unlock(&root->fs_info->trans_mutex);
957 goto out;
958 }
959 mutex_unlock(&root->fs_info->trans_mutex);
960
Chris Mason49eb7e42008-09-11 15:53:12 -0400961 root->fs_info->tree_log_batch++;
Chris Masone02119d2008-09-05 16:13:11 -0400962 filemap_fdatawait(inode->i_mapping);
Chris Mason49eb7e42008-09-11 15:53:12 -0400963 root->fs_info->tree_log_batch++;
Chris Masone02119d2008-09-05 16:13:11 -0400964
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400965 /*
Chris Masona52d9a82007-08-27 16:49:44 -0400966 * ok we haven't committed the transaction yet, lets do a commit
967 */
Sage Weil6bf13c02008-06-10 10:07:39 -0400968 if (file->private_data)
969 btrfs_ioctl_trans_end(file);
970
Chris Mason39279cc2007-06-12 06:35:45 -0400971 trans = btrfs_start_transaction(root, 1);
972 if (!trans) {
973 ret = -ENOMEM;
974 goto out;
975 }
Chris Masone02119d2008-09-05 16:13:11 -0400976
977 ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
Chris Mason49eb7e42008-09-11 15:53:12 -0400978 if (ret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -0400979 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -0400980 }
981
982 /* we've logged all the items and now have a consistent
983 * version of the file in the log. It is possible that
984 * someone will come in and modify the file, but that's
985 * fine because the log is consistent on disk, and we
986 * have references to all of the file's extents
987 *
988 * It is possible that someone will come in and log the
989 * file again, but that will end up using the synchronization
990 * inside btrfs_sync_log to keep things safe.
991 */
992 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
993
Chris Masone02119d2008-09-05 16:13:11 -0400994 if (ret > 0) {
995 ret = btrfs_commit_transaction(trans, root);
996 } else {
997 btrfs_sync_log(trans, root);
998 ret = btrfs_end_transaction(trans, root);
999 }
Chris Mason49eb7e42008-09-11 15:53:12 -04001000 mutex_lock(&file->f_dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001001out:
1002 return ret > 0 ? EIO : ret;
1003}
1004
Chris Mason9ebefb182007-06-15 13:50:00 -04001005static struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001006 .fault = filemap_fault,
Chris Mason9ebefb182007-06-15 13:50:00 -04001007 .page_mkwrite = btrfs_page_mkwrite,
1008};
1009
1010static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1011{
1012 vma->vm_ops = &btrfs_file_vm_ops;
1013 file_accessed(filp);
1014 return 0;
1015}
1016
Chris Mason39279cc2007-06-12 06:35:45 -04001017struct file_operations btrfs_file_operations = {
1018 .llseek = generic_file_llseek,
1019 .read = do_sync_read,
Chris Mason9ebefb182007-06-15 13:50:00 -04001020 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001021 .splice_read = generic_file_splice_read,
Chris Mason39279cc2007-06-12 06:35:45 -04001022 .write = btrfs_file_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001023 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001024 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001025 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001026 .fsync = btrfs_sync_file,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001027 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001028#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001029 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001030#endif
1031};