blob: 55b2e14260243cfa9312cffc1bd716da6f43eb79 [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/buffer_head.h>
20#include <linux/fs.h>
21#include <linux/pagemap.h>
22#include <linux/highmem.h>
23#include <linux/time.h>
24#include <linux/init.h>
25#include <linux/string.h>
26#include <linux/smp_lock.h>
27#include <linux/backing-dev.h>
28#include <linux/mpage.h>
29#include <linux/swap.h>
30#include <linux/writeback.h>
31#include <linux/statfs.h>
32#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040033#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040034#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
40#include "ioctl.h"
41#include "print-tree.h"
42
43struct btrfs_iget_args {
44 u64 ino;
45 struct btrfs_root *root;
46};
47
48static struct inode_operations btrfs_dir_inode_operations;
49static struct inode_operations btrfs_symlink_inode_operations;
50static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040051static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040052static struct inode_operations btrfs_file_inode_operations;
53static struct address_space_operations btrfs_aops;
54static struct address_space_operations btrfs_symlink_aops;
55static struct file_operations btrfs_dir_file_operations;
Chris Mason07157aa2007-08-30 08:50:51 -040056static struct extent_map_ops btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040057
58static struct kmem_cache *btrfs_inode_cachep;
59struct kmem_cache *btrfs_trans_handle_cachep;
60struct kmem_cache *btrfs_transaction_cachep;
61struct kmem_cache *btrfs_bit_radix_cachep;
62struct kmem_cache *btrfs_path_cachep;
63
64#define S_SHIFT 12
65static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
66 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
67 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
68 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
69 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
70 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
71 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
73};
74
Chris Masonbe20aa92007-12-17 20:14:01 -050075static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -040076{
77 struct btrfs_root *root = BTRFS_I(inode)->root;
78 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -040079 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -040080 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -050081 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -040082 u64 blocksize = root->sectorsize;
Chris Masonbe20aa92007-12-17 20:14:01 -050083 struct btrfs_key ins;
84 int ret;
Chris Masonb888db22007-08-27 16:49:44 -040085
Chris Masonb888db22007-08-27 16:49:44 -040086 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -040087 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -050088 btrfs_set_trans_block_group(trans, inode);
89
Chris Masondb945352007-10-15 16:15:53 -040090 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -050091 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -040092 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -040093 start, start + num_bytes, start, &alloc_hint);
Chris Masondb945352007-10-15 16:15:53 -040094
Chris Mason179e29e2007-11-01 11:28:41 -040095 if (alloc_hint == EXTENT_MAP_INLINE)
96 goto out;
97
Chris Masonc59f8952007-12-17 20:14:04 -050098 while(num_bytes > 0) {
99 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
100 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
101 root->root_key.objectid,
102 trans->transid,
103 inode->i_ino, start, 0,
104 alloc_hint, (u64)-1, &ins, 1);
105 if (ret) {
106 WARN_ON(1);
107 goto out;
108 }
109 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
110 start, ins.objectid, ins.offset,
111 ins.offset);
112 num_bytes -= cur_alloc_size;
113 alloc_hint = ins.objectid + ins.offset;
114 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400115 }
Chris Masonb888db22007-08-27 16:49:44 -0400116out:
117 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500118 return ret;
119}
120
121static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
122{
123 u64 extent_start;
124 u64 extent_end;
125 u64 bytenr;
126 u64 cow_end;
127 struct btrfs_root *root = BTRFS_I(inode)->root;
128 struct extent_buffer *leaf;
129 int found_type;
130 struct btrfs_path *path;
131 struct btrfs_file_extent_item *item;
132 int ret;
133 int err;
134 struct btrfs_key found_key;
135
136 path = btrfs_alloc_path();
137 BUG_ON(!path);
138again:
139 ret = btrfs_lookup_file_extent(NULL, root, path,
140 inode->i_ino, start, 0);
141 if (ret < 0) {
142 btrfs_free_path(path);
143 return ret;
144 }
145
146 cow_end = end;
147 if (ret != 0) {
148 if (path->slots[0] == 0)
149 goto not_found;
150 path->slots[0]--;
151 }
152
153 leaf = path->nodes[0];
154 item = btrfs_item_ptr(leaf, path->slots[0],
155 struct btrfs_file_extent_item);
156
157 /* are we inside the extent that was found? */
158 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
159 found_type = btrfs_key_type(&found_key);
160 if (found_key.objectid != inode->i_ino ||
161 found_type != BTRFS_EXTENT_DATA_KEY) {
162 goto not_found;
163 }
164
165 found_type = btrfs_file_extent_type(leaf, item);
166 extent_start = found_key.offset;
167 if (found_type == BTRFS_FILE_EXTENT_REG) {
168 extent_end = extent_start +
169 btrfs_file_extent_num_bytes(leaf, item);
170 err = 0;
171
172 if (start < extent_start || start >= extent_end)
173 goto not_found;
174
175 cow_end = min(end, extent_end - 1);
176 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
177 if (bytenr == 0)
178 goto not_found;
179
180 bytenr += btrfs_file_extent_offset(leaf, item);
181 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
182 goto not_found;
183 }
184
185 start = extent_end;
186 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
187 goto not_found;
188 }
189loop:
190 if (start > end) {
191 btrfs_free_path(path);
192 return 0;
193 }
194 btrfs_release_path(root, path);
195 goto again;
196
197not_found:
198 cow_file_range(inode, start, cow_end);
199 start = cow_end + 1;
200 goto loop;
201}
202
203static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
204{
205 struct btrfs_root *root = BTRFS_I(inode)->root;
206 int ret;
207
208 mutex_lock(&root->fs_info->fs_mutex);
209 if (btrfs_test_opt(root, NODATACOW))
210 ret = run_delalloc_nocow(inode, start, end);
211 else
212 ret = cow_file_range(inode, start, end);
Chris Masonb888db22007-08-27 16:49:44 -0400213 mutex_unlock(&root->fs_info->fs_mutex);
214 return ret;
215}
216
Chris Mason07157aa2007-08-30 08:50:51 -0400217int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
218{
219 struct inode *inode = page->mapping->host;
220 struct btrfs_root *root = BTRFS_I(inode)->root;
221 struct btrfs_trans_handle *trans;
222 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500223 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400224 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400225 size_t offset = start - page_start;
226
Chris Masonb6cda9b2007-12-14 15:30:32 -0500227 if (btrfs_test_opt(root, NODATASUM))
228 return 0;
229
Chris Mason07157aa2007-08-30 08:50:51 -0400230 mutex_lock(&root->fs_info->fs_mutex);
231 trans = btrfs_start_transaction(root, 1);
232 btrfs_set_trans_block_group(trans, inode);
233 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400234 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400235 start, kaddr + offset, end - start + 1);
236 kunmap(page);
237 ret = btrfs_end_transaction(trans, root);
238 BUG_ON(ret);
239 mutex_unlock(&root->fs_info->fs_mutex);
240 return ret;
241}
242
243int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
244{
245 int ret = 0;
246 struct inode *inode = page->mapping->host;
247 struct btrfs_root *root = BTRFS_I(inode)->root;
248 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
249 struct btrfs_csum_item *item;
250 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400251 u32 csum;
Chris Mason07157aa2007-08-30 08:50:51 -0400252
Chris Masonb6cda9b2007-12-14 15:30:32 -0500253 if (btrfs_test_opt(root, NODATASUM))
254 return 0;
255
Chris Mason07157aa2007-08-30 08:50:51 -0400256 mutex_lock(&root->fs_info->fs_mutex);
257 path = btrfs_alloc_path();
258 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
259 if (IS_ERR(item)) {
260 ret = PTR_ERR(item);
261 /* a csum that isn't present is a preallocated region. */
262 if (ret == -ENOENT || ret == -EFBIG)
263 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400264 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400265 goto out;
266 }
Chris Masonff79f812007-10-15 16:22:25 -0400267 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
268 BTRFS_CRC32_SIZE);
269 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400270out:
271 if (path)
272 btrfs_free_path(path);
273 mutex_unlock(&root->fs_info->fs_mutex);
274 return ret;
275}
276
277int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
278{
Chris Mason35ebb932007-10-30 16:56:53 -0400279 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400280 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400281 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
282 char *kaddr;
283 u64 private;
284 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400285 struct btrfs_root *root = BTRFS_I(inode)->root;
286 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400287 unsigned long flags;
Chris Mason07157aa2007-08-30 08:50:51 -0400288
Chris Masonb6cda9b2007-12-14 15:30:32 -0500289 if (btrfs_test_opt(root, NODATASUM))
290 return 0;
291
Chris Mason07157aa2007-08-30 08:50:51 -0400292 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400293 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400294 kaddr = kmap_atomic(page, KM_IRQ0);
295 if (ret) {
296 goto zeroit;
297 }
Chris Masonff79f812007-10-15 16:22:25 -0400298 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
299 btrfs_csum_final(csum, (char *)&csum);
300 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400301 goto zeroit;
302 }
303 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400304 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400305 return 0;
306
307zeroit:
308 printk("btrfs csum failed ino %lu off %llu\n",
309 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400310 memset(kaddr + offset, 1, end - start + 1);
311 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400312 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400313 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400314 return 0;
315}
Chris Masonb888db22007-08-27 16:49:44 -0400316
Chris Mason39279cc2007-06-12 06:35:45 -0400317void btrfs_read_locked_inode(struct inode *inode)
318{
319 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400320 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400321 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400322 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400323 struct btrfs_root *root = BTRFS_I(inode)->root;
324 struct btrfs_key location;
325 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400326 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400327 int ret;
328
329 path = btrfs_alloc_path();
330 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400331 mutex_lock(&root->fs_info->fs_mutex);
332
333 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
334 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400335 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400336 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400337
Chris Mason5f39d392007-10-15 16:14:19 -0400338 leaf = path->nodes[0];
339 inode_item = btrfs_item_ptr(leaf, path->slots[0],
340 struct btrfs_inode_item);
341
342 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
343 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
344 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
345 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
346 inode->i_size = btrfs_inode_size(leaf, inode_item);
347
348 tspec = btrfs_inode_atime(inode_item);
349 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
350 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
351
352 tspec = btrfs_inode_mtime(inode_item);
353 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
354 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
355
356 tspec = btrfs_inode_ctime(inode_item);
357 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
358 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
359
360 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
361 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400362 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400363 rdev = btrfs_inode_rdev(leaf, inode_item);
364
365 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400366 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
367 alloc_group_block);
368
369 btrfs_free_path(path);
370 inode_item = NULL;
371
372 mutex_unlock(&root->fs_info->fs_mutex);
373
374 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400375 case S_IFREG:
376 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400377 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400378 inode->i_fop = &btrfs_file_operations;
379 inode->i_op = &btrfs_file_inode_operations;
380 break;
381 case S_IFDIR:
382 inode->i_fop = &btrfs_dir_file_operations;
383 if (root == root->fs_info->tree_root)
384 inode->i_op = &btrfs_dir_ro_inode_operations;
385 else
386 inode->i_op = &btrfs_dir_inode_operations;
387 break;
388 case S_IFLNK:
389 inode->i_op = &btrfs_symlink_inode_operations;
390 inode->i_mapping->a_ops = &btrfs_symlink_aops;
391 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400392 default:
393 init_special_inode(inode, inode->i_mode, rdev);
394 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400395 }
396 return;
397
398make_bad:
399 btrfs_release_path(root, path);
400 btrfs_free_path(path);
401 mutex_unlock(&root->fs_info->fs_mutex);
402 make_bad_inode(inode);
403}
404
Chris Mason5f39d392007-10-15 16:14:19 -0400405static void fill_inode_item(struct extent_buffer *leaf,
406 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400407 struct inode *inode)
408{
Chris Mason5f39d392007-10-15 16:14:19 -0400409 btrfs_set_inode_uid(leaf, item, inode->i_uid);
410 btrfs_set_inode_gid(leaf, item, inode->i_gid);
411 btrfs_set_inode_size(leaf, item, inode->i_size);
412 btrfs_set_inode_mode(leaf, item, inode->i_mode);
413 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
414
415 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
416 inode->i_atime.tv_sec);
417 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
418 inode->i_atime.tv_nsec);
419
420 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
421 inode->i_mtime.tv_sec);
422 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
423 inode->i_mtime.tv_nsec);
424
425 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
426 inode->i_ctime.tv_sec);
427 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
428 inode->i_ctime.tv_nsec);
429
430 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
431 btrfs_set_inode_generation(leaf, item, inode->i_generation);
432 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
433 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400434 BTRFS_I(inode)->block_group->key.objectid);
435}
436
Chris Masona52d9a82007-08-27 16:49:44 -0400437int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400438 struct btrfs_root *root,
439 struct inode *inode)
440{
441 struct btrfs_inode_item *inode_item;
442 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400443 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400444 int ret;
445
446 path = btrfs_alloc_path();
447 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400448 ret = btrfs_lookup_inode(trans, root, path,
449 &BTRFS_I(inode)->location, 1);
450 if (ret) {
451 if (ret > 0)
452 ret = -ENOENT;
453 goto failed;
454 }
455
Chris Mason5f39d392007-10-15 16:14:19 -0400456 leaf = path->nodes[0];
457 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400458 struct btrfs_inode_item);
459
Chris Mason5f39d392007-10-15 16:14:19 -0400460 fill_inode_item(leaf, inode_item, inode);
461 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400462 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400463 ret = 0;
464failed:
465 btrfs_release_path(root, path);
466 btrfs_free_path(path);
467 return ret;
468}
469
470
471static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
472 struct btrfs_root *root,
473 struct inode *dir,
474 struct dentry *dentry)
475{
476 struct btrfs_path *path;
477 const char *name = dentry->d_name.name;
478 int name_len = dentry->d_name.len;
479 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400480 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400481 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400482 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400483
484 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400485 if (!path) {
486 ret = -ENOMEM;
487 goto err;
488 }
489
Chris Mason39279cc2007-06-12 06:35:45 -0400490 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
491 name, name_len, -1);
492 if (IS_ERR(di)) {
493 ret = PTR_ERR(di);
494 goto err;
495 }
496 if (!di) {
497 ret = -ENOENT;
498 goto err;
499 }
Chris Mason5f39d392007-10-15 16:14:19 -0400500 leaf = path->nodes[0];
501 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400502 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400503 if (ret)
504 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400505 btrfs_release_path(root, path);
506
507 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400508 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400509 if (IS_ERR(di)) {
510 ret = PTR_ERR(di);
511 goto err;
512 }
513 if (!di) {
514 ret = -ENOENT;
515 goto err;
516 }
517 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400518
519 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500520 ret = btrfs_del_inode_ref(trans, root, name, name_len,
521 dentry->d_inode->i_ino,
522 dentry->d_parent->d_inode->i_ino);
523 if (ret) {
524 printk("failed to delete reference to %.*s, "
525 "inode %lu parent %lu\n", name_len, name,
526 dentry->d_inode->i_ino,
527 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500528 }
Chris Mason39279cc2007-06-12 06:35:45 -0400529err:
530 btrfs_free_path(path);
531 if (!ret) {
532 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400533 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400534 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500535#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
536 dentry->d_inode->i_nlink--;
537#else
Chris Mason39279cc2007-06-12 06:35:45 -0400538 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500539#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400540 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400541 dir->i_sb->s_dirt = 1;
542 }
543 return ret;
544}
545
546static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
547{
548 struct btrfs_root *root;
549 struct btrfs_trans_handle *trans;
550 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400551 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400552
553 root = BTRFS_I(dir)->root;
554 mutex_lock(&root->fs_info->fs_mutex);
555 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400556
Chris Mason39279cc2007-06-12 06:35:45 -0400557 btrfs_set_trans_block_group(trans, dir);
558 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400559 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400560
Chris Mason39279cc2007-06-12 06:35:45 -0400561 btrfs_end_transaction(trans, root);
562 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400563 btrfs_btree_balance_dirty(root, nr);
Chris Mason5f39d392007-10-15 16:14:19 -0400564
Chris Mason39279cc2007-06-12 06:35:45 -0400565 return ret;
566}
567
568static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
569{
570 struct inode *inode = dentry->d_inode;
571 int err;
572 int ret;
573 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400574 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400575 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400576
Yan134d4512007-10-25 15:49:25 -0400577 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
578 return -ENOTEMPTY;
579
Chris Mason39279cc2007-06-12 06:35:45 -0400580 mutex_lock(&root->fs_info->fs_mutex);
581 trans = btrfs_start_transaction(root, 1);
582 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400583
584 /* now the directory is empty */
585 err = btrfs_unlink_trans(trans, root, dir, dentry);
586 if (!err) {
587 inode->i_size = 0;
588 }
Chris Mason39544012007-12-12 14:38:19 -0500589
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400590 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400591 ret = btrfs_end_transaction(trans, root);
Yan134d4512007-10-25 15:49:25 -0400592 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400593 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -0500594
Chris Mason39279cc2007-06-12 06:35:45 -0400595 if (ret && !err)
596 err = ret;
597 return err;
598}
599
600static int btrfs_free_inode(struct btrfs_trans_handle *trans,
601 struct btrfs_root *root,
602 struct inode *inode)
603{
604 struct btrfs_path *path;
605 int ret;
606
607 clear_inode(inode);
608
609 path = btrfs_alloc_path();
610 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400611 ret = btrfs_lookup_inode(trans, root, path,
612 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400613 if (ret > 0)
614 ret = -ENOENT;
615 if (!ret)
616 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400617 btrfs_free_path(path);
618 return ret;
619}
620
621/*
Chris Mason39279cc2007-06-12 06:35:45 -0400622 * this can truncate away extent items, csum items and directory items.
623 * It starts at a high offset and removes keys until it can't find
624 * any higher than i_size.
625 *
626 * csum items that cross the new i_size are truncated to the new size
627 * as well.
628 */
629static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
630 struct btrfs_root *root,
631 struct inode *inode)
632{
633 int ret;
634 struct btrfs_path *path;
635 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400636 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400637 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400638 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400639 struct btrfs_file_extent_item *fi;
640 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400641 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400642 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500643 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500644 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400645 int found_extent;
646 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400647 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400648
Chris Masona52d9a82007-08-27 16:49:44 -0400649 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400650 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400651 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400652 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400653
Chris Mason39279cc2007-06-12 06:35:45 -0400654 /* FIXME, add redo link to tree so we don't leak on crash */
655 key.objectid = inode->i_ino;
656 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400657 key.type = (u8)-1;
658
Chris Mason39279cc2007-06-12 06:35:45 -0400659 while(1) {
660 btrfs_init_path(path);
661 fi = NULL;
662 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
663 if (ret < 0) {
664 goto error;
665 }
666 if (ret > 0) {
667 BUG_ON(path->slots[0] == 0);
668 path->slots[0]--;
669 }
Chris Mason5f39d392007-10-15 16:14:19 -0400670 leaf = path->nodes[0];
671 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
672 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400673
Chris Mason5f39d392007-10-15 16:14:19 -0400674 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400675 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400676
Chris Mason39279cc2007-06-12 06:35:45 -0400677 if (found_type != BTRFS_CSUM_ITEM_KEY &&
678 found_type != BTRFS_DIR_ITEM_KEY &&
679 found_type != BTRFS_DIR_INDEX_KEY &&
680 found_type != BTRFS_EXTENT_DATA_KEY)
681 break;
682
Chris Mason5f39d392007-10-15 16:14:19 -0400683 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400684 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400685 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400686 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400687 extent_type = btrfs_file_extent_type(leaf, fi);
688 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400689 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400690 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400691 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
692 struct btrfs_item *item = btrfs_item_nr(leaf,
693 path->slots[0]);
694 item_end += btrfs_file_extent_inline_len(leaf,
695 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400696 }
Yan008630c2007-11-07 13:31:09 -0500697 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400698 }
699 if (found_type == BTRFS_CSUM_ITEM_KEY) {
700 ret = btrfs_csum_truncate(trans, root, path,
701 inode->i_size);
702 BUG_ON(ret);
703 }
Yan008630c2007-11-07 13:31:09 -0500704 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400705 if (found_type == BTRFS_DIR_ITEM_KEY) {
706 found_type = BTRFS_INODE_ITEM_KEY;
707 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
708 found_type = BTRFS_CSUM_ITEM_KEY;
709 } else if (found_type) {
710 found_type--;
711 } else {
712 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400713 }
Yana61721d2007-09-17 11:08:38 -0400714 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400715 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400716 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400717 }
Chris Mason5f39d392007-10-15 16:14:19 -0400718 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400719 del_item = 1;
720 else
721 del_item = 0;
722 found_extent = 0;
723
724 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400725 if (found_type != BTRFS_EXTENT_DATA_KEY)
726 goto delete;
727
728 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400729 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400730 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400731 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400732 u64 orig_num_bytes =
733 btrfs_file_extent_num_bytes(leaf, fi);
734 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400735 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400736 btrfs_set_file_extent_num_bytes(leaf, fi,
737 extent_num_bytes);
738 num_dec = (orig_num_bytes -
739 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400740 if (extent_start != 0) {
741 inode->i_blocks -= num_dec;
742 }
Chris Mason5f39d392007-10-15 16:14:19 -0400743 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400744 } else {
Chris Masondb945352007-10-15 16:15:53 -0400745 extent_num_bytes =
746 btrfs_file_extent_disk_num_bytes(leaf,
747 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400748 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400749 num_dec = btrfs_file_extent_num_bytes(leaf,
750 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400751 if (extent_start != 0) {
752 found_extent = 1;
753 inode->i_blocks -= num_dec;
754 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500755 root_gen = btrfs_header_generation(leaf);
756 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400757 }
Chris Mason179e29e2007-11-01 11:28:41 -0400758 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
759 !del_item) {
760 u32 newsize = inode->i_size - found_key.offset;
761 newsize = btrfs_file_extent_calc_inline_size(newsize);
762 ret = btrfs_truncate_item(trans, root, path,
763 newsize, 1);
764 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400765 }
Chris Mason179e29e2007-11-01 11:28:41 -0400766delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400767 if (del_item) {
768 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400769 if (ret)
770 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400771 } else {
772 break;
773 }
774 btrfs_release_path(root, path);
775 if (found_extent) {
776 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500777 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500778 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500779 root_gen, inode->i_ino,
780 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400781 BUG_ON(ret);
782 }
783 }
784 ret = 0;
785error:
786 btrfs_release_path(root, path);
787 btrfs_free_path(path);
788 inode->i_sb->s_dirt = 1;
789 return ret;
790}
791
Chris Masonb888db22007-08-27 16:49:44 -0400792static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400793 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400794{
Chris Mason39279cc2007-06-12 06:35:45 -0400795 char *kaddr;
796 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400797 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400798 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400799 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400800
Chris Mason190662b2007-12-18 16:25:45 -0500801 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400802 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400803
Chris Masonb888db22007-08-27 16:49:44 -0400804 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
805 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
806 page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400807 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400808 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400809 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
810 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400811 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400812 }
Chris Masonb888db22007-08-27 16:49:44 -0400813 set_page_dirty(page);
814 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400815
Chris Masona52d9a82007-08-27 16:49:44 -0400816 return ret;
817}
818
819/*
820 * taken from block_truncate_page, but does cow as it zeros out
821 * any bytes left in the last page in the file.
822 */
823static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
824{
825 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400826 struct btrfs_root *root = BTRFS_I(inode)->root;
827 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400828 pgoff_t index = from >> PAGE_CACHE_SHIFT;
829 unsigned offset = from & (PAGE_CACHE_SIZE-1);
830 struct page *page;
831 int ret = 0;
832 u64 page_start;
833
834 if ((offset & (blocksize - 1)) == 0)
835 goto out;
836
Chris Masondb945352007-10-15 16:15:53 -0400837 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400838 ret = -ENOMEM;
839 page = grab_cache_page(mapping, index);
840 if (!page)
841 goto out;
842 if (!PageUptodate(page)) {
843 ret = btrfs_readpage(NULL, page);
844 lock_page(page);
845 if (!PageUptodate(page)) {
846 ret = -EIO;
847 goto out;
848 }
849 }
Chris Mason35ebb932007-10-30 16:56:53 -0400850 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400851
Chris Masonb888db22007-08-27 16:49:44 -0400852 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400853
Chris Mason39279cc2007-06-12 06:35:45 -0400854 unlock_page(page);
855 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400856 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400857out:
858 return ret;
859}
860
861static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
862{
863 struct inode *inode = dentry->d_inode;
864 int err;
865
866 err = inode_change_ok(inode, attr);
867 if (err)
868 return err;
869
870 if (S_ISREG(inode->i_mode) &&
871 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
872 struct btrfs_trans_handle *trans;
873 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400874 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
875
Chris Mason5f39d392007-10-15 16:14:19 -0400876 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400877 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400878 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400879 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400880 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400881
882 if (attr->ia_size <= pos)
883 goto out;
884
885 btrfs_truncate_page(inode->i_mapping, inode->i_size);
886
Chris Mason2bf5a722007-08-30 11:54:02 -0400887 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400888 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400889
890 mutex_lock(&root->fs_info->fs_mutex);
891 trans = btrfs_start_transaction(root, 1);
892 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400893 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400894 pos, pos + hole_size, pos,
895 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400896
Chris Mason179e29e2007-11-01 11:28:41 -0400897 if (alloc_hint != EXTENT_MAP_INLINE) {
898 err = btrfs_insert_file_extent(trans, root,
899 inode->i_ino,
900 pos, 0, 0, hole_size);
901 }
Chris Mason39279cc2007-06-12 06:35:45 -0400902 btrfs_end_transaction(trans, root);
903 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400904 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400905 if (err)
906 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400907 }
908out:
909 err = inode_setattr(inode, attr);
910
911 return err;
912}
913void btrfs_delete_inode(struct inode *inode)
914{
915 struct btrfs_trans_handle *trans;
916 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400917 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400918 int ret;
919
920 truncate_inode_pages(&inode->i_data, 0);
921 if (is_bad_inode(inode)) {
922 goto no_delete;
923 }
Chris Mason5f39d392007-10-15 16:14:19 -0400924
Chris Mason39279cc2007-06-12 06:35:45 -0400925 inode->i_size = 0;
926 mutex_lock(&root->fs_info->fs_mutex);
927 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400928
Chris Mason39279cc2007-06-12 06:35:45 -0400929 btrfs_set_trans_block_group(trans, inode);
930 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400931 if (ret)
932 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -0500933 ret = btrfs_delete_xattrs(trans, root, inode);
934 if (ret)
935 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -0400936 ret = btrfs_free_inode(trans, root, inode);
937 if (ret)
938 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400939 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400940
Chris Mason39279cc2007-06-12 06:35:45 -0400941 btrfs_end_transaction(trans, root);
942 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400943 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400944 return;
Chris Mason54aa1f42007-06-22 14:16:25 -0400945
946no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400947 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400948 btrfs_end_transaction(trans, root);
949 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400950 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400951no_delete:
952 clear_inode(inode);
953}
954
955/*
956 * this returns the key found in the dir entry in the location pointer.
957 * If no dir entries were found, location->objectid is 0.
958 */
959static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
960 struct btrfs_key *location)
961{
962 const char *name = dentry->d_name.name;
963 int namelen = dentry->d_name.len;
964 struct btrfs_dir_item *di;
965 struct btrfs_path *path;
966 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -0400967 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400968
Chris Mason39544012007-12-12 14:38:19 -0500969 if (namelen == 1 && strcmp(name, ".") == 0) {
970 location->objectid = dir->i_ino;
971 location->type = BTRFS_INODE_ITEM_KEY;
972 location->offset = 0;
973 return 0;
974 }
Chris Mason39279cc2007-06-12 06:35:45 -0400975 path = btrfs_alloc_path();
976 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -0500977
Chris Mason7a720532007-12-13 09:06:59 -0500978 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -0500979 struct btrfs_key key;
980 struct extent_buffer *leaf;
981 u32 nritems;
982 int slot;
983
984 key.objectid = dir->i_ino;
985 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
986 key.offset = 0;
987 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
988 BUG_ON(ret == 0);
989 ret = 0;
990
991 leaf = path->nodes[0];
992 slot = path->slots[0];
993 nritems = btrfs_header_nritems(leaf);
994 if (slot >= nritems)
995 goto out_err;
996
997 btrfs_item_key_to_cpu(leaf, &key, slot);
998 if (key.objectid != dir->i_ino ||
999 key.type != BTRFS_INODE_REF_KEY) {
1000 goto out_err;
1001 }
1002 location->objectid = key.offset;
1003 location->type = BTRFS_INODE_ITEM_KEY;
1004 location->offset = 0;
1005 goto out;
1006 }
1007
Chris Mason39279cc2007-06-12 06:35:45 -04001008 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1009 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001010 if (IS_ERR(di))
1011 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001012 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001013 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001014 }
Chris Mason5f39d392007-10-15 16:14:19 -04001015 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001016out:
Chris Mason39279cc2007-06-12 06:35:45 -04001017 btrfs_free_path(path);
1018 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001019out_err:
1020 location->objectid = 0;
1021 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001022}
1023
1024/*
1025 * when we hit a tree root in a directory, the btrfs part of the inode
1026 * needs to be changed to reflect the root directory of the tree root. This
1027 * is kind of like crossing a mount point.
1028 */
1029static int fixup_tree_root_location(struct btrfs_root *root,
1030 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001031 struct btrfs_root **sub_root,
1032 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001033{
1034 struct btrfs_path *path;
1035 struct btrfs_root_item *ri;
1036
1037 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1038 return 0;
1039 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1040 return 0;
1041
1042 path = btrfs_alloc_path();
1043 BUG_ON(!path);
1044 mutex_lock(&root->fs_info->fs_mutex);
1045
Josef Bacik58176a92007-08-29 15:47:34 -04001046 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1047 dentry->d_name.name,
1048 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001049 if (IS_ERR(*sub_root))
1050 return PTR_ERR(*sub_root);
1051
1052 ri = &(*sub_root)->root_item;
1053 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001054 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1055 location->offset = 0;
1056
1057 btrfs_free_path(path);
1058 mutex_unlock(&root->fs_info->fs_mutex);
1059 return 0;
1060}
1061
1062static int btrfs_init_locked_inode(struct inode *inode, void *p)
1063{
1064 struct btrfs_iget_args *args = p;
1065 inode->i_ino = args->ino;
1066 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -04001067 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1068 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001069 return 0;
1070}
1071
1072static int btrfs_find_actor(struct inode *inode, void *opaque)
1073{
1074 struct btrfs_iget_args *args = opaque;
1075 return (args->ino == inode->i_ino &&
1076 args->root == BTRFS_I(inode)->root);
1077}
1078
1079struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1080 struct btrfs_root *root)
1081{
1082 struct inode *inode;
1083 struct btrfs_iget_args args;
1084 args.ino = objectid;
1085 args.root = root;
1086
1087 inode = iget5_locked(s, objectid, btrfs_find_actor,
1088 btrfs_init_locked_inode,
1089 (void *)&args);
1090 return inode;
1091}
1092
1093static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1094 struct nameidata *nd)
1095{
1096 struct inode * inode;
1097 struct btrfs_inode *bi = BTRFS_I(dir);
1098 struct btrfs_root *root = bi->root;
1099 struct btrfs_root *sub_root = root;
1100 struct btrfs_key location;
1101 int ret;
1102
1103 if (dentry->d_name.len > BTRFS_NAME_LEN)
1104 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001105
Chris Mason39279cc2007-06-12 06:35:45 -04001106 mutex_lock(&root->fs_info->fs_mutex);
1107 ret = btrfs_inode_by_name(dir, dentry, &location);
1108 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001109
Chris Mason39279cc2007-06-12 06:35:45 -04001110 if (ret < 0)
1111 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001112
Chris Mason39279cc2007-06-12 06:35:45 -04001113 inode = NULL;
1114 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001115 ret = fixup_tree_root_location(root, &location, &sub_root,
1116 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001117 if (ret < 0)
1118 return ERR_PTR(ret);
1119 if (ret > 0)
1120 return ERR_PTR(-ENOENT);
1121 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1122 sub_root);
1123 if (!inode)
1124 return ERR_PTR(-EACCES);
1125 if (inode->i_state & I_NEW) {
1126 /* the inode and parent dir are two different roots */
1127 if (sub_root != root) {
1128 igrab(inode);
1129 sub_root->inode = inode;
1130 }
1131 BTRFS_I(inode)->root = sub_root;
1132 memcpy(&BTRFS_I(inode)->location, &location,
1133 sizeof(location));
1134 btrfs_read_locked_inode(inode);
1135 unlock_new_inode(inode);
1136 }
1137 }
1138 return d_splice_alias(inode, dentry);
1139}
1140
Chris Mason39279cc2007-06-12 06:35:45 -04001141static unsigned char btrfs_filetype_table[] = {
1142 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1143};
1144
1145static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1146{
Chris Mason6da6aba2007-12-18 16:15:09 -05001147 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001148 struct btrfs_root *root = BTRFS_I(inode)->root;
1149 struct btrfs_item *item;
1150 struct btrfs_dir_item *di;
1151 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001152 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001153 struct btrfs_path *path;
1154 int ret;
1155 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001156 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001157 int slot;
1158 int advance;
1159 unsigned char d_type;
1160 int over = 0;
1161 u32 di_cur;
1162 u32 di_total;
1163 u32 di_len;
1164 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001165 char tmp_name[32];
1166 char *name_ptr;
1167 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001168
1169 /* FIXME, use a real flag for deciding about the key type */
1170 if (root->fs_info->tree_root == root)
1171 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001172
Chris Mason39544012007-12-12 14:38:19 -05001173 /* special case for "." */
1174 if (filp->f_pos == 0) {
1175 over = filldir(dirent, ".", 1,
1176 1, inode->i_ino,
1177 DT_DIR);
1178 if (over)
1179 return 0;
1180 filp->f_pos = 1;
1181 }
1182
Chris Mason39279cc2007-06-12 06:35:45 -04001183 mutex_lock(&root->fs_info->fs_mutex);
1184 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001185 path = btrfs_alloc_path();
1186 path->reada = 2;
1187
1188 /* special case for .., just use the back ref */
1189 if (filp->f_pos == 1) {
1190 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1191 key.offset = 0;
1192 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1193 BUG_ON(ret == 0);
1194 leaf = path->nodes[0];
1195 slot = path->slots[0];
1196 nritems = btrfs_header_nritems(leaf);
1197 if (slot >= nritems) {
1198 btrfs_release_path(root, path);
1199 goto read_dir_items;
1200 }
1201 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1202 btrfs_release_path(root, path);
1203 if (found_key.objectid != key.objectid ||
1204 found_key.type != BTRFS_INODE_REF_KEY)
1205 goto read_dir_items;
1206 over = filldir(dirent, "..", 2,
1207 2, found_key.offset, DT_DIR);
1208 if (over)
1209 goto nopos;
1210 filp->f_pos = 2;
1211 }
1212
1213read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001214 btrfs_set_key_type(&key, key_type);
1215 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001216
Chris Mason39279cc2007-06-12 06:35:45 -04001217 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1218 if (ret < 0)
1219 goto err;
1220 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001221 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001222 leaf = path->nodes[0];
1223 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001224 slot = path->slots[0];
1225 if (advance || slot >= nritems) {
1226 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001227 ret = btrfs_next_leaf(root, path);
1228 if (ret)
1229 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001230 leaf = path->nodes[0];
1231 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001232 slot = path->slots[0];
1233 } else {
1234 slot++;
1235 path->slots[0]++;
1236 }
1237 }
1238 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001239 item = btrfs_item_nr(leaf, slot);
1240 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1241
1242 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001243 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001244 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001245 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001246 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001247 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001248
1249 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001250 advance = 1;
1251 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1252 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001253 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001254 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001255 struct btrfs_key location;
1256
1257 name_len = btrfs_dir_name_len(leaf, di);
1258 if (name_len < 32) {
1259 name_ptr = tmp_name;
1260 } else {
1261 name_ptr = kmalloc(name_len, GFP_NOFS);
1262 BUG_ON(!name_ptr);
1263 }
1264 read_extent_buffer(leaf, name_ptr,
1265 (unsigned long)(di + 1), name_len);
1266
1267 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1268 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1269
1270 over = filldir(dirent, name_ptr, name_len,
1271 found_key.offset,
1272 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001273 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001274
1275 if (name_ptr != tmp_name)
1276 kfree(name_ptr);
1277
Chris Mason39279cc2007-06-12 06:35:45 -04001278 if (over)
1279 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001280 di_len = btrfs_dir_name_len(leaf, di) +
1281 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001282 di_cur += di_len;
1283 di = (struct btrfs_dir_item *)((char *)di + di_len);
1284 }
1285 }
1286 filp->f_pos++;
1287nopos:
1288 ret = 0;
1289err:
1290 btrfs_release_path(root, path);
1291 btrfs_free_path(path);
1292 mutex_unlock(&root->fs_info->fs_mutex);
1293 return ret;
1294}
1295
1296int btrfs_write_inode(struct inode *inode, int wait)
1297{
1298 struct btrfs_root *root = BTRFS_I(inode)->root;
1299 struct btrfs_trans_handle *trans;
1300 int ret = 0;
1301
1302 if (wait) {
1303 mutex_lock(&root->fs_info->fs_mutex);
1304 trans = btrfs_start_transaction(root, 1);
1305 btrfs_set_trans_block_group(trans, inode);
1306 ret = btrfs_commit_transaction(trans, root);
1307 mutex_unlock(&root->fs_info->fs_mutex);
1308 }
1309 return ret;
1310}
1311
1312/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001313 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001314 * inode changes. But, it is most likely to find the inode in cache.
1315 * FIXME, needs more benchmarking...there are no reasons other than performance
1316 * to keep or drop this code.
1317 */
1318void btrfs_dirty_inode(struct inode *inode)
1319{
1320 struct btrfs_root *root = BTRFS_I(inode)->root;
1321 struct btrfs_trans_handle *trans;
1322
1323 mutex_lock(&root->fs_info->fs_mutex);
1324 trans = btrfs_start_transaction(root, 1);
1325 btrfs_set_trans_block_group(trans, inode);
1326 btrfs_update_inode(trans, root, inode);
1327 btrfs_end_transaction(trans, root);
1328 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001329}
1330
1331static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1332 struct btrfs_root *root,
1333 u64 objectid,
1334 struct btrfs_block_group_cache *group,
1335 int mode)
1336{
1337 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001338 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001339 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001340 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001341 int ret;
1342 int owner;
1343
Chris Mason5f39d392007-10-15 16:14:19 -04001344 path = btrfs_alloc_path();
1345 BUG_ON(!path);
1346
Chris Mason39279cc2007-06-12 06:35:45 -04001347 inode = new_inode(root->fs_info->sb);
1348 if (!inode)
1349 return ERR_PTR(-ENOMEM);
1350
Chris Masonb888db22007-08-27 16:49:44 -04001351 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1352 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001353 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001354
Chris Mason39279cc2007-06-12 06:35:45 -04001355 if (mode & S_IFDIR)
1356 owner = 0;
1357 else
1358 owner = 1;
1359 group = btrfs_find_block_group(root, group, 0, 0, owner);
1360 BTRFS_I(inode)->block_group = group;
1361
Chris Mason5f39d392007-10-15 16:14:19 -04001362 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1363 if (ret)
1364 goto fail;
1365
Chris Mason39279cc2007-06-12 06:35:45 -04001366 inode->i_uid = current->fsuid;
1367 inode->i_gid = current->fsgid;
1368 inode->i_mode = mode;
1369 inode->i_ino = objectid;
1370 inode->i_blocks = 0;
1371 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001372 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1373 struct btrfs_inode_item);
1374 fill_inode_item(path->nodes[0], inode_item, inode);
1375 btrfs_mark_buffer_dirty(path->nodes[0]);
1376 btrfs_free_path(path);
1377
Chris Mason39279cc2007-06-12 06:35:45 -04001378 location = &BTRFS_I(inode)->location;
1379 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001380 location->offset = 0;
1381 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1382
Chris Mason39279cc2007-06-12 06:35:45 -04001383 insert_inode_hash(inode);
1384 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001385fail:
1386 btrfs_free_path(path);
1387 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001388}
1389
1390static inline u8 btrfs_inode_type(struct inode *inode)
1391{
1392 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1393}
1394
1395static int btrfs_add_link(struct btrfs_trans_handle *trans,
1396 struct dentry *dentry, struct inode *inode)
1397{
1398 int ret;
1399 struct btrfs_key key;
1400 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001401 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001402
Chris Mason39279cc2007-06-12 06:35:45 -04001403 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001404 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1405 key.offset = 0;
1406
1407 ret = btrfs_insert_dir_item(trans, root,
1408 dentry->d_name.name, dentry->d_name.len,
1409 dentry->d_parent->d_inode->i_ino,
1410 &key, btrfs_inode_type(inode));
1411 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001412 ret = btrfs_insert_inode_ref(trans, root,
1413 dentry->d_name.name,
1414 dentry->d_name.len,
1415 inode->i_ino,
1416 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001417 parent_inode = dentry->d_parent->d_inode;
1418 parent_inode->i_size += dentry->d_name.len * 2;
1419 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001420 ret = btrfs_update_inode(trans, root,
1421 dentry->d_parent->d_inode);
1422 }
1423 return ret;
1424}
1425
1426static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1427 struct dentry *dentry, struct inode *inode)
1428{
1429 int err = btrfs_add_link(trans, dentry, inode);
1430 if (!err) {
1431 d_instantiate(dentry, inode);
1432 return 0;
1433 }
1434 if (err > 0)
1435 err = -EEXIST;
1436 return err;
1437}
1438
Josef Bacik618e21d2007-07-11 10:18:17 -04001439static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1440 int mode, dev_t rdev)
1441{
1442 struct btrfs_trans_handle *trans;
1443 struct btrfs_root *root = BTRFS_I(dir)->root;
1444 struct inode *inode;
1445 int err;
1446 int drop_inode = 0;
1447 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001448 unsigned long nr;
Josef Bacik618e21d2007-07-11 10:18:17 -04001449
1450 if (!new_valid_dev(rdev))
1451 return -EINVAL;
1452
1453 mutex_lock(&root->fs_info->fs_mutex);
1454 trans = btrfs_start_transaction(root, 1);
1455 btrfs_set_trans_block_group(trans, dir);
1456
1457 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1458 if (err) {
1459 err = -ENOSPC;
1460 goto out_unlock;
1461 }
1462
1463 inode = btrfs_new_inode(trans, root, objectid,
1464 BTRFS_I(dir)->block_group, mode);
1465 err = PTR_ERR(inode);
1466 if (IS_ERR(inode))
1467 goto out_unlock;
1468
1469 btrfs_set_trans_block_group(trans, inode);
1470 err = btrfs_add_nondir(trans, dentry, inode);
1471 if (err)
1472 drop_inode = 1;
1473 else {
1474 inode->i_op = &btrfs_special_inode_operations;
1475 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001476 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001477 }
1478 dir->i_sb->s_dirt = 1;
1479 btrfs_update_inode_block_group(trans, inode);
1480 btrfs_update_inode_block_group(trans, dir);
1481out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001482 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001483 btrfs_end_transaction(trans, root);
1484 mutex_unlock(&root->fs_info->fs_mutex);
1485
1486 if (drop_inode) {
1487 inode_dec_link_count(inode);
1488 iput(inode);
1489 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001490 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001491 return err;
1492}
1493
Chris Mason39279cc2007-06-12 06:35:45 -04001494static int btrfs_create(struct inode *dir, struct dentry *dentry,
1495 int mode, struct nameidata *nd)
1496{
1497 struct btrfs_trans_handle *trans;
1498 struct btrfs_root *root = BTRFS_I(dir)->root;
1499 struct inode *inode;
1500 int err;
1501 int drop_inode = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001502 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001503 u64 objectid;
1504
1505 mutex_lock(&root->fs_info->fs_mutex);
1506 trans = btrfs_start_transaction(root, 1);
1507 btrfs_set_trans_block_group(trans, dir);
1508
1509 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1510 if (err) {
1511 err = -ENOSPC;
1512 goto out_unlock;
1513 }
1514
1515 inode = btrfs_new_inode(trans, root, objectid,
1516 BTRFS_I(dir)->block_group, mode);
1517 err = PTR_ERR(inode);
1518 if (IS_ERR(inode))
1519 goto out_unlock;
1520
1521 btrfs_set_trans_block_group(trans, inode);
1522 err = btrfs_add_nondir(trans, dentry, inode);
1523 if (err)
1524 drop_inode = 1;
1525 else {
1526 inode->i_mapping->a_ops = &btrfs_aops;
1527 inode->i_fop = &btrfs_file_operations;
1528 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001529 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1530 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001531 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001532 }
1533 dir->i_sb->s_dirt = 1;
1534 btrfs_update_inode_block_group(trans, inode);
1535 btrfs_update_inode_block_group(trans, dir);
1536out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001537 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001538 btrfs_end_transaction(trans, root);
1539 mutex_unlock(&root->fs_info->fs_mutex);
1540
1541 if (drop_inode) {
1542 inode_dec_link_count(inode);
1543 iput(inode);
1544 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001545 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001546 return err;
1547}
1548
1549static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1550 struct dentry *dentry)
1551{
1552 struct btrfs_trans_handle *trans;
1553 struct btrfs_root *root = BTRFS_I(dir)->root;
1554 struct inode *inode = old_dentry->d_inode;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001555 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001556 int err;
1557 int drop_inode = 0;
1558
1559 if (inode->i_nlink == 0)
1560 return -ENOENT;
1561
Chris Mason6da6aba2007-12-18 16:15:09 -05001562#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1563 inode->i_nlink++;
1564#else
Chris Mason39279cc2007-06-12 06:35:45 -04001565 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001566#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001567 mutex_lock(&root->fs_info->fs_mutex);
1568 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001569
Chris Mason39279cc2007-06-12 06:35:45 -04001570 btrfs_set_trans_block_group(trans, dir);
1571 atomic_inc(&inode->i_count);
1572 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001573
Chris Mason39279cc2007-06-12 06:35:45 -04001574 if (err)
1575 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001576
Chris Mason39279cc2007-06-12 06:35:45 -04001577 dir->i_sb->s_dirt = 1;
1578 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001579 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001580
Chris Mason54aa1f42007-06-22 14:16:25 -04001581 if (err)
1582 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001583
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001584 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001585 btrfs_end_transaction(trans, root);
1586 mutex_unlock(&root->fs_info->fs_mutex);
1587
1588 if (drop_inode) {
1589 inode_dec_link_count(inode);
1590 iput(inode);
1591 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001592 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001593 return err;
1594}
1595
Chris Mason39279cc2007-06-12 06:35:45 -04001596static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1597{
1598 struct inode *inode;
1599 struct btrfs_trans_handle *trans;
1600 struct btrfs_root *root = BTRFS_I(dir)->root;
1601 int err = 0;
1602 int drop_on_err = 0;
1603 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001604 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001605
1606 mutex_lock(&root->fs_info->fs_mutex);
1607 trans = btrfs_start_transaction(root, 1);
1608 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001609
Chris Mason39279cc2007-06-12 06:35:45 -04001610 if (IS_ERR(trans)) {
1611 err = PTR_ERR(trans);
1612 goto out_unlock;
1613 }
1614
1615 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1616 if (err) {
1617 err = -ENOSPC;
1618 goto out_unlock;
1619 }
1620
1621 inode = btrfs_new_inode(trans, root, objectid,
1622 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1623 if (IS_ERR(inode)) {
1624 err = PTR_ERR(inode);
1625 goto out_fail;
1626 }
Chris Mason5f39d392007-10-15 16:14:19 -04001627
Chris Mason39279cc2007-06-12 06:35:45 -04001628 drop_on_err = 1;
1629 inode->i_op = &btrfs_dir_inode_operations;
1630 inode->i_fop = &btrfs_dir_file_operations;
1631 btrfs_set_trans_block_group(trans, inode);
1632
Chris Mason39544012007-12-12 14:38:19 -05001633 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001634 err = btrfs_update_inode(trans, root, inode);
1635 if (err)
1636 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001637
Chris Mason39279cc2007-06-12 06:35:45 -04001638 err = btrfs_add_link(trans, dentry, inode);
1639 if (err)
1640 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001641
Chris Mason39279cc2007-06-12 06:35:45 -04001642 d_instantiate(dentry, inode);
1643 drop_on_err = 0;
1644 dir->i_sb->s_dirt = 1;
1645 btrfs_update_inode_block_group(trans, inode);
1646 btrfs_update_inode_block_group(trans, dir);
1647
1648out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001649 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001650 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001651
Chris Mason39279cc2007-06-12 06:35:45 -04001652out_unlock:
1653 mutex_unlock(&root->fs_info->fs_mutex);
1654 if (drop_on_err)
1655 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001656 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001657 return err;
1658}
1659
Chris Masona52d9a82007-08-27 16:49:44 -04001660struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1661 size_t page_offset, u64 start, u64 end,
1662 int create)
1663{
1664 int ret;
1665 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001666 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001667 u64 extent_start = 0;
1668 u64 extent_end = 0;
1669 u64 objectid = inode->i_ino;
1670 u32 found_type;
1671 int failed_insert = 0;
1672 struct btrfs_path *path;
1673 struct btrfs_root *root = BTRFS_I(inode)->root;
1674 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001675 struct extent_buffer *leaf;
1676 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001677 struct extent_map *em = NULL;
1678 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1679 struct btrfs_trans_handle *trans = NULL;
1680
1681 path = btrfs_alloc_path();
1682 BUG_ON(!path);
1683 mutex_lock(&root->fs_info->fs_mutex);
1684
1685again:
1686 em = lookup_extent_mapping(em_tree, start, end);
1687 if (em) {
1688 goto out;
1689 }
1690 if (!em) {
1691 em = alloc_extent_map(GFP_NOFS);
1692 if (!em) {
1693 err = -ENOMEM;
1694 goto out;
1695 }
Chris Mason5f39d392007-10-15 16:14:19 -04001696 em->start = EXTENT_MAP_HOLE;
1697 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001698 }
1699 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001700 ret = btrfs_lookup_file_extent(trans, root, path,
1701 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001702 if (ret < 0) {
1703 err = ret;
1704 goto out;
1705 }
1706
1707 if (ret != 0) {
1708 if (path->slots[0] == 0)
1709 goto not_found;
1710 path->slots[0]--;
1711 }
1712
Chris Mason5f39d392007-10-15 16:14:19 -04001713 leaf = path->nodes[0];
1714 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001715 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001716 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001717 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1718 found_type = btrfs_key_type(&found_key);
1719 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001720 found_type != BTRFS_EXTENT_DATA_KEY) {
1721 goto not_found;
1722 }
1723
Chris Mason5f39d392007-10-15 16:14:19 -04001724 found_type = btrfs_file_extent_type(leaf, item);
1725 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001726 if (found_type == BTRFS_FILE_EXTENT_REG) {
1727 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001728 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001729 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001730 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001731 em->start = start;
1732 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001733 if (end < extent_start)
1734 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001735 em->end = extent_end - 1;
1736 } else {
1737 em->end = end;
1738 }
1739 goto not_found_em;
1740 }
Chris Masondb945352007-10-15 16:15:53 -04001741 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1742 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001743 em->start = extent_start;
1744 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001745 em->block_start = EXTENT_MAP_HOLE;
1746 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001747 goto insert;
1748 }
Chris Masondb945352007-10-15 16:15:53 -04001749 bytenr += btrfs_file_extent_offset(leaf, item);
1750 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001751 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001752 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001753 em->start = extent_start;
1754 em->end = extent_end - 1;
1755 goto insert;
1756 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001757 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001758 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001759 size_t size;
1760 size_t extent_offset;
1761 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001762
Chris Mason5f39d392007-10-15 16:14:19 -04001763 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1764 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001765 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001766 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001767 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001768 em->start = start;
1769 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001770 if (end < extent_start)
1771 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001772 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001773 } else {
1774 em->end = end;
1775 }
1776 goto not_found_em;
1777 }
1778 em->block_start = EXTENT_MAP_INLINE;
1779 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001780
1781 if (!page) {
1782 em->start = extent_start;
1783 em->end = extent_start + size - 1;
1784 goto out;
1785 }
1786
Chris Mason35ebb932007-10-30 16:56:53 -04001787 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001788 extent_start + page_offset;
1789 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1790 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001791 em->start = extent_start + extent_offset;
1792 em->end = (em->start + copy_size -1) |
1793 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001794 map = kmap(page);
1795 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001796 if (create == 0 && !PageUptodate(page)) {
1797 read_extent_buffer(leaf, map + page_offset, ptr,
1798 copy_size);
1799 flush_dcache_page(page);
1800 } else if (create && PageUptodate(page)) {
1801 if (!trans) {
1802 kunmap(page);
1803 free_extent_map(em);
1804 em = NULL;
1805 btrfs_release_path(root, path);
1806 trans = btrfs_start_transaction(root, 1);
1807 goto again;
1808 }
1809 write_extent_buffer(leaf, map + page_offset, ptr,
1810 copy_size);
1811 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001812 }
Chris Masona52d9a82007-08-27 16:49:44 -04001813 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001814 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001815 goto insert;
1816 } else {
1817 printk("unkknown found_type %d\n", found_type);
1818 WARN_ON(1);
1819 }
1820not_found:
1821 em->start = start;
1822 em->end = end;
1823not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001824 em->block_start = EXTENT_MAP_HOLE;
1825 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001826insert:
1827 btrfs_release_path(root, path);
1828 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001829 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001830 err = -EIO;
1831 goto out;
1832 }
1833 ret = add_extent_mapping(em_tree, em);
1834 if (ret == -EEXIST) {
1835 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001836 em = NULL;
Chris Mason6da6aba2007-12-18 16:15:09 -05001837 if (0 && failed_insert == 1) {
1838 btrfs_drop_extent_cache(inode, start, end);
1839 }
Chris Masona52d9a82007-08-27 16:49:44 -04001840 failed_insert++;
1841 if (failed_insert > 5) {
1842 printk("failing to insert %Lu %Lu\n", start, end);
1843 err = -EIO;
1844 goto out;
1845 }
Chris Masona52d9a82007-08-27 16:49:44 -04001846 goto again;
1847 }
1848 err = 0;
1849out:
1850 btrfs_free_path(path);
1851 if (trans) {
1852 ret = btrfs_end_transaction(trans, root);
1853 if (!err)
1854 err = ret;
1855 }
1856 mutex_unlock(&root->fs_info->fs_mutex);
1857 if (err) {
1858 free_extent_map(em);
1859 WARN_ON(1);
1860 return ERR_PTR(err);
1861 }
1862 return em;
1863}
1864
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001865static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001866{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001867 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001868}
1869
1870static int btrfs_prepare_write(struct file *file, struct page *page,
1871 unsigned from, unsigned to)
1872{
Chris Masona52d9a82007-08-27 16:49:44 -04001873 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1874 page->mapping->host, page, from, to,
1875 btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001876}
1877
Chris Mason9ebefb182007-06-15 13:50:00 -04001878int btrfs_readpage(struct file *file, struct page *page)
1879{
Chris Masona52d9a82007-08-27 16:49:44 -04001880 struct extent_map_tree *tree;
1881 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1882 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001883}
Chris Mason39279cc2007-06-12 06:35:45 -04001884static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1885{
Chris Masona52d9a82007-08-27 16:49:44 -04001886 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001887
1888
1889 if (current->flags & PF_MEMALLOC) {
1890 redirty_page_for_writepage(wbc, page);
1891 unlock_page(page);
1892 return 0;
1893 }
Chris Masona52d9a82007-08-27 16:49:44 -04001894 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1895 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1896}
Chris Mason39279cc2007-06-12 06:35:45 -04001897
Chris Masonb293f022007-11-01 19:45:34 -04001898static int btrfs_writepages(struct address_space *mapping,
1899 struct writeback_control *wbc)
1900{
1901 struct extent_map_tree *tree;
1902 tree = &BTRFS_I(mapping->host)->extent_tree;
1903 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1904}
1905
Chris Mason3ab2fb52007-11-08 10:59:22 -05001906static int
1907btrfs_readpages(struct file *file, struct address_space *mapping,
1908 struct list_head *pages, unsigned nr_pages)
1909{
1910 struct extent_map_tree *tree;
1911 tree = &BTRFS_I(mapping->host)->extent_tree;
1912 return extent_readpages(tree, mapping, pages, nr_pages,
1913 btrfs_get_extent);
1914}
1915
Chris Masona52d9a82007-08-27 16:49:44 -04001916static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1917{
1918 struct extent_map_tree *tree;
1919 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001920
Chris Masona52d9a82007-08-27 16:49:44 -04001921 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1922 ret = try_release_extent_mapping(tree, page);
1923 if (ret == 1) {
1924 ClearPagePrivate(page);
1925 set_page_private(page, 0);
1926 page_cache_release(page);
1927 }
1928 return ret;
1929}
Chris Mason39279cc2007-06-12 06:35:45 -04001930
Chris Masona52d9a82007-08-27 16:49:44 -04001931static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1932{
1933 struct extent_map_tree *tree;
1934
1935 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1936 extent_invalidatepage(tree, page, offset);
1937 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001938}
1939
Chris Mason9ebefb182007-06-15 13:50:00 -04001940/*
1941 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1942 * called from a page fault handler when a page is first dirtied. Hence we must
1943 * be careful to check for EOF conditions here. We set the page up correctly
1944 * for a written page which means we get ENOSPC checking when writing into
1945 * holes and correct delalloc and unwritten extent mapping on filesystems that
1946 * support these features.
1947 *
1948 * We are not allowed to take the i_mutex here so we have to play games to
1949 * protect against truncate races as the page could now be beyond EOF. Because
1950 * vmtruncate() writes the inode size before removing pages, once we have the
1951 * page lock we can determine safely if the page is beyond EOF. If it is not
1952 * beyond EOF, then the page is guaranteed safe against truncation until we
1953 * unlock the page.
1954 */
1955int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1956{
Chris Mason6da6aba2007-12-18 16:15:09 -05001957 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason9ebefb182007-06-15 13:50:00 -04001958 unsigned long end;
1959 loff_t size;
1960 int ret = -EINVAL;
Chris Masona52d9a82007-08-27 16:49:44 -04001961 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04001962
Chris Mason011410b2007-09-10 19:58:36 -04001963 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001964 lock_page(page);
1965 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04001966 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04001967 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001968
Chris Mason9ebefb182007-06-15 13:50:00 -04001969 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04001970 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04001971 /* page got truncated out from underneath us */
1972 goto out_unlock;
1973 }
1974
1975 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04001976 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04001977 end = size & ~PAGE_CACHE_MASK;
1978 else
1979 end = PAGE_CACHE_SIZE;
1980
Chris Masonb888db22007-08-27 16:49:44 -04001981 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04001982
1983out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04001984 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001985 unlock_page(page);
1986 return ret;
1987}
1988
Chris Mason39279cc2007-06-12 06:35:45 -04001989static void btrfs_truncate(struct inode *inode)
1990{
1991 struct btrfs_root *root = BTRFS_I(inode)->root;
1992 int ret;
1993 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001994 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001995
1996 if (!S_ISREG(inode->i_mode))
1997 return;
1998 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1999 return;
2000
2001 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2002
2003 mutex_lock(&root->fs_info->fs_mutex);
2004 trans = btrfs_start_transaction(root, 1);
2005 btrfs_set_trans_block_group(trans, inode);
2006
2007 /* FIXME, add redo link to tree so we don't leak on crash */
2008 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002009 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002010 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002011
Chris Mason39279cc2007-06-12 06:35:45 -04002012 ret = btrfs_end_transaction(trans, root);
2013 BUG_ON(ret);
2014 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002015 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002016}
2017
2018int btrfs_commit_write(struct file *file, struct page *page,
2019 unsigned from, unsigned to)
2020{
Chris Masone9906a92007-12-14 12:56:58 -05002021 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2022 struct inode *inode = page->mapping->host;
2023
2024 btrfs_cow_one_page(inode, page, PAGE_CACHE_SIZE);
2025
Chris Masone9906a92007-12-14 12:56:58 -05002026 if (pos > inode->i_size) {
2027 i_size_write(inode, pos);
2028 mark_inode_dirty(inode);
2029 }
2030 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002031}
2032
2033static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2034{
2035 struct btrfs_trans_handle *trans;
2036 struct btrfs_key key;
2037 struct btrfs_root_item root_item;
2038 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002039 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002040 struct btrfs_root *new_root;
2041 struct inode *inode;
2042 struct inode *dir;
2043 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002044 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002045 u64 objectid;
2046 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002047 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002048
2049 mutex_lock(&root->fs_info->fs_mutex);
2050 trans = btrfs_start_transaction(root, 1);
2051 BUG_ON(!trans);
2052
Chris Mason7bb86312007-12-11 09:25:06 -05002053 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2054 0, &objectid);
2055 if (ret)
2056 goto fail;
2057
2058 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2059 objectid, trans->transid, 0, 0,
2060 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002061 if (IS_ERR(leaf))
2062 return PTR_ERR(leaf);
2063
2064 btrfs_set_header_nritems(leaf, 0);
2065 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002066 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002067 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002068 btrfs_set_header_owner(leaf, objectid);
2069
Chris Mason5f39d392007-10-15 16:14:19 -04002070 write_extent_buffer(leaf, root->fs_info->fsid,
2071 (unsigned long)btrfs_header_fsid(leaf),
2072 BTRFS_FSID_SIZE);
2073 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002074
2075 inode_item = &root_item.inode;
2076 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002077 inode_item->generation = cpu_to_le64(1);
2078 inode_item->size = cpu_to_le64(3);
2079 inode_item->nlink = cpu_to_le32(1);
2080 inode_item->nblocks = cpu_to_le64(1);
2081 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002082
Chris Masondb945352007-10-15 16:15:53 -04002083 btrfs_set_root_bytenr(&root_item, leaf->start);
2084 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002085 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002086 btrfs_set_root_used(&root_item, 0);
2087
Chris Mason5eda7b52007-06-22 14:16:25 -04002088 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2089 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002090
2091 free_extent_buffer(leaf);
2092 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002093
Chris Mason39279cc2007-06-12 06:35:45 -04002094 btrfs_set_root_dirid(&root_item, new_dirid);
2095
2096 key.objectid = objectid;
2097 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002098 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2099 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2100 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002101 if (ret)
2102 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002103
2104 /*
2105 * insert the directory item
2106 */
2107 key.offset = (u64)-1;
2108 dir = root->fs_info->sb->s_root->d_inode;
2109 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2110 name, namelen, dir->i_ino, &key,
2111 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002112 if (ret)
2113 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002114
Chris Mason39544012007-12-12 14:38:19 -05002115 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2116 name, namelen, objectid,
2117 root->fs_info->sb->s_root->d_inode->i_ino);
2118 if (ret)
2119 goto fail;
2120
Chris Mason39279cc2007-06-12 06:35:45 -04002121 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002122 if (ret)
2123 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002124
Josef Bacik58176a92007-08-29 15:47:34 -04002125 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002126 BUG_ON(!new_root);
2127
2128 trans = btrfs_start_transaction(new_root, 1);
2129 BUG_ON(!trans);
2130
2131 inode = btrfs_new_inode(trans, new_root, new_dirid,
2132 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002133 if (IS_ERR(inode))
2134 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002135 inode->i_op = &btrfs_dir_inode_operations;
2136 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002137 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002138
Chris Mason39544012007-12-12 14:38:19 -05002139 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2140 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002141 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002142 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002143 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002144 if (ret)
2145 goto fail;
2146fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002147 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002148 err = btrfs_commit_transaction(trans, root);
2149 if (err && !ret)
2150 ret = err;
2151fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002152 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002153 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002154 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002155}
2156
2157static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2158{
2159 struct btrfs_trans_handle *trans;
2160 struct btrfs_key key;
2161 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002162 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04002163 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002164 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002165 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002166 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002167
2168 if (!root->ref_cows)
2169 return -EINVAL;
2170
Chris Mason011410b2007-09-10 19:58:36 -04002171 down_write(&root->snap_sem);
2172 freeze_bdev(root->fs_info->sb->s_bdev);
2173 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2174
Chris Mason39279cc2007-06-12 06:35:45 -04002175 mutex_lock(&root->fs_info->fs_mutex);
2176 trans = btrfs_start_transaction(root, 1);
2177 BUG_ON(!trans);
2178
2179 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002180 if (ret)
2181 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002182
2183 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2184 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002185 if (ret)
2186 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002187
2188 memcpy(&new_root_item, &root->root_item,
2189 sizeof(new_root_item));
2190
2191 key.objectid = objectid;
2192 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002193 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Masonbe20aa92007-12-17 20:14:01 -05002194
Yan96919752007-12-04 13:20:20 -05002195 extent_buffer_get(root->node);
Chris Mason83df7c12007-08-27 16:49:44 -04002196 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Yan96919752007-12-04 13:20:20 -05002197 free_extent_buffer(tmp);
Chris Masonbe20aa92007-12-17 20:14:01 -05002198
2199 btrfs_copy_root(trans, root, root->node, &tmp, objectid);
2200
2201 btrfs_set_root_bytenr(&new_root_item, tmp->start);
2202 btrfs_set_root_level(&new_root_item, btrfs_header_level(tmp));
Chris Mason39279cc2007-06-12 06:35:45 -04002203 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2204 &new_root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05002205 free_extent_buffer(tmp);
Chris Mason54aa1f42007-06-22 14:16:25 -04002206 if (ret)
2207 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002208
2209 /*
2210 * insert the directory item
2211 */
2212 key.offset = (u64)-1;
2213 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2214 name, namelen,
2215 root->fs_info->sb->s_root->d_inode->i_ino,
2216 &key, BTRFS_FT_DIR);
2217
Chris Mason54aa1f42007-06-22 14:16:25 -04002218 if (ret)
2219 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002220
Chris Mason39544012007-12-12 14:38:19 -05002221 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2222 name, namelen, objectid,
2223 root->fs_info->sb->s_root->d_inode->i_ino);
2224
2225 if (ret)
2226 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002227fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002228 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002229 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002230
Chris Mason54aa1f42007-06-22 14:16:25 -04002231 if (err && !ret)
2232 ret = err;
Chris Mason5f39d392007-10-15 16:14:19 -04002233
Chris Mason39279cc2007-06-12 06:35:45 -04002234 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002235 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002236 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002237 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002238}
2239
Chris Mason86479a02007-09-10 19:58:16 -04002240static unsigned long force_ra(struct address_space *mapping,
2241 struct file_ra_state *ra, struct file *file,
2242 pgoff_t offset, pgoff_t last_index)
2243{
2244 pgoff_t req_size;
2245
2246#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2247 req_size = last_index - offset + 1;
2248 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2249 return offset;
2250#else
2251 req_size = min(last_index - offset + 1, (pgoff_t)128);
2252 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2253 return offset + req_size;
2254#endif
2255}
2256
2257int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002258 struct inode *inode = fdentry(file)->d_inode;
Chris Mason86479a02007-09-10 19:58:16 -04002259 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2260 struct page *page;
2261 unsigned long last_index;
2262 unsigned long ra_index = 0;
2263 u64 page_start;
2264 u64 page_end;
2265 unsigned long i;
2266
2267 mutex_lock(&inode->i_mutex);
2268 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2269 for (i = 0; i <= last_index; i++) {
2270 if (i == ra_index) {
2271 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2272 file, ra_index, last_index);
2273 }
2274 page = grab_cache_page(inode->i_mapping, i);
2275 if (!page)
2276 goto out_unlock;
2277 if (!PageUptodate(page)) {
2278 btrfs_readpage(NULL, page);
2279 lock_page(page);
2280 if (!PageUptodate(page)) {
2281 unlock_page(page);
2282 page_cache_release(page);
2283 goto out_unlock;
2284 }
2285 }
Chris Mason35ebb932007-10-30 16:56:53 -04002286 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002287 page_end = page_start + PAGE_CACHE_SIZE - 1;
2288
2289 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2290 set_extent_delalloc(em_tree, page_start,
2291 page_end, GFP_NOFS);
2292 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2293 set_page_dirty(page);
2294 unlock_page(page);
2295 page_cache_release(page);
2296 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2297 }
2298
2299out_unlock:
2300 mutex_unlock(&inode->i_mutex);
2301 return 0;
2302}
2303
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002304static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2305{
Chris Mason4aec2b52007-12-18 16:25:45 -05002306 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002307 struct btrfs_dir_item *di;
2308 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002309 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002310 int namelen;
2311 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002312
Chris Mason4aec2b52007-12-18 16:25:45 -05002313 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002314
Chris Mason4aec2b52007-12-18 16:25:45 -05002315 if (!vol_args)
2316 return -ENOMEM;
2317
2318 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2319 ret = -EFAULT;
2320 goto out;
2321 }
2322
2323 namelen = strlen(vol_args->name);
2324 if (namelen > BTRFS_VOL_NAME_MAX) {
2325 ret = -EINVAL;
2326 goto out;
2327 }
2328 if (strchr(vol_args->name, '/')) {
2329 ret = -EINVAL;
2330 goto out;
2331 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002332
2333 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002334 if (!path) {
2335 ret = -ENOMEM;
2336 goto out;
2337 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002338
2339 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2340 mutex_lock(&root->fs_info->fs_mutex);
2341 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2342 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002343 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002344 mutex_unlock(&root->fs_info->fs_mutex);
2345 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002346
2347 if (di && !IS_ERR(di)) {
2348 ret = -EEXIST;
2349 goto out;
2350 }
2351
2352 if (IS_ERR(di)) {
2353 ret = PTR_ERR(di);
2354 goto out;
2355 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002356
2357 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002358 ret = create_subvol(root, vol_args->name, namelen);
2359 else
2360 ret = create_snapshot(root, vol_args->name, namelen);
2361out:
2362 kfree(vol_args);
2363 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002364}
2365
2366static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002367{
Chris Mason6da6aba2007-12-18 16:15:09 -05002368 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002369 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002370
2371 switch (inode->i_mode & S_IFMT) {
2372 case S_IFDIR:
2373 mutex_lock(&root->fs_info->fs_mutex);
2374 btrfs_defrag_root(root, 0);
2375 btrfs_defrag_root(root->fs_info->extent_root, 0);
2376 mutex_unlock(&root->fs_info->fs_mutex);
2377 break;
2378 case S_IFREG:
2379 btrfs_defrag_file(file);
2380 break;
2381 }
2382
2383 return 0;
2384}
2385
2386long btrfs_ioctl(struct file *file, unsigned int
2387 cmd, unsigned long arg)
2388{
Chris Mason6da6aba2007-12-18 16:15:09 -05002389 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002390
2391 switch (cmd) {
2392 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002393 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002394 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002395 return btrfs_ioctl_defrag(file);
Chris Mason39279cc2007-06-12 06:35:45 -04002396 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002397
2398 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002399}
2400
Chris Mason39279cc2007-06-12 06:35:45 -04002401/*
2402 * Called inside transaction, so use GFP_NOFS
2403 */
2404struct inode *btrfs_alloc_inode(struct super_block *sb)
2405{
2406 struct btrfs_inode *ei;
2407
2408 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2409 if (!ei)
2410 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002411 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002412 return &ei->vfs_inode;
2413}
2414
2415void btrfs_destroy_inode(struct inode *inode)
2416{
2417 WARN_ON(!list_empty(&inode->i_dentry));
2418 WARN_ON(inode->i_data.nrpages);
2419
2420 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2421}
2422
Chris Mason44ec0b72007-10-29 10:55:05 -04002423#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2424static void init_once(struct kmem_cache * cachep, void *foo)
2425#else
Chris Mason39279cc2007-06-12 06:35:45 -04002426static void init_once(void * foo, struct kmem_cache * cachep,
2427 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002428#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002429{
2430 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2431
2432 inode_init_once(&ei->vfs_inode);
2433}
2434
2435void btrfs_destroy_cachep(void)
2436{
2437 if (btrfs_inode_cachep)
2438 kmem_cache_destroy(btrfs_inode_cachep);
2439 if (btrfs_trans_handle_cachep)
2440 kmem_cache_destroy(btrfs_trans_handle_cachep);
2441 if (btrfs_transaction_cachep)
2442 kmem_cache_destroy(btrfs_transaction_cachep);
2443 if (btrfs_bit_radix_cachep)
2444 kmem_cache_destroy(btrfs_bit_radix_cachep);
2445 if (btrfs_path_cachep)
2446 kmem_cache_destroy(btrfs_path_cachep);
2447}
2448
Chris Mason86479a02007-09-10 19:58:16 -04002449struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002450 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002451#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2452 void (*ctor)(struct kmem_cache *, void *)
2453#else
Chris Mason92fee662007-07-25 12:31:35 -04002454 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002455 unsigned long)
2456#endif
2457 )
Chris Mason92fee662007-07-25 12:31:35 -04002458{
2459 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2460 SLAB_MEM_SPREAD | extra_flags), ctor
2461#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2462 ,NULL
2463#endif
2464 );
2465}
2466
Chris Mason39279cc2007-06-12 06:35:45 -04002467int btrfs_init_cachep(void)
2468{
Chris Mason86479a02007-09-10 19:58:16 -04002469 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002470 sizeof(struct btrfs_inode),
2471 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002472 if (!btrfs_inode_cachep)
2473 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002474 btrfs_trans_handle_cachep =
2475 btrfs_cache_create("btrfs_trans_handle_cache",
2476 sizeof(struct btrfs_trans_handle),
2477 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002478 if (!btrfs_trans_handle_cachep)
2479 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002480 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002481 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002482 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002483 if (!btrfs_transaction_cachep)
2484 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002485 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002486 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002487 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002488 if (!btrfs_path_cachep)
2489 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002490 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002491 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002492 if (!btrfs_bit_radix_cachep)
2493 goto fail;
2494 return 0;
2495fail:
2496 btrfs_destroy_cachep();
2497 return -ENOMEM;
2498}
2499
2500static int btrfs_getattr(struct vfsmount *mnt,
2501 struct dentry *dentry, struct kstat *stat)
2502{
2503 struct inode *inode = dentry->d_inode;
2504 generic_fillattr(inode, stat);
2505 stat->blksize = 256 * 1024;
2506 return 0;
2507}
2508
2509static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2510 struct inode * new_dir,struct dentry *new_dentry)
2511{
2512 struct btrfs_trans_handle *trans;
2513 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2514 struct inode *new_inode = new_dentry->d_inode;
2515 struct inode *old_inode = old_dentry->d_inode;
2516 struct timespec ctime = CURRENT_TIME;
2517 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002518 int ret;
2519
2520 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2521 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2522 return -ENOTEMPTY;
2523 }
Chris Mason5f39d392007-10-15 16:14:19 -04002524
Chris Mason39279cc2007-06-12 06:35:45 -04002525 mutex_lock(&root->fs_info->fs_mutex);
2526 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002527
Chris Mason39279cc2007-06-12 06:35:45 -04002528 btrfs_set_trans_block_group(trans, new_dir);
2529 path = btrfs_alloc_path();
2530 if (!path) {
2531 ret = -ENOMEM;
2532 goto out_fail;
2533 }
2534
2535 old_dentry->d_inode->i_nlink++;
2536 old_dir->i_ctime = old_dir->i_mtime = ctime;
2537 new_dir->i_ctime = new_dir->i_mtime = ctime;
2538 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002539
Chris Mason39279cc2007-06-12 06:35:45 -04002540 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2541 if (ret)
2542 goto out_fail;
2543
2544 if (new_inode) {
2545 new_inode->i_ctime = CURRENT_TIME;
2546 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2547 if (ret)
2548 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002549 }
2550 ret = btrfs_add_link(trans, new_dentry, old_inode);
2551 if (ret)
2552 goto out_fail;
2553
2554out_fail:
2555 btrfs_free_path(path);
2556 btrfs_end_transaction(trans, root);
2557 mutex_unlock(&root->fs_info->fs_mutex);
2558 return ret;
2559}
2560
2561static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2562 const char *symname)
2563{
2564 struct btrfs_trans_handle *trans;
2565 struct btrfs_root *root = BTRFS_I(dir)->root;
2566 struct btrfs_path *path;
2567 struct btrfs_key key;
2568 struct inode *inode;
2569 int err;
2570 int drop_inode = 0;
2571 u64 objectid;
2572 int name_len;
2573 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002574 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002575 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002576 struct extent_buffer *leaf;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002577 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002578
2579 name_len = strlen(symname) + 1;
2580 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2581 return -ENAMETOOLONG;
2582 mutex_lock(&root->fs_info->fs_mutex);
2583 trans = btrfs_start_transaction(root, 1);
2584 btrfs_set_trans_block_group(trans, dir);
2585
2586 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2587 if (err) {
2588 err = -ENOSPC;
2589 goto out_unlock;
2590 }
2591
2592 inode = btrfs_new_inode(trans, root, objectid,
2593 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2594 err = PTR_ERR(inode);
2595 if (IS_ERR(inode))
2596 goto out_unlock;
2597
2598 btrfs_set_trans_block_group(trans, inode);
2599 err = btrfs_add_nondir(trans, dentry, inode);
2600 if (err)
2601 drop_inode = 1;
2602 else {
2603 inode->i_mapping->a_ops = &btrfs_aops;
2604 inode->i_fop = &btrfs_file_operations;
2605 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002606 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2607 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002608 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002609 }
2610 dir->i_sb->s_dirt = 1;
2611 btrfs_update_inode_block_group(trans, inode);
2612 btrfs_update_inode_block_group(trans, dir);
2613 if (drop_inode)
2614 goto out_unlock;
2615
2616 path = btrfs_alloc_path();
2617 BUG_ON(!path);
2618 key.objectid = inode->i_ino;
2619 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002620 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2621 datasize = btrfs_file_extent_calc_inline_size(name_len);
2622 err = btrfs_insert_empty_item(trans, root, path, &key,
2623 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002624 if (err) {
2625 drop_inode = 1;
2626 goto out_unlock;
2627 }
Chris Mason5f39d392007-10-15 16:14:19 -04002628 leaf = path->nodes[0];
2629 ei = btrfs_item_ptr(leaf, path->slots[0],
2630 struct btrfs_file_extent_item);
2631 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2632 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002633 BTRFS_FILE_EXTENT_INLINE);
2634 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002635 write_extent_buffer(leaf, symname, ptr, name_len);
2636 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002637 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002638
Chris Mason39279cc2007-06-12 06:35:45 -04002639 inode->i_op = &btrfs_symlink_inode_operations;
2640 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2641 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002642 err = btrfs_update_inode(trans, root, inode);
2643 if (err)
2644 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002645
2646out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002647 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002648 btrfs_end_transaction(trans, root);
2649 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002650 if (drop_inode) {
2651 inode_dec_link_count(inode);
2652 iput(inode);
2653 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002654 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002655 return err;
2656}
2657
2658static struct inode_operations btrfs_dir_inode_operations = {
2659 .lookup = btrfs_lookup,
2660 .create = btrfs_create,
2661 .unlink = btrfs_unlink,
2662 .link = btrfs_link,
2663 .mkdir = btrfs_mkdir,
2664 .rmdir = btrfs_rmdir,
2665 .rename = btrfs_rename,
2666 .symlink = btrfs_symlink,
2667 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002668 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002669 .setxattr = generic_setxattr,
2670 .getxattr = generic_getxattr,
2671 .listxattr = btrfs_listxattr,
2672 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002673};
2674
2675static struct inode_operations btrfs_dir_ro_inode_operations = {
2676 .lookup = btrfs_lookup,
2677};
2678
2679static struct file_operations btrfs_dir_file_operations = {
2680 .llseek = generic_file_llseek,
2681 .read = generic_read_dir,
2682 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002683 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002684#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002685 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002686#endif
2687};
2688
Chris Mason07157aa2007-08-30 08:50:51 -04002689static struct extent_map_ops btrfs_extent_map_ops = {
2690 .fill_delalloc = run_delalloc_range,
2691 .writepage_io_hook = btrfs_writepage_io_hook,
2692 .readpage_io_hook = btrfs_readpage_io_hook,
2693 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2694};
2695
Chris Mason39279cc2007-06-12 06:35:45 -04002696static struct address_space_operations btrfs_aops = {
2697 .readpage = btrfs_readpage,
2698 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002699 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002700 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002701 .sync_page = block_sync_page,
2702 .prepare_write = btrfs_prepare_write,
2703 .commit_write = btrfs_commit_write,
2704 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002705 .invalidatepage = btrfs_invalidatepage,
2706 .releasepage = btrfs_releasepage,
2707 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002708};
2709
2710static struct address_space_operations btrfs_symlink_aops = {
2711 .readpage = btrfs_readpage,
2712 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002713 .invalidatepage = btrfs_invalidatepage,
2714 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002715};
2716
2717static struct inode_operations btrfs_file_inode_operations = {
2718 .truncate = btrfs_truncate,
2719 .getattr = btrfs_getattr,
2720 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002721 .setxattr = generic_setxattr,
2722 .getxattr = generic_getxattr,
2723 .listxattr = btrfs_listxattr,
2724 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002725};
2726
Josef Bacik618e21d2007-07-11 10:18:17 -04002727static struct inode_operations btrfs_special_inode_operations = {
2728 .getattr = btrfs_getattr,
2729 .setattr = btrfs_setattr,
2730};
2731
Chris Mason39279cc2007-06-12 06:35:45 -04002732static struct inode_operations btrfs_symlink_inode_operations = {
2733 .readlink = generic_readlink,
2734 .follow_link = page_follow_link_light,
2735 .put_link = page_put_link,
2736};