blob: 686dd03f34f25d8d6fc0e17c6116e71672592b3f [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);
535 drop_nlink(dentry->d_inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400536 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400537 dir->i_sb->s_dirt = 1;
538 }
539 return ret;
540}
541
542static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
543{
544 struct btrfs_root *root;
545 struct btrfs_trans_handle *trans;
546 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400547 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400548
549 root = BTRFS_I(dir)->root;
550 mutex_lock(&root->fs_info->fs_mutex);
551 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400552
Chris Mason39279cc2007-06-12 06:35:45 -0400553 btrfs_set_trans_block_group(trans, dir);
554 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400555 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400556
Chris Mason39279cc2007-06-12 06:35:45 -0400557 btrfs_end_transaction(trans, root);
558 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400559 btrfs_btree_balance_dirty(root, nr);
Chris Mason5f39d392007-10-15 16:14:19 -0400560
Chris Mason39279cc2007-06-12 06:35:45 -0400561 return ret;
562}
563
564static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
565{
566 struct inode *inode = dentry->d_inode;
567 int err;
568 int ret;
569 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400570 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400571 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400572
Yan134d4512007-10-25 15:49:25 -0400573 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
574 return -ENOTEMPTY;
575
Chris Mason39279cc2007-06-12 06:35:45 -0400576 mutex_lock(&root->fs_info->fs_mutex);
577 trans = btrfs_start_transaction(root, 1);
578 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400579
580 /* now the directory is empty */
581 err = btrfs_unlink_trans(trans, root, dir, dentry);
582 if (!err) {
583 inode->i_size = 0;
584 }
Chris Mason39544012007-12-12 14:38:19 -0500585
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400586 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400587 ret = btrfs_end_transaction(trans, root);
Yan134d4512007-10-25 15:49:25 -0400588 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400589 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -0500590
Chris Mason39279cc2007-06-12 06:35:45 -0400591 if (ret && !err)
592 err = ret;
593 return err;
594}
595
596static int btrfs_free_inode(struct btrfs_trans_handle *trans,
597 struct btrfs_root *root,
598 struct inode *inode)
599{
600 struct btrfs_path *path;
601 int ret;
602
603 clear_inode(inode);
604
605 path = btrfs_alloc_path();
606 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400607 ret = btrfs_lookup_inode(trans, root, path,
608 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400609 if (ret > 0)
610 ret = -ENOENT;
611 if (!ret)
612 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400613 btrfs_free_path(path);
614 return ret;
615}
616
617/*
Chris Mason39279cc2007-06-12 06:35:45 -0400618 * this can truncate away extent items, csum items and directory items.
619 * It starts at a high offset and removes keys until it can't find
620 * any higher than i_size.
621 *
622 * csum items that cross the new i_size are truncated to the new size
623 * as well.
624 */
625static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
626 struct btrfs_root *root,
627 struct inode *inode)
628{
629 int ret;
630 struct btrfs_path *path;
631 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400632 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400633 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400634 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400635 struct btrfs_file_extent_item *fi;
636 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400637 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400638 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500639 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500640 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400641 int found_extent;
642 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400643 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400644
Chris Masona52d9a82007-08-27 16:49:44 -0400645 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400646 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400647 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400648 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400649
Chris Mason39279cc2007-06-12 06:35:45 -0400650 /* FIXME, add redo link to tree so we don't leak on crash */
651 key.objectid = inode->i_ino;
652 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400653 key.type = (u8)-1;
654
Chris Mason39279cc2007-06-12 06:35:45 -0400655 while(1) {
656 btrfs_init_path(path);
657 fi = NULL;
658 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
659 if (ret < 0) {
660 goto error;
661 }
662 if (ret > 0) {
663 BUG_ON(path->slots[0] == 0);
664 path->slots[0]--;
665 }
Chris Mason5f39d392007-10-15 16:14:19 -0400666 leaf = path->nodes[0];
667 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
668 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400669
Chris Mason5f39d392007-10-15 16:14:19 -0400670 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400671 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400672
Chris Mason39279cc2007-06-12 06:35:45 -0400673 if (found_type != BTRFS_CSUM_ITEM_KEY &&
674 found_type != BTRFS_DIR_ITEM_KEY &&
675 found_type != BTRFS_DIR_INDEX_KEY &&
676 found_type != BTRFS_EXTENT_DATA_KEY)
677 break;
678
Chris Mason5f39d392007-10-15 16:14:19 -0400679 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400680 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400681 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400682 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400683 extent_type = btrfs_file_extent_type(leaf, fi);
684 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400685 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400686 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400687 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
688 struct btrfs_item *item = btrfs_item_nr(leaf,
689 path->slots[0]);
690 item_end += btrfs_file_extent_inline_len(leaf,
691 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400692 }
Yan008630c2007-11-07 13:31:09 -0500693 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400694 }
695 if (found_type == BTRFS_CSUM_ITEM_KEY) {
696 ret = btrfs_csum_truncate(trans, root, path,
697 inode->i_size);
698 BUG_ON(ret);
699 }
Yan008630c2007-11-07 13:31:09 -0500700 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400701 if (found_type == BTRFS_DIR_ITEM_KEY) {
702 found_type = BTRFS_INODE_ITEM_KEY;
703 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
704 found_type = BTRFS_CSUM_ITEM_KEY;
705 } else if (found_type) {
706 found_type--;
707 } else {
708 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400709 }
Yana61721d2007-09-17 11:08:38 -0400710 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400711 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400712 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400713 }
Chris Mason5f39d392007-10-15 16:14:19 -0400714 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400715 del_item = 1;
716 else
717 del_item = 0;
718 found_extent = 0;
719
720 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400721 if (found_type != BTRFS_EXTENT_DATA_KEY)
722 goto delete;
723
724 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400725 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400726 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400727 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400728 u64 orig_num_bytes =
729 btrfs_file_extent_num_bytes(leaf, fi);
730 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400731 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400732 btrfs_set_file_extent_num_bytes(leaf, fi,
733 extent_num_bytes);
734 num_dec = (orig_num_bytes -
735 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400736 if (extent_start != 0) {
737 inode->i_blocks -= num_dec;
738 }
Chris Mason5f39d392007-10-15 16:14:19 -0400739 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400740 } else {
Chris Masondb945352007-10-15 16:15:53 -0400741 extent_num_bytes =
742 btrfs_file_extent_disk_num_bytes(leaf,
743 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400744 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400745 num_dec = btrfs_file_extent_num_bytes(leaf,
746 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400747 if (extent_start != 0) {
748 found_extent = 1;
749 inode->i_blocks -= num_dec;
750 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500751 root_gen = btrfs_header_generation(leaf);
752 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400753 }
Chris Mason179e29e2007-11-01 11:28:41 -0400754 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
755 !del_item) {
756 u32 newsize = inode->i_size - found_key.offset;
757 newsize = btrfs_file_extent_calc_inline_size(newsize);
758 ret = btrfs_truncate_item(trans, root, path,
759 newsize, 1);
760 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400761 }
Chris Mason179e29e2007-11-01 11:28:41 -0400762delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400763 if (del_item) {
764 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400765 if (ret)
766 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400767 } else {
768 break;
769 }
770 btrfs_release_path(root, path);
771 if (found_extent) {
772 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500773 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500774 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500775 root_gen, inode->i_ino,
776 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400777 BUG_ON(ret);
778 }
779 }
780 ret = 0;
781error:
782 btrfs_release_path(root, path);
783 btrfs_free_path(path);
784 inode->i_sb->s_dirt = 1;
785 return ret;
786}
787
Chris Masonb888db22007-08-27 16:49:44 -0400788static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400789 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400790{
Chris Mason39279cc2007-06-12 06:35:45 -0400791 char *kaddr;
792 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400793 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400794 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400795 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400796
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400797 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400798
Chris Masonb888db22007-08-27 16:49:44 -0400799 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
800 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
801 page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400802 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400803 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400804 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
805 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400806 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400807 }
Chris Masonb888db22007-08-27 16:49:44 -0400808 set_page_dirty(page);
809 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400810
Chris Masona52d9a82007-08-27 16:49:44 -0400811 return ret;
812}
813
814/*
815 * taken from block_truncate_page, but does cow as it zeros out
816 * any bytes left in the last page in the file.
817 */
818static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
819{
820 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400821 struct btrfs_root *root = BTRFS_I(inode)->root;
822 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400823 pgoff_t index = from >> PAGE_CACHE_SHIFT;
824 unsigned offset = from & (PAGE_CACHE_SIZE-1);
825 struct page *page;
826 int ret = 0;
827 u64 page_start;
828
829 if ((offset & (blocksize - 1)) == 0)
830 goto out;
831
Chris Masondb945352007-10-15 16:15:53 -0400832 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400833 ret = -ENOMEM;
834 page = grab_cache_page(mapping, index);
835 if (!page)
836 goto out;
837 if (!PageUptodate(page)) {
838 ret = btrfs_readpage(NULL, page);
839 lock_page(page);
840 if (!PageUptodate(page)) {
841 ret = -EIO;
842 goto out;
843 }
844 }
Chris Mason35ebb932007-10-30 16:56:53 -0400845 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400846
Chris Masonb888db22007-08-27 16:49:44 -0400847 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400848
Chris Mason39279cc2007-06-12 06:35:45 -0400849 unlock_page(page);
850 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400851 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400852out:
853 return ret;
854}
855
856static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
857{
858 struct inode *inode = dentry->d_inode;
859 int err;
860
861 err = inode_change_ok(inode, attr);
862 if (err)
863 return err;
864
865 if (S_ISREG(inode->i_mode) &&
866 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
867 struct btrfs_trans_handle *trans;
868 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400869 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
870
Chris Mason5f39d392007-10-15 16:14:19 -0400871 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400872 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400873 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400874 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400875 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400876
877 if (attr->ia_size <= pos)
878 goto out;
879
880 btrfs_truncate_page(inode->i_mapping, inode->i_size);
881
Chris Mason2bf5a722007-08-30 11:54:02 -0400882 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400883 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400884
885 mutex_lock(&root->fs_info->fs_mutex);
886 trans = btrfs_start_transaction(root, 1);
887 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400888 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400889 pos, pos + hole_size, pos,
890 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400891
Chris Mason179e29e2007-11-01 11:28:41 -0400892 if (alloc_hint != EXTENT_MAP_INLINE) {
893 err = btrfs_insert_file_extent(trans, root,
894 inode->i_ino,
895 pos, 0, 0, hole_size);
896 }
Chris Mason39279cc2007-06-12 06:35:45 -0400897 btrfs_end_transaction(trans, root);
898 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400899 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400900 if (err)
901 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400902 }
903out:
904 err = inode_setattr(inode, attr);
905
906 return err;
907}
908void btrfs_delete_inode(struct inode *inode)
909{
910 struct btrfs_trans_handle *trans;
911 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400912 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400913 int ret;
914
915 truncate_inode_pages(&inode->i_data, 0);
916 if (is_bad_inode(inode)) {
917 goto no_delete;
918 }
Chris Mason5f39d392007-10-15 16:14:19 -0400919
Chris Mason39279cc2007-06-12 06:35:45 -0400920 inode->i_size = 0;
921 mutex_lock(&root->fs_info->fs_mutex);
922 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400923
Chris Mason39279cc2007-06-12 06:35:45 -0400924 btrfs_set_trans_block_group(trans, inode);
925 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400926 if (ret)
927 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -0500928 ret = btrfs_delete_xattrs(trans, root, inode);
929 if (ret)
930 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -0400931 ret = btrfs_free_inode(trans, root, inode);
932 if (ret)
933 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400934 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400935
Chris Mason39279cc2007-06-12 06:35:45 -0400936 btrfs_end_transaction(trans, root);
937 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400938 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400939 return;
Chris Mason54aa1f42007-06-22 14:16:25 -0400940
941no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400942 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400943 btrfs_end_transaction(trans, root);
944 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400945 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400946no_delete:
947 clear_inode(inode);
948}
949
950/*
951 * this returns the key found in the dir entry in the location pointer.
952 * If no dir entries were found, location->objectid is 0.
953 */
954static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
955 struct btrfs_key *location)
956{
957 const char *name = dentry->d_name.name;
958 int namelen = dentry->d_name.len;
959 struct btrfs_dir_item *di;
960 struct btrfs_path *path;
961 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -0400962 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400963
Chris Mason39544012007-12-12 14:38:19 -0500964 if (namelen == 1 && strcmp(name, ".") == 0) {
965 location->objectid = dir->i_ino;
966 location->type = BTRFS_INODE_ITEM_KEY;
967 location->offset = 0;
968 return 0;
969 }
Chris Mason39279cc2007-06-12 06:35:45 -0400970 path = btrfs_alloc_path();
971 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -0500972
Chris Mason7a720532007-12-13 09:06:59 -0500973 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -0500974 struct btrfs_key key;
975 struct extent_buffer *leaf;
976 u32 nritems;
977 int slot;
978
979 key.objectid = dir->i_ino;
980 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
981 key.offset = 0;
982 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
983 BUG_ON(ret == 0);
984 ret = 0;
985
986 leaf = path->nodes[0];
987 slot = path->slots[0];
988 nritems = btrfs_header_nritems(leaf);
989 if (slot >= nritems)
990 goto out_err;
991
992 btrfs_item_key_to_cpu(leaf, &key, slot);
993 if (key.objectid != dir->i_ino ||
994 key.type != BTRFS_INODE_REF_KEY) {
995 goto out_err;
996 }
997 location->objectid = key.offset;
998 location->type = BTRFS_INODE_ITEM_KEY;
999 location->offset = 0;
1000 goto out;
1001 }
1002
Chris Mason39279cc2007-06-12 06:35:45 -04001003 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1004 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001005 if (IS_ERR(di))
1006 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001007 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001008 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001009 }
Chris Mason5f39d392007-10-15 16:14:19 -04001010 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001011out:
Chris Mason39279cc2007-06-12 06:35:45 -04001012 btrfs_free_path(path);
1013 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001014out_err:
1015 location->objectid = 0;
1016 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001017}
1018
1019/*
1020 * when we hit a tree root in a directory, the btrfs part of the inode
1021 * needs to be changed to reflect the root directory of the tree root. This
1022 * is kind of like crossing a mount point.
1023 */
1024static int fixup_tree_root_location(struct btrfs_root *root,
1025 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001026 struct btrfs_root **sub_root,
1027 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001028{
1029 struct btrfs_path *path;
1030 struct btrfs_root_item *ri;
1031
1032 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1033 return 0;
1034 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1035 return 0;
1036
1037 path = btrfs_alloc_path();
1038 BUG_ON(!path);
1039 mutex_lock(&root->fs_info->fs_mutex);
1040
Josef Bacik58176a92007-08-29 15:47:34 -04001041 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1042 dentry->d_name.name,
1043 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001044 if (IS_ERR(*sub_root))
1045 return PTR_ERR(*sub_root);
1046
1047 ri = &(*sub_root)->root_item;
1048 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001049 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1050 location->offset = 0;
1051
1052 btrfs_free_path(path);
1053 mutex_unlock(&root->fs_info->fs_mutex);
1054 return 0;
1055}
1056
1057static int btrfs_init_locked_inode(struct inode *inode, void *p)
1058{
1059 struct btrfs_iget_args *args = p;
1060 inode->i_ino = args->ino;
1061 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -04001062 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1063 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001064 return 0;
1065}
1066
1067static int btrfs_find_actor(struct inode *inode, void *opaque)
1068{
1069 struct btrfs_iget_args *args = opaque;
1070 return (args->ino == inode->i_ino &&
1071 args->root == BTRFS_I(inode)->root);
1072}
1073
1074struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1075 struct btrfs_root *root)
1076{
1077 struct inode *inode;
1078 struct btrfs_iget_args args;
1079 args.ino = objectid;
1080 args.root = root;
1081
1082 inode = iget5_locked(s, objectid, btrfs_find_actor,
1083 btrfs_init_locked_inode,
1084 (void *)&args);
1085 return inode;
1086}
1087
1088static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1089 struct nameidata *nd)
1090{
1091 struct inode * inode;
1092 struct btrfs_inode *bi = BTRFS_I(dir);
1093 struct btrfs_root *root = bi->root;
1094 struct btrfs_root *sub_root = root;
1095 struct btrfs_key location;
1096 int ret;
1097
1098 if (dentry->d_name.len > BTRFS_NAME_LEN)
1099 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001100
Chris Mason39279cc2007-06-12 06:35:45 -04001101 mutex_lock(&root->fs_info->fs_mutex);
1102 ret = btrfs_inode_by_name(dir, dentry, &location);
1103 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001104
Chris Mason39279cc2007-06-12 06:35:45 -04001105 if (ret < 0)
1106 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001107
Chris Mason39279cc2007-06-12 06:35:45 -04001108 inode = NULL;
1109 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001110 ret = fixup_tree_root_location(root, &location, &sub_root,
1111 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001112 if (ret < 0)
1113 return ERR_PTR(ret);
1114 if (ret > 0)
1115 return ERR_PTR(-ENOENT);
1116 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1117 sub_root);
1118 if (!inode)
1119 return ERR_PTR(-EACCES);
1120 if (inode->i_state & I_NEW) {
1121 /* the inode and parent dir are two different roots */
1122 if (sub_root != root) {
1123 igrab(inode);
1124 sub_root->inode = inode;
1125 }
1126 BTRFS_I(inode)->root = sub_root;
1127 memcpy(&BTRFS_I(inode)->location, &location,
1128 sizeof(location));
1129 btrfs_read_locked_inode(inode);
1130 unlock_new_inode(inode);
1131 }
1132 }
1133 return d_splice_alias(inode, dentry);
1134}
1135
Chris Mason39279cc2007-06-12 06:35:45 -04001136static unsigned char btrfs_filetype_table[] = {
1137 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1138};
1139
1140static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1141{
1142 struct inode *inode = filp->f_path.dentry->d_inode;
1143 struct btrfs_root *root = BTRFS_I(inode)->root;
1144 struct btrfs_item *item;
1145 struct btrfs_dir_item *di;
1146 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001147 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001148 struct btrfs_path *path;
1149 int ret;
1150 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001151 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001152 int slot;
1153 int advance;
1154 unsigned char d_type;
1155 int over = 0;
1156 u32 di_cur;
1157 u32 di_total;
1158 u32 di_len;
1159 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001160 char tmp_name[32];
1161 char *name_ptr;
1162 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001163
1164 /* FIXME, use a real flag for deciding about the key type */
1165 if (root->fs_info->tree_root == root)
1166 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001167
Chris Mason39544012007-12-12 14:38:19 -05001168 /* special case for "." */
1169 if (filp->f_pos == 0) {
1170 over = filldir(dirent, ".", 1,
1171 1, inode->i_ino,
1172 DT_DIR);
1173 if (over)
1174 return 0;
1175 filp->f_pos = 1;
1176 }
1177
Chris Mason39279cc2007-06-12 06:35:45 -04001178 mutex_lock(&root->fs_info->fs_mutex);
1179 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001180 path = btrfs_alloc_path();
1181 path->reada = 2;
1182
1183 /* special case for .., just use the back ref */
1184 if (filp->f_pos == 1) {
1185 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1186 key.offset = 0;
1187 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1188 BUG_ON(ret == 0);
1189 leaf = path->nodes[0];
1190 slot = path->slots[0];
1191 nritems = btrfs_header_nritems(leaf);
1192 if (slot >= nritems) {
1193 btrfs_release_path(root, path);
1194 goto read_dir_items;
1195 }
1196 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1197 btrfs_release_path(root, path);
1198 if (found_key.objectid != key.objectid ||
1199 found_key.type != BTRFS_INODE_REF_KEY)
1200 goto read_dir_items;
1201 over = filldir(dirent, "..", 2,
1202 2, found_key.offset, DT_DIR);
1203 if (over)
1204 goto nopos;
1205 filp->f_pos = 2;
1206 }
1207
1208read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001209 btrfs_set_key_type(&key, key_type);
1210 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001211
Chris Mason39279cc2007-06-12 06:35:45 -04001212 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1213 if (ret < 0)
1214 goto err;
1215 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001216 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001217 leaf = path->nodes[0];
1218 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001219 slot = path->slots[0];
1220 if (advance || slot >= nritems) {
1221 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001222 ret = btrfs_next_leaf(root, path);
1223 if (ret)
1224 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001225 leaf = path->nodes[0];
1226 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001227 slot = path->slots[0];
1228 } else {
1229 slot++;
1230 path->slots[0]++;
1231 }
1232 }
1233 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001234 item = btrfs_item_nr(leaf, slot);
1235 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1236
1237 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001238 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001239 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001240 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001241 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001242 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001243
1244 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001245 advance = 1;
1246 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1247 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001248 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001249 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001250 struct btrfs_key location;
1251
1252 name_len = btrfs_dir_name_len(leaf, di);
1253 if (name_len < 32) {
1254 name_ptr = tmp_name;
1255 } else {
1256 name_ptr = kmalloc(name_len, GFP_NOFS);
1257 BUG_ON(!name_ptr);
1258 }
1259 read_extent_buffer(leaf, name_ptr,
1260 (unsigned long)(di + 1), name_len);
1261
1262 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1263 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1264
1265 over = filldir(dirent, name_ptr, name_len,
1266 found_key.offset,
1267 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001268 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001269
1270 if (name_ptr != tmp_name)
1271 kfree(name_ptr);
1272
Chris Mason39279cc2007-06-12 06:35:45 -04001273 if (over)
1274 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001275 di_len = btrfs_dir_name_len(leaf, di) +
1276 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001277 di_cur += di_len;
1278 di = (struct btrfs_dir_item *)((char *)di + di_len);
1279 }
1280 }
1281 filp->f_pos++;
1282nopos:
1283 ret = 0;
1284err:
1285 btrfs_release_path(root, path);
1286 btrfs_free_path(path);
1287 mutex_unlock(&root->fs_info->fs_mutex);
1288 return ret;
1289}
1290
1291int btrfs_write_inode(struct inode *inode, int wait)
1292{
1293 struct btrfs_root *root = BTRFS_I(inode)->root;
1294 struct btrfs_trans_handle *trans;
1295 int ret = 0;
1296
1297 if (wait) {
1298 mutex_lock(&root->fs_info->fs_mutex);
1299 trans = btrfs_start_transaction(root, 1);
1300 btrfs_set_trans_block_group(trans, inode);
1301 ret = btrfs_commit_transaction(trans, root);
1302 mutex_unlock(&root->fs_info->fs_mutex);
1303 }
1304 return ret;
1305}
1306
1307/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001308 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001309 * inode changes. But, it is most likely to find the inode in cache.
1310 * FIXME, needs more benchmarking...there are no reasons other than performance
1311 * to keep or drop this code.
1312 */
1313void btrfs_dirty_inode(struct inode *inode)
1314{
1315 struct btrfs_root *root = BTRFS_I(inode)->root;
1316 struct btrfs_trans_handle *trans;
1317
1318 mutex_lock(&root->fs_info->fs_mutex);
1319 trans = btrfs_start_transaction(root, 1);
1320 btrfs_set_trans_block_group(trans, inode);
1321 btrfs_update_inode(trans, root, inode);
1322 btrfs_end_transaction(trans, root);
1323 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001324}
1325
1326static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1327 struct btrfs_root *root,
1328 u64 objectid,
1329 struct btrfs_block_group_cache *group,
1330 int mode)
1331{
1332 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001333 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001334 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001335 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001336 int ret;
1337 int owner;
1338
Chris Mason5f39d392007-10-15 16:14:19 -04001339 path = btrfs_alloc_path();
1340 BUG_ON(!path);
1341
Chris Mason39279cc2007-06-12 06:35:45 -04001342 inode = new_inode(root->fs_info->sb);
1343 if (!inode)
1344 return ERR_PTR(-ENOMEM);
1345
Chris Masonb888db22007-08-27 16:49:44 -04001346 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1347 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001348 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001349
Chris Mason39279cc2007-06-12 06:35:45 -04001350 if (mode & S_IFDIR)
1351 owner = 0;
1352 else
1353 owner = 1;
1354 group = btrfs_find_block_group(root, group, 0, 0, owner);
1355 BTRFS_I(inode)->block_group = group;
1356
Chris Mason5f39d392007-10-15 16:14:19 -04001357 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1358 if (ret)
1359 goto fail;
1360
Chris Mason39279cc2007-06-12 06:35:45 -04001361 inode->i_uid = current->fsuid;
1362 inode->i_gid = current->fsgid;
1363 inode->i_mode = mode;
1364 inode->i_ino = objectid;
1365 inode->i_blocks = 0;
1366 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001367 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1368 struct btrfs_inode_item);
1369 fill_inode_item(path->nodes[0], inode_item, inode);
1370 btrfs_mark_buffer_dirty(path->nodes[0]);
1371 btrfs_free_path(path);
1372
Chris Mason39279cc2007-06-12 06:35:45 -04001373 location = &BTRFS_I(inode)->location;
1374 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001375 location->offset = 0;
1376 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1377
Chris Mason39279cc2007-06-12 06:35:45 -04001378 insert_inode_hash(inode);
1379 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001380fail:
1381 btrfs_free_path(path);
1382 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001383}
1384
1385static inline u8 btrfs_inode_type(struct inode *inode)
1386{
1387 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1388}
1389
1390static int btrfs_add_link(struct btrfs_trans_handle *trans,
1391 struct dentry *dentry, struct inode *inode)
1392{
1393 int ret;
1394 struct btrfs_key key;
1395 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001396 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001397
Chris Mason39279cc2007-06-12 06:35:45 -04001398 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001399 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1400 key.offset = 0;
1401
1402 ret = btrfs_insert_dir_item(trans, root,
1403 dentry->d_name.name, dentry->d_name.len,
1404 dentry->d_parent->d_inode->i_ino,
1405 &key, btrfs_inode_type(inode));
1406 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001407 ret = btrfs_insert_inode_ref(trans, root,
1408 dentry->d_name.name,
1409 dentry->d_name.len,
1410 inode->i_ino,
1411 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001412 parent_inode = dentry->d_parent->d_inode;
1413 parent_inode->i_size += dentry->d_name.len * 2;
1414 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001415 ret = btrfs_update_inode(trans, root,
1416 dentry->d_parent->d_inode);
1417 }
1418 return ret;
1419}
1420
1421static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1422 struct dentry *dentry, struct inode *inode)
1423{
1424 int err = btrfs_add_link(trans, dentry, inode);
1425 if (!err) {
1426 d_instantiate(dentry, inode);
1427 return 0;
1428 }
1429 if (err > 0)
1430 err = -EEXIST;
1431 return err;
1432}
1433
Josef Bacik618e21d2007-07-11 10:18:17 -04001434static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1435 int mode, dev_t rdev)
1436{
1437 struct btrfs_trans_handle *trans;
1438 struct btrfs_root *root = BTRFS_I(dir)->root;
1439 struct inode *inode;
1440 int err;
1441 int drop_inode = 0;
1442 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001443 unsigned long nr;
Josef Bacik618e21d2007-07-11 10:18:17 -04001444
1445 if (!new_valid_dev(rdev))
1446 return -EINVAL;
1447
1448 mutex_lock(&root->fs_info->fs_mutex);
1449 trans = btrfs_start_transaction(root, 1);
1450 btrfs_set_trans_block_group(trans, dir);
1451
1452 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1453 if (err) {
1454 err = -ENOSPC;
1455 goto out_unlock;
1456 }
1457
1458 inode = btrfs_new_inode(trans, root, objectid,
1459 BTRFS_I(dir)->block_group, mode);
1460 err = PTR_ERR(inode);
1461 if (IS_ERR(inode))
1462 goto out_unlock;
1463
1464 btrfs_set_trans_block_group(trans, inode);
1465 err = btrfs_add_nondir(trans, dentry, inode);
1466 if (err)
1467 drop_inode = 1;
1468 else {
1469 inode->i_op = &btrfs_special_inode_operations;
1470 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001471 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001472 }
1473 dir->i_sb->s_dirt = 1;
1474 btrfs_update_inode_block_group(trans, inode);
1475 btrfs_update_inode_block_group(trans, dir);
1476out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001477 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001478 btrfs_end_transaction(trans, root);
1479 mutex_unlock(&root->fs_info->fs_mutex);
1480
1481 if (drop_inode) {
1482 inode_dec_link_count(inode);
1483 iput(inode);
1484 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001485 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001486 return err;
1487}
1488
Chris Mason39279cc2007-06-12 06:35:45 -04001489static int btrfs_create(struct inode *dir, struct dentry *dentry,
1490 int mode, struct nameidata *nd)
1491{
1492 struct btrfs_trans_handle *trans;
1493 struct btrfs_root *root = BTRFS_I(dir)->root;
1494 struct inode *inode;
1495 int err;
1496 int drop_inode = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001497 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001498 u64 objectid;
1499
1500 mutex_lock(&root->fs_info->fs_mutex);
1501 trans = btrfs_start_transaction(root, 1);
1502 btrfs_set_trans_block_group(trans, dir);
1503
1504 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1505 if (err) {
1506 err = -ENOSPC;
1507 goto out_unlock;
1508 }
1509
1510 inode = btrfs_new_inode(trans, root, objectid,
1511 BTRFS_I(dir)->block_group, mode);
1512 err = PTR_ERR(inode);
1513 if (IS_ERR(inode))
1514 goto out_unlock;
1515
1516 btrfs_set_trans_block_group(trans, inode);
1517 err = btrfs_add_nondir(trans, dentry, inode);
1518 if (err)
1519 drop_inode = 1;
1520 else {
1521 inode->i_mapping->a_ops = &btrfs_aops;
1522 inode->i_fop = &btrfs_file_operations;
1523 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001524 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1525 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001526 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001527 }
1528 dir->i_sb->s_dirt = 1;
1529 btrfs_update_inode_block_group(trans, inode);
1530 btrfs_update_inode_block_group(trans, dir);
1531out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001532 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001533 btrfs_end_transaction(trans, root);
1534 mutex_unlock(&root->fs_info->fs_mutex);
1535
1536 if (drop_inode) {
1537 inode_dec_link_count(inode);
1538 iput(inode);
1539 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001540 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001541 return err;
1542}
1543
1544static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1545 struct dentry *dentry)
1546{
1547 struct btrfs_trans_handle *trans;
1548 struct btrfs_root *root = BTRFS_I(dir)->root;
1549 struct inode *inode = old_dentry->d_inode;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001550 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001551 int err;
1552 int drop_inode = 0;
1553
1554 if (inode->i_nlink == 0)
1555 return -ENOENT;
1556
1557 inc_nlink(inode);
1558 mutex_lock(&root->fs_info->fs_mutex);
1559 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001560
Chris Mason39279cc2007-06-12 06:35:45 -04001561 btrfs_set_trans_block_group(trans, dir);
1562 atomic_inc(&inode->i_count);
1563 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001564
Chris Mason39279cc2007-06-12 06:35:45 -04001565 if (err)
1566 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001567
Chris Mason39279cc2007-06-12 06:35:45 -04001568 dir->i_sb->s_dirt = 1;
1569 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001570 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001571
Chris Mason54aa1f42007-06-22 14:16:25 -04001572 if (err)
1573 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001574
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001575 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001576 btrfs_end_transaction(trans, root);
1577 mutex_unlock(&root->fs_info->fs_mutex);
1578
1579 if (drop_inode) {
1580 inode_dec_link_count(inode);
1581 iput(inode);
1582 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001583 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001584 return err;
1585}
1586
Chris Mason39279cc2007-06-12 06:35:45 -04001587static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1588{
1589 struct inode *inode;
1590 struct btrfs_trans_handle *trans;
1591 struct btrfs_root *root = BTRFS_I(dir)->root;
1592 int err = 0;
1593 int drop_on_err = 0;
1594 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001595 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001596
1597 mutex_lock(&root->fs_info->fs_mutex);
1598 trans = btrfs_start_transaction(root, 1);
1599 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001600
Chris Mason39279cc2007-06-12 06:35:45 -04001601 if (IS_ERR(trans)) {
1602 err = PTR_ERR(trans);
1603 goto out_unlock;
1604 }
1605
1606 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1607 if (err) {
1608 err = -ENOSPC;
1609 goto out_unlock;
1610 }
1611
1612 inode = btrfs_new_inode(trans, root, objectid,
1613 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1614 if (IS_ERR(inode)) {
1615 err = PTR_ERR(inode);
1616 goto out_fail;
1617 }
Chris Mason5f39d392007-10-15 16:14:19 -04001618
Chris Mason39279cc2007-06-12 06:35:45 -04001619 drop_on_err = 1;
1620 inode->i_op = &btrfs_dir_inode_operations;
1621 inode->i_fop = &btrfs_dir_file_operations;
1622 btrfs_set_trans_block_group(trans, inode);
1623
Chris Mason39544012007-12-12 14:38:19 -05001624 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001625 err = btrfs_update_inode(trans, root, inode);
1626 if (err)
1627 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001628
Chris Mason39279cc2007-06-12 06:35:45 -04001629 err = btrfs_add_link(trans, dentry, inode);
1630 if (err)
1631 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001632
Chris Mason39279cc2007-06-12 06:35:45 -04001633 d_instantiate(dentry, inode);
1634 drop_on_err = 0;
1635 dir->i_sb->s_dirt = 1;
1636 btrfs_update_inode_block_group(trans, inode);
1637 btrfs_update_inode_block_group(trans, dir);
1638
1639out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001640 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001641 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001642
Chris Mason39279cc2007-06-12 06:35:45 -04001643out_unlock:
1644 mutex_unlock(&root->fs_info->fs_mutex);
1645 if (drop_on_err)
1646 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001647 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001648 return err;
1649}
1650
Chris Masona52d9a82007-08-27 16:49:44 -04001651struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1652 size_t page_offset, u64 start, u64 end,
1653 int create)
1654{
1655 int ret;
1656 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001657 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001658 u64 extent_start = 0;
1659 u64 extent_end = 0;
1660 u64 objectid = inode->i_ino;
1661 u32 found_type;
1662 int failed_insert = 0;
1663 struct btrfs_path *path;
1664 struct btrfs_root *root = BTRFS_I(inode)->root;
1665 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001666 struct extent_buffer *leaf;
1667 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001668 struct extent_map *em = NULL;
1669 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1670 struct btrfs_trans_handle *trans = NULL;
1671
1672 path = btrfs_alloc_path();
1673 BUG_ON(!path);
1674 mutex_lock(&root->fs_info->fs_mutex);
1675
1676again:
1677 em = lookup_extent_mapping(em_tree, start, end);
1678 if (em) {
1679 goto out;
1680 }
1681 if (!em) {
1682 em = alloc_extent_map(GFP_NOFS);
1683 if (!em) {
1684 err = -ENOMEM;
1685 goto out;
1686 }
Chris Mason5f39d392007-10-15 16:14:19 -04001687 em->start = EXTENT_MAP_HOLE;
1688 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001689 }
1690 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001691 ret = btrfs_lookup_file_extent(trans, root, path,
1692 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001693 if (ret < 0) {
1694 err = ret;
1695 goto out;
1696 }
1697
1698 if (ret != 0) {
1699 if (path->slots[0] == 0)
1700 goto not_found;
1701 path->slots[0]--;
1702 }
1703
Chris Mason5f39d392007-10-15 16:14:19 -04001704 leaf = path->nodes[0];
1705 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001706 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001707 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001708 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1709 found_type = btrfs_key_type(&found_key);
1710 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001711 found_type != BTRFS_EXTENT_DATA_KEY) {
1712 goto not_found;
1713 }
1714
Chris Mason5f39d392007-10-15 16:14:19 -04001715 found_type = btrfs_file_extent_type(leaf, item);
1716 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001717 if (found_type == BTRFS_FILE_EXTENT_REG) {
1718 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001719 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001720 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001721 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001722 em->start = start;
1723 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001724 if (end < extent_start)
1725 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001726 em->end = extent_end - 1;
1727 } else {
1728 em->end = end;
1729 }
1730 goto not_found_em;
1731 }
Chris Masondb945352007-10-15 16:15:53 -04001732 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1733 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001734 em->start = extent_start;
1735 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001736 em->block_start = EXTENT_MAP_HOLE;
1737 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001738 goto insert;
1739 }
Chris Masondb945352007-10-15 16:15:53 -04001740 bytenr += btrfs_file_extent_offset(leaf, item);
1741 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001742 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001743 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001744 em->start = extent_start;
1745 em->end = extent_end - 1;
1746 goto insert;
1747 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001748 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001749 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001750 size_t size;
1751 size_t extent_offset;
1752 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001753
Chris Mason5f39d392007-10-15 16:14:19 -04001754 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1755 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001756 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001757 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001758 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001759 em->start = start;
1760 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001761 if (end < extent_start)
1762 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001763 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001764 } else {
1765 em->end = end;
1766 }
1767 goto not_found_em;
1768 }
1769 em->block_start = EXTENT_MAP_INLINE;
1770 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001771
1772 if (!page) {
1773 em->start = extent_start;
1774 em->end = extent_start + size - 1;
1775 goto out;
1776 }
1777
Chris Mason35ebb932007-10-30 16:56:53 -04001778 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001779 extent_start + page_offset;
1780 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1781 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001782 em->start = extent_start + extent_offset;
1783 em->end = (em->start + copy_size -1) |
1784 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001785 map = kmap(page);
1786 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001787 if (create == 0 && !PageUptodate(page)) {
1788 read_extent_buffer(leaf, map + page_offset, ptr,
1789 copy_size);
1790 flush_dcache_page(page);
1791 } else if (create && PageUptodate(page)) {
1792 if (!trans) {
1793 kunmap(page);
1794 free_extent_map(em);
1795 em = NULL;
1796 btrfs_release_path(root, path);
1797 trans = btrfs_start_transaction(root, 1);
1798 goto again;
1799 }
1800 write_extent_buffer(leaf, map + page_offset, ptr,
1801 copy_size);
1802 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001803 }
Chris Masona52d9a82007-08-27 16:49:44 -04001804 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001805 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001806 goto insert;
1807 } else {
1808 printk("unkknown found_type %d\n", found_type);
1809 WARN_ON(1);
1810 }
1811not_found:
1812 em->start = start;
1813 em->end = end;
1814not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001815 em->block_start = EXTENT_MAP_HOLE;
1816 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001817insert:
1818 btrfs_release_path(root, path);
1819 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001820 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001821 err = -EIO;
1822 goto out;
1823 }
1824 ret = add_extent_mapping(em_tree, em);
1825 if (ret == -EEXIST) {
1826 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001827 em = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04001828 failed_insert++;
1829 if (failed_insert > 5) {
1830 printk("failing to insert %Lu %Lu\n", start, end);
1831 err = -EIO;
1832 goto out;
1833 }
Chris Masona52d9a82007-08-27 16:49:44 -04001834 goto again;
1835 }
1836 err = 0;
1837out:
1838 btrfs_free_path(path);
1839 if (trans) {
1840 ret = btrfs_end_transaction(trans, root);
1841 if (!err)
1842 err = ret;
1843 }
1844 mutex_unlock(&root->fs_info->fs_mutex);
1845 if (err) {
1846 free_extent_map(em);
1847 WARN_ON(1);
1848 return ERR_PTR(err);
1849 }
1850 return em;
1851}
1852
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001853static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001854{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001855 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001856}
1857
1858static int btrfs_prepare_write(struct file *file, struct page *page,
1859 unsigned from, unsigned to)
1860{
Chris Masona52d9a82007-08-27 16:49:44 -04001861 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1862 page->mapping->host, page, from, to,
1863 btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001864}
1865
Chris Mason9ebefb182007-06-15 13:50:00 -04001866int btrfs_readpage(struct file *file, struct page *page)
1867{
Chris Masona52d9a82007-08-27 16:49:44 -04001868 struct extent_map_tree *tree;
1869 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1870 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001871}
Chris Mason39279cc2007-06-12 06:35:45 -04001872static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1873{
Chris Masona52d9a82007-08-27 16:49:44 -04001874 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001875
1876
1877 if (current->flags & PF_MEMALLOC) {
1878 redirty_page_for_writepage(wbc, page);
1879 unlock_page(page);
1880 return 0;
1881 }
Chris Masona52d9a82007-08-27 16:49:44 -04001882 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1883 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1884}
Chris Mason39279cc2007-06-12 06:35:45 -04001885
Chris Masonb293f022007-11-01 19:45:34 -04001886static int btrfs_writepages(struct address_space *mapping,
1887 struct writeback_control *wbc)
1888{
1889 struct extent_map_tree *tree;
1890 tree = &BTRFS_I(mapping->host)->extent_tree;
1891 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1892}
1893
Chris Mason3ab2fb52007-11-08 10:59:22 -05001894static int
1895btrfs_readpages(struct file *file, struct address_space *mapping,
1896 struct list_head *pages, unsigned nr_pages)
1897{
1898 struct extent_map_tree *tree;
1899 tree = &BTRFS_I(mapping->host)->extent_tree;
1900 return extent_readpages(tree, mapping, pages, nr_pages,
1901 btrfs_get_extent);
1902}
1903
Chris Masona52d9a82007-08-27 16:49:44 -04001904static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1905{
1906 struct extent_map_tree *tree;
1907 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001908
Chris Masona52d9a82007-08-27 16:49:44 -04001909 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1910 ret = try_release_extent_mapping(tree, page);
1911 if (ret == 1) {
1912 ClearPagePrivate(page);
1913 set_page_private(page, 0);
1914 page_cache_release(page);
1915 }
1916 return ret;
1917}
Chris Mason39279cc2007-06-12 06:35:45 -04001918
Chris Masona52d9a82007-08-27 16:49:44 -04001919static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1920{
1921 struct extent_map_tree *tree;
1922
1923 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1924 extent_invalidatepage(tree, page, offset);
1925 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001926}
1927
Chris Mason9ebefb182007-06-15 13:50:00 -04001928/*
1929 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1930 * called from a page fault handler when a page is first dirtied. Hence we must
1931 * be careful to check for EOF conditions here. We set the page up correctly
1932 * for a written page which means we get ENOSPC checking when writing into
1933 * holes and correct delalloc and unwritten extent mapping on filesystems that
1934 * support these features.
1935 *
1936 * We are not allowed to take the i_mutex here so we have to play games to
1937 * protect against truncate races as the page could now be beyond EOF. Because
1938 * vmtruncate() writes the inode size before removing pages, once we have the
1939 * page lock we can determine safely if the page is beyond EOF. If it is not
1940 * beyond EOF, then the page is guaranteed safe against truncation until we
1941 * unlock the page.
1942 */
1943int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1944{
1945 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1946 unsigned long end;
1947 loff_t size;
1948 int ret = -EINVAL;
Chris Masona52d9a82007-08-27 16:49:44 -04001949 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04001950
Chris Mason011410b2007-09-10 19:58:36 -04001951 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001952 lock_page(page);
1953 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04001954 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04001955 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001956
Chris Mason9ebefb182007-06-15 13:50:00 -04001957 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04001958 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04001959 /* page got truncated out from underneath us */
1960 goto out_unlock;
1961 }
1962
1963 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04001964 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04001965 end = size & ~PAGE_CACHE_MASK;
1966 else
1967 end = PAGE_CACHE_SIZE;
1968
Chris Masonb888db22007-08-27 16:49:44 -04001969 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04001970
1971out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04001972 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001973 unlock_page(page);
1974 return ret;
1975}
1976
Chris Mason39279cc2007-06-12 06:35:45 -04001977static void btrfs_truncate(struct inode *inode)
1978{
1979 struct btrfs_root *root = BTRFS_I(inode)->root;
1980 int ret;
1981 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001982 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001983
1984 if (!S_ISREG(inode->i_mode))
1985 return;
1986 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1987 return;
1988
1989 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1990
1991 mutex_lock(&root->fs_info->fs_mutex);
1992 trans = btrfs_start_transaction(root, 1);
1993 btrfs_set_trans_block_group(trans, inode);
1994
1995 /* FIXME, add redo link to tree so we don't leak on crash */
1996 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001997 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001998 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001999
Chris Mason39279cc2007-06-12 06:35:45 -04002000 ret = btrfs_end_transaction(trans, root);
2001 BUG_ON(ret);
2002 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002003 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002004}
2005
2006int btrfs_commit_write(struct file *file, struct page *page,
2007 unsigned from, unsigned to)
2008{
Chris Masone9906a92007-12-14 12:56:58 -05002009 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2010 struct inode *inode = page->mapping->host;
2011
2012 btrfs_cow_one_page(inode, page, PAGE_CACHE_SIZE);
2013
Chris Masone9906a92007-12-14 12:56:58 -05002014 if (pos > inode->i_size) {
2015 i_size_write(inode, pos);
2016 mark_inode_dirty(inode);
2017 }
2018 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002019}
2020
2021static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2022{
2023 struct btrfs_trans_handle *trans;
2024 struct btrfs_key key;
2025 struct btrfs_root_item root_item;
2026 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002027 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002028 struct btrfs_root *new_root;
2029 struct inode *inode;
2030 struct inode *dir;
2031 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002032 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002033 u64 objectid;
2034 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002035 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002036
2037 mutex_lock(&root->fs_info->fs_mutex);
2038 trans = btrfs_start_transaction(root, 1);
2039 BUG_ON(!trans);
2040
Chris Mason7bb86312007-12-11 09:25:06 -05002041 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2042 0, &objectid);
2043 if (ret)
2044 goto fail;
2045
2046 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2047 objectid, trans->transid, 0, 0,
2048 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002049 if (IS_ERR(leaf))
2050 return PTR_ERR(leaf);
2051
2052 btrfs_set_header_nritems(leaf, 0);
2053 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002054 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002055 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002056 btrfs_set_header_owner(leaf, objectid);
2057
Chris Mason5f39d392007-10-15 16:14:19 -04002058 write_extent_buffer(leaf, root->fs_info->fsid,
2059 (unsigned long)btrfs_header_fsid(leaf),
2060 BTRFS_FSID_SIZE);
2061 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002062
2063 inode_item = &root_item.inode;
2064 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002065 inode_item->generation = cpu_to_le64(1);
2066 inode_item->size = cpu_to_le64(3);
2067 inode_item->nlink = cpu_to_le32(1);
2068 inode_item->nblocks = cpu_to_le64(1);
2069 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002070
Chris Masondb945352007-10-15 16:15:53 -04002071 btrfs_set_root_bytenr(&root_item, leaf->start);
2072 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002073 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002074 btrfs_set_root_used(&root_item, 0);
2075
Chris Mason5eda7b52007-06-22 14:16:25 -04002076 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2077 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002078
2079 free_extent_buffer(leaf);
2080 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002081
Chris Mason39279cc2007-06-12 06:35:45 -04002082 btrfs_set_root_dirid(&root_item, new_dirid);
2083
2084 key.objectid = objectid;
2085 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002086 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2087 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2088 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002089 if (ret)
2090 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002091
2092 /*
2093 * insert the directory item
2094 */
2095 key.offset = (u64)-1;
2096 dir = root->fs_info->sb->s_root->d_inode;
2097 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2098 name, namelen, dir->i_ino, &key,
2099 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002100 if (ret)
2101 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002102
Chris Mason39544012007-12-12 14:38:19 -05002103 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2104 name, namelen, objectid,
2105 root->fs_info->sb->s_root->d_inode->i_ino);
2106 if (ret)
2107 goto fail;
2108
Chris Mason39279cc2007-06-12 06:35:45 -04002109 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002110 if (ret)
2111 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002112
Josef Bacik58176a92007-08-29 15:47:34 -04002113 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002114 BUG_ON(!new_root);
2115
2116 trans = btrfs_start_transaction(new_root, 1);
2117 BUG_ON(!trans);
2118
2119 inode = btrfs_new_inode(trans, new_root, new_dirid,
2120 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002121 if (IS_ERR(inode))
2122 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002123 inode->i_op = &btrfs_dir_inode_operations;
2124 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002125 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002126
Chris Mason39544012007-12-12 14:38:19 -05002127 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2128 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002129 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002130 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002131 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002132 if (ret)
2133 goto fail;
2134fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002135 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002136 err = btrfs_commit_transaction(trans, root);
2137 if (err && !ret)
2138 ret = err;
2139fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002140 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002141 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002142 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002143}
2144
2145static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2146{
2147 struct btrfs_trans_handle *trans;
2148 struct btrfs_key key;
2149 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002150 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04002151 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002152 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002153 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002154 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002155
2156 if (!root->ref_cows)
2157 return -EINVAL;
2158
Chris Mason011410b2007-09-10 19:58:36 -04002159 down_write(&root->snap_sem);
2160 freeze_bdev(root->fs_info->sb->s_bdev);
2161 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2162
Chris Mason39279cc2007-06-12 06:35:45 -04002163 mutex_lock(&root->fs_info->fs_mutex);
2164 trans = btrfs_start_transaction(root, 1);
2165 BUG_ON(!trans);
2166
2167 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002168 if (ret)
2169 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002170
2171 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2172 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002173 if (ret)
2174 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002175
2176 memcpy(&new_root_item, &root->root_item,
2177 sizeof(new_root_item));
2178
2179 key.objectid = objectid;
2180 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002181 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Masonbe20aa92007-12-17 20:14:01 -05002182
Yan96919752007-12-04 13:20:20 -05002183 extent_buffer_get(root->node);
Chris Mason83df7c12007-08-27 16:49:44 -04002184 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Yan96919752007-12-04 13:20:20 -05002185 free_extent_buffer(tmp);
Chris Masonbe20aa92007-12-17 20:14:01 -05002186
2187 btrfs_copy_root(trans, root, root->node, &tmp, objectid);
2188
2189 btrfs_set_root_bytenr(&new_root_item, tmp->start);
2190 btrfs_set_root_level(&new_root_item, btrfs_header_level(tmp));
Chris Mason39279cc2007-06-12 06:35:45 -04002191 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2192 &new_root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05002193 free_extent_buffer(tmp);
Chris Mason54aa1f42007-06-22 14:16:25 -04002194 if (ret)
2195 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002196
2197 /*
2198 * insert the directory item
2199 */
2200 key.offset = (u64)-1;
2201 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2202 name, namelen,
2203 root->fs_info->sb->s_root->d_inode->i_ino,
2204 &key, BTRFS_FT_DIR);
2205
Chris Mason54aa1f42007-06-22 14:16:25 -04002206 if (ret)
2207 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002208
Chris Mason39544012007-12-12 14:38:19 -05002209 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2210 name, namelen, objectid,
2211 root->fs_info->sb->s_root->d_inode->i_ino);
2212
2213 if (ret)
2214 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002215fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002216 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002217 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002218
Chris Mason54aa1f42007-06-22 14:16:25 -04002219 if (err && !ret)
2220 ret = err;
Chris Mason5f39d392007-10-15 16:14:19 -04002221
Chris Mason39279cc2007-06-12 06:35:45 -04002222 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002223 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002224 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002225 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002226}
2227
Chris Mason86479a02007-09-10 19:58:16 -04002228static unsigned long force_ra(struct address_space *mapping,
2229 struct file_ra_state *ra, struct file *file,
2230 pgoff_t offset, pgoff_t last_index)
2231{
2232 pgoff_t req_size;
2233
2234#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2235 req_size = last_index - offset + 1;
2236 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2237 return offset;
2238#else
2239 req_size = min(last_index - offset + 1, (pgoff_t)128);
2240 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2241 return offset + req_size;
2242#endif
2243}
2244
2245int btrfs_defrag_file(struct file *file) {
2246 struct inode *inode = file->f_path.dentry->d_inode;
2247 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2248 struct page *page;
2249 unsigned long last_index;
2250 unsigned long ra_index = 0;
2251 u64 page_start;
2252 u64 page_end;
2253 unsigned long i;
2254
2255 mutex_lock(&inode->i_mutex);
2256 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2257 for (i = 0; i <= last_index; i++) {
2258 if (i == ra_index) {
2259 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2260 file, ra_index, last_index);
2261 }
2262 page = grab_cache_page(inode->i_mapping, i);
2263 if (!page)
2264 goto out_unlock;
2265 if (!PageUptodate(page)) {
2266 btrfs_readpage(NULL, page);
2267 lock_page(page);
2268 if (!PageUptodate(page)) {
2269 unlock_page(page);
2270 page_cache_release(page);
2271 goto out_unlock;
2272 }
2273 }
Chris Mason35ebb932007-10-30 16:56:53 -04002274 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002275 page_end = page_start + PAGE_CACHE_SIZE - 1;
2276
2277 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2278 set_extent_delalloc(em_tree, page_start,
2279 page_end, GFP_NOFS);
2280 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2281 set_page_dirty(page);
2282 unlock_page(page);
2283 page_cache_release(page);
2284 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2285 }
2286
2287out_unlock:
2288 mutex_unlock(&inode->i_mutex);
2289 return 0;
2290}
2291
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002292static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2293{
2294 struct btrfs_ioctl_vol_args vol_args;
2295 struct btrfs_dir_item *di;
2296 struct btrfs_path *path;
2297 int namelen;
2298 u64 root_dirid;
2299
2300 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2301 return -EFAULT;
Chris Mason5f39d392007-10-15 16:14:19 -04002302
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002303 namelen = strlen(vol_args.name);
2304 if (namelen > BTRFS_VOL_NAME_MAX)
2305 return -EINVAL;
2306 if (strchr(vol_args.name, '/'))
2307 return -EINVAL;
2308
2309 path = btrfs_alloc_path();
2310 if (!path)
2311 return -ENOMEM;
2312
2313 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2314 mutex_lock(&root->fs_info->fs_mutex);
2315 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2316 path, root_dirid,
2317 vol_args.name, namelen, 0);
2318 mutex_unlock(&root->fs_info->fs_mutex);
2319 btrfs_free_path(path);
2320 if (di && !IS_ERR(di))
2321 return -EEXIST;
2322 if (IS_ERR(di))
2323 return PTR_ERR(di);
2324
2325 if (root == root->fs_info->tree_root)
2326 return create_subvol(root, vol_args.name, namelen);
2327 return create_snapshot(root, vol_args.name, namelen);
2328}
2329
2330static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002331{
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002332 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002333 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002334
2335 switch (inode->i_mode & S_IFMT) {
2336 case S_IFDIR:
2337 mutex_lock(&root->fs_info->fs_mutex);
2338 btrfs_defrag_root(root, 0);
2339 btrfs_defrag_root(root->fs_info->extent_root, 0);
2340 mutex_unlock(&root->fs_info->fs_mutex);
2341 break;
2342 case S_IFREG:
2343 btrfs_defrag_file(file);
2344 break;
2345 }
2346
2347 return 0;
2348}
2349
2350long btrfs_ioctl(struct file *file, unsigned int
2351 cmd, unsigned long arg)
2352{
2353 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002354
2355 switch (cmd) {
2356 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002357 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002358 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002359 return btrfs_ioctl_defrag(file);
Chris Mason39279cc2007-06-12 06:35:45 -04002360 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002361
2362 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002363}
2364
Chris Mason39279cc2007-06-12 06:35:45 -04002365/*
2366 * Called inside transaction, so use GFP_NOFS
2367 */
2368struct inode *btrfs_alloc_inode(struct super_block *sb)
2369{
2370 struct btrfs_inode *ei;
2371
2372 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2373 if (!ei)
2374 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002375 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002376 return &ei->vfs_inode;
2377}
2378
2379void btrfs_destroy_inode(struct inode *inode)
2380{
2381 WARN_ON(!list_empty(&inode->i_dentry));
2382 WARN_ON(inode->i_data.nrpages);
2383
2384 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2385}
2386
Chris Mason44ec0b72007-10-29 10:55:05 -04002387#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2388static void init_once(struct kmem_cache * cachep, void *foo)
2389#else
Chris Mason39279cc2007-06-12 06:35:45 -04002390static void init_once(void * foo, struct kmem_cache * cachep,
2391 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002392#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002393{
2394 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2395
2396 inode_init_once(&ei->vfs_inode);
2397}
2398
2399void btrfs_destroy_cachep(void)
2400{
2401 if (btrfs_inode_cachep)
2402 kmem_cache_destroy(btrfs_inode_cachep);
2403 if (btrfs_trans_handle_cachep)
2404 kmem_cache_destroy(btrfs_trans_handle_cachep);
2405 if (btrfs_transaction_cachep)
2406 kmem_cache_destroy(btrfs_transaction_cachep);
2407 if (btrfs_bit_radix_cachep)
2408 kmem_cache_destroy(btrfs_bit_radix_cachep);
2409 if (btrfs_path_cachep)
2410 kmem_cache_destroy(btrfs_path_cachep);
2411}
2412
Chris Mason86479a02007-09-10 19:58:16 -04002413struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002414 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002415#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2416 void (*ctor)(struct kmem_cache *, void *)
2417#else
Chris Mason92fee662007-07-25 12:31:35 -04002418 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002419 unsigned long)
2420#endif
2421 )
Chris Mason92fee662007-07-25 12:31:35 -04002422{
2423 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2424 SLAB_MEM_SPREAD | extra_flags), ctor
2425#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2426 ,NULL
2427#endif
2428 );
2429}
2430
Chris Mason39279cc2007-06-12 06:35:45 -04002431int btrfs_init_cachep(void)
2432{
Chris Mason86479a02007-09-10 19:58:16 -04002433 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002434 sizeof(struct btrfs_inode),
2435 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002436 if (!btrfs_inode_cachep)
2437 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002438 btrfs_trans_handle_cachep =
2439 btrfs_cache_create("btrfs_trans_handle_cache",
2440 sizeof(struct btrfs_trans_handle),
2441 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002442 if (!btrfs_trans_handle_cachep)
2443 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002444 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002445 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002446 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002447 if (!btrfs_transaction_cachep)
2448 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002449 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002450 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002451 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002452 if (!btrfs_path_cachep)
2453 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002454 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002455 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002456 if (!btrfs_bit_radix_cachep)
2457 goto fail;
2458 return 0;
2459fail:
2460 btrfs_destroy_cachep();
2461 return -ENOMEM;
2462}
2463
2464static int btrfs_getattr(struct vfsmount *mnt,
2465 struct dentry *dentry, struct kstat *stat)
2466{
2467 struct inode *inode = dentry->d_inode;
2468 generic_fillattr(inode, stat);
2469 stat->blksize = 256 * 1024;
2470 return 0;
2471}
2472
2473static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2474 struct inode * new_dir,struct dentry *new_dentry)
2475{
2476 struct btrfs_trans_handle *trans;
2477 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2478 struct inode *new_inode = new_dentry->d_inode;
2479 struct inode *old_inode = old_dentry->d_inode;
2480 struct timespec ctime = CURRENT_TIME;
2481 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002482 int ret;
2483
2484 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2485 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2486 return -ENOTEMPTY;
2487 }
Chris Mason5f39d392007-10-15 16:14:19 -04002488
Chris Mason39279cc2007-06-12 06:35:45 -04002489 mutex_lock(&root->fs_info->fs_mutex);
2490 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002491
Chris Mason39279cc2007-06-12 06:35:45 -04002492 btrfs_set_trans_block_group(trans, new_dir);
2493 path = btrfs_alloc_path();
2494 if (!path) {
2495 ret = -ENOMEM;
2496 goto out_fail;
2497 }
2498
2499 old_dentry->d_inode->i_nlink++;
2500 old_dir->i_ctime = old_dir->i_mtime = ctime;
2501 new_dir->i_ctime = new_dir->i_mtime = ctime;
2502 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002503
Chris Mason39279cc2007-06-12 06:35:45 -04002504 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2505 if (ret)
2506 goto out_fail;
2507
2508 if (new_inode) {
2509 new_inode->i_ctime = CURRENT_TIME;
2510 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2511 if (ret)
2512 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002513 }
2514 ret = btrfs_add_link(trans, new_dentry, old_inode);
2515 if (ret)
2516 goto out_fail;
2517
2518out_fail:
2519 btrfs_free_path(path);
2520 btrfs_end_transaction(trans, root);
2521 mutex_unlock(&root->fs_info->fs_mutex);
2522 return ret;
2523}
2524
2525static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2526 const char *symname)
2527{
2528 struct btrfs_trans_handle *trans;
2529 struct btrfs_root *root = BTRFS_I(dir)->root;
2530 struct btrfs_path *path;
2531 struct btrfs_key key;
2532 struct inode *inode;
2533 int err;
2534 int drop_inode = 0;
2535 u64 objectid;
2536 int name_len;
2537 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002538 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002539 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002540 struct extent_buffer *leaf;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002541 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002542
2543 name_len = strlen(symname) + 1;
2544 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2545 return -ENAMETOOLONG;
2546 mutex_lock(&root->fs_info->fs_mutex);
2547 trans = btrfs_start_transaction(root, 1);
2548 btrfs_set_trans_block_group(trans, dir);
2549
2550 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2551 if (err) {
2552 err = -ENOSPC;
2553 goto out_unlock;
2554 }
2555
2556 inode = btrfs_new_inode(trans, root, objectid,
2557 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2558 err = PTR_ERR(inode);
2559 if (IS_ERR(inode))
2560 goto out_unlock;
2561
2562 btrfs_set_trans_block_group(trans, inode);
2563 err = btrfs_add_nondir(trans, dentry, inode);
2564 if (err)
2565 drop_inode = 1;
2566 else {
2567 inode->i_mapping->a_ops = &btrfs_aops;
2568 inode->i_fop = &btrfs_file_operations;
2569 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002570 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2571 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002572 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002573 }
2574 dir->i_sb->s_dirt = 1;
2575 btrfs_update_inode_block_group(trans, inode);
2576 btrfs_update_inode_block_group(trans, dir);
2577 if (drop_inode)
2578 goto out_unlock;
2579
2580 path = btrfs_alloc_path();
2581 BUG_ON(!path);
2582 key.objectid = inode->i_ino;
2583 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002584 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2585 datasize = btrfs_file_extent_calc_inline_size(name_len);
2586 err = btrfs_insert_empty_item(trans, root, path, &key,
2587 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002588 if (err) {
2589 drop_inode = 1;
2590 goto out_unlock;
2591 }
Chris Mason5f39d392007-10-15 16:14:19 -04002592 leaf = path->nodes[0];
2593 ei = btrfs_item_ptr(leaf, path->slots[0],
2594 struct btrfs_file_extent_item);
2595 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2596 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002597 BTRFS_FILE_EXTENT_INLINE);
2598 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002599 write_extent_buffer(leaf, symname, ptr, name_len);
2600 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002601 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002602
Chris Mason39279cc2007-06-12 06:35:45 -04002603 inode->i_op = &btrfs_symlink_inode_operations;
2604 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2605 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002606 err = btrfs_update_inode(trans, root, inode);
2607 if (err)
2608 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002609
2610out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002611 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002612 btrfs_end_transaction(trans, root);
2613 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002614 if (drop_inode) {
2615 inode_dec_link_count(inode);
2616 iput(inode);
2617 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002618 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002619 return err;
2620}
2621
2622static struct inode_operations btrfs_dir_inode_operations = {
2623 .lookup = btrfs_lookup,
2624 .create = btrfs_create,
2625 .unlink = btrfs_unlink,
2626 .link = btrfs_link,
2627 .mkdir = btrfs_mkdir,
2628 .rmdir = btrfs_rmdir,
2629 .rename = btrfs_rename,
2630 .symlink = btrfs_symlink,
2631 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002632 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002633 .setxattr = generic_setxattr,
2634 .getxattr = generic_getxattr,
2635 .listxattr = btrfs_listxattr,
2636 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002637};
2638
2639static struct inode_operations btrfs_dir_ro_inode_operations = {
2640 .lookup = btrfs_lookup,
2641};
2642
2643static struct file_operations btrfs_dir_file_operations = {
2644 .llseek = generic_file_llseek,
2645 .read = generic_read_dir,
2646 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002647 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002648#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002649 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002650#endif
2651};
2652
Chris Mason07157aa2007-08-30 08:50:51 -04002653static struct extent_map_ops btrfs_extent_map_ops = {
2654 .fill_delalloc = run_delalloc_range,
2655 .writepage_io_hook = btrfs_writepage_io_hook,
2656 .readpage_io_hook = btrfs_readpage_io_hook,
2657 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2658};
2659
Chris Mason39279cc2007-06-12 06:35:45 -04002660static struct address_space_operations btrfs_aops = {
2661 .readpage = btrfs_readpage,
2662 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002663 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002664 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002665 .sync_page = block_sync_page,
2666 .prepare_write = btrfs_prepare_write,
2667 .commit_write = btrfs_commit_write,
2668 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002669 .invalidatepage = btrfs_invalidatepage,
2670 .releasepage = btrfs_releasepage,
2671 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002672};
2673
2674static struct address_space_operations btrfs_symlink_aops = {
2675 .readpage = btrfs_readpage,
2676 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002677 .invalidatepage = btrfs_invalidatepage,
2678 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002679};
2680
2681static struct inode_operations btrfs_file_inode_operations = {
2682 .truncate = btrfs_truncate,
2683 .getattr = btrfs_getattr,
2684 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002685 .setxattr = generic_setxattr,
2686 .getxattr = generic_getxattr,
2687 .listxattr = btrfs_listxattr,
2688 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002689};
2690
Josef Bacik618e21d2007-07-11 10:18:17 -04002691static struct inode_operations btrfs_special_inode_operations = {
2692 .getattr = btrfs_getattr,
2693 .setattr = btrfs_setattr,
2694};
2695
Chris Mason39279cc2007-06-12 06:35:45 -04002696static struct inode_operations btrfs_symlink_inode_operations = {
2697 .readlink = generic_readlink,
2698 .follow_link = page_follow_link_light,
2699 .put_link = page_put_link,
2700};