blob: 844d8807e44a6aee49c9b9b1ec5d31dfb6875b96 [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"
39
40
41static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes,
42 struct page **prepared_pages,
43 const char __user * buf)
44{
45 long page_fault = 0;
46 int i;
47 int offset = pos & (PAGE_CACHE_SIZE - 1);
48
49 for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) {
50 size_t count = min_t(size_t,
51 PAGE_CACHE_SIZE - offset, write_bytes);
52 struct page *page = prepared_pages[i];
53 fault_in_pages_readable(buf, count);
54
55 /* Copy data from userspace to the current page */
56 kmap(page);
57 page_fault = __copy_from_user(page_address(page) + offset,
58 buf, count);
59 /* Flush processor's dcache for this page */
60 flush_dcache_page(page);
61 kunmap(page);
62 buf += count;
63 write_bytes -= count;
64
65 if (page_fault)
66 break;
67 }
68 return page_fault ? -EFAULT : 0;
69}
70
71static void btrfs_drop_pages(struct page **pages, size_t num_pages)
72{
73 size_t i;
74 for (i = 0; i < num_pages; i++) {
75 if (!pages[i])
76 break;
77 unlock_page(pages[i]);
78 mark_page_accessed(pages[i]);
79 page_cache_release(pages[i]);
80 }
81}
82
Chris Masona52d9a82007-08-27 16:49:44 -040083static int insert_inline_extent(struct btrfs_trans_handle *trans,
84 struct btrfs_root *root, struct inode *inode,
Chris Mason54aa1f42007-06-22 14:16:25 -040085 u64 offset, ssize_t size,
Chris Masona52d9a82007-08-27 16:49:44 -040086 struct page *page, size_t page_offset)
Chris Mason54aa1f42007-06-22 14:16:25 -040087{
88 struct btrfs_key key;
89 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -040090 struct extent_buffer *leaf;
91 char *kaddr;
92 unsigned long ptr;
Chris Mason54aa1f42007-06-22 14:16:25 -040093 struct btrfs_file_extent_item *ei;
94 u32 datasize;
95 int err = 0;
96 int ret;
97
98 path = btrfs_alloc_path();
99 if (!path)
100 return -ENOMEM;
101
Chris Mason54aa1f42007-06-22 14:16:25 -0400102 btrfs_set_trans_block_group(trans, inode);
103
104 key.objectid = inode->i_ino;
105 key.offset = offset;
Chris Mason54aa1f42007-06-22 14:16:25 -0400106 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
107 BUG_ON(size >= PAGE_CACHE_SIZE);
108 datasize = btrfs_file_extent_calc_inline_size(size);
109
110 ret = btrfs_insert_empty_item(trans, root, path, &key,
111 datasize);
112 if (ret) {
113 err = ret;
114 goto fail;
115 }
Chris Mason5f39d392007-10-15 16:14:19 -0400116 leaf = path->nodes[0];
117 ei = btrfs_item_ptr(leaf, path->slots[0],
118 struct btrfs_file_extent_item);
119 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
120 btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
Chris Mason54aa1f42007-06-22 14:16:25 -0400121 ptr = btrfs_file_extent_inline_start(ei);
122
Chris Masondb945352007-10-15 16:15:53 -0400123 kaddr = kmap_atomic(page, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -0400124 write_extent_buffer(leaf, kaddr + page_offset, ptr, size);
Chris Masondb945352007-10-15 16:15:53 -0400125 kunmap_atomic(kaddr, KM_USER1);
Chris Mason5f39d392007-10-15 16:14:19 -0400126 btrfs_mark_buffer_dirty(leaf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400127fail:
128 btrfs_free_path(path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400129 return err;
130}
131
Chris Mason39279cc2007-06-12 06:35:45 -0400132static int dirty_and_release_pages(struct btrfs_trans_handle *trans,
133 struct btrfs_root *root,
134 struct file *file,
135 struct page **pages,
136 size_t num_pages,
137 loff_t pos,
138 size_t write_bytes)
139{
Chris Mason39279cc2007-06-12 06:35:45 -0400140 int err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400141 int i;
Chris Mason39279cc2007-06-12 06:35:45 -0400142 struct inode *inode = file->f_path.dentry->d_inode;
Chris Masona52d9a82007-08-27 16:49:44 -0400143 struct extent_map *em;
144 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masondb945352007-10-15 16:15:53 -0400145 u64 hint_byte;
146 u64 num_bytes;
Chris Masona52d9a82007-08-27 16:49:44 -0400147 u64 start_pos;
148 u64 end_of_last_block;
149 u64 end_pos = pos + write_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400150 u32 inline_size;
Chris Masona52d9a82007-08-27 16:49:44 -0400151 loff_t isize = i_size_read(inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400152
Chris Masona52d9a82007-08-27 16:49:44 -0400153 em = alloc_extent_map(GFP_NOFS);
154 if (!em)
155 return -ENOMEM;
Chris Mason39279cc2007-06-12 06:35:45 -0400156
Chris Masona52d9a82007-08-27 16:49:44 -0400157 em->bdev = inode->i_sb->s_bdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400158
Chris Mason5f39d392007-10-15 16:14:19 -0400159 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400160 num_bytes = (write_bytes + pos - start_pos +
161 root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400162
Chris Mason011410b2007-09-10 19:58:36 -0400163 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Masondb945352007-10-15 16:15:53 -0400164 end_of_last_block = start_pos + num_bytes - 1;
165
Chris Masonb888db22007-08-27 16:49:44 -0400166 lock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400167 mutex_lock(&root->fs_info->fs_mutex);
168 trans = btrfs_start_transaction(root, 1);
169 if (!trans) {
170 err = -ENOMEM;
171 goto out_unlock;
172 }
173 btrfs_set_trans_block_group(trans, inode);
Chris Masondb945352007-10-15 16:15:53 -0400174 inode->i_blocks += num_bytes >> 9;
175 hint_byte = 0;
Chris Masona52d9a82007-08-27 16:49:44 -0400176
177 if ((end_of_last_block & 4095) == 0) {
Christoph Hellwig94330632007-09-10 20:02:22 -0400178 printk("strange end of last %Lu %zu %Lu\n", start_pos, write_bytes, end_of_last_block);
Chris Masona52d9a82007-08-27 16:49:44 -0400179 }
180 set_extent_uptodate(em_tree, start_pos, end_of_last_block, GFP_NOFS);
181
182 /* FIXME...EIEIO, ENOSPC and more */
183
Chris Masona52d9a82007-08-27 16:49:44 -0400184 /* insert any holes we need to create */
185 if (inode->i_size < start_pos) {
186 u64 last_pos_in_file;
187 u64 hole_size;
Chris Mason5f39d392007-10-15 16:14:19 -0400188 u64 mask = root->sectorsize - 1;
Chris Masona52d9a82007-08-27 16:49:44 -0400189 last_pos_in_file = (isize + mask) & ~mask;
190 hole_size = (start_pos - last_pos_in_file + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400191
Chris Masona52d9a82007-08-27 16:49:44 -0400192 if (last_pos_in_file < start_pos) {
Chris Mason2bf5a722007-08-30 11:54:02 -0400193 err = btrfs_drop_extents(trans, root, inode,
194 last_pos_in_file,
195 last_pos_in_file + hole_size,
Chris Masondb945352007-10-15 16:15:53 -0400196 &hint_byte);
Chris Mason2bf5a722007-08-30 11:54:02 -0400197 if (err)
198 goto failed;
199
Chris Masona52d9a82007-08-27 16:49:44 -0400200 err = btrfs_insert_file_extent(trans, root,
201 inode->i_ino,
202 last_pos_in_file,
203 0, 0, hole_size);
Chris Mason39279cc2007-06-12 06:35:45 -0400204 }
Chris Masona52d9a82007-08-27 16:49:44 -0400205 if (err)
206 goto failed;
207 }
208
209 /*
210 * either allocate an extent for the new bytes or setup the key
211 * to show we are doing inline data in the extent
212 */
Chris Masondb945352007-10-15 16:15:53 -0400213 inline_size = end_pos - start_pos;
Chris Masona52d9a82007-08-27 16:49:44 -0400214 if (isize >= PAGE_CACHE_SIZE || pos + write_bytes < inode->i_size ||
Chris Masondb945352007-10-15 16:15:53 -0400215 inline_size >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
216 inline_size >= PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400217 u64 last_end;
Chris Masona52d9a82007-08-27 16:49:44 -0400218 for (i = 0; i < num_pages; i++) {
219 struct page *p = pages[i];
220 SetPageUptodate(p);
Chris Masonb888db22007-08-27 16:49:44 -0400221 set_page_dirty(p);
Chris Masona52d9a82007-08-27 16:49:44 -0400222 }
Chris Masonb888db22007-08-27 16:49:44 -0400223 last_end = pages[num_pages -1]->index << PAGE_CACHE_SHIFT;
224 last_end += PAGE_CACHE_SIZE - 1;
225 set_extent_delalloc(em_tree, start_pos, end_of_last_block,
226 GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400227 } else {
228 struct page *p = pages[0];
Chris Masonb888db22007-08-27 16:49:44 -0400229 /* step one, delete the existing extents in this range */
Chris Mason2bf5a722007-08-30 11:54:02 -0400230 err = btrfs_drop_extents(trans, root, inode, start_pos,
Chris Mason5f39d392007-10-15 16:14:19 -0400231 (pos + write_bytes + root->sectorsize -1) &
Chris Masondb945352007-10-15 16:15:53 -0400232 ~((u64)root->sectorsize - 1), &hint_byte);
Chris Mason2bf5a722007-08-30 11:54:02 -0400233 if (err)
234 goto failed;
Chris Masonb888db22007-08-27 16:49:44 -0400235
Chris Masona52d9a82007-08-27 16:49:44 -0400236 err = insert_inline_extent(trans, root, inode, start_pos,
237 end_pos - start_pos, p, 0);
238 BUG_ON(err);
239 em->start = start_pos;
Yan6af858b2007-09-14 10:23:29 -0400240 em->end = end_pos - 1;
Chris Masona52d9a82007-08-27 16:49:44 -0400241 em->block_start = EXTENT_MAP_INLINE;
242 em->block_end = EXTENT_MAP_INLINE;
243 add_extent_mapping(em_tree, em);
244 }
245 if (end_pos > isize) {
246 i_size_write(inode, end_pos);
247 btrfs_update_inode(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400248 }
249failed:
Chris Masona52d9a82007-08-27 16:49:44 -0400250 err = btrfs_end_transaction(trans, root);
251out_unlock:
252 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masonb888db22007-08-27 16:49:44 -0400253 unlock_extent(em_tree, start_pos, end_of_last_block, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400254 free_extent_map(em);
Chris Mason011410b2007-09-10 19:58:36 -0400255 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400256 return err;
257}
258
Chris Masona52d9a82007-08-27 16:49:44 -0400259int btrfs_drop_extent_cache(struct inode *inode, u64 start, u64 end)
260{
261 struct extent_map *em;
262 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
263
264 while(1) {
265 em = lookup_extent_mapping(em_tree, start, end);
266 if (!em)
267 break;
268 remove_extent_mapping(em_tree, em);
269 /* once for us */
270 free_extent_map(em);
271 /* once for the tree*/
272 free_extent_map(em);
273 }
274 return 0;
275}
276
Chris Mason39279cc2007-06-12 06:35:45 -0400277/*
278 * this is very complex, but the basic idea is to drop all extents
279 * in the range start - end. hint_block is filled in with a block number
280 * that would be a good hint to the block allocator for this file.
281 *
282 * If an extent intersects the range but is not entirely inside the range
283 * it is either truncated or split. Anything entirely inside the range
284 * is deleted from the tree.
285 */
286int btrfs_drop_extents(struct btrfs_trans_handle *trans,
287 struct btrfs_root *root, struct inode *inode,
Chris Masondb945352007-10-15 16:15:53 -0400288 u64 start, u64 end, u64 *hint_byte)
Chris Mason39279cc2007-06-12 06:35:45 -0400289{
290 int ret;
291 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400292 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400293 int slot;
294 struct btrfs_file_extent_item *extent;
295 u64 extent_end = 0;
296 int keep;
297 struct btrfs_file_extent_item old;
298 struct btrfs_path *path;
299 u64 search_start = start;
300 int bookend;
301 int found_type;
302 int found_extent;
303 int found_inline;
Chris Masonccd467d2007-06-28 15:57:36 -0400304 int recow;
Chris Mason39279cc2007-06-12 06:35:45 -0400305
Chris Masona52d9a82007-08-27 16:49:44 -0400306 btrfs_drop_extent_cache(inode, start, end - 1);
307
Chris Mason39279cc2007-06-12 06:35:45 -0400308 path = btrfs_alloc_path();
309 if (!path)
310 return -ENOMEM;
311 while(1) {
Chris Masonccd467d2007-06-28 15:57:36 -0400312 recow = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400313 btrfs_release_path(root, path);
314 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
315 search_start, -1);
316 if (ret < 0)
317 goto out;
318 if (ret > 0) {
319 if (path->slots[0] == 0) {
320 ret = 0;
321 goto out;
322 }
323 path->slots[0]--;
324 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400325next_slot:
Chris Mason39279cc2007-06-12 06:35:45 -0400326 keep = 0;
327 bookend = 0;
328 found_extent = 0;
329 found_inline = 0;
330 extent = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400331 leaf = path->nodes[0];
Chris Mason39279cc2007-06-12 06:35:45 -0400332 slot = path->slots[0];
Chris Mason8c2383c2007-06-18 09:57:58 -0400333 ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400334 btrfs_item_key_to_cpu(leaf, &key, slot);
Chris Mason39279cc2007-06-12 06:35:45 -0400335 if (key.offset >= end || key.objectid != inode->i_ino) {
Chris Mason39279cc2007-06-12 06:35:45 -0400336 goto out;
337 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400338 if (btrfs_key_type(&key) > BTRFS_EXTENT_DATA_KEY) {
Chris Mason39279cc2007-06-12 06:35:45 -0400339 goto out;
340 }
Chris Masonccd467d2007-06-28 15:57:36 -0400341 if (recow) {
342 search_start = key.offset;
343 continue;
344 }
Chris Mason8c2383c2007-06-18 09:57:58 -0400345 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
346 extent = btrfs_item_ptr(leaf, slot,
347 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400348 found_type = btrfs_file_extent_type(leaf, extent);
Chris Mason8c2383c2007-06-18 09:57:58 -0400349 if (found_type == BTRFS_FILE_EXTENT_REG) {
350 extent_end = key.offset +
Chris Masondb945352007-10-15 16:15:53 -0400351 btrfs_file_extent_num_bytes(leaf, extent);
Chris Mason8c2383c2007-06-18 09:57:58 -0400352 found_extent = 1;
353 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400354 struct btrfs_item *item;
355 item = btrfs_item_nr(leaf, slot);
Chris Mason8c2383c2007-06-18 09:57:58 -0400356 found_inline = 1;
357 extent_end = key.offset +
Chris Mason5f39d392007-10-15 16:14:19 -0400358 btrfs_file_extent_inline_len(leaf, item);
Chris Mason8c2383c2007-06-18 09:57:58 -0400359 }
360 } else {
361 extent_end = search_start;
Chris Mason39279cc2007-06-12 06:35:45 -0400362 }
363
364 /* we found nothing we can drop */
Chris Mason8c2383c2007-06-18 09:57:58 -0400365 if ((!found_extent && !found_inline) ||
366 search_start >= extent_end) {
367 int nextret;
368 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -0400369 nritems = btrfs_header_nritems(leaf);
Chris Mason8c2383c2007-06-18 09:57:58 -0400370 if (slot >= nritems - 1) {
371 nextret = btrfs_next_leaf(root, path);
372 if (nextret)
373 goto out;
Chris Masonccd467d2007-06-28 15:57:36 -0400374 recow = 1;
Chris Mason8c2383c2007-06-18 09:57:58 -0400375 } else {
376 path->slots[0]++;
377 }
378 goto next_slot;
Chris Mason39279cc2007-06-12 06:35:45 -0400379 }
380
381 /* FIXME, there's only one inline extent allowed right now */
382 if (found_inline) {
Chris Mason5f39d392007-10-15 16:14:19 -0400383 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400384 search_start = (extent_end + mask) & ~mask;
385 } else
386 search_start = extent_end;
387
388 if (end < extent_end && end >= key.offset) {
389 if (found_extent) {
Chris Masondb945352007-10-15 16:15:53 -0400390 u64 disk_bytenr =
391 btrfs_file_extent_disk_bytenr(leaf, extent);
392 u64 disk_num_bytes =
393 btrfs_file_extent_disk_num_bytes(leaf,
Chris Mason5f39d392007-10-15 16:14:19 -0400394 extent);
395 read_extent_buffer(leaf, &old,
396 (unsigned long)extent,
397 sizeof(old));
Chris Masondb945352007-10-15 16:15:53 -0400398 if (disk_bytenr != 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400399 ret = btrfs_inc_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -0400400 disk_bytenr, disk_num_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -0400401 BUG_ON(ret);
402 }
403 }
404 WARN_ON(found_inline);
405 bookend = 1;
406 }
Chris Mason39279cc2007-06-12 06:35:45 -0400407 /* truncate existing extent */
408 if (start > key.offset) {
409 u64 new_num;
410 u64 old_num;
411 keep = 1;
Chris Mason5f39d392007-10-15 16:14:19 -0400412 WARN_ON(start & (root->sectorsize - 1));
Chris Mason39279cc2007-06-12 06:35:45 -0400413 if (found_extent) {
Chris Masondb945352007-10-15 16:15:53 -0400414 new_num = start - key.offset;
415 old_num = btrfs_file_extent_num_bytes(leaf,
416 extent);
417 *hint_byte =
418 btrfs_file_extent_disk_bytenr(leaf,
419 extent);
420 if (btrfs_file_extent_disk_bytenr(leaf,
421 extent)) {
Chris Mason39279cc2007-06-12 06:35:45 -0400422 inode->i_blocks -=
Chris Masondb945352007-10-15 16:15:53 -0400423 (old_num - new_num) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400424 }
Chris Masondb945352007-10-15 16:15:53 -0400425 btrfs_set_file_extent_num_bytes(leaf, extent,
426 new_num);
Chris Mason5f39d392007-10-15 16:14:19 -0400427 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400428 } else {
429 WARN_ON(1);
430 }
431 }
432 /* delete the entire extent */
433 if (!keep) {
Chris Masondb945352007-10-15 16:15:53 -0400434 u64 disk_bytenr = 0;
435 u64 disk_num_bytes = 0;
436 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400437 if (found_extent) {
Chris Masondb945352007-10-15 16:15:53 -0400438 disk_bytenr =
439 btrfs_file_extent_disk_bytenr(leaf,
Chris Mason5f39d392007-10-15 16:14:19 -0400440 extent);
Chris Masondb945352007-10-15 16:15:53 -0400441 disk_num_bytes =
442 btrfs_file_extent_disk_num_bytes(leaf,
Chris Mason5f39d392007-10-15 16:14:19 -0400443 extent);
Chris Masondb945352007-10-15 16:15:53 -0400444 extent_num_bytes =
445 btrfs_file_extent_num_bytes(leaf, extent);
446 *hint_byte =
447 btrfs_file_extent_disk_bytenr(leaf,
448 extent);
Chris Mason39279cc2007-06-12 06:35:45 -0400449 }
450 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400451 /* TODO update progress marker and return */
Chris Mason39279cc2007-06-12 06:35:45 -0400452 BUG_ON(ret);
453 btrfs_release_path(root, path);
454 extent = NULL;
Chris Masondb945352007-10-15 16:15:53 -0400455 if (found_extent && disk_bytenr != 0) {
456 inode->i_blocks -= extent_num_bytes >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400457 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -0400458 disk_bytenr,
459 disk_num_bytes, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400460 }
461
462 BUG_ON(ret);
463 if (!bookend && search_start >= end) {
464 ret = 0;
465 goto out;
466 }
467 if (!bookend)
468 continue;
469 }
470 /* create bookend, splitting the extent in two */
471 if (bookend && found_extent) {
472 struct btrfs_key ins;
473 ins.objectid = inode->i_ino;
474 ins.offset = end;
Chris Mason39279cc2007-06-12 06:35:45 -0400475 btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -0400476 btrfs_release_path(root, path);
477 ret = btrfs_insert_empty_item(trans, root, path, &ins,
478 sizeof(*extent));
Chris Mason8c2383c2007-06-18 09:57:58 -0400479
Chris Mason5f39d392007-10-15 16:14:19 -0400480 leaf = path->nodes[0];
Chris Mason8c2383c2007-06-18 09:57:58 -0400481 if (ret) {
Chris Mason5f39d392007-10-15 16:14:19 -0400482 btrfs_print_leaf(root, leaf);
483 printk("got %d on inserting %Lu %u %Lu start %Lu end %Lu found %Lu %Lu keep was %d\n", ret , ins.objectid, ins.type, ins.offset, start, end, key.offset, extent_end, keep);
Chris Mason8c2383c2007-06-18 09:57:58 -0400484 }
Chris Mason39279cc2007-06-12 06:35:45 -0400485 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400486 extent = btrfs_item_ptr(leaf, path->slots[0],
487 struct btrfs_file_extent_item);
488 write_extent_buffer(leaf, &old,
489 (unsigned long)extent, sizeof(old));
Chris Mason39279cc2007-06-12 06:35:45 -0400490
Chris Mason5f39d392007-10-15 16:14:19 -0400491 btrfs_set_file_extent_offset(leaf, extent,
Chris Masondb945352007-10-15 16:15:53 -0400492 le64_to_cpu(old.offset) + end - key.offset);
493 WARN_ON(le64_to_cpu(old.num_bytes) <
494 (extent_end - end));
495 btrfs_set_file_extent_num_bytes(leaf, extent,
496 extent_end - end);
Chris Mason5f39d392007-10-15 16:14:19 -0400497 btrfs_set_file_extent_type(leaf, extent,
Chris Mason39279cc2007-06-12 06:35:45 -0400498 BTRFS_FILE_EXTENT_REG);
Chris Masondb945352007-10-15 16:15:53 -0400499
Chris Mason39279cc2007-06-12 06:35:45 -0400500 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400501 if (le64_to_cpu(old.disk_bytenr) != 0) {
Chris Mason39279cc2007-06-12 06:35:45 -0400502 inode->i_blocks +=
Chris Masondb945352007-10-15 16:15:53 -0400503 btrfs_file_extent_num_bytes(leaf,
504 extent) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400505 }
506 ret = 0;
507 goto out;
508 }
509 }
510out:
511 btrfs_free_path(path);
512 return ret;
513}
514
515/*
516 * this gets pages into the page cache and locks them down
517 */
518static int prepare_pages(struct btrfs_root *root,
519 struct file *file,
520 struct page **pages,
521 size_t num_pages,
522 loff_t pos,
523 unsigned long first_index,
524 unsigned long last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400525 size_t write_bytes)
Chris Mason39279cc2007-06-12 06:35:45 -0400526{
527 int i;
528 unsigned long index = pos >> PAGE_CACHE_SHIFT;
529 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400530 int err = 0;
Chris Mason8c2383c2007-06-18 09:57:58 -0400531 u64 start_pos;
Chris Mason8c2383c2007-06-18 09:57:58 -0400532
Chris Mason5f39d392007-10-15 16:14:19 -0400533 start_pos = pos & ~((u64)root->sectorsize - 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400534
535 memset(pages, 0, num_pages * sizeof(struct page *));
536
537 for (i = 0; i < num_pages; i++) {
538 pages[i] = grab_cache_page(inode->i_mapping, index + i);
539 if (!pages[i]) {
540 err = -ENOMEM;
Chris Masona52d9a82007-08-27 16:49:44 -0400541 BUG_ON(1);
Chris Mason39279cc2007-06-12 06:35:45 -0400542 }
Chris Masonccd467d2007-06-28 15:57:36 -0400543 cancel_dirty_page(pages[i], PAGE_CACHE_SIZE);
544 wait_on_page_writeback(pages[i]);
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400545 set_page_extent_mapped(pages[i]);
Chris Masonb888db22007-08-27 16:49:44 -0400546 WARN_ON(!PageLocked(pages[i]));
Chris Mason39279cc2007-06-12 06:35:45 -0400547 }
548 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400549}
550
551static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
552 size_t count, loff_t *ppos)
553{
554 loff_t pos;
555 size_t num_written = 0;
556 int err = 0;
557 int ret = 0;
558 struct inode *inode = file->f_path.dentry->d_inode;
559 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason8c2383c2007-06-18 09:57:58 -0400560 struct page **pages = NULL;
561 int nrptrs;
Chris Mason39279cc2007-06-12 06:35:45 -0400562 struct page *pinned[2];
563 unsigned long first_index;
564 unsigned long last_index;
Chris Mason8c2383c2007-06-18 09:57:58 -0400565
566 nrptrs = min((count + PAGE_CACHE_SIZE - 1) / PAGE_CACHE_SIZE,
567 PAGE_CACHE_SIZE / (sizeof(struct page *)));
Chris Mason39279cc2007-06-12 06:35:45 -0400568 pinned[0] = NULL;
569 pinned[1] = NULL;
570 if (file->f_flags & O_DIRECT)
571 return -EINVAL;
572 pos = *ppos;
573 vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
574 current->backing_dev_info = inode->i_mapping->backing_dev_info;
575 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
576 if (err)
577 goto out;
578 if (count == 0)
579 goto out;
580 err = remove_suid(file->f_path.dentry);
581 if (err)
582 goto out;
583 file_update_time(file);
584
Chris Mason8c2383c2007-06-18 09:57:58 -0400585 pages = kmalloc(nrptrs * sizeof(struct page *), GFP_KERNEL);
Chris Mason39279cc2007-06-12 06:35:45 -0400586
587 mutex_lock(&inode->i_mutex);
588 first_index = pos >> PAGE_CACHE_SHIFT;
589 last_index = (pos + count) >> PAGE_CACHE_SHIFT;
590
591 /*
592 * there are lots of better ways to do this, but this code
593 * makes sure the first and last page in the file range are
594 * up to date and ready for cow
595 */
596 if ((pos & (PAGE_CACHE_SIZE - 1))) {
597 pinned[0] = grab_cache_page(inode->i_mapping, first_index);
598 if (!PageUptodate(pinned[0])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400599 ret = btrfs_readpage(NULL, pinned[0]);
Chris Mason39279cc2007-06-12 06:35:45 -0400600 BUG_ON(ret);
601 wait_on_page_locked(pinned[0]);
602 } else {
603 unlock_page(pinned[0]);
604 }
605 }
606 if ((pos + count) & (PAGE_CACHE_SIZE - 1)) {
607 pinned[1] = grab_cache_page(inode->i_mapping, last_index);
608 if (!PageUptodate(pinned[1])) {
Chris Mason9ebefb182007-06-15 13:50:00 -0400609 ret = btrfs_readpage(NULL, pinned[1]);
Chris Mason39279cc2007-06-12 06:35:45 -0400610 BUG_ON(ret);
611 wait_on_page_locked(pinned[1]);
612 } else {
613 unlock_page(pinned[1]);
614 }
615 }
616
Chris Mason39279cc2007-06-12 06:35:45 -0400617 while(count > 0) {
618 size_t offset = pos & (PAGE_CACHE_SIZE - 1);
Chris Mason11bd1432007-06-22 14:16:24 -0400619 size_t write_bytes = min(count, nrptrs *
620 (size_t)PAGE_CACHE_SIZE -
Chris Mason8c2383c2007-06-18 09:57:58 -0400621 offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400622 size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >>
623 PAGE_CACHE_SHIFT;
624
Chris Mason8c2383c2007-06-18 09:57:58 -0400625 WARN_ON(num_pages > nrptrs);
Chris Mason39279cc2007-06-12 06:35:45 -0400626 memset(pages, 0, sizeof(pages));
627 ret = prepare_pages(root, file, pages, num_pages,
628 pos, first_index, last_index,
Chris Mason8c2383c2007-06-18 09:57:58 -0400629 write_bytes);
Chris Mason54aa1f42007-06-22 14:16:25 -0400630 if (ret)
631 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -0400632
Chris Mason39279cc2007-06-12 06:35:45 -0400633 ret = btrfs_copy_from_user(pos, num_pages,
634 write_bytes, pages, buf);
Chris Mason54aa1f42007-06-22 14:16:25 -0400635 if (ret) {
636 btrfs_drop_pages(pages, num_pages);
637 goto out;
638 }
Chris Mason39279cc2007-06-12 06:35:45 -0400639
640 ret = dirty_and_release_pages(NULL, root, file, pages,
641 num_pages, pos, write_bytes);
Chris Mason39279cc2007-06-12 06:35:45 -0400642 btrfs_drop_pages(pages, num_pages);
Chris Mason54aa1f42007-06-22 14:16:25 -0400643 if (ret)
644 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -0400645
646 buf += write_bytes;
647 count -= write_bytes;
648 pos += write_bytes;
649 num_written += write_bytes;
650
Chris Mason8c2383c2007-06-18 09:57:58 -0400651 balance_dirty_pages_ratelimited_nr(inode->i_mapping, num_pages);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400652 btrfs_btree_balance_dirty(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -0400653 cond_resched();
654 }
Chris Mason39279cc2007-06-12 06:35:45 -0400655 mutex_unlock(&inode->i_mutex);
656out:
Chris Mason8c2383c2007-06-18 09:57:58 -0400657 kfree(pages);
Chris Mason39279cc2007-06-12 06:35:45 -0400658 if (pinned[0])
659 page_cache_release(pinned[0]);
660 if (pinned[1])
661 page_cache_release(pinned[1]);
662 *ppos = pos;
663 current->backing_dev_info = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -0400664 return num_written ? num_written : err;
665}
666
Chris Mason39279cc2007-06-12 06:35:45 -0400667static int btrfs_sync_file(struct file *file,
668 struct dentry *dentry, int datasync)
669{
670 struct inode *inode = dentry->d_inode;
671 struct btrfs_root *root = BTRFS_I(inode)->root;
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400672 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400673 struct btrfs_trans_handle *trans;
674
675 /*
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400676 * check the transaction that last modified this inode
677 * and see if its already been committed
Chris Mason39279cc2007-06-12 06:35:45 -0400678 */
679 mutex_lock(&root->fs_info->fs_mutex);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400680 if (!BTRFS_I(inode)->last_trans)
681 goto out;
682 mutex_lock(&root->fs_info->trans_mutex);
683 if (BTRFS_I(inode)->last_trans <=
684 root->fs_info->last_trans_committed) {
685 BTRFS_I(inode)->last_trans = 0;
686 mutex_unlock(&root->fs_info->trans_mutex);
687 goto out;
688 }
689 mutex_unlock(&root->fs_info->trans_mutex);
690
691 /*
Chris Masona52d9a82007-08-27 16:49:44 -0400692 * ok we haven't committed the transaction yet, lets do a commit
693 */
Chris Mason39279cc2007-06-12 06:35:45 -0400694 trans = btrfs_start_transaction(root, 1);
695 if (!trans) {
696 ret = -ENOMEM;
697 goto out;
698 }
699 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -0400700out:
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400701 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400702 return ret > 0 ? EIO : ret;
703}
704
Chris Mason9ebefb182007-06-15 13:50:00 -0400705static struct vm_operations_struct btrfs_file_vm_ops = {
Chris Mason92fee662007-07-25 12:31:35 -0400706#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
707 .nopage = filemap_nopage,
708 .populate = filemap_populate,
709#else
710 .fault = filemap_fault,
711#endif
Chris Mason9ebefb182007-06-15 13:50:00 -0400712 .page_mkwrite = btrfs_page_mkwrite,
713};
714
715static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
716{
717 vma->vm_ops = &btrfs_file_vm_ops;
718 file_accessed(filp);
719 return 0;
720}
721
Chris Mason39279cc2007-06-12 06:35:45 -0400722struct file_operations btrfs_file_operations = {
723 .llseek = generic_file_llseek,
724 .read = do_sync_read,
Chris Mason9ebefb182007-06-15 13:50:00 -0400725 .aio_read = generic_file_aio_read,
Chris Mason39279cc2007-06-12 06:35:45 -0400726 .write = btrfs_file_write,
Chris Mason9ebefb182007-06-15 13:50:00 -0400727 .mmap = btrfs_file_mmap,
Chris Mason39279cc2007-06-12 06:35:45 -0400728 .open = generic_file_open,
Chris Mason39279cc2007-06-12 06:35:45 -0400729 .fsync = btrfs_sync_file,
Christoph Hellwig34287aa2007-09-14 10:22:47 -0400730 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -0400731#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -0400732 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -0400733#endif
734};
735