blob: 48a702d41c8c8370778679a582df4e7412b89cfd [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 Masona1b32a52008-09-05 16:09:51 -040044static int noinline btrfs_copy_from_user(loff_t pos, int num_pages,
45 int write_bytes,
46 struct page **prepared_pages,
47 const char __user * buf)
Chris Mason39279cc2007-06-12 06:35:45 -040048{
49 long page_fault = 0;
50 int i;
51 int offset = pos & (PAGE_CACHE_SIZE - 1);
52
53 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
54 size_t count = min_t(size_t,
55 PAGE_CACHE_SIZE - offset, write_bytes);
56 struct page *page = prepared_pages[i];
57 fault_in_pages_readable(buf, count);
58
59 /* Copy data from userspace to the current page */
60 kmap(page);
61 page_fault = __copy_from_user(page_address(page) + offset,
62 buf, count);
63 /* Flush processor's dcache for this page */
64 flush_dcache_page(page);
65 kunmap(page);
66 buf += count;
67 write_bytes -= count;
68
69 if (page_fault)
70 break;
71 }
72 return page_fault ? -EFAULT : 0;
73}
74
Chris Masona1b32a52008-09-05 16:09:51 -040075static void noinline btrfs_drop_pages(struct page **pages, size_t num_pages)
Chris Mason39279cc2007-06-12 06:35:45 -040076{
77 size_t i;
78 for (i = 0; i < num_pages; i++) {
79 if (!pages[i])
80 break;
Chris Mason4a096752008-07-21 10:29:44 -040081 ClearPageChecked(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -040082 unlock_page(pages[i]);
83 mark_page_accessed(pages[i]);
84 page_cache_release(pages[i]);
85 }
86}
87
Chris Mason98ed5172008-01-03 10:01:48 -050088static int noinline insert_inline_extent(struct btrfs_trans_handle *trans,
Chris Masona52d9a82007-08-27 16:49:44 -040089 struct btrfs_root *root, struct inode *inode,
Chris Mason3326d1b2007-10-15 16:18:25 -040090 u64 offset, size_t size,
91 struct page **pages, size_t page_offset,
92 int num_pages)
Chris Mason54aa1f42007-06-22 14:16:25 -040093{
94 struct btrfs_key key;
95 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -040096 struct extent_buffer *leaf;
97 char *kaddr;
98 unsigned long ptr;
Chris Mason54aa1f42007-06-22 14:16:25 -040099 struct btrfs_file_extent_item *ei;
Chris Mason3326d1b2007-10-15 16:18:25 -0400100 struct page *page;
Chris Mason54aa1f42007-06-22 14:16:25 -0400101 u32 datasize;
102 int err = 0;
103 int ret;
Chris Mason3326d1b2007-10-15 16:18:25 -0400104 int i;
105 ssize_t cur_size;
Chris Mason54aa1f42007-06-22 14:16:25 -0400106
107 path = btrfs_alloc_path();
108 if (!path)
109 return -ENOMEM;
110
Chris Mason54aa1f42007-06-22 14:16:25 -0400111 btrfs_set_trans_block_group(trans, inode);
112
113 key.objectid = inode->i_ino;
114 key.offset = offset;
Chris Mason54aa1f42007-06-22 14:16:25 -0400115 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
Chris Mason54aa1f42007-06-22 14:16:25 -0400116
Chris Mason3326d1b2007-10-15 16:18:25 -0400117 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
118 if (ret < 0) {
Chris Mason54aa1f42007-06-22 14:16:25 -0400119 err = ret;
120 goto fail;
121 }
Chris Mason3326d1b2007-10-15 16:18:25 -0400122 if (ret == 1) {
Chris Mason179e29e2007-11-01 11:28:41 -0400123 struct btrfs_key found_key;
124
125 if (path->slots[0] == 0)
126 goto insert;
127
Chris Mason3326d1b2007-10-15 16:18:25 -0400128 path->slots[0]--;
129 leaf = path->nodes[0];
Chris Mason179e29e2007-11-01 11:28:41 -0400130 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
131
132 if (found_key.objectid != inode->i_ino)
133 goto insert;
134
135 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
136 goto insert;
Chris Mason3326d1b2007-10-15 16:18:25 -0400137 ei = btrfs_item_ptr(leaf, path->slots[0],
138 struct btrfs_file_extent_item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400139
Chris Mason3326d1b2007-10-15 16:18:25 -0400140 if (btrfs_file_extent_type(leaf, ei) !=
141 BTRFS_FILE_EXTENT_INLINE) {
142 goto insert;
143 }
144 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
145 ret = 0;
146 }
147 if (ret == 0) {
148 u32 found_size;
Yan18f16f72007-10-25 15:42:57 -0400149 u64 found_end;
Chris Mason3326d1b2007-10-15 16:18:25 -0400150
151 leaf = path->nodes[0];
152 ei = btrfs_item_ptr(leaf, path->slots[0],
153 struct btrfs_file_extent_item);
154
155 if (btrfs_file_extent_type(leaf, ei) !=
156 BTRFS_FILE_EXTENT_INLINE) {
157 err = ret;
158 btrfs_print_leaf(root, leaf);
159 printk("found wasn't inline offset %Lu inode %lu\n",
160 offset, inode->i_ino);
161 goto fail;
162 }
Chris Mason3326d1b2007-10-15 16:18:25 -0400163 found_size = btrfs_file_extent_inline_len(leaf,
164 btrfs_item_nr(leaf, path->slots[0]));
Yan18f16f72007-10-25 15:42:57 -0400165 found_end = key.offset + found_size;
Chris Mason3326d1b2007-10-15 16:18:25 -0400166
Yan18f16f72007-10-25 15:42:57 -0400167 if (found_end < offset + size) {
Chris Mason3326d1b2007-10-15 16:18:25 -0400168 btrfs_release_path(root, path);
169 ret = btrfs_search_slot(trans, root, &key, path,
Yan18f16f72007-10-25 15:42:57 -0400170 offset + size - found_end, 1);
Chris Mason3326d1b2007-10-15 16:18:25 -0400171 BUG_ON(ret != 0);
Chris Mason179e29e2007-11-01 11:28:41 -0400172
Chris Mason3326d1b2007-10-15 16:18:25 -0400173 ret = btrfs_extend_item(trans, root, path,
Yan18f16f72007-10-25 15:42:57 -0400174 offset + size - found_end);
Chris Mason3326d1b2007-10-15 16:18:25 -0400175 if (ret) {
176 err = ret;
177 goto fail;
178 }
179 leaf = path->nodes[0];
180 ei = btrfs_item_ptr(leaf, path->slots[0],
181 struct btrfs_file_extent_item);
Chris Mason90692182008-02-08 13:49:28 -0500182 inode->i_blocks += (offset + size - found_end) >> 9;
Chris Mason3326d1b2007-10-15 16:18:25 -0400183 }
Yan18f16f72007-10-25 15:42:57 -0400184 if (found_end < offset) {
185 ptr = btrfs_file_extent_inline_start(ei) + found_size;
186 memset_extent_buffer(leaf, 0, ptr, offset - found_end);
187 }
Chris Mason3326d1b2007-10-15 16:18:25 -0400188 } else {
189insert:
190 btrfs_release_path(root, path);
Yan18f16f72007-10-25 15:42:57 -0400191 datasize = offset + size - key.offset;
Chris Mason90692182008-02-08 13:49:28 -0500192 inode->i_blocks += datasize >> 9;
Yan18f16f72007-10-25 15:42:57 -0400193 datasize = btrfs_file_extent_calc_inline_size(datasize);
Chris Mason3326d1b2007-10-15 16:18:25 -0400194 ret = btrfs_insert_empty_item(trans, root, path, &key,
195 datasize);
196 if (ret) {
197 err = ret;
198 printk("got bad ret %d\n", ret);
199 goto fail;
200 }
201 leaf = path->nodes[0];
202 ei = btrfs_item_ptr(leaf, path->slots[0],
203 struct btrfs_file_extent_item);
204 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
205 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
206 }
Yan18f16f72007-10-25 15:42:57 -0400207 ptr = btrfs_file_extent_inline_start(ei) + offset - key.offset;
Chris Mason3326d1b2007-10-15 16:18:25 -0400208
209 cur_size = size;
210 i = 0;
211 while (size > 0) {
212 page = pages[i];
213 kaddr = kmap_atomic(page, KM_USER0);
Jens Axboeae2f5412007-10-19 09:22:59 -0400214 cur_size = min_t(size_t, PAGE_CACHE_SIZE - page_offset, size);
Chris Mason3326d1b2007-10-15 16:18:25 -0400215 write_extent_buffer(leaf, kaddr + page_offset, ptr, cur_size);
216 kunmap_atomic(kaddr, KM_USER0);
217 page_offset = 0;
218 ptr += cur_size;
219 size -= cur_size;
220 if (i >= num_pages) {
221 printk("i %d num_pages %d\n", i, num_pages);
222 }
223 i++;
224 }
Chris Mason5f39d392007-10-15 16:14:19 -0400225 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400226fail:
227 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400228 return err;
229}
230
Chris Mason98ed5172008-01-03 10:01:48 -0500231static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400232 struct btrfs_root *root,
233 struct file *file,
234 struct page **pages,
235 size_t num_pages,
236 loff_t pos,
237 size_t write_bytes)
238{
Chris Mason39279cc2007-06-12 06:35:45 -0400239 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400240 int i;
Chris Mason6da6aba2007-12-18 16:15:09 -0500241 struct inode *inode = fdentry(file)->d_inode;
Chris Masond1310b22008-01-24 16:13:08 -0500242 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masondb945352007-10-15 16:15:53 -0400243 u64 hint_byte;
244 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400245 u64 start_pos;
246 u64 end_of_last_block;
247 u64 end_pos = pos + write_bytes;
Yandcfec0d2007-11-06 10:26:26 -0500248 u64 inline_size;
Chris Masonee6e6502008-07-17 12:54:40 -0400249 int did_inline = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400250 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400251
Chris Mason5f39d392007-10-15 16:14:19 -0400252 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400253 num_bytes = (write_bytes + pos - start_pos +
254 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400255
Chris Masondb945352007-10-15 16:15:53 -0400256 end_of_last_block = start_pos + num_bytes - 1;
257
Chris Masond1310b22008-01-24 16:13:08 -0500258 lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
Chris Mason37d1aee2008-07-31 10:48:37 -0400259 trans = btrfs_join_transaction(root, 1);
Chris Masona52d9a82007-08-27 16:49:44 -0400260 if (!trans) {
261 err = -ENOMEM;
262 goto out_unlock;
263 }
264 btrfs_set_trans_block_group(trans, inode);
Chris Masondb945352007-10-15 16:15:53 -0400265 hint_byte = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400266
267 if ((end_of_last_block & 4095) == 0) {
Christoph Hellwig94330632007-09-10 20:02:22 -0400268 printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
Chris Masona52d9a82007-08-27 16:49:44 -0400269 }
Chris Masond1310b22008-01-24 16:13:08 -0500270 set_extent_uptodate(io_tree, start_pos, end_of_last_block, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400271
272 /* FIXME...EIEIO, ENOSPC and more */
Chris Masona52d9a82007-08-27 16:49:44 -0400273 /* insert any holes we need to create */
Chris Mason1b1e2132008-06-25 16:01:31 -0400274 if (isize < start_pos) {
Chris Masona52d9a82007-08-27 16:49:44 -0400275 u64 last_pos_in_file;
276 u64 hole_size;
Chris Mason5f39d392007-10-15 16:14:19 -0400277 u64 mask = root->sectorsize - 1;
Chris Masona52d9a82007-08-27 16:49:44 -0400278 last_pos_in_file = (isize + mask) & ~mask;
Chris Mason1b1e2132008-06-25 16:01:31 -0400279 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400280 if (hole_size > 0) {
281 btrfs_wait_ordered_range(inode, last_pos_in_file,
282 last_pos_in_file + hole_size);
Chris Masonee6e6502008-07-17 12:54:40 -0400283 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400284 err = btrfs_drop_extents(trans, root, inode,
285 last_pos_in_file,
286 last_pos_in_file + hole_size,
Chris Mason3326d1b2007-10-15 16:18:25 -0400287 last_pos_in_file,
Chris Masondb945352007-10-15 16:15:53 -0400288 &hint_byte);
Chris Mason2bf5a722007-08-30 11:54:02 -0400289 if (err)
290 goto failed;
291
Chris Masona52d9a82007-08-27 16:49:44 -0400292 err = btrfs_insert_file_extent(trans, root,
293 inode->i_ino,
294 last_pos_in_file,
Sage Weilf2eb0a22008-05-02 14:43:14 -0400295 0, 0, hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -0500296 btrfs_drop_extent_cache(inode, last_pos_in_file,
297 last_pos_in_file + hole_size -1);
Chris Masonee6e6502008-07-17 12:54:40 -0400298 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason5f564062008-01-22 16:47:59 -0500299 btrfs_check_file(root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400300 }
Chris Masona52d9a82007-08-27 16:49:44 -0400301 if (err)
302 goto failed;
303 }
304
305 /*
306 * either allocate an extent for the new bytes or setup the key
307 * to show we are doing inline data in the extent
308 */
Chris Mason3326d1b2007-10-15 16:18:25 -0400309 inline_size = end_pos;
310 if (isize >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
Chris Mason6f568d32008-01-29 16:03:38 -0500311 inline_size > root->fs_info->max_inline ||
312 (inline_size & (root->sectorsize -1)) == 0 ||
Chris Mason3326d1b2007-10-15 16:18:25 -0400313 inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400314 /* check for reserved extents on each page, we don't want
315 * to reset the delalloc bit on things that already have
316 * extents reserved.
317 */
Chris Masonea8c2812008-08-04 23:17:27 -0400318 btrfs_set_extent_delalloc(inode, start_pos, end_of_last_block);
Chris Masona52d9a82007-08-27 16:49:44 -0400319 for (i = 0; i < num_pages; i++) {
320 struct page *p = pages[i];
321 SetPageUptodate(p);
Chris Mason247e7432008-07-17 12:53:51 -0400322 ClearPageChecked(p);
Chris Masonb888db22007-08-27 16:49:44 -0400323 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400324 }
325 } else {
Chris Mason3326d1b2007-10-15 16:18:25 -0400326 u64 aligned_end;
Chris Masonb888db22007-08-27 16:49:44 -0400327 /* step one, delete the existing extents in this range */
Chris Mason3326d1b2007-10-15 16:18:25 -0400328 aligned_end = (pos + write_bytes + root->sectorsize - 1) &
329 ~((u64)root->sectorsize - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400330 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400331 err = btrfs_drop_extents(trans, root, inode, start_pos,
Chris Mason179e29e2007-11-01 11:28:41 -0400332 aligned_end, aligned_end, &hint_byte);
Chris Mason2bf5a722007-08-30 11:54:02 -0400333 if (err)
334 goto failed;
Yandcfec0d2007-11-06 10:26:26 -0500335 if (isize > inline_size)
336 inline_size = min_t(u64, isize, aligned_end);
337 inline_size -= start_pos;
Chris Masona52d9a82007-08-27 16:49:44 -0400338 err = insert_inline_extent(trans, root, inode, start_pos,
Yandcfec0d2007-11-06 10:26:26 -0500339 inline_size, pages, 0, num_pages);
Chris Masond1310b22008-01-24 16:13:08 -0500340 btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1);
Chris Masona52d9a82007-08-27 16:49:44 -0400341 BUG_ON(err);
Chris Masonee6e6502008-07-17 12:54:40 -0400342 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masonf87f0572008-08-01 11:27:23 -0400343
344 /*
345 * an ugly way to do all the prop accounting around
346 * the page bits and mapping tags
347 */
348 set_page_writeback(pages[0]);
349 end_page_writeback(pages[0]);
Chris Masonee6e6502008-07-17 12:54:40 -0400350 did_inline = 1;
Chris Masona52d9a82007-08-27 16:49:44 -0400351 }
352 if (end_pos > isize) {
353 i_size_write(inode, end_pos);
Chris Masonee6e6502008-07-17 12:54:40 -0400354 if (did_inline)
355 BTRFS_I(inode)->disk_i_size = end_pos;
Chris Masona52d9a82007-08-27 16:49:44 -0400356 btrfs_update_inode(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400357 }
358failed:
Chris Mason017e5362008-07-28 15:32:51 -0400359 err = btrfs_end_transaction(trans, root);
Chris Masona52d9a82007-08-27 16:49:44 -0400360out_unlock:
Chris Masond1310b22008-01-24 16:13:08 -0500361 unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400362 return err;
363}
364
Chris Masona1b32a52008-09-05 16:09:51 -0400365int noinline btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
Chris Masona52d9a82007-08-27 16:49:44 -0400366{
367 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400368 struct extent_map *split = NULL;
369 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400370 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500371 u64 len = end - start + 1;
Chris Mason3b951512008-04-17 11:29:12 -0400372 int ret;
373 int testend = 1;
Chris Masona52d9a82007-08-27 16:49:44 -0400374
Chris Masone6dcd2d2008-07-17 12:53:50 -0400375 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400376 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500377 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400378 testend = 0;
379 }
Chris Masona52d9a82007-08-27 16:49:44 -0400380 while(1) {
Chris Mason3b951512008-04-17 11:29:12 -0400381 if (!split)
382 split = alloc_extent_map(GFP_NOFS);
383 if (!split2)
384 split2 = alloc_extent_map(GFP_NOFS);
385
Chris Masond1310b22008-01-24 16:13:08 -0500386 spin_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500387 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500388 if (!em) {
389 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400390 break;
Chris Masond1310b22008-01-24 16:13:08 -0500391 }
Chris Mason3ce7e672008-07-31 15:42:54 -0400392 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400393 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400394
395 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
396 em->start < start) {
397 split->start = em->start;
398 split->len = start - em->start;
399 split->block_start = em->block_start;
400 split->bdev = em->bdev;
401 split->flags = em->flags;
402 ret = add_extent_mapping(em_tree, split);
403 BUG_ON(ret);
404 free_extent_map(split);
405 split = split2;
406 split2 = NULL;
407 }
408 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
409 testend && em->start + em->len > start + len) {
410 u64 diff = start + len - em->start;
411
412 split->start = start + len;
413 split->len = em->start + em->len - (start + len);
414 split->bdev = em->bdev;
415 split->flags = em->flags;
416
417 split->block_start = em->block_start + diff;
418
419 ret = add_extent_mapping(em_tree, split);
420 BUG_ON(ret);
421 free_extent_map(split);
422 split = NULL;
423 }
Chris Masond1310b22008-01-24 16:13:08 -0500424 spin_unlock(&em_tree->lock);
425
Chris Masona52d9a82007-08-27 16:49:44 -0400426 /* once for us */
427 free_extent_map(em);
428 /* once for the tree*/
429 free_extent_map(em);
430 }
Chris Mason3b951512008-04-17 11:29:12 -0400431 if (split)
432 free_extent_map(split);
433 if (split2)
434 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400435 return 0;
436}
437
Chris Mason5f564062008-01-22 16:47:59 -0500438int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
439{
440 return 0;
441#if 0
442 struct btrfs_path *path;
443 struct btrfs_key found_key;
444 struct extent_buffer *leaf;
445 struct btrfs_file_extent_item *extent;
446 u64 last_offset = 0;
447 int nritems;
448 int slot;
449 int found_type;
450 int ret;
451 int err = 0;
452 u64 extent_end = 0;
453
454 path = btrfs_alloc_path();
455 ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
456 last_offset, 0);
457 while(1) {
458 nritems = btrfs_header_nritems(path->nodes[0]);
459 if (path->slots[0] >= nritems) {
460 ret = btrfs_next_leaf(root, path);
461 if (ret)
462 goto out;
463 nritems = btrfs_header_nritems(path->nodes[0]);
464 }
465 slot = path->slots[0];
466 leaf = path->nodes[0];
467 btrfs_item_key_to_cpu(leaf, &found_key, slot);
468 if (found_key.objectid != inode->i_ino)
469 break;
470 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
471 goto out;
472
Chris Mason90692182008-02-08 13:49:28 -0500473 if (found_key.offset < last_offset) {
Chris Mason5f564062008-01-22 16:47:59 -0500474 WARN_ON(1);
475 btrfs_print_leaf(root, leaf);
476 printk("inode %lu found offset %Lu expected %Lu\n",
477 inode->i_ino, found_key.offset, last_offset);
478 err = 1;
479 goto out;
480 }
481 extent = btrfs_item_ptr(leaf, slot,
482 struct btrfs_file_extent_item);
483 found_type = btrfs_file_extent_type(leaf, extent);
484 if (found_type == BTRFS_FILE_EXTENT_REG) {
485 extent_end = found_key.offset +
486 btrfs_file_extent_num_bytes(leaf, extent);
487 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
488 struct btrfs_item *item;
489 item = btrfs_item_nr(leaf, slot);
490 extent_end = found_key.offset +
491 btrfs_file_extent_inline_len(leaf, item);
492 extent_end = (extent_end + root->sectorsize - 1) &
493 ~((u64)root->sectorsize -1 );
494 }
495 last_offset = extent_end;
496 path->slots[0]++;
497 }
Chris Mason90692182008-02-08 13:49:28 -0500498 if (0 && last_offset < inode->i_size) {
Chris Mason5f564062008-01-22 16:47:59 -0500499 WARN_ON(1);
500 btrfs_print_leaf(root, leaf);
501 printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
502 last_offset, inode->i_size);
503 err = 1;
504
505 }
506out:
507 btrfs_free_path(path);
508 return err;
509#endif
510}
511
Chris Mason39279cc2007-06-12 06:35:45 -0400512/*
513 * this is very complex, but the basic idea is to drop all extents
514 * in the range start - end. hint_block is filled in with a block number
515 * that would be a good hint to the block allocator for this file.
516 *
517 * If an extent intersects the range but is not entirely inside the range
518 * it is either truncated or split. Anything entirely inside the range
519 * is deleted from the tree.
520 */
Chris Masona1b32a52008-09-05 16:09:51 -0400521int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400522 struct btrfs_root *root, struct inode *inode,
Chris Mason00f5c792007-11-30 10:09:33 -0500523 u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
Chris Mason39279cc2007-06-12 06:35:45 -0400524{
Chris Mason39279cc2007-06-12 06:35:45 -0400525 u64 extent_end = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400526 u64 search_start = start;
Zheng Yan31840ae2008-09-23 13:14:14 -0400527 u64 leaf_start;
528 u64 root_gen;
529 u64 root_owner;
Chris Mason00f5c792007-11-30 10:09:33 -0500530 struct extent_buffer *leaf;
531 struct btrfs_file_extent_item *extent;
532 struct btrfs_path *path;
533 struct btrfs_key key;
534 struct btrfs_file_extent_item old;
535 int keep;
536 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400537 int bookend;
538 int found_type;
539 int found_extent;
540 int found_inline;
Chris Masonccd467d2007-06-28 15:57:36 -0400541 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500542 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400543
Chris Masona52d9a82007-08-27 16:49:44 -0400544 btrfs_drop_extent_cache(inode, start, end - 1);
545
Chris Mason39279cc2007-06-12 06:35:45 -0400546 path = btrfs_alloc_path();
547 if (!path)
548 return -ENOMEM;
549 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400550 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400551 btrfs_release_path(root, path);
552 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
553 search_start, -1);
554 if (ret < 0)
555 goto out;
556 if (ret > 0) {
557 if (path->slots[0] == 0) {
558 ret = 0;
559 goto out;
560 }
561 path->slots[0]--;
562 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400563next_slot:
Chris Mason39279cc2007-06-12 06:35:45 -0400564 keep = 0;
565 bookend = 0;
566 found_extent = 0;
567 found_inline = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400568 leaf_start = 0;
569 root_gen = 0;
570 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400571 extent = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400572 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -0400573 slot = path->slots[0];
Chris Mason8c2383c2007-06-18 09:57:58 -0400574 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400575 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan72610092008-02-05 15:40:36 -0500576 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
577 key.offset >= end) {
Chris Mason39279cc2007-06-12 06:35:45 -0400578 goto out;
579 }
Yan72610092008-02-05 15:40:36 -0500580 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
581 key.objectid != inode->i_ino) {
Chris Mason39279cc2007-06-12 06:35:45 -0400582 goto out;
583 }
Chris Masonccd467d2007-06-28 15:57:36 -0400584 if (recow) {
585 search_start = key.offset;
586 continue;
587 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400588 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
589 extent = btrfs_item_ptr(leaf, slot,
590 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400591 found_type = btrfs_file_extent_type(leaf, extent);
Chris Mason8c2383c2007-06-18 09:57:58 -0400592 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500593 extent_end =
594 btrfs_file_extent_disk_bytenr(leaf,
595 extent);
596 if (extent_end)
597 *hint_byte = extent_end;
598
Chris Mason8c2383c2007-06-18 09:57:58 -0400599 extent_end = key.offset +
Chris Masondb945352007-10-15 16:15:53 -0400600 btrfs_file_extent_num_bytes(leaf, extent);
Chris Mason8c2383c2007-06-18 09:57:58 -0400601 found_extent = 1;
602 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400603 struct btrfs_item *item;
604 item = btrfs_item_nr(leaf, slot);
Chris Mason8c2383c2007-06-18 09:57:58 -0400605 found_inline = 1;
606 extent_end = key.offset +
Chris Mason5f39d392007-10-15 16:14:19 -0400607 btrfs_file_extent_inline_len(leaf, item);
Chris Mason8c2383c2007-06-18 09:57:58 -0400608 }
609 } else {
610 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400611 }
612
613 /* we found nothing we can drop */
Chris Mason8c2383c2007-06-18 09:57:58 -0400614 if ((!found_extent && !found_inline) ||
615 search_start >= extent_end) {
616 int nextret;
617 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400618 nritems = btrfs_header_nritems(leaf);
Chris Mason8c2383c2007-06-18 09:57:58 -0400619 if (slot >= nritems - 1) {
620 nextret = btrfs_next_leaf(root, path);
621 if (nextret)
622 goto out;
Chris Masonccd467d2007-06-28 15:57:36 -0400623 recow = 1;
Chris Mason8c2383c2007-06-18 09:57:58 -0400624 } else {
625 path->slots[0]++;
626 }
627 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400628 }
629
Chris Mason39279cc2007-06-12 06:35:45 -0400630 if (found_inline) {
Chris Mason5f39d392007-10-15 16:14:19 -0400631 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400632 search_start = (extent_end + mask) & ~mask;
633 } else
634 search_start = extent_end;
Yan6e3b9662007-12-14 11:14:42 -0500635 if (end <= extent_end && start >= key.offset && found_inline) {
Chris Mason179e29e2007-11-01 11:28:41 -0400636 *hint_byte = EXTENT_MAP_INLINE;
Zheng Yan31840ae2008-09-23 13:14:14 -0400637 goto out;
Chris Mason179e29e2007-11-01 11:28:41 -0400638 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400639
640 if (found_extent) {
641 read_extent_buffer(leaf, &old, (unsigned long)extent,
642 sizeof(old));
643 root_gen = btrfs_header_generation(leaf);
644 root_owner = btrfs_header_owner(leaf);
645 leaf_start = leaf->start;
646 }
647
Chris Mason39279cc2007-06-12 06:35:45 -0400648 if (end < extent_end && end >= key.offset) {
Chris Mason179e29e2007-11-01 11:28:41 -0400649 bookend = 1;
Yan0181e582008-01-30 14:39:54 -0500650 if (found_inline && start <= key.offset)
Chris Mason179e29e2007-11-01 11:28:41 -0400651 keep = 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400652 }
Chris Mason39279cc2007-06-12 06:35:45 -0400653 /* truncate existing extent */
654 if (start > key.offset) {
655 u64 new_num;
656 u64 old_num;
657 keep = 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400658 WARN_ON(start & (root->sectorsize - 1));
Chris Mason39279cc2007-06-12 06:35:45 -0400659 if (found_extent) {
Chris Masondb945352007-10-15 16:15:53 -0400660 new_num = start - key.offset;
661 old_num = btrfs_file_extent_num_bytes(leaf,
662 extent);
663 *hint_byte =
664 btrfs_file_extent_disk_bytenr(leaf,
665 extent);
666 if (btrfs_file_extent_disk_bytenr(leaf,
667 extent)) {
Chris Mason90692182008-02-08 13:49:28 -0500668 dec_i_blocks(inode, old_num - new_num);
Chris Mason39279cc2007-06-12 06:35:45 -0400669 }
Chris Masondb945352007-10-15 16:15:53 -0400670 btrfs_set_file_extent_num_bytes(leaf, extent,
671 new_num);
Chris Mason5f39d392007-10-15 16:14:19 -0400672 btrfs_mark_buffer_dirty(leaf);
Chris Mason00f5c792007-11-30 10:09:33 -0500673 } else if (key.offset < inline_limit &&
674 (end > extent_end) &&
675 (inline_limit < extent_end)) {
Chris Mason3326d1b2007-10-15 16:18:25 -0400676 u32 new_size;
677 new_size = btrfs_file_extent_calc_inline_size(
Chris Mason00f5c792007-11-30 10:09:33 -0500678 inline_limit - key.offset);
Chris Mason90692182008-02-08 13:49:28 -0500679 dec_i_blocks(inode, (extent_end - key.offset) -
680 (inline_limit - key.offset));
Chris Mason3326d1b2007-10-15 16:18:25 -0400681 btrfs_truncate_item(trans, root, path,
Chris Mason179e29e2007-11-01 11:28:41 -0400682 new_size, 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400683 }
684 }
685 /* delete the entire extent */
686 if (!keep) {
Chris Mason39279cc2007-06-12 06:35:45 -0400687 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400688 /* TODO update progress marker and return */
Chris Mason39279cc2007-06-12 06:35:45 -0400689 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400690 extent = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400691 btrfs_release_path(root, path);
692 /* the extent will be freed later */
Chris Mason39279cc2007-06-12 06:35:45 -0400693 }
Yan0181e582008-01-30 14:39:54 -0500694 if (bookend && found_inline && start <= key.offset) {
Chris Mason179e29e2007-11-01 11:28:41 -0400695 u32 new_size;
696 new_size = btrfs_file_extent_calc_inline_size(
Yan0181e582008-01-30 14:39:54 -0500697 extent_end - end);
Chris Mason90692182008-02-08 13:49:28 -0500698 dec_i_blocks(inode, (extent_end - key.offset) -
699 (extent_end - end));
Zheng Yan31840ae2008-09-23 13:14:14 -0400700 ret = btrfs_truncate_item(trans, root, path,
701 new_size, 0);
702 BUG_ON(ret);
Chris Mason179e29e2007-11-01 11:28:41 -0400703 }
Chris Mason39279cc2007-06-12 06:35:45 -0400704 /* create bookend, splitting the extent in two */
705 if (bookend && found_extent) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400706 u64 disk_bytenr;
Chris Mason39279cc2007-06-12 06:35:45 -0400707 struct btrfs_key ins;
708 ins.objectid = inode->i_ino;
709 ins.offset = end;
Chris Mason39279cc2007-06-12 06:35:45 -0400710 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -0400711 btrfs_release_path(root, path);
712 ret = btrfs_insert_empty_item(trans, root, path, &ins,
713 sizeof(*extent));
Zheng Yan31840ae2008-09-23 13:14:14 -0400714 BUG_ON(ret);
Chris Mason8c2383c2007-06-18 09:57:58 -0400715
Chris Mason5f39d392007-10-15 16:14:19 -0400716 leaf = path->nodes[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400717 extent = btrfs_item_ptr(leaf, path->slots[0],
718 struct btrfs_file_extent_item);
719 write_extent_buffer(leaf, &old,
720 (unsigned long)extent, sizeof(old));
Chris Mason39279cc2007-06-12 06:35:45 -0400721
Chris Mason5f39d392007-10-15 16:14:19 -0400722 btrfs_set_file_extent_offset(leaf, extent,
Chris Masondb945352007-10-15 16:15:53 -0400723 le64_to_cpu(old.offset) + end - key.offset);
724 WARN_ON(le64_to_cpu(old.num_bytes) <
725 (extent_end - end));
726 btrfs_set_file_extent_num_bytes(leaf, extent,
727 extent_end - end);
Chris Mason5f39d392007-10-15 16:14:19 -0400728 btrfs_set_file_extent_type(leaf, extent,
Chris Mason39279cc2007-06-12 06:35:45 -0400729 BTRFS_FILE_EXTENT_REG);
Chris Masondb945352007-10-15 16:15:53 -0400730
Chris Mason39279cc2007-06-12 06:35:45 -0400731 btrfs_mark_buffer_dirty(path->nodes[0]);
Zheng Yan31840ae2008-09-23 13:14:14 -0400732
733 disk_bytenr = le64_to_cpu(old.disk_bytenr);
734 if (disk_bytenr != 0) {
735 ret = btrfs_inc_extent_ref(trans, root,
736 disk_bytenr,
737 le64_to_cpu(old.disk_num_bytes),
738 leaf->start,
739 root->root_key.objectid,
740 trans->transid,
741 ins.objectid, ins.offset);
742 BUG_ON(ret);
743 }
744 btrfs_release_path(root, path);
745 if (disk_bytenr != 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400746 inode->i_blocks +=
Chris Masondb945352007-10-15 16:15:53 -0400747 btrfs_file_extent_num_bytes(leaf,
748 extent) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400749 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400750 }
751
752 if (found_extent && !keep) {
753 u64 disk_bytenr = le64_to_cpu(old.disk_bytenr);
754
755 if (disk_bytenr != 0) {
756 dec_i_blocks(inode, le64_to_cpu(old.num_bytes));
757 ret = btrfs_free_extent(trans, root,
758 disk_bytenr,
759 le64_to_cpu(old.disk_num_bytes),
760 leaf_start, root_owner,
761 root_gen, key.objectid,
762 key.offset, 0);
763 BUG_ON(ret);
764 *hint_byte = disk_bytenr;
765 }
766 }
767
768 if (search_start >= end) {
Chris Mason39279cc2007-06-12 06:35:45 -0400769 ret = 0;
770 goto out;
771 }
772 }
773out:
774 btrfs_free_path(path);
Chris Mason90692182008-02-08 13:49:28 -0500775 btrfs_check_file(root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400776 return ret;
777}
778
779/*
780 * this gets pages into the page cache and locks them down
781 */
Chris Masona1b32a52008-09-05 16:09:51 -0400782static int noinline prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500783 struct page **pages, size_t num_pages,
784 loff_t pos, unsigned long first_index,
785 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400786{
787 int i;
788 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500789 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400790 int err = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400791 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400792 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400793
Chris Mason5f39d392007-10-15 16:14:19 -0400794 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400795 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400796
797 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400798again:
Chris Mason39279cc2007-06-12 06:35:45 -0400799 for (i = 0; i < num_pages; i++) {
800 pages[i] = grab_cache_page(inode->i_mapping, index + i);
801 if (!pages[i]) {
802 err = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -0400803 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400804 }
Chris Masonccd467d2007-06-28 15:57:36 -0400805 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400806 }
Chris Mason07627042008-02-19 11:29:24 -0500807 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400808 struct btrfs_ordered_extent *ordered;
Chris Masond99cb302008-02-19 12:55:05 -0500809 lock_extent(&BTRFS_I(inode)->io_tree,
810 start_pos, last_pos - 1, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400811 ordered = btrfs_lookup_first_ordered_extent(inode, last_pos -1);
812 if (ordered &&
813 ordered->file_offset + ordered->len > start_pos &&
814 ordered->file_offset < last_pos) {
815 btrfs_put_ordered_extent(ordered);
816 unlock_extent(&BTRFS_I(inode)->io_tree,
817 start_pos, last_pos - 1, GFP_NOFS);
818 for (i = 0; i < num_pages; i++) {
819 unlock_page(pages[i]);
820 page_cache_release(pages[i]);
821 }
822 btrfs_wait_ordered_range(inode, start_pos,
823 last_pos - start_pos);
824 goto again;
825 }
826 if (ordered)
827 btrfs_put_ordered_extent(ordered);
828
Chris Mason07627042008-02-19 11:29:24 -0500829 clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
830 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
831 GFP_NOFS);
Chris Masond99cb302008-02-19 12:55:05 -0500832 unlock_extent(&BTRFS_I(inode)->io_tree,
833 start_pos, last_pos - 1, GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500834 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400835 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400836 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400837 set_page_extent_mapped(pages[i]);
838 WARN_ON(!PageLocked(pages[i]));
839 }
Chris Mason39279cc2007-06-12 06:35:45 -0400840 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400841}
842
843static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
844 size_t count, loff_t *ppos)
845{
846 loff_t pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400847 loff_t start_pos;
848 ssize_t num_written = 0;
849 ssize_t err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400850 int ret = 0;
Chris Mason6da6aba2007-12-18 16:15:09 -0500851 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400852 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8c2383c2007-06-18 09:57:58 -0400853 struct page **pages = NULL;
854 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400855 struct page *pinned[2];
856 unsigned long first_index;
857 unsigned long last_index;
Chris Mason8c2383c2007-06-18 09:57:58 -0400858
859 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
860 PAGE_CACHE_SIZE / (sizeof(struct page *)));
Chris Mason39279cc2007-06-12 06:35:45 -0400861 pinned[0] = NULL;
862 pinned[1] = NULL;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400863
Chris Mason39279cc2007-06-12 06:35:45 -0400864 pos = *ppos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400865 start_pos = pos;
866
Chris Mason39279cc2007-06-12 06:35:45 -0400867 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
868 current->backing_dev_info = inode->i_mapping->backing_dev_info;
869 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
870 if (err)
Chris Mason1832a6d2007-12-21 16:27:21 -0500871 goto out_nolock;
Chris Mason39279cc2007-06-12 06:35:45 -0400872 if (count == 0)
Chris Mason1832a6d2007-12-21 16:27:21 -0500873 goto out_nolock;
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -0400874#ifdef REMOVE_SUID_PATH
875 err = remove_suid(&file->f_path);
876#else
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400877# if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
878 err = file_remove_suid(file);
879# else
Chris Mason6da6aba2007-12-18 16:15:09 -0500880 err = remove_suid(fdentry(file));
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400881# endif
Jeff Mahoney12fa8ec2008-05-02 15:03:58 -0400882#endif
Chris Mason39279cc2007-06-12 06:35:45 -0400883 if (err)
Chris Mason1832a6d2007-12-21 16:27:21 -0500884 goto out_nolock;
Chris Mason39279cc2007-06-12 06:35:45 -0400885 file_update_time(file);
886
Chris Mason8c2383c2007-06-18 09:57:58 -0400887 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -0400888
889 mutex_lock(&inode->i_mutex);
890 first_index = pos >> PAGE_CACHE_SHIFT;
891 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
892
893 /*
Chris Mason409c6112008-04-22 09:24:20 -0400894 * if this is a nodatasum mount, force summing off for the inode
895 * all the time. That way a later mount with summing on won't
896 * get confused
897 */
898 if (btrfs_test_opt(root, NODATASUM))
899 btrfs_set_flag(inode, NODATASUM);
900
901 /*
Chris Mason39279cc2007-06-12 06:35:45 -0400902 * there are lots of better ways to do this, but this code
903 * makes sure the first and last page in the file range are
904 * up to date and ready for cow
905 */
906 if ((pos & (PAGE_CACHE_SIZE - 1))) {
907 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
908 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400909 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400910 BUG_ON(ret);
911 wait_on_page_locked(pinned[0]);
912 } else {
913 unlock_page(pinned[0]);
914 }
915 }
916 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
917 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
918 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400919 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400920 BUG_ON(ret);
921 wait_on_page_locked(pinned[1]);
922 } else {
923 unlock_page(pinned[1]);
924 }
925 }
926
Chris Mason39279cc2007-06-12 06:35:45 -0400927 while(count > 0) {
928 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Chris Mason11bd1432007-06-22 14:16:24 -0400929 size_t write_bytes = min(count, nrptrs *
930 (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400931 offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400932 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
933 PAGE_CACHE_SHIFT;
934
Chris Mason8c2383c2007-06-18 09:57:58 -0400935 WARN_ON(num_pages > nrptrs);
Chris Mason39279cc2007-06-12 06:35:45 -0400936 memset(pages, 0, sizeof(pages));
Chris Mason1832a6d2007-12-21 16:27:21 -0500937
Chris Mason1832a6d2007-12-21 16:27:21 -0500938 ret = btrfs_check_free_space(root, write_bytes, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -0500939 if (ret)
940 goto out;
941
Chris Mason39279cc2007-06-12 06:35:45 -0400942 ret = prepare_pages(root, file, pages, num_pages,
943 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400944 write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400945 if (ret)
946 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -0400947
Chris Mason39279cc2007-06-12 06:35:45 -0400948 ret = btrfs_copy_from_user(pos, num_pages,
949 write_bytes, pages, buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400950 if (ret) {
951 btrfs_drop_pages(pages, num_pages);
952 goto out;
953 }
Chris Mason39279cc2007-06-12 06:35:45 -0400954
955 ret = dirty_and_release_pages(NULL, root, file, pages,
956 num_pages, pos, write_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -0400957 btrfs_drop_pages(pages, num_pages);
Chris Mason54aa1f42007-06-22 14:16:25 -0400958 if (ret)
959 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -0400960
961 buf += write_bytes;
962 count -= write_bytes;
963 pos += write_bytes;
964 num_written += write_bytes;
965
Chris Mason8c2383c2007-06-18 09:57:58 -0400966 balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
Chris Mason448d6402007-11-27 07:52:01 -0800967 if (num_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
968 btrfs_btree_balance_dirty(root, 1);
Chris Masonab78c842008-07-29 16:15:18 -0400969 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400970 cond_resched();
971 }
Chris Mason39279cc2007-06-12 06:35:45 -0400972out:
Chris Mason1832a6d2007-12-21 16:27:21 -0500973 mutex_unlock(&inode->i_mutex);
Chris Mason5b92ee72008-01-03 13:46:11 -0500974
Chris Mason1832a6d2007-12-21 16:27:21 -0500975out_nolock:
Chris Mason8c2383c2007-06-18 09:57:58 -0400976 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -0400977 if (pinned[0])
978 page_cache_release(pinned[0]);
979 if (pinned[1])
980 page_cache_release(pinned[1]);
981 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400982
983 if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
Chris Masone02119d2008-09-05 16:13:11 -0400984 struct btrfs_trans_handle *trans;
985
986 err = btrfs_fdatawrite_range(inode->i_mapping, start_pos,
987 start_pos + num_written -1,
988 WB_SYNC_NONE);
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400989 if (err < 0)
990 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -0400991
992 err = btrfs_wait_on_page_writeback_range(inode->i_mapping,
993 start_pos, start_pos + num_written - 1);
994 if (err < 0)
995 num_written = err;
996
997 trans = btrfs_start_transaction(root, 1);
998 ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
999 if (ret == 0) {
1000 btrfs_sync_log(trans, root);
1001 btrfs_end_transaction(trans, root);
1002 } else {
1003 btrfs_commit_transaction(trans, root);
1004 }
Chris Mason16432982008-04-10 10:23:21 -04001005 } else if (num_written > 0 && (file->f_flags & O_DIRECT)) {
Chris Masonbb8885c2008-05-02 14:49:33 -04001006#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22)
Chris Masonb248a412008-04-14 09:48:18 -04001007 do_sync_file_range(file, start_pos,
1008 start_pos + num_written - 1,
1009 SYNC_FILE_RANGE_WRITE |
1010 SYNC_FILE_RANGE_WAIT_AFTER);
1011#else
Chris Mason16432982008-04-10 10:23:21 -04001012 do_sync_mapping_range(inode->i_mapping, start_pos,
1013 start_pos + num_written - 1,
1014 SYNC_FILE_RANGE_WRITE |
1015 SYNC_FILE_RANGE_WAIT_AFTER);
Chris Masonb248a412008-04-14 09:48:18 -04001016#endif
Chris Mason16432982008-04-10 10:23:21 -04001017 invalidate_mapping_pages(inode->i_mapping,
1018 start_pos >> PAGE_CACHE_SHIFT,
1019 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001020 }
Chris Mason39279cc2007-06-12 06:35:45 -04001021 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001022 return num_written ? num_written : err;
1023}
1024
Sage Weil6bf13c02008-06-10 10:07:39 -04001025int btrfs_release_file(struct inode * inode, struct file * filp)
Mingminge1b81e62008-05-27 10:55:43 -04001026{
Sage Weil6bf13c02008-06-10 10:07:39 -04001027 if (filp->private_data)
1028 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001029 return 0;
1030}
1031
Chris Masone02119d2008-09-05 16:13:11 -04001032int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001033{
1034 struct inode *inode = dentry->d_inode;
1035 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001036 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001037 struct btrfs_trans_handle *trans;
1038
1039 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001040 * check the transaction that last modified this inode
1041 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001042 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001043 if (!BTRFS_I(inode)->last_trans)
1044 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001045
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001046 mutex_lock(&root->fs_info->trans_mutex);
1047 if (BTRFS_I(inode)->last_trans <=
1048 root->fs_info->last_trans_committed) {
1049 BTRFS_I(inode)->last_trans = 0;
1050 mutex_unlock(&root->fs_info->trans_mutex);
1051 goto out;
1052 }
1053 mutex_unlock(&root->fs_info->trans_mutex);
1054
Chris Mason49eb7e42008-09-11 15:53:12 -04001055 root->fs_info->tree_log_batch++;
Chris Masone02119d2008-09-05 16:13:11 -04001056 filemap_fdatawait(inode->i_mapping);
Chris Mason49eb7e42008-09-11 15:53:12 -04001057 root->fs_info->tree_log_batch++;
Chris Masone02119d2008-09-05 16:13:11 -04001058
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001059 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001060 * ok we haven't committed the transaction yet, lets do a commit
1061 */
Sage Weil6bf13c02008-06-10 10:07:39 -04001062 if (file->private_data)
1063 btrfs_ioctl_trans_end(file);
1064
Chris Mason39279cc2007-06-12 06:35:45 -04001065 trans = btrfs_start_transaction(root, 1);
1066 if (!trans) {
1067 ret = -ENOMEM;
1068 goto out;
1069 }
Chris Masone02119d2008-09-05 16:13:11 -04001070
1071 ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
Chris Mason49eb7e42008-09-11 15:53:12 -04001072 if (ret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001073 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001074 }
1075
1076 /* we've logged all the items and now have a consistent
1077 * version of the file in the log. It is possible that
1078 * someone will come in and modify the file, but that's
1079 * fine because the log is consistent on disk, and we
1080 * have references to all of the file's extents
1081 *
1082 * It is possible that someone will come in and log the
1083 * file again, but that will end up using the synchronization
1084 * inside btrfs_sync_log to keep things safe.
1085 */
1086 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
1087
Chris Masone02119d2008-09-05 16:13:11 -04001088 if (ret > 0) {
1089 ret = btrfs_commit_transaction(trans, root);
1090 } else {
1091 btrfs_sync_log(trans, root);
1092 ret = btrfs_end_transaction(trans, root);
1093 }
Chris Mason49eb7e42008-09-11 15:53:12 -04001094 mutex_lock(&file->f_dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001095out:
1096 return ret > 0 ? EIO : ret;
1097}
1098
Chris Mason9ebefb182007-06-15 13:50:00 -04001099static struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001100#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
1101 .nopage = filemap_nopage,
1102 .populate = filemap_populate,
1103#else
1104 .fault = filemap_fault,
1105#endif
Chris Mason9ebefb182007-06-15 13:50:00 -04001106 .page_mkwrite = btrfs_page_mkwrite,
1107};
1108
1109static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1110{
1111 vma->vm_ops = &btrfs_file_vm_ops;
1112 file_accessed(filp);
1113 return 0;
1114}
1115
Chris Mason39279cc2007-06-12 06:35:45 -04001116struct file_operations btrfs_file_operations = {
1117 .llseek = generic_file_llseek,
1118 .read = do_sync_read,
Chris Mason9ebefb182007-06-15 13:50:00 -04001119 .aio_read = generic_file_aio_read,
Chris Masone9906a92007-12-14 12:56:58 -05001120 .splice_read = generic_file_splice_read,
Chris Mason6da6aba2007-12-18 16:15:09 -05001121#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1122 .sendfile = generic_file_sendfile,
1123#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001124 .write = btrfs_file_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001125 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001126 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001127 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001128 .fsync = btrfs_sync_file,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001129 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001130#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001131 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001132#endif
1133};