blob: 1b7e51a9db0f533db3652f701b0d5a52ea46117f [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,
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400297 last_pos_in_file + hole_size - 1, 0);
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);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400340 btrfs_drop_extent_cache(inode, start_pos, aligned_end - 1, 0);
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
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400365int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end,
366 int skip_pinned)
Chris Masona52d9a82007-08-27 16:49:44 -0400367{
368 struct extent_map *em;
Chris Mason3b951512008-04-17 11:29:12 -0400369 struct extent_map *split = NULL;
370 struct extent_map *split2 = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -0400371 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Yan39b56372008-02-15 10:40:50 -0500372 u64 len = end - start + 1;
Chris Mason3b951512008-04-17 11:29:12 -0400373 int ret;
374 int testend = 1;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400375 unsigned long flags;
Chris Masona52d9a82007-08-27 16:49:44 -0400376
Chris Masone6dcd2d2008-07-17 12:53:50 -0400377 WARN_ON(end < start);
Chris Mason3b951512008-04-17 11:29:12 -0400378 if (end == (u64)-1) {
Yan39b56372008-02-15 10:40:50 -0500379 len = (u64)-1;
Chris Mason3b951512008-04-17 11:29:12 -0400380 testend = 0;
381 }
Chris Masona52d9a82007-08-27 16:49:44 -0400382 while(1) {
Chris Mason3b951512008-04-17 11:29:12 -0400383 if (!split)
384 split = alloc_extent_map(GFP_NOFS);
385 if (!split2)
386 split2 = alloc_extent_map(GFP_NOFS);
387
Chris Masond1310b22008-01-24 16:13:08 -0500388 spin_lock(&em_tree->lock);
Yan39b56372008-02-15 10:40:50 -0500389 em = lookup_extent_mapping(em_tree, start, len);
Chris Masond1310b22008-01-24 16:13:08 -0500390 if (!em) {
391 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -0400392 break;
Chris Masond1310b22008-01-24 16:13:08 -0500393 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400394 flags = em->flags;
395 if (skip_pinned && test_bit(EXTENT_FLAG_PINNED, &em->flags)) {
396 spin_unlock(&em_tree->lock);
397 if (em->start <= start &&
398 (!testend || em->start + em->len >= start + len)) {
399 free_extent_map(em);
400 break;
401 }
402 if (start < em->start) {
403 len = em->start - start;
404 } else {
405 len = start + len - (em->start + em->len);
406 start = em->start + em->len;
407 }
408 free_extent_map(em);
409 continue;
410 }
Chris Mason3ce7e672008-07-31 15:42:54 -0400411 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masona52d9a82007-08-27 16:49:44 -0400412 remove_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -0400413
414 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
415 em->start < start) {
416 split->start = em->start;
417 split->len = start - em->start;
418 split->block_start = em->block_start;
419 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400420 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400421 ret = add_extent_mapping(em_tree, split);
422 BUG_ON(ret);
423 free_extent_map(split);
424 split = split2;
425 split2 = NULL;
426 }
427 if (em->block_start < EXTENT_MAP_LAST_BYTE &&
428 testend && em->start + em->len > start + len) {
429 u64 diff = start + len - em->start;
430
431 split->start = start + len;
432 split->len = em->start + em->len - (start + len);
433 split->bdev = em->bdev;
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400434 split->flags = flags;
Chris Mason3b951512008-04-17 11:29:12 -0400435
436 split->block_start = em->block_start + diff;
437
438 ret = add_extent_mapping(em_tree, split);
439 BUG_ON(ret);
440 free_extent_map(split);
441 split = NULL;
442 }
Chris Masond1310b22008-01-24 16:13:08 -0500443 spin_unlock(&em_tree->lock);
444
Chris Masona52d9a82007-08-27 16:49:44 -0400445 /* once for us */
446 free_extent_map(em);
447 /* once for the tree*/
448 free_extent_map(em);
449 }
Chris Mason3b951512008-04-17 11:29:12 -0400450 if (split)
451 free_extent_map(split);
452 if (split2)
453 free_extent_map(split2);
Chris Masona52d9a82007-08-27 16:49:44 -0400454 return 0;
455}
456
Chris Mason5f564062008-01-22 16:47:59 -0500457int btrfs_check_file(struct btrfs_root *root, struct inode *inode)
458{
459 return 0;
460#if 0
461 struct btrfs_path *path;
462 struct btrfs_key found_key;
463 struct extent_buffer *leaf;
464 struct btrfs_file_extent_item *extent;
465 u64 last_offset = 0;
466 int nritems;
467 int slot;
468 int found_type;
469 int ret;
470 int err = 0;
471 u64 extent_end = 0;
472
473 path = btrfs_alloc_path();
474 ret = btrfs_lookup_file_extent(NULL, root, path, inode->i_ino,
475 last_offset, 0);
476 while(1) {
477 nritems = btrfs_header_nritems(path->nodes[0]);
478 if (path->slots[0] >= nritems) {
479 ret = btrfs_next_leaf(root, path);
480 if (ret)
481 goto out;
482 nritems = btrfs_header_nritems(path->nodes[0]);
483 }
484 slot = path->slots[0];
485 leaf = path->nodes[0];
486 btrfs_item_key_to_cpu(leaf, &found_key, slot);
487 if (found_key.objectid != inode->i_ino)
488 break;
489 if (found_key.type != BTRFS_EXTENT_DATA_KEY)
490 goto out;
491
Chris Mason90692182008-02-08 13:49:28 -0500492 if (found_key.offset < last_offset) {
Chris Mason5f564062008-01-22 16:47:59 -0500493 WARN_ON(1);
494 btrfs_print_leaf(root, leaf);
495 printk("inode %lu found offset %Lu expected %Lu\n",
496 inode->i_ino, found_key.offset, last_offset);
497 err = 1;
498 goto out;
499 }
500 extent = btrfs_item_ptr(leaf, slot,
501 struct btrfs_file_extent_item);
502 found_type = btrfs_file_extent_type(leaf, extent);
503 if (found_type == BTRFS_FILE_EXTENT_REG) {
504 extent_end = found_key.offset +
505 btrfs_file_extent_num_bytes(leaf, extent);
506 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
507 struct btrfs_item *item;
508 item = btrfs_item_nr(leaf, slot);
509 extent_end = found_key.offset +
510 btrfs_file_extent_inline_len(leaf, item);
511 extent_end = (extent_end + root->sectorsize - 1) &
512 ~((u64)root->sectorsize -1 );
513 }
514 last_offset = extent_end;
515 path->slots[0]++;
516 }
Chris Mason90692182008-02-08 13:49:28 -0500517 if (0 && last_offset < inode->i_size) {
Chris Mason5f564062008-01-22 16:47:59 -0500518 WARN_ON(1);
519 btrfs_print_leaf(root, leaf);
520 printk("inode %lu found offset %Lu size %Lu\n", inode->i_ino,
521 last_offset, inode->i_size);
522 err = 1;
523
524 }
525out:
526 btrfs_free_path(path);
527 return err;
528#endif
529}
530
Chris Mason39279cc2007-06-12 06:35:45 -0400531/*
532 * this is very complex, but the basic idea is to drop all extents
533 * in the range start - end. hint_block is filled in with a block number
534 * that would be a good hint to the block allocator for this file.
535 *
536 * If an extent intersects the range but is not entirely inside the range
537 * it is either truncated or split. Anything entirely inside the range
538 * is deleted from the tree.
539 */
Chris Masona1b32a52008-09-05 16:09:51 -0400540int noinline btrfs_drop_extents(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400541 struct btrfs_root *root, struct inode *inode,
Chris Mason00f5c792007-11-30 10:09:33 -0500542 u64 start, u64 end, u64 inline_limit, u64 *hint_byte)
Chris Mason39279cc2007-06-12 06:35:45 -0400543{
Chris Mason39279cc2007-06-12 06:35:45 -0400544 u64 extent_end = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400545 u64 search_start = start;
Zheng Yan31840ae2008-09-23 13:14:14 -0400546 u64 leaf_start;
547 u64 root_gen;
548 u64 root_owner;
Chris Mason00f5c792007-11-30 10:09:33 -0500549 struct extent_buffer *leaf;
550 struct btrfs_file_extent_item *extent;
551 struct btrfs_path *path;
552 struct btrfs_key key;
553 struct btrfs_file_extent_item old;
554 int keep;
555 int slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400556 int bookend;
557 int found_type;
558 int found_extent;
559 int found_inline;
Chris Masonccd467d2007-06-28 15:57:36 -0400560 int recow;
Chris Mason00f5c792007-11-30 10:09:33 -0500561 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -0400562
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400563 btrfs_drop_extent_cache(inode, start, end - 1, 0);
Chris Masona52d9a82007-08-27 16:49:44 -0400564
Chris Mason39279cc2007-06-12 06:35:45 -0400565 path = btrfs_alloc_path();
566 if (!path)
567 return -ENOMEM;
568 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400569 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400570 btrfs_release_path(root, path);
571 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
572 search_start, -1);
573 if (ret < 0)
574 goto out;
575 if (ret > 0) {
576 if (path->slots[0] == 0) {
577 ret = 0;
578 goto out;
579 }
580 path->slots[0]--;
581 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400582next_slot:
Chris Mason39279cc2007-06-12 06:35:45 -0400583 keep = 0;
584 bookend = 0;
585 found_extent = 0;
586 found_inline = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400587 leaf_start = 0;
588 root_gen = 0;
589 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400590 extent = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400591 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -0400592 slot = path->slots[0];
Chris Mason8c2383c2007-06-18 09:57:58 -0400593 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400594 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan72610092008-02-05 15:40:36 -0500595 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY &&
596 key.offset >= end) {
Chris Mason39279cc2007-06-12 06:35:45 -0400597 goto out;
598 }
Yan72610092008-02-05 15:40:36 -0500599 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY ||
600 key.objectid != inode->i_ino) {
Chris Mason39279cc2007-06-12 06:35:45 -0400601 goto out;
602 }
Chris Masonccd467d2007-06-28 15:57:36 -0400603 if (recow) {
604 search_start = key.offset;
605 continue;
606 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400607 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
608 extent = btrfs_item_ptr(leaf, slot,
609 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400610 found_type = btrfs_file_extent_type(leaf, extent);
Chris Mason8c2383c2007-06-18 09:57:58 -0400611 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500612 extent_end =
613 btrfs_file_extent_disk_bytenr(leaf,
614 extent);
615 if (extent_end)
616 *hint_byte = extent_end;
617
Chris Mason8c2383c2007-06-18 09:57:58 -0400618 extent_end = key.offset +
Chris Masondb945352007-10-15 16:15:53 -0400619 btrfs_file_extent_num_bytes(leaf, extent);
Chris Mason8c2383c2007-06-18 09:57:58 -0400620 found_extent = 1;
621 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400622 struct btrfs_item *item;
623 item = btrfs_item_nr(leaf, slot);
Chris Mason8c2383c2007-06-18 09:57:58 -0400624 found_inline = 1;
625 extent_end = key.offset +
Chris Mason5f39d392007-10-15 16:14:19 -0400626 btrfs_file_extent_inline_len(leaf, item);
Chris Mason8c2383c2007-06-18 09:57:58 -0400627 }
628 } else {
629 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400630 }
631
632 /* we found nothing we can drop */
Chris Mason8c2383c2007-06-18 09:57:58 -0400633 if ((!found_extent && !found_inline) ||
634 search_start >= extent_end) {
635 int nextret;
636 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400637 nritems = btrfs_header_nritems(leaf);
Chris Mason8c2383c2007-06-18 09:57:58 -0400638 if (slot >= nritems - 1) {
639 nextret = btrfs_next_leaf(root, path);
640 if (nextret)
641 goto out;
Chris Masonccd467d2007-06-28 15:57:36 -0400642 recow = 1;
Chris Mason8c2383c2007-06-18 09:57:58 -0400643 } else {
644 path->slots[0]++;
645 }
646 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400647 }
648
Chris Mason39279cc2007-06-12 06:35:45 -0400649 if (found_inline) {
Chris Mason5f39d392007-10-15 16:14:19 -0400650 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400651 search_start = (extent_end + mask) & ~mask;
652 } else
653 search_start = extent_end;
Yan6e3b9662007-12-14 11:14:42 -0500654 if (end <= extent_end && start >= key.offset && found_inline) {
Chris Mason179e29e2007-11-01 11:28:41 -0400655 *hint_byte = EXTENT_MAP_INLINE;
Zheng Yan31840ae2008-09-23 13:14:14 -0400656 goto out;
Chris Mason179e29e2007-11-01 11:28:41 -0400657 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400658
659 if (found_extent) {
660 read_extent_buffer(leaf, &old, (unsigned long)extent,
661 sizeof(old));
662 root_gen = btrfs_header_generation(leaf);
663 root_owner = btrfs_header_owner(leaf);
664 leaf_start = leaf->start;
665 }
666
Chris Mason39279cc2007-06-12 06:35:45 -0400667 if (end < extent_end && end >= key.offset) {
Chris Mason179e29e2007-11-01 11:28:41 -0400668 bookend = 1;
Yan0181e582008-01-30 14:39:54 -0500669 if (found_inline && start <= key.offset)
Chris Mason179e29e2007-11-01 11:28:41 -0400670 keep = 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400671 }
Chris Mason39279cc2007-06-12 06:35:45 -0400672 /* truncate existing extent */
673 if (start > key.offset) {
674 u64 new_num;
675 u64 old_num;
676 keep = 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400677 WARN_ON(start & (root->sectorsize - 1));
Chris Mason39279cc2007-06-12 06:35:45 -0400678 if (found_extent) {
Chris Masondb945352007-10-15 16:15:53 -0400679 new_num = start - key.offset;
680 old_num = btrfs_file_extent_num_bytes(leaf,
681 extent);
682 *hint_byte =
683 btrfs_file_extent_disk_bytenr(leaf,
684 extent);
685 if (btrfs_file_extent_disk_bytenr(leaf,
686 extent)) {
Chris Mason90692182008-02-08 13:49:28 -0500687 dec_i_blocks(inode, old_num - new_num);
Chris Mason39279cc2007-06-12 06:35:45 -0400688 }
Chris Masondb945352007-10-15 16:15:53 -0400689 btrfs_set_file_extent_num_bytes(leaf, extent,
690 new_num);
Chris Mason5f39d392007-10-15 16:14:19 -0400691 btrfs_mark_buffer_dirty(leaf);
Chris Mason00f5c792007-11-30 10:09:33 -0500692 } else if (key.offset < inline_limit &&
693 (end > extent_end) &&
694 (inline_limit < extent_end)) {
Chris Mason3326d1b2007-10-15 16:18:25 -0400695 u32 new_size;
696 new_size = btrfs_file_extent_calc_inline_size(
Chris Mason00f5c792007-11-30 10:09:33 -0500697 inline_limit - key.offset);
Chris Mason90692182008-02-08 13:49:28 -0500698 dec_i_blocks(inode, (extent_end - key.offset) -
699 (inline_limit - key.offset));
Chris Mason3326d1b2007-10-15 16:18:25 -0400700 btrfs_truncate_item(trans, root, path,
Chris Mason179e29e2007-11-01 11:28:41 -0400701 new_size, 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400702 }
703 }
704 /* delete the entire extent */
705 if (!keep) {
Chris Mason39279cc2007-06-12 06:35:45 -0400706 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400707 /* TODO update progress marker and return */
Chris Mason39279cc2007-06-12 06:35:45 -0400708 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400709 extent = NULL;
Zheng Yan31840ae2008-09-23 13:14:14 -0400710 btrfs_release_path(root, path);
711 /* the extent will be freed later */
Chris Mason39279cc2007-06-12 06:35:45 -0400712 }
Yan0181e582008-01-30 14:39:54 -0500713 if (bookend && found_inline && start <= key.offset) {
Chris Mason179e29e2007-11-01 11:28:41 -0400714 u32 new_size;
715 new_size = btrfs_file_extent_calc_inline_size(
Yan0181e582008-01-30 14:39:54 -0500716 extent_end - end);
Chris Mason90692182008-02-08 13:49:28 -0500717 dec_i_blocks(inode, (extent_end - key.offset) -
718 (extent_end - end));
Zheng Yan31840ae2008-09-23 13:14:14 -0400719 ret = btrfs_truncate_item(trans, root, path,
720 new_size, 0);
721 BUG_ON(ret);
Chris Mason179e29e2007-11-01 11:28:41 -0400722 }
Chris Mason39279cc2007-06-12 06:35:45 -0400723 /* create bookend, splitting the extent in two */
724 if (bookend && found_extent) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400725 u64 disk_bytenr;
Chris Mason39279cc2007-06-12 06:35:45 -0400726 struct btrfs_key ins;
727 ins.objectid = inode->i_ino;
728 ins.offset = end;
Chris Mason39279cc2007-06-12 06:35:45 -0400729 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -0400730 btrfs_release_path(root, path);
731 ret = btrfs_insert_empty_item(trans, root, path, &ins,
732 sizeof(*extent));
Zheng Yan31840ae2008-09-23 13:14:14 -0400733 BUG_ON(ret);
Chris Mason8c2383c2007-06-18 09:57:58 -0400734
Chris Mason5f39d392007-10-15 16:14:19 -0400735 leaf = path->nodes[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400736 extent = btrfs_item_ptr(leaf, path->slots[0],
737 struct btrfs_file_extent_item);
738 write_extent_buffer(leaf, &old,
739 (unsigned long)extent, sizeof(old));
Chris Mason39279cc2007-06-12 06:35:45 -0400740
Chris Mason5f39d392007-10-15 16:14:19 -0400741 btrfs_set_file_extent_offset(leaf, extent,
Chris Masondb945352007-10-15 16:15:53 -0400742 le64_to_cpu(old.offset) + end - key.offset);
743 WARN_ON(le64_to_cpu(old.num_bytes) <
744 (extent_end - end));
745 btrfs_set_file_extent_num_bytes(leaf, extent,
746 extent_end - end);
Chris Mason5f39d392007-10-15 16:14:19 -0400747 btrfs_set_file_extent_type(leaf, extent,
Chris Mason39279cc2007-06-12 06:35:45 -0400748 BTRFS_FILE_EXTENT_REG);
Chris Masondb945352007-10-15 16:15:53 -0400749
Chris Mason39279cc2007-06-12 06:35:45 -0400750 btrfs_mark_buffer_dirty(path->nodes[0]);
Zheng Yan31840ae2008-09-23 13:14:14 -0400751
752 disk_bytenr = le64_to_cpu(old.disk_bytenr);
753 if (disk_bytenr != 0) {
754 ret = btrfs_inc_extent_ref(trans, root,
755 disk_bytenr,
756 le64_to_cpu(old.disk_num_bytes),
757 leaf->start,
758 root->root_key.objectid,
759 trans->transid,
760 ins.objectid, ins.offset);
761 BUG_ON(ret);
762 }
763 btrfs_release_path(root, path);
764 if (disk_bytenr != 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400765 inode->i_blocks +=
Chris Masondb945352007-10-15 16:15:53 -0400766 btrfs_file_extent_num_bytes(leaf,
767 extent) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400768 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400769 }
770
771 if (found_extent && !keep) {
772 u64 disk_bytenr = le64_to_cpu(old.disk_bytenr);
773
774 if (disk_bytenr != 0) {
775 dec_i_blocks(inode, le64_to_cpu(old.num_bytes));
776 ret = btrfs_free_extent(trans, root,
777 disk_bytenr,
778 le64_to_cpu(old.disk_num_bytes),
779 leaf_start, root_owner,
780 root_gen, key.objectid,
781 key.offset, 0);
782 BUG_ON(ret);
783 *hint_byte = disk_bytenr;
784 }
785 }
786
787 if (search_start >= end) {
Chris Mason39279cc2007-06-12 06:35:45 -0400788 ret = 0;
789 goto out;
790 }
791 }
792out:
793 btrfs_free_path(path);
Chris Mason90692182008-02-08 13:49:28 -0500794 btrfs_check_file(root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400795 return ret;
796}
797
798/*
799 * this gets pages into the page cache and locks them down
800 */
Chris Masona1b32a52008-09-05 16:09:51 -0400801static int noinline prepare_pages(struct btrfs_root *root, struct file *file,
Chris Mason98ed5172008-01-03 10:01:48 -0500802 struct page **pages, size_t num_pages,
803 loff_t pos, unsigned long first_index,
804 unsigned long last_index, size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400805{
806 int i;
807 unsigned long index = pos >> PAGE_CACHE_SHIFT;
Chris Mason6da6aba2007-12-18 16:15:09 -0500808 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400809 int err = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400810 u64 start_pos;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400811 u64 last_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400812
Chris Mason5f39d392007-10-15 16:14:19 -0400813 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400814 last_pos = ((u64)index + num_pages) << PAGE_CACHE_SHIFT;
Chris Mason39279cc2007-06-12 06:35:45 -0400815
816 memset(pages, 0, num_pages * sizeof(struct page *));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400817again:
Chris Mason39279cc2007-06-12 06:35:45 -0400818 for (i = 0; i < num_pages; i++) {
819 pages[i] = grab_cache_page(inode->i_mapping, index + i);
820 if (!pages[i]) {
821 err = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -0400822 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400823 }
Chris Masonccd467d2007-06-28 15:57:36 -0400824 wait_on_page_writeback(pages[i]);
Chris Mason39279cc2007-06-12 06:35:45 -0400825 }
Chris Mason07627042008-02-19 11:29:24 -0500826 if (start_pos < inode->i_size) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400827 struct btrfs_ordered_extent *ordered;
Chris Masond99cb302008-02-19 12:55:05 -0500828 lock_extent(&BTRFS_I(inode)->io_tree,
829 start_pos, last_pos - 1, GFP_NOFS);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400830 ordered = btrfs_lookup_first_ordered_extent(inode, last_pos -1);
831 if (ordered &&
832 ordered->file_offset + ordered->len > start_pos &&
833 ordered->file_offset < last_pos) {
834 btrfs_put_ordered_extent(ordered);
835 unlock_extent(&BTRFS_I(inode)->io_tree,
836 start_pos, last_pos - 1, GFP_NOFS);
837 for (i = 0; i < num_pages; i++) {
838 unlock_page(pages[i]);
839 page_cache_release(pages[i]);
840 }
841 btrfs_wait_ordered_range(inode, start_pos,
842 last_pos - start_pos);
843 goto again;
844 }
845 if (ordered)
846 btrfs_put_ordered_extent(ordered);
847
Chris Mason07627042008-02-19 11:29:24 -0500848 clear_extent_bits(&BTRFS_I(inode)->io_tree, start_pos,
849 last_pos - 1, EXTENT_DIRTY | EXTENT_DELALLOC,
850 GFP_NOFS);
Chris Masond99cb302008-02-19 12:55:05 -0500851 unlock_extent(&BTRFS_I(inode)->io_tree,
852 start_pos, last_pos - 1, GFP_NOFS);
Chris Mason07627042008-02-19 11:29:24 -0500853 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400854 for (i = 0; i < num_pages; i++) {
Chris Masonf87f0572008-08-01 11:27:23 -0400855 clear_page_dirty_for_io(pages[i]);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400856 set_page_extent_mapped(pages[i]);
857 WARN_ON(!PageLocked(pages[i]));
858 }
Chris Mason39279cc2007-06-12 06:35:45 -0400859 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400860}
861
862static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
863 size_t count, loff_t *ppos)
864{
865 loff_t pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400866 loff_t start_pos;
867 ssize_t num_written = 0;
868 ssize_t err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400869 int ret = 0;
Chris Mason6da6aba2007-12-18 16:15:09 -0500870 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400871 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8c2383c2007-06-18 09:57:58 -0400872 struct page **pages = NULL;
873 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400874 struct page *pinned[2];
875 unsigned long first_index;
876 unsigned long last_index;
Chris Mason8c2383c2007-06-18 09:57:58 -0400877
878 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
879 PAGE_CACHE_SIZE / (sizeof(struct page *)));
Chris Mason39279cc2007-06-12 06:35:45 -0400880 pinned[0] = NULL;
881 pinned[1] = NULL;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400882
Chris Mason39279cc2007-06-12 06:35:45 -0400883 pos = *ppos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400884 start_pos = pos;
885
Chris Mason39279cc2007-06-12 06:35:45 -0400886 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
887 current->backing_dev_info = inode->i_mapping->backing_dev_info;
888 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
889 if (err)
Chris Mason1832a6d2007-12-21 16:27:21 -0500890 goto out_nolock;
Chris Mason39279cc2007-06-12 06:35:45 -0400891 if (count == 0)
Chris Mason1832a6d2007-12-21 16:27:21 -0500892 goto out_nolock;
Chris Mason2b1f55b2008-09-24 11:48:04 -0400893
Sven Wegener0ee0fda2008-07-30 16:54:26 -0400894 err = file_remove_suid(file);
Chris Mason39279cc2007-06-12 06:35:45 -0400895 if (err)
Chris Mason1832a6d2007-12-21 16:27:21 -0500896 goto out_nolock;
Chris Mason39279cc2007-06-12 06:35:45 -0400897 file_update_time(file);
898
Chris Mason8c2383c2007-06-18 09:57:58 -0400899 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -0400900
901 mutex_lock(&inode->i_mutex);
902 first_index = pos >> PAGE_CACHE_SHIFT;
903 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
904
905 /*
Chris Mason409c6112008-04-22 09:24:20 -0400906 * if this is a nodatasum mount, force summing off for the inode
907 * all the time. That way a later mount with summing on won't
908 * get confused
909 */
910 if (btrfs_test_opt(root, NODATASUM))
911 btrfs_set_flag(inode, NODATASUM);
912
913 /*
Chris Mason39279cc2007-06-12 06:35:45 -0400914 * there are lots of better ways to do this, but this code
915 * makes sure the first and last page in the file range are
916 * up to date and ready for cow
917 */
918 if ((pos & (PAGE_CACHE_SIZE - 1))) {
919 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
920 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400921 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400922 BUG_ON(ret);
923 wait_on_page_locked(pinned[0]);
924 } else {
925 unlock_page(pinned[0]);
926 }
927 }
928 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
929 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
930 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400931 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400932 BUG_ON(ret);
933 wait_on_page_locked(pinned[1]);
934 } else {
935 unlock_page(pinned[1]);
936 }
937 }
938
Chris Mason39279cc2007-06-12 06:35:45 -0400939 while(count > 0) {
940 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Chris Mason11bd1432007-06-22 14:16:24 -0400941 size_t write_bytes = min(count, nrptrs *
942 (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400943 offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400944 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
945 PAGE_CACHE_SHIFT;
946
Chris Mason8c2383c2007-06-18 09:57:58 -0400947 WARN_ON(num_pages > nrptrs);
Chris Mason39279cc2007-06-12 06:35:45 -0400948 memset(pages, 0, sizeof(pages));
Chris Mason1832a6d2007-12-21 16:27:21 -0500949
Chris Mason1832a6d2007-12-21 16:27:21 -0500950 ret = btrfs_check_free_space(root, write_bytes, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -0500951 if (ret)
952 goto out;
953
Chris Mason39279cc2007-06-12 06:35:45 -0400954 ret = prepare_pages(root, file, pages, num_pages,
955 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400956 write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400957 if (ret)
958 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -0400959
Chris Mason39279cc2007-06-12 06:35:45 -0400960 ret = btrfs_copy_from_user(pos, num_pages,
961 write_bytes, pages, buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400962 if (ret) {
963 btrfs_drop_pages(pages, num_pages);
964 goto out;
965 }
Chris Mason39279cc2007-06-12 06:35:45 -0400966
967 ret = dirty_and_release_pages(NULL, root, file, pages,
968 num_pages, pos, write_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -0400969 btrfs_drop_pages(pages, num_pages);
Chris Mason54aa1f42007-06-22 14:16:25 -0400970 if (ret)
971 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -0400972
973 buf += write_bytes;
974 count -= write_bytes;
975 pos += write_bytes;
976 num_written += write_bytes;
977
Chris Mason8c2383c2007-06-18 09:57:58 -0400978 balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
Chris Mason448d6402007-11-27 07:52:01 -0800979 if (num_pages < (root->leafsize >> PAGE_CACHE_SHIFT) + 1)
980 btrfs_btree_balance_dirty(root, 1);
Chris Masonab78c842008-07-29 16:15:18 -0400981 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400982 cond_resched();
983 }
Chris Mason39279cc2007-06-12 06:35:45 -0400984out:
Chris Mason1832a6d2007-12-21 16:27:21 -0500985 mutex_unlock(&inode->i_mutex);
Chris Mason5b92ee72008-01-03 13:46:11 -0500986
Chris Mason1832a6d2007-12-21 16:27:21 -0500987out_nolock:
Chris Mason8c2383c2007-06-18 09:57:58 -0400988 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -0400989 if (pinned[0])
990 page_cache_release(pinned[0]);
991 if (pinned[1])
992 page_cache_release(pinned[1]);
993 *ppos = pos;
Chris Mason2ff3e9b2007-10-29 14:36:41 -0400994
995 if (num_written > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
Chris Masone02119d2008-09-05 16:13:11 -0400996 struct btrfs_trans_handle *trans;
997
998 err = btrfs_fdatawrite_range(inode->i_mapping, start_pos,
999 start_pos + num_written -1,
1000 WB_SYNC_NONE);
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001001 if (err < 0)
1002 num_written = err;
Chris Masone02119d2008-09-05 16:13:11 -04001003
1004 err = btrfs_wait_on_page_writeback_range(inode->i_mapping,
1005 start_pos, start_pos + num_written - 1);
1006 if (err < 0)
1007 num_written = err;
1008
1009 trans = btrfs_start_transaction(root, 1);
1010 ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
1011 if (ret == 0) {
1012 btrfs_sync_log(trans, root);
1013 btrfs_end_transaction(trans, root);
1014 } else {
1015 btrfs_commit_transaction(trans, root);
1016 }
Chris Mason16432982008-04-10 10:23:21 -04001017 } else if (num_written > 0 && (file->f_flags & O_DIRECT)) {
1018 do_sync_mapping_range(inode->i_mapping, start_pos,
1019 start_pos + num_written - 1,
1020 SYNC_FILE_RANGE_WRITE |
1021 SYNC_FILE_RANGE_WAIT_AFTER);
Chris Mason16432982008-04-10 10:23:21 -04001022 invalidate_mapping_pages(inode->i_mapping,
1023 start_pos >> PAGE_CACHE_SHIFT,
1024 (start_pos + num_written - 1) >> PAGE_CACHE_SHIFT);
Chris Mason2ff3e9b2007-10-29 14:36:41 -04001025 }
Chris Mason39279cc2007-06-12 06:35:45 -04001026 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001027 return num_written ? num_written : err;
1028}
1029
Sage Weil6bf13c02008-06-10 10:07:39 -04001030int btrfs_release_file(struct inode * inode, struct file * filp)
Mingminge1b81e62008-05-27 10:55:43 -04001031{
Sage Weil6bf13c02008-06-10 10:07:39 -04001032 if (filp->private_data)
1033 btrfs_ioctl_trans_end(filp);
Mingminge1b81e62008-05-27 10:55:43 -04001034 return 0;
1035}
1036
Chris Masone02119d2008-09-05 16:13:11 -04001037int btrfs_sync_file(struct file *file, struct dentry *dentry, int datasync)
Chris Mason39279cc2007-06-12 06:35:45 -04001038{
1039 struct inode *inode = dentry->d_inode;
1040 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001041 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001042 struct btrfs_trans_handle *trans;
1043
1044 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001045 * check the transaction that last modified this inode
1046 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -04001047 */
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001048 if (!BTRFS_I(inode)->last_trans)
1049 goto out;
Chris Masona2135012008-06-25 16:01:30 -04001050
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001051 mutex_lock(&root->fs_info->trans_mutex);
1052 if (BTRFS_I(inode)->last_trans <=
1053 root->fs_info->last_trans_committed) {
1054 BTRFS_I(inode)->last_trans = 0;
1055 mutex_unlock(&root->fs_info->trans_mutex);
1056 goto out;
1057 }
1058 mutex_unlock(&root->fs_info->trans_mutex);
1059
Chris Mason49eb7e42008-09-11 15:53:12 -04001060 root->fs_info->tree_log_batch++;
Chris Masone02119d2008-09-05 16:13:11 -04001061 filemap_fdatawait(inode->i_mapping);
Chris Mason49eb7e42008-09-11 15:53:12 -04001062 root->fs_info->tree_log_batch++;
Chris Masone02119d2008-09-05 16:13:11 -04001063
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001064 /*
Chris Masona52d9a82007-08-27 16:49:44 -04001065 * ok we haven't committed the transaction yet, lets do a commit
1066 */
Sage Weil6bf13c02008-06-10 10:07:39 -04001067 if (file->private_data)
1068 btrfs_ioctl_trans_end(file);
1069
Chris Mason39279cc2007-06-12 06:35:45 -04001070 trans = btrfs_start_transaction(root, 1);
1071 if (!trans) {
1072 ret = -ENOMEM;
1073 goto out;
1074 }
Chris Masone02119d2008-09-05 16:13:11 -04001075
1076 ret = btrfs_log_dentry_safe(trans, root, file->f_dentry);
Chris Mason49eb7e42008-09-11 15:53:12 -04001077 if (ret < 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001078 goto out;
Chris Mason49eb7e42008-09-11 15:53:12 -04001079 }
1080
1081 /* we've logged all the items and now have a consistent
1082 * version of the file in the log. It is possible that
1083 * someone will come in and modify the file, but that's
1084 * fine because the log is consistent on disk, and we
1085 * have references to all of the file's extents
1086 *
1087 * It is possible that someone will come in and log the
1088 * file again, but that will end up using the synchronization
1089 * inside btrfs_sync_log to keep things safe.
1090 */
1091 mutex_unlock(&file->f_dentry->d_inode->i_mutex);
1092
Chris Masone02119d2008-09-05 16:13:11 -04001093 if (ret > 0) {
1094 ret = btrfs_commit_transaction(trans, root);
1095 } else {
1096 btrfs_sync_log(trans, root);
1097 ret = btrfs_end_transaction(trans, root);
1098 }
Chris Mason49eb7e42008-09-11 15:53:12 -04001099 mutex_lock(&file->f_dentry->d_inode->i_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001100out:
1101 return ret > 0 ? EIO : ret;
1102}
1103
Chris Mason9ebefb182007-06-15 13:50:00 -04001104static struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -04001105 .fault = filemap_fault,
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 Mason39279cc2007-06-12 06:35:45 -04001121 .write = btrfs_file_write,
Chris Mason9ebefb182007-06-15 13:50:00 -04001122 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -04001123 .open = generic_file_open,
Mingminge1b81e62008-05-27 10:55:43 -04001124 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04001125 .fsync = btrfs_sync_file,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001126 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001127#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04001128 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04001129#endif
1130};