blob: 91f3fc43e2a98c2a89c1e36981e4f10c15019309 [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 Masondb945352007-10-15 16:15:53 -040081 u64 blocksize = root->sectorsize;
Chris Masonbe20aa92007-12-17 20:14:01 -050082 struct btrfs_key ins;
83 int ret;
Chris Masonb888db22007-08-27 16:49:44 -040084
Chris Masonb888db22007-08-27 16:49:44 -040085 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -040086 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -050087 btrfs_set_trans_block_group(trans, inode);
88
Chris Masondb945352007-10-15 16:15:53 -040089 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -050090 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -040091 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -040092 start, start + num_bytes, start, &alloc_hint);
Chris Masondb945352007-10-15 16:15:53 -040093
Chris Mason179e29e2007-11-01 11:28:41 -040094 if (alloc_hint == EXTENT_MAP_INLINE)
95 goto out;
96
Chris Mason7bb86312007-12-11 09:25:06 -050097 ret = btrfs_alloc_extent(trans, root, num_bytes,
98 root->root_key.objectid, trans->transid,
99 inode->i_ino, start, 0,
Chris Masonb888db22007-08-27 16:49:44 -0400100 alloc_hint, (u64)-1, &ins, 1);
101 if (ret) {
102 WARN_ON(1);
103 goto out;
104 }
105 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
106 start, ins.objectid, ins.offset,
107 ins.offset);
108out:
109 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500110 return ret;
111}
112
113static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
114{
115 u64 extent_start;
116 u64 extent_end;
117 u64 bytenr;
118 u64 cow_end;
119 struct btrfs_root *root = BTRFS_I(inode)->root;
120 struct extent_buffer *leaf;
121 int found_type;
122 struct btrfs_path *path;
123 struct btrfs_file_extent_item *item;
124 int ret;
125 int err;
126 struct btrfs_key found_key;
127
128 path = btrfs_alloc_path();
129 BUG_ON(!path);
130again:
131 ret = btrfs_lookup_file_extent(NULL, root, path,
132 inode->i_ino, start, 0);
133 if (ret < 0) {
134 btrfs_free_path(path);
135 return ret;
136 }
137
138 cow_end = end;
139 if (ret != 0) {
140 if (path->slots[0] == 0)
141 goto not_found;
142 path->slots[0]--;
143 }
144
145 leaf = path->nodes[0];
146 item = btrfs_item_ptr(leaf, path->slots[0],
147 struct btrfs_file_extent_item);
148
149 /* are we inside the extent that was found? */
150 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
151 found_type = btrfs_key_type(&found_key);
152 if (found_key.objectid != inode->i_ino ||
153 found_type != BTRFS_EXTENT_DATA_KEY) {
154 goto not_found;
155 }
156
157 found_type = btrfs_file_extent_type(leaf, item);
158 extent_start = found_key.offset;
159 if (found_type == BTRFS_FILE_EXTENT_REG) {
160 extent_end = extent_start +
161 btrfs_file_extent_num_bytes(leaf, item);
162 err = 0;
163
164 if (start < extent_start || start >= extent_end)
165 goto not_found;
166
167 cow_end = min(end, extent_end - 1);
168 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
169 if (bytenr == 0)
170 goto not_found;
171
172 bytenr += btrfs_file_extent_offset(leaf, item);
173 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
174 goto not_found;
175 }
176
177 start = extent_end;
178 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
179 goto not_found;
180 }
181loop:
182 if (start > end) {
183 btrfs_free_path(path);
184 return 0;
185 }
186 btrfs_release_path(root, path);
187 goto again;
188
189not_found:
190 cow_file_range(inode, start, cow_end);
191 start = cow_end + 1;
192 goto loop;
193}
194
195static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
196{
197 struct btrfs_root *root = BTRFS_I(inode)->root;
198 int ret;
199
200 mutex_lock(&root->fs_info->fs_mutex);
201 if (btrfs_test_opt(root, NODATACOW))
202 ret = run_delalloc_nocow(inode, start, end);
203 else
204 ret = cow_file_range(inode, start, end);
Chris Masonb888db22007-08-27 16:49:44 -0400205 mutex_unlock(&root->fs_info->fs_mutex);
206 return ret;
207}
208
Chris Mason07157aa2007-08-30 08:50:51 -0400209int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
210{
211 struct inode *inode = page->mapping->host;
212 struct btrfs_root *root = BTRFS_I(inode)->root;
213 struct btrfs_trans_handle *trans;
214 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500215 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400216 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400217 size_t offset = start - page_start;
218
Chris Masonb6cda9b2007-12-14 15:30:32 -0500219 if (btrfs_test_opt(root, NODATASUM))
220 return 0;
221
Chris Mason07157aa2007-08-30 08:50:51 -0400222 mutex_lock(&root->fs_info->fs_mutex);
223 trans = btrfs_start_transaction(root, 1);
224 btrfs_set_trans_block_group(trans, inode);
225 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400226 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400227 start, kaddr + offset, end - start + 1);
228 kunmap(page);
229 ret = btrfs_end_transaction(trans, root);
230 BUG_ON(ret);
231 mutex_unlock(&root->fs_info->fs_mutex);
232 return ret;
233}
234
235int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
236{
237 int ret = 0;
238 struct inode *inode = page->mapping->host;
239 struct btrfs_root *root = BTRFS_I(inode)->root;
240 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
241 struct btrfs_csum_item *item;
242 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400243 u32 csum;
Chris Mason07157aa2007-08-30 08:50:51 -0400244
Chris Masonb6cda9b2007-12-14 15:30:32 -0500245 if (btrfs_test_opt(root, NODATASUM))
246 return 0;
247
Chris Mason07157aa2007-08-30 08:50:51 -0400248 mutex_lock(&root->fs_info->fs_mutex);
249 path = btrfs_alloc_path();
250 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
251 if (IS_ERR(item)) {
252 ret = PTR_ERR(item);
253 /* a csum that isn't present is a preallocated region. */
254 if (ret == -ENOENT || ret == -EFBIG)
255 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400256 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400257 goto out;
258 }
Chris Masonff79f812007-10-15 16:22:25 -0400259 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
260 BTRFS_CRC32_SIZE);
261 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400262out:
263 if (path)
264 btrfs_free_path(path);
265 mutex_unlock(&root->fs_info->fs_mutex);
266 return ret;
267}
268
269int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
270{
Chris Mason35ebb932007-10-30 16:56:53 -0400271 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400272 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400273 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
274 char *kaddr;
275 u64 private;
276 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400277 struct btrfs_root *root = BTRFS_I(inode)->root;
278 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400279 unsigned long flags;
Chris Mason07157aa2007-08-30 08:50:51 -0400280
Chris Masonb6cda9b2007-12-14 15:30:32 -0500281 if (btrfs_test_opt(root, NODATASUM))
282 return 0;
283
Chris Mason07157aa2007-08-30 08:50:51 -0400284 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400285 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400286 kaddr = kmap_atomic(page, KM_IRQ0);
287 if (ret) {
288 goto zeroit;
289 }
Chris Masonff79f812007-10-15 16:22:25 -0400290 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
291 btrfs_csum_final(csum, (char *)&csum);
292 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400293 goto zeroit;
294 }
295 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400296 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400297 return 0;
298
299zeroit:
300 printk("btrfs csum failed ino %lu off %llu\n",
301 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400302 memset(kaddr + offset, 1, end - start + 1);
303 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400304 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400305 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400306 return 0;
307}
Chris Masonb888db22007-08-27 16:49:44 -0400308
Chris Mason39279cc2007-06-12 06:35:45 -0400309void btrfs_read_locked_inode(struct inode *inode)
310{
311 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400312 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400313 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400314 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400315 struct btrfs_root *root = BTRFS_I(inode)->root;
316 struct btrfs_key location;
317 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400318 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400319 int ret;
320
321 path = btrfs_alloc_path();
322 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400323 mutex_lock(&root->fs_info->fs_mutex);
324
325 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
326 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400327 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400328 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400329
Chris Mason5f39d392007-10-15 16:14:19 -0400330 leaf = path->nodes[0];
331 inode_item = btrfs_item_ptr(leaf, path->slots[0],
332 struct btrfs_inode_item);
333
334 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
335 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
336 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
337 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
338 inode->i_size = btrfs_inode_size(leaf, inode_item);
339
340 tspec = btrfs_inode_atime(inode_item);
341 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
342 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
343
344 tspec = btrfs_inode_mtime(inode_item);
345 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
346 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
347
348 tspec = btrfs_inode_ctime(inode_item);
349 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
350 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
351
352 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
353 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400354 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400355 rdev = btrfs_inode_rdev(leaf, inode_item);
356
357 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400358 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
359 alloc_group_block);
360
361 btrfs_free_path(path);
362 inode_item = NULL;
363
364 mutex_unlock(&root->fs_info->fs_mutex);
365
366 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400367 case S_IFREG:
368 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400369 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400370 inode->i_fop = &btrfs_file_operations;
371 inode->i_op = &btrfs_file_inode_operations;
372 break;
373 case S_IFDIR:
374 inode->i_fop = &btrfs_dir_file_operations;
375 if (root == root->fs_info->tree_root)
376 inode->i_op = &btrfs_dir_ro_inode_operations;
377 else
378 inode->i_op = &btrfs_dir_inode_operations;
379 break;
380 case S_IFLNK:
381 inode->i_op = &btrfs_symlink_inode_operations;
382 inode->i_mapping->a_ops = &btrfs_symlink_aops;
383 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400384 default:
385 init_special_inode(inode, inode->i_mode, rdev);
386 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400387 }
388 return;
389
390make_bad:
391 btrfs_release_path(root, path);
392 btrfs_free_path(path);
393 mutex_unlock(&root->fs_info->fs_mutex);
394 make_bad_inode(inode);
395}
396
Chris Mason5f39d392007-10-15 16:14:19 -0400397static void fill_inode_item(struct extent_buffer *leaf,
398 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400399 struct inode *inode)
400{
Chris Mason5f39d392007-10-15 16:14:19 -0400401 btrfs_set_inode_uid(leaf, item, inode->i_uid);
402 btrfs_set_inode_gid(leaf, item, inode->i_gid);
403 btrfs_set_inode_size(leaf, item, inode->i_size);
404 btrfs_set_inode_mode(leaf, item, inode->i_mode);
405 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
406
407 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
408 inode->i_atime.tv_sec);
409 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
410 inode->i_atime.tv_nsec);
411
412 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
413 inode->i_mtime.tv_sec);
414 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
415 inode->i_mtime.tv_nsec);
416
417 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
418 inode->i_ctime.tv_sec);
419 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
420 inode->i_ctime.tv_nsec);
421
422 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
423 btrfs_set_inode_generation(leaf, item, inode->i_generation);
424 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
425 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400426 BTRFS_I(inode)->block_group->key.objectid);
427}
428
Chris Masona52d9a82007-08-27 16:49:44 -0400429int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400430 struct btrfs_root *root,
431 struct inode *inode)
432{
433 struct btrfs_inode_item *inode_item;
434 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400435 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400436 int ret;
437
438 path = btrfs_alloc_path();
439 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400440 ret = btrfs_lookup_inode(trans, root, path,
441 &BTRFS_I(inode)->location, 1);
442 if (ret) {
443 if (ret > 0)
444 ret = -ENOENT;
445 goto failed;
446 }
447
Chris Mason5f39d392007-10-15 16:14:19 -0400448 leaf = path->nodes[0];
449 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400450 struct btrfs_inode_item);
451
Chris Mason5f39d392007-10-15 16:14:19 -0400452 fill_inode_item(leaf, inode_item, inode);
453 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400454 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400455 ret = 0;
456failed:
457 btrfs_release_path(root, path);
458 btrfs_free_path(path);
459 return ret;
460}
461
462
463static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
464 struct btrfs_root *root,
465 struct inode *dir,
466 struct dentry *dentry)
467{
468 struct btrfs_path *path;
469 const char *name = dentry->d_name.name;
470 int name_len = dentry->d_name.len;
471 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400472 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400473 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400474 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400475
476 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400477 if (!path) {
478 ret = -ENOMEM;
479 goto err;
480 }
481
Chris Mason39279cc2007-06-12 06:35:45 -0400482 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
483 name, name_len, -1);
484 if (IS_ERR(di)) {
485 ret = PTR_ERR(di);
486 goto err;
487 }
488 if (!di) {
489 ret = -ENOENT;
490 goto err;
491 }
Chris Mason5f39d392007-10-15 16:14:19 -0400492 leaf = path->nodes[0];
493 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400494 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400495 if (ret)
496 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400497 btrfs_release_path(root, path);
498
499 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400500 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400501 if (IS_ERR(di)) {
502 ret = PTR_ERR(di);
503 goto err;
504 }
505 if (!di) {
506 ret = -ENOENT;
507 goto err;
508 }
509 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400510
511 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500512 ret = btrfs_del_inode_ref(trans, root, name, name_len,
513 dentry->d_inode->i_ino,
514 dentry->d_parent->d_inode->i_ino);
515 if (ret) {
516 printk("failed to delete reference to %.*s, "
517 "inode %lu parent %lu\n", name_len, name,
518 dentry->d_inode->i_ino,
519 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500520 }
Chris Mason39279cc2007-06-12 06:35:45 -0400521err:
522 btrfs_free_path(path);
523 if (!ret) {
524 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400525 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400526 btrfs_update_inode(trans, root, dir);
527 drop_nlink(dentry->d_inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400528 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400529 dir->i_sb->s_dirt = 1;
530 }
531 return ret;
532}
533
534static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
535{
536 struct btrfs_root *root;
537 struct btrfs_trans_handle *trans;
538 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400539 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400540
541 root = BTRFS_I(dir)->root;
542 mutex_lock(&root->fs_info->fs_mutex);
543 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400544
Chris Mason39279cc2007-06-12 06:35:45 -0400545 btrfs_set_trans_block_group(trans, dir);
546 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400547 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400548
Chris Mason39279cc2007-06-12 06:35:45 -0400549 btrfs_end_transaction(trans, root);
550 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400551 btrfs_btree_balance_dirty(root, nr);
Chris Mason5f39d392007-10-15 16:14:19 -0400552
Chris Mason39279cc2007-06-12 06:35:45 -0400553 return ret;
554}
555
556static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
557{
558 struct inode *inode = dentry->d_inode;
559 int err;
560 int ret;
561 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400562 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400563 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400564
Yan134d4512007-10-25 15:49:25 -0400565 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
566 return -ENOTEMPTY;
567
Chris Mason39279cc2007-06-12 06:35:45 -0400568 mutex_lock(&root->fs_info->fs_mutex);
569 trans = btrfs_start_transaction(root, 1);
570 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400571
572 /* now the directory is empty */
573 err = btrfs_unlink_trans(trans, root, dir, dentry);
574 if (!err) {
575 inode->i_size = 0;
576 }
Chris Mason39544012007-12-12 14:38:19 -0500577
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400578 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400579 ret = btrfs_end_transaction(trans, root);
Yan134d4512007-10-25 15:49:25 -0400580 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400581 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -0500582
Chris Mason39279cc2007-06-12 06:35:45 -0400583 if (ret && !err)
584 err = ret;
585 return err;
586}
587
588static int btrfs_free_inode(struct btrfs_trans_handle *trans,
589 struct btrfs_root *root,
590 struct inode *inode)
591{
592 struct btrfs_path *path;
593 int ret;
594
595 clear_inode(inode);
596
597 path = btrfs_alloc_path();
598 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400599 ret = btrfs_lookup_inode(trans, root, path,
600 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400601 if (ret > 0)
602 ret = -ENOENT;
603 if (!ret)
604 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400605 btrfs_free_path(path);
606 return ret;
607}
608
609/*
Chris Mason39279cc2007-06-12 06:35:45 -0400610 * this can truncate away extent items, csum items and directory items.
611 * It starts at a high offset and removes keys until it can't find
612 * any higher than i_size.
613 *
614 * csum items that cross the new i_size are truncated to the new size
615 * as well.
616 */
617static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
618 struct btrfs_root *root,
619 struct inode *inode)
620{
621 int ret;
622 struct btrfs_path *path;
623 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400624 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400625 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400626 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400627 struct btrfs_file_extent_item *fi;
628 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400629 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400630 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500631 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500632 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400633 int found_extent;
634 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400635 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400636
Chris Masona52d9a82007-08-27 16:49:44 -0400637 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400638 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400639 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400640 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400641
Chris Mason39279cc2007-06-12 06:35:45 -0400642 /* FIXME, add redo link to tree so we don't leak on crash */
643 key.objectid = inode->i_ino;
644 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400645 key.type = (u8)-1;
646
Chris Mason39279cc2007-06-12 06:35:45 -0400647 while(1) {
648 btrfs_init_path(path);
649 fi = NULL;
650 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
651 if (ret < 0) {
652 goto error;
653 }
654 if (ret > 0) {
655 BUG_ON(path->slots[0] == 0);
656 path->slots[0]--;
657 }
Chris Mason5f39d392007-10-15 16:14:19 -0400658 leaf = path->nodes[0];
659 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
660 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400661
Chris Mason5f39d392007-10-15 16:14:19 -0400662 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400663 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400664
Chris Mason39279cc2007-06-12 06:35:45 -0400665 if (found_type != BTRFS_CSUM_ITEM_KEY &&
666 found_type != BTRFS_DIR_ITEM_KEY &&
667 found_type != BTRFS_DIR_INDEX_KEY &&
668 found_type != BTRFS_EXTENT_DATA_KEY)
669 break;
670
Chris Mason5f39d392007-10-15 16:14:19 -0400671 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400672 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400673 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400674 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400675 extent_type = btrfs_file_extent_type(leaf, fi);
676 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400677 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400678 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400679 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
680 struct btrfs_item *item = btrfs_item_nr(leaf,
681 path->slots[0]);
682 item_end += btrfs_file_extent_inline_len(leaf,
683 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400684 }
Yan008630c2007-11-07 13:31:09 -0500685 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400686 }
687 if (found_type == BTRFS_CSUM_ITEM_KEY) {
688 ret = btrfs_csum_truncate(trans, root, path,
689 inode->i_size);
690 BUG_ON(ret);
691 }
Yan008630c2007-11-07 13:31:09 -0500692 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400693 if (found_type == BTRFS_DIR_ITEM_KEY) {
694 found_type = BTRFS_INODE_ITEM_KEY;
695 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
696 found_type = BTRFS_CSUM_ITEM_KEY;
697 } else if (found_type) {
698 found_type--;
699 } else {
700 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400701 }
Yana61721d2007-09-17 11:08:38 -0400702 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400703 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400704 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400705 }
Chris Mason5f39d392007-10-15 16:14:19 -0400706 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400707 del_item = 1;
708 else
709 del_item = 0;
710 found_extent = 0;
711
712 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400713 if (found_type != BTRFS_EXTENT_DATA_KEY)
714 goto delete;
715
716 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400717 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400718 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400719 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400720 u64 orig_num_bytes =
721 btrfs_file_extent_num_bytes(leaf, fi);
722 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400723 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400724 btrfs_set_file_extent_num_bytes(leaf, fi,
725 extent_num_bytes);
726 num_dec = (orig_num_bytes -
727 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400728 if (extent_start != 0) {
729 inode->i_blocks -= num_dec;
730 }
Chris Mason5f39d392007-10-15 16:14:19 -0400731 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400732 } else {
Chris Masondb945352007-10-15 16:15:53 -0400733 extent_num_bytes =
734 btrfs_file_extent_disk_num_bytes(leaf,
735 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400736 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400737 num_dec = btrfs_file_extent_num_bytes(leaf,
738 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400739 if (extent_start != 0) {
740 found_extent = 1;
741 inode->i_blocks -= num_dec;
742 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500743 root_gen = btrfs_header_generation(leaf);
744 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400745 }
Chris Mason179e29e2007-11-01 11:28:41 -0400746 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
747 !del_item) {
748 u32 newsize = inode->i_size - found_key.offset;
749 newsize = btrfs_file_extent_calc_inline_size(newsize);
750 ret = btrfs_truncate_item(trans, root, path,
751 newsize, 1);
752 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400753 }
Chris Mason179e29e2007-11-01 11:28:41 -0400754delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400755 if (del_item) {
756 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400757 if (ret)
758 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400759 } else {
760 break;
761 }
762 btrfs_release_path(root, path);
763 if (found_extent) {
764 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500765 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500766 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500767 root_gen, inode->i_ino,
768 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400769 BUG_ON(ret);
770 }
771 }
772 ret = 0;
773error:
774 btrfs_release_path(root, path);
775 btrfs_free_path(path);
776 inode->i_sb->s_dirt = 1;
777 return ret;
778}
779
Chris Masonb888db22007-08-27 16:49:44 -0400780static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400781 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400782{
Chris Mason39279cc2007-06-12 06:35:45 -0400783 char *kaddr;
784 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400785 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400786 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400787 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400788
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400789 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400790
Chris Masonb888db22007-08-27 16:49:44 -0400791 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
792 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
793 page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400794 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400795 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400796 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
797 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400798 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400799 }
Chris Masonb888db22007-08-27 16:49:44 -0400800 set_page_dirty(page);
801 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400802
Chris Masona52d9a82007-08-27 16:49:44 -0400803 return ret;
804}
805
806/*
807 * taken from block_truncate_page, but does cow as it zeros out
808 * any bytes left in the last page in the file.
809 */
810static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
811{
812 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400813 struct btrfs_root *root = BTRFS_I(inode)->root;
814 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400815 pgoff_t index = from >> PAGE_CACHE_SHIFT;
816 unsigned offset = from & (PAGE_CACHE_SIZE-1);
817 struct page *page;
818 int ret = 0;
819 u64 page_start;
820
821 if ((offset & (blocksize - 1)) == 0)
822 goto out;
823
Chris Masondb945352007-10-15 16:15:53 -0400824 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400825 ret = -ENOMEM;
826 page = grab_cache_page(mapping, index);
827 if (!page)
828 goto out;
829 if (!PageUptodate(page)) {
830 ret = btrfs_readpage(NULL, page);
831 lock_page(page);
832 if (!PageUptodate(page)) {
833 ret = -EIO;
834 goto out;
835 }
836 }
Chris Mason35ebb932007-10-30 16:56:53 -0400837 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400838
Chris Masonb888db22007-08-27 16:49:44 -0400839 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400840
Chris Mason39279cc2007-06-12 06:35:45 -0400841 unlock_page(page);
842 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400843 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400844out:
845 return ret;
846}
847
848static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
849{
850 struct inode *inode = dentry->d_inode;
851 int err;
852
853 err = inode_change_ok(inode, attr);
854 if (err)
855 return err;
856
857 if (S_ISREG(inode->i_mode) &&
858 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
859 struct btrfs_trans_handle *trans;
860 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400861 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
862
Chris Mason5f39d392007-10-15 16:14:19 -0400863 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400864 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400865 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400866 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400867 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400868
869 if (attr->ia_size <= pos)
870 goto out;
871
872 btrfs_truncate_page(inode->i_mapping, inode->i_size);
873
Chris Mason2bf5a722007-08-30 11:54:02 -0400874 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400875 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400876
877 mutex_lock(&root->fs_info->fs_mutex);
878 trans = btrfs_start_transaction(root, 1);
879 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400880 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400881 pos, pos + hole_size, pos,
882 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400883
Chris Mason179e29e2007-11-01 11:28:41 -0400884 if (alloc_hint != EXTENT_MAP_INLINE) {
885 err = btrfs_insert_file_extent(trans, root,
886 inode->i_ino,
887 pos, 0, 0, hole_size);
888 }
Chris Mason39279cc2007-06-12 06:35:45 -0400889 btrfs_end_transaction(trans, root);
890 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400891 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400892 if (err)
893 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400894 }
895out:
896 err = inode_setattr(inode, attr);
897
898 return err;
899}
900void btrfs_delete_inode(struct inode *inode)
901{
902 struct btrfs_trans_handle *trans;
903 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400904 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400905 int ret;
906
907 truncate_inode_pages(&inode->i_data, 0);
908 if (is_bad_inode(inode)) {
909 goto no_delete;
910 }
Chris Mason5f39d392007-10-15 16:14:19 -0400911
Chris Mason39279cc2007-06-12 06:35:45 -0400912 inode->i_size = 0;
913 mutex_lock(&root->fs_info->fs_mutex);
914 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400915
Chris Mason39279cc2007-06-12 06:35:45 -0400916 btrfs_set_trans_block_group(trans, inode);
917 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400918 if (ret)
919 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -0500920 ret = btrfs_delete_xattrs(trans, root, inode);
921 if (ret)
922 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -0400923 ret = btrfs_free_inode(trans, root, inode);
924 if (ret)
925 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400926 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400927
Chris Mason39279cc2007-06-12 06:35:45 -0400928 btrfs_end_transaction(trans, root);
929 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400930 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400931 return;
Chris Mason54aa1f42007-06-22 14:16:25 -0400932
933no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400934 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400935 btrfs_end_transaction(trans, root);
936 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400937 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400938no_delete:
939 clear_inode(inode);
940}
941
942/*
943 * this returns the key found in the dir entry in the location pointer.
944 * If no dir entries were found, location->objectid is 0.
945 */
946static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
947 struct btrfs_key *location)
948{
949 const char *name = dentry->d_name.name;
950 int namelen = dentry->d_name.len;
951 struct btrfs_dir_item *di;
952 struct btrfs_path *path;
953 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -0400954 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400955
Chris Mason39544012007-12-12 14:38:19 -0500956 if (namelen == 1 && strcmp(name, ".") == 0) {
957 location->objectid = dir->i_ino;
958 location->type = BTRFS_INODE_ITEM_KEY;
959 location->offset = 0;
960 return 0;
961 }
Chris Mason39279cc2007-06-12 06:35:45 -0400962 path = btrfs_alloc_path();
963 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -0500964
Chris Mason7a720532007-12-13 09:06:59 -0500965 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -0500966 struct btrfs_key key;
967 struct extent_buffer *leaf;
968 u32 nritems;
969 int slot;
970
971 key.objectid = dir->i_ino;
972 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
973 key.offset = 0;
974 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
975 BUG_ON(ret == 0);
976 ret = 0;
977
978 leaf = path->nodes[0];
979 slot = path->slots[0];
980 nritems = btrfs_header_nritems(leaf);
981 if (slot >= nritems)
982 goto out_err;
983
984 btrfs_item_key_to_cpu(leaf, &key, slot);
985 if (key.objectid != dir->i_ino ||
986 key.type != BTRFS_INODE_REF_KEY) {
987 goto out_err;
988 }
989 location->objectid = key.offset;
990 location->type = BTRFS_INODE_ITEM_KEY;
991 location->offset = 0;
992 goto out;
993 }
994
Chris Mason39279cc2007-06-12 06:35:45 -0400995 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
996 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -0400997 if (IS_ERR(di))
998 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -0400999 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001000 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001001 }
Chris Mason5f39d392007-10-15 16:14:19 -04001002 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001003out:
Chris Mason39279cc2007-06-12 06:35:45 -04001004 btrfs_free_path(path);
1005 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001006out_err:
1007 location->objectid = 0;
1008 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001009}
1010
1011/*
1012 * when we hit a tree root in a directory, the btrfs part of the inode
1013 * needs to be changed to reflect the root directory of the tree root. This
1014 * is kind of like crossing a mount point.
1015 */
1016static int fixup_tree_root_location(struct btrfs_root *root,
1017 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001018 struct btrfs_root **sub_root,
1019 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001020{
1021 struct btrfs_path *path;
1022 struct btrfs_root_item *ri;
1023
1024 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1025 return 0;
1026 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1027 return 0;
1028
1029 path = btrfs_alloc_path();
1030 BUG_ON(!path);
1031 mutex_lock(&root->fs_info->fs_mutex);
1032
Josef Bacik58176a92007-08-29 15:47:34 -04001033 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1034 dentry->d_name.name,
1035 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001036 if (IS_ERR(*sub_root))
1037 return PTR_ERR(*sub_root);
1038
1039 ri = &(*sub_root)->root_item;
1040 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001041 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1042 location->offset = 0;
1043
1044 btrfs_free_path(path);
1045 mutex_unlock(&root->fs_info->fs_mutex);
1046 return 0;
1047}
1048
1049static int btrfs_init_locked_inode(struct inode *inode, void *p)
1050{
1051 struct btrfs_iget_args *args = p;
1052 inode->i_ino = args->ino;
1053 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -04001054 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1055 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001056 return 0;
1057}
1058
1059static int btrfs_find_actor(struct inode *inode, void *opaque)
1060{
1061 struct btrfs_iget_args *args = opaque;
1062 return (args->ino == inode->i_ino &&
1063 args->root == BTRFS_I(inode)->root);
1064}
1065
1066struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1067 struct btrfs_root *root)
1068{
1069 struct inode *inode;
1070 struct btrfs_iget_args args;
1071 args.ino = objectid;
1072 args.root = root;
1073
1074 inode = iget5_locked(s, objectid, btrfs_find_actor,
1075 btrfs_init_locked_inode,
1076 (void *)&args);
1077 return inode;
1078}
1079
1080static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1081 struct nameidata *nd)
1082{
1083 struct inode * inode;
1084 struct btrfs_inode *bi = BTRFS_I(dir);
1085 struct btrfs_root *root = bi->root;
1086 struct btrfs_root *sub_root = root;
1087 struct btrfs_key location;
1088 int ret;
1089
1090 if (dentry->d_name.len > BTRFS_NAME_LEN)
1091 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001092
Chris Mason39279cc2007-06-12 06:35:45 -04001093 mutex_lock(&root->fs_info->fs_mutex);
1094 ret = btrfs_inode_by_name(dir, dentry, &location);
1095 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001096
Chris Mason39279cc2007-06-12 06:35:45 -04001097 if (ret < 0)
1098 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001099
Chris Mason39279cc2007-06-12 06:35:45 -04001100 inode = NULL;
1101 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001102 ret = fixup_tree_root_location(root, &location, &sub_root,
1103 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001104 if (ret < 0)
1105 return ERR_PTR(ret);
1106 if (ret > 0)
1107 return ERR_PTR(-ENOENT);
1108 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1109 sub_root);
1110 if (!inode)
1111 return ERR_PTR(-EACCES);
1112 if (inode->i_state & I_NEW) {
1113 /* the inode and parent dir are two different roots */
1114 if (sub_root != root) {
1115 igrab(inode);
1116 sub_root->inode = inode;
1117 }
1118 BTRFS_I(inode)->root = sub_root;
1119 memcpy(&BTRFS_I(inode)->location, &location,
1120 sizeof(location));
1121 btrfs_read_locked_inode(inode);
1122 unlock_new_inode(inode);
1123 }
1124 }
1125 return d_splice_alias(inode, dentry);
1126}
1127
Chris Mason39279cc2007-06-12 06:35:45 -04001128static unsigned char btrfs_filetype_table[] = {
1129 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1130};
1131
1132static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1133{
1134 struct inode *inode = filp->f_path.dentry->d_inode;
1135 struct btrfs_root *root = BTRFS_I(inode)->root;
1136 struct btrfs_item *item;
1137 struct btrfs_dir_item *di;
1138 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001139 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001140 struct btrfs_path *path;
1141 int ret;
1142 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001143 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001144 int slot;
1145 int advance;
1146 unsigned char d_type;
1147 int over = 0;
1148 u32 di_cur;
1149 u32 di_total;
1150 u32 di_len;
1151 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001152 char tmp_name[32];
1153 char *name_ptr;
1154 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001155
1156 /* FIXME, use a real flag for deciding about the key type */
1157 if (root->fs_info->tree_root == root)
1158 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001159
Chris Mason39544012007-12-12 14:38:19 -05001160 /* special case for "." */
1161 if (filp->f_pos == 0) {
1162 over = filldir(dirent, ".", 1,
1163 1, inode->i_ino,
1164 DT_DIR);
1165 if (over)
1166 return 0;
1167 filp->f_pos = 1;
1168 }
1169
Chris Mason39279cc2007-06-12 06:35:45 -04001170 mutex_lock(&root->fs_info->fs_mutex);
1171 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001172 path = btrfs_alloc_path();
1173 path->reada = 2;
1174
1175 /* special case for .., just use the back ref */
1176 if (filp->f_pos == 1) {
1177 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1178 key.offset = 0;
1179 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1180 BUG_ON(ret == 0);
1181 leaf = path->nodes[0];
1182 slot = path->slots[0];
1183 nritems = btrfs_header_nritems(leaf);
1184 if (slot >= nritems) {
1185 btrfs_release_path(root, path);
1186 goto read_dir_items;
1187 }
1188 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1189 btrfs_release_path(root, path);
1190 if (found_key.objectid != key.objectid ||
1191 found_key.type != BTRFS_INODE_REF_KEY)
1192 goto read_dir_items;
1193 over = filldir(dirent, "..", 2,
1194 2, found_key.offset, DT_DIR);
1195 if (over)
1196 goto nopos;
1197 filp->f_pos = 2;
1198 }
1199
1200read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001201 btrfs_set_key_type(&key, key_type);
1202 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001203
Chris Mason39279cc2007-06-12 06:35:45 -04001204 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1205 if (ret < 0)
1206 goto err;
1207 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001208 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001209 leaf = path->nodes[0];
1210 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001211 slot = path->slots[0];
1212 if (advance || slot >= nritems) {
1213 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001214 ret = btrfs_next_leaf(root, path);
1215 if (ret)
1216 break;
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 } else {
1221 slot++;
1222 path->slots[0]++;
1223 }
1224 }
1225 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001226 item = btrfs_item_nr(leaf, slot);
1227 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1228
1229 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001230 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001231 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001232 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001233 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001234 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001235
1236 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001237 advance = 1;
1238 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1239 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001240 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001241 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001242 struct btrfs_key location;
1243
1244 name_len = btrfs_dir_name_len(leaf, di);
1245 if (name_len < 32) {
1246 name_ptr = tmp_name;
1247 } else {
1248 name_ptr = kmalloc(name_len, GFP_NOFS);
1249 BUG_ON(!name_ptr);
1250 }
1251 read_extent_buffer(leaf, name_ptr,
1252 (unsigned long)(di + 1), name_len);
1253
1254 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1255 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1256
1257 over = filldir(dirent, name_ptr, name_len,
1258 found_key.offset,
1259 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001260 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001261
1262 if (name_ptr != tmp_name)
1263 kfree(name_ptr);
1264
Chris Mason39279cc2007-06-12 06:35:45 -04001265 if (over)
1266 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001267 di_len = btrfs_dir_name_len(leaf, di) +
1268 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001269 di_cur += di_len;
1270 di = (struct btrfs_dir_item *)((char *)di + di_len);
1271 }
1272 }
1273 filp->f_pos++;
1274nopos:
1275 ret = 0;
1276err:
1277 btrfs_release_path(root, path);
1278 btrfs_free_path(path);
1279 mutex_unlock(&root->fs_info->fs_mutex);
1280 return ret;
1281}
1282
1283int btrfs_write_inode(struct inode *inode, int wait)
1284{
1285 struct btrfs_root *root = BTRFS_I(inode)->root;
1286 struct btrfs_trans_handle *trans;
1287 int ret = 0;
1288
1289 if (wait) {
1290 mutex_lock(&root->fs_info->fs_mutex);
1291 trans = btrfs_start_transaction(root, 1);
1292 btrfs_set_trans_block_group(trans, inode);
1293 ret = btrfs_commit_transaction(trans, root);
1294 mutex_unlock(&root->fs_info->fs_mutex);
1295 }
1296 return ret;
1297}
1298
1299/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001300 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001301 * inode changes. But, it is most likely to find the inode in cache.
1302 * FIXME, needs more benchmarking...there are no reasons other than performance
1303 * to keep or drop this code.
1304 */
1305void btrfs_dirty_inode(struct inode *inode)
1306{
1307 struct btrfs_root *root = BTRFS_I(inode)->root;
1308 struct btrfs_trans_handle *trans;
1309
1310 mutex_lock(&root->fs_info->fs_mutex);
1311 trans = btrfs_start_transaction(root, 1);
1312 btrfs_set_trans_block_group(trans, inode);
1313 btrfs_update_inode(trans, root, inode);
1314 btrfs_end_transaction(trans, root);
1315 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001316}
1317
1318static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1319 struct btrfs_root *root,
1320 u64 objectid,
1321 struct btrfs_block_group_cache *group,
1322 int mode)
1323{
1324 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001325 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001326 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001327 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001328 int ret;
1329 int owner;
1330
Chris Mason5f39d392007-10-15 16:14:19 -04001331 path = btrfs_alloc_path();
1332 BUG_ON(!path);
1333
Chris Mason39279cc2007-06-12 06:35:45 -04001334 inode = new_inode(root->fs_info->sb);
1335 if (!inode)
1336 return ERR_PTR(-ENOMEM);
1337
Chris Masonb888db22007-08-27 16:49:44 -04001338 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1339 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001340 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001341
Chris Mason39279cc2007-06-12 06:35:45 -04001342 if (mode & S_IFDIR)
1343 owner = 0;
1344 else
1345 owner = 1;
1346 group = btrfs_find_block_group(root, group, 0, 0, owner);
1347 BTRFS_I(inode)->block_group = group;
1348
Chris Mason5f39d392007-10-15 16:14:19 -04001349 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1350 if (ret)
1351 goto fail;
1352
Chris Mason39279cc2007-06-12 06:35:45 -04001353 inode->i_uid = current->fsuid;
1354 inode->i_gid = current->fsgid;
1355 inode->i_mode = mode;
1356 inode->i_ino = objectid;
1357 inode->i_blocks = 0;
1358 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001359 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1360 struct btrfs_inode_item);
1361 fill_inode_item(path->nodes[0], inode_item, inode);
1362 btrfs_mark_buffer_dirty(path->nodes[0]);
1363 btrfs_free_path(path);
1364
Chris Mason39279cc2007-06-12 06:35:45 -04001365 location = &BTRFS_I(inode)->location;
1366 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001367 location->offset = 0;
1368 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1369
Chris Mason39279cc2007-06-12 06:35:45 -04001370 insert_inode_hash(inode);
1371 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001372fail:
1373 btrfs_free_path(path);
1374 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001375}
1376
1377static inline u8 btrfs_inode_type(struct inode *inode)
1378{
1379 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1380}
1381
1382static int btrfs_add_link(struct btrfs_trans_handle *trans,
1383 struct dentry *dentry, struct inode *inode)
1384{
1385 int ret;
1386 struct btrfs_key key;
1387 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001388 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001389
Chris Mason39279cc2007-06-12 06:35:45 -04001390 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001391 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1392 key.offset = 0;
1393
1394 ret = btrfs_insert_dir_item(trans, root,
1395 dentry->d_name.name, dentry->d_name.len,
1396 dentry->d_parent->d_inode->i_ino,
1397 &key, btrfs_inode_type(inode));
1398 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001399 ret = btrfs_insert_inode_ref(trans, root,
1400 dentry->d_name.name,
1401 dentry->d_name.len,
1402 inode->i_ino,
1403 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001404 parent_inode = dentry->d_parent->d_inode;
1405 parent_inode->i_size += dentry->d_name.len * 2;
1406 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001407 ret = btrfs_update_inode(trans, root,
1408 dentry->d_parent->d_inode);
1409 }
1410 return ret;
1411}
1412
1413static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1414 struct dentry *dentry, struct inode *inode)
1415{
1416 int err = btrfs_add_link(trans, dentry, inode);
1417 if (!err) {
1418 d_instantiate(dentry, inode);
1419 return 0;
1420 }
1421 if (err > 0)
1422 err = -EEXIST;
1423 return err;
1424}
1425
Josef Bacik618e21d2007-07-11 10:18:17 -04001426static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1427 int mode, dev_t rdev)
1428{
1429 struct btrfs_trans_handle *trans;
1430 struct btrfs_root *root = BTRFS_I(dir)->root;
1431 struct inode *inode;
1432 int err;
1433 int drop_inode = 0;
1434 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001435 unsigned long nr;
Josef Bacik618e21d2007-07-11 10:18:17 -04001436
1437 if (!new_valid_dev(rdev))
1438 return -EINVAL;
1439
1440 mutex_lock(&root->fs_info->fs_mutex);
1441 trans = btrfs_start_transaction(root, 1);
1442 btrfs_set_trans_block_group(trans, dir);
1443
1444 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1445 if (err) {
1446 err = -ENOSPC;
1447 goto out_unlock;
1448 }
1449
1450 inode = btrfs_new_inode(trans, root, objectid,
1451 BTRFS_I(dir)->block_group, mode);
1452 err = PTR_ERR(inode);
1453 if (IS_ERR(inode))
1454 goto out_unlock;
1455
1456 btrfs_set_trans_block_group(trans, inode);
1457 err = btrfs_add_nondir(trans, dentry, inode);
1458 if (err)
1459 drop_inode = 1;
1460 else {
1461 inode->i_op = &btrfs_special_inode_operations;
1462 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001463 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001464 }
1465 dir->i_sb->s_dirt = 1;
1466 btrfs_update_inode_block_group(trans, inode);
1467 btrfs_update_inode_block_group(trans, dir);
1468out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001469 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001470 btrfs_end_transaction(trans, root);
1471 mutex_unlock(&root->fs_info->fs_mutex);
1472
1473 if (drop_inode) {
1474 inode_dec_link_count(inode);
1475 iput(inode);
1476 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001477 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001478 return err;
1479}
1480
Chris Mason39279cc2007-06-12 06:35:45 -04001481static int btrfs_create(struct inode *dir, struct dentry *dentry,
1482 int mode, struct nameidata *nd)
1483{
1484 struct btrfs_trans_handle *trans;
1485 struct btrfs_root *root = BTRFS_I(dir)->root;
1486 struct inode *inode;
1487 int err;
1488 int drop_inode = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001489 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001490 u64 objectid;
1491
1492 mutex_lock(&root->fs_info->fs_mutex);
1493 trans = btrfs_start_transaction(root, 1);
1494 btrfs_set_trans_block_group(trans, dir);
1495
1496 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1497 if (err) {
1498 err = -ENOSPC;
1499 goto out_unlock;
1500 }
1501
1502 inode = btrfs_new_inode(trans, root, objectid,
1503 BTRFS_I(dir)->block_group, mode);
1504 err = PTR_ERR(inode);
1505 if (IS_ERR(inode))
1506 goto out_unlock;
1507
1508 btrfs_set_trans_block_group(trans, inode);
1509 err = btrfs_add_nondir(trans, dentry, inode);
1510 if (err)
1511 drop_inode = 1;
1512 else {
1513 inode->i_mapping->a_ops = &btrfs_aops;
1514 inode->i_fop = &btrfs_file_operations;
1515 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001516 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1517 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001518 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001519 }
1520 dir->i_sb->s_dirt = 1;
1521 btrfs_update_inode_block_group(trans, inode);
1522 btrfs_update_inode_block_group(trans, dir);
1523out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001524 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001525 btrfs_end_transaction(trans, root);
1526 mutex_unlock(&root->fs_info->fs_mutex);
1527
1528 if (drop_inode) {
1529 inode_dec_link_count(inode);
1530 iput(inode);
1531 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001532 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001533 return err;
1534}
1535
1536static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1537 struct dentry *dentry)
1538{
1539 struct btrfs_trans_handle *trans;
1540 struct btrfs_root *root = BTRFS_I(dir)->root;
1541 struct inode *inode = old_dentry->d_inode;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001542 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001543 int err;
1544 int drop_inode = 0;
1545
1546 if (inode->i_nlink == 0)
1547 return -ENOENT;
1548
1549 inc_nlink(inode);
1550 mutex_lock(&root->fs_info->fs_mutex);
1551 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001552
Chris Mason39279cc2007-06-12 06:35:45 -04001553 btrfs_set_trans_block_group(trans, dir);
1554 atomic_inc(&inode->i_count);
1555 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001556
Chris Mason39279cc2007-06-12 06:35:45 -04001557 if (err)
1558 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001559
Chris Mason39279cc2007-06-12 06:35:45 -04001560 dir->i_sb->s_dirt = 1;
1561 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001562 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001563
Chris Mason54aa1f42007-06-22 14:16:25 -04001564 if (err)
1565 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001566
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001567 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001568 btrfs_end_transaction(trans, root);
1569 mutex_unlock(&root->fs_info->fs_mutex);
1570
1571 if (drop_inode) {
1572 inode_dec_link_count(inode);
1573 iput(inode);
1574 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001575 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001576 return err;
1577}
1578
Chris Mason39279cc2007-06-12 06:35:45 -04001579static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1580{
1581 struct inode *inode;
1582 struct btrfs_trans_handle *trans;
1583 struct btrfs_root *root = BTRFS_I(dir)->root;
1584 int err = 0;
1585 int drop_on_err = 0;
1586 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001587 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001588
1589 mutex_lock(&root->fs_info->fs_mutex);
1590 trans = btrfs_start_transaction(root, 1);
1591 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001592
Chris Mason39279cc2007-06-12 06:35:45 -04001593 if (IS_ERR(trans)) {
1594 err = PTR_ERR(trans);
1595 goto out_unlock;
1596 }
1597
1598 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1599 if (err) {
1600 err = -ENOSPC;
1601 goto out_unlock;
1602 }
1603
1604 inode = btrfs_new_inode(trans, root, objectid,
1605 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1606 if (IS_ERR(inode)) {
1607 err = PTR_ERR(inode);
1608 goto out_fail;
1609 }
Chris Mason5f39d392007-10-15 16:14:19 -04001610
Chris Mason39279cc2007-06-12 06:35:45 -04001611 drop_on_err = 1;
1612 inode->i_op = &btrfs_dir_inode_operations;
1613 inode->i_fop = &btrfs_dir_file_operations;
1614 btrfs_set_trans_block_group(trans, inode);
1615
Chris Mason39544012007-12-12 14:38:19 -05001616 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001617 err = btrfs_update_inode(trans, root, inode);
1618 if (err)
1619 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001620
Chris Mason39279cc2007-06-12 06:35:45 -04001621 err = btrfs_add_link(trans, dentry, inode);
1622 if (err)
1623 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001624
Chris Mason39279cc2007-06-12 06:35:45 -04001625 d_instantiate(dentry, inode);
1626 drop_on_err = 0;
1627 dir->i_sb->s_dirt = 1;
1628 btrfs_update_inode_block_group(trans, inode);
1629 btrfs_update_inode_block_group(trans, dir);
1630
1631out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001632 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001633 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001634
Chris Mason39279cc2007-06-12 06:35:45 -04001635out_unlock:
1636 mutex_unlock(&root->fs_info->fs_mutex);
1637 if (drop_on_err)
1638 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001639 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001640 return err;
1641}
1642
Chris Masona52d9a82007-08-27 16:49:44 -04001643struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1644 size_t page_offset, u64 start, u64 end,
1645 int create)
1646{
1647 int ret;
1648 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001649 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001650 u64 extent_start = 0;
1651 u64 extent_end = 0;
1652 u64 objectid = inode->i_ino;
1653 u32 found_type;
1654 int failed_insert = 0;
1655 struct btrfs_path *path;
1656 struct btrfs_root *root = BTRFS_I(inode)->root;
1657 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001658 struct extent_buffer *leaf;
1659 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001660 struct extent_map *em = NULL;
1661 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1662 struct btrfs_trans_handle *trans = NULL;
1663
1664 path = btrfs_alloc_path();
1665 BUG_ON(!path);
1666 mutex_lock(&root->fs_info->fs_mutex);
1667
1668again:
1669 em = lookup_extent_mapping(em_tree, start, end);
1670 if (em) {
1671 goto out;
1672 }
1673 if (!em) {
1674 em = alloc_extent_map(GFP_NOFS);
1675 if (!em) {
1676 err = -ENOMEM;
1677 goto out;
1678 }
Chris Mason5f39d392007-10-15 16:14:19 -04001679 em->start = EXTENT_MAP_HOLE;
1680 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001681 }
1682 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001683 ret = btrfs_lookup_file_extent(trans, root, path,
1684 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001685 if (ret < 0) {
1686 err = ret;
1687 goto out;
1688 }
1689
1690 if (ret != 0) {
1691 if (path->slots[0] == 0)
1692 goto not_found;
1693 path->slots[0]--;
1694 }
1695
Chris Mason5f39d392007-10-15 16:14:19 -04001696 leaf = path->nodes[0];
1697 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001698 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001699 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001700 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1701 found_type = btrfs_key_type(&found_key);
1702 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001703 found_type != BTRFS_EXTENT_DATA_KEY) {
1704 goto not_found;
1705 }
1706
Chris Mason5f39d392007-10-15 16:14:19 -04001707 found_type = btrfs_file_extent_type(leaf, item);
1708 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001709 if (found_type == BTRFS_FILE_EXTENT_REG) {
1710 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001711 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001712 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001713 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001714 em->start = start;
1715 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001716 if (end < extent_start)
1717 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001718 em->end = extent_end - 1;
1719 } else {
1720 em->end = end;
1721 }
1722 goto not_found_em;
1723 }
Chris Masondb945352007-10-15 16:15:53 -04001724 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1725 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001726 em->start = extent_start;
1727 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001728 em->block_start = EXTENT_MAP_HOLE;
1729 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001730 goto insert;
1731 }
Chris Masondb945352007-10-15 16:15:53 -04001732 bytenr += btrfs_file_extent_offset(leaf, item);
1733 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001734 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001735 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001736 em->start = extent_start;
1737 em->end = extent_end - 1;
1738 goto insert;
1739 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001740 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001741 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001742 size_t size;
1743 size_t extent_offset;
1744 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001745
Chris Mason5f39d392007-10-15 16:14:19 -04001746 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1747 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001748 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001749 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001750 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001751 em->start = start;
1752 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001753 if (end < extent_start)
1754 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001755 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001756 } else {
1757 em->end = end;
1758 }
1759 goto not_found_em;
1760 }
1761 em->block_start = EXTENT_MAP_INLINE;
1762 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001763
1764 if (!page) {
1765 em->start = extent_start;
1766 em->end = extent_start + size - 1;
1767 goto out;
1768 }
1769
Chris Mason35ebb932007-10-30 16:56:53 -04001770 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001771 extent_start + page_offset;
1772 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1773 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001774 em->start = extent_start + extent_offset;
1775 em->end = (em->start + copy_size -1) |
1776 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001777 map = kmap(page);
1778 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001779 if (create == 0 && !PageUptodate(page)) {
1780 read_extent_buffer(leaf, map + page_offset, ptr,
1781 copy_size);
1782 flush_dcache_page(page);
1783 } else if (create && PageUptodate(page)) {
1784 if (!trans) {
1785 kunmap(page);
1786 free_extent_map(em);
1787 em = NULL;
1788 btrfs_release_path(root, path);
1789 trans = btrfs_start_transaction(root, 1);
1790 goto again;
1791 }
1792 write_extent_buffer(leaf, map + page_offset, ptr,
1793 copy_size);
1794 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001795 }
Chris Masona52d9a82007-08-27 16:49:44 -04001796 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001797 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001798 goto insert;
1799 } else {
1800 printk("unkknown found_type %d\n", found_type);
1801 WARN_ON(1);
1802 }
1803not_found:
1804 em->start = start;
1805 em->end = end;
1806not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001807 em->block_start = EXTENT_MAP_HOLE;
1808 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001809insert:
1810 btrfs_release_path(root, path);
1811 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001812 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001813 err = -EIO;
1814 goto out;
1815 }
1816 ret = add_extent_mapping(em_tree, em);
1817 if (ret == -EEXIST) {
1818 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001819 em = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04001820 failed_insert++;
1821 if (failed_insert > 5) {
1822 printk("failing to insert %Lu %Lu\n", start, end);
1823 err = -EIO;
1824 goto out;
1825 }
Chris Masona52d9a82007-08-27 16:49:44 -04001826 goto again;
1827 }
1828 err = 0;
1829out:
1830 btrfs_free_path(path);
1831 if (trans) {
1832 ret = btrfs_end_transaction(trans, root);
1833 if (!err)
1834 err = ret;
1835 }
1836 mutex_unlock(&root->fs_info->fs_mutex);
1837 if (err) {
1838 free_extent_map(em);
1839 WARN_ON(1);
1840 return ERR_PTR(err);
1841 }
1842 return em;
1843}
1844
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001845static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001846{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001847 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001848}
1849
1850static int btrfs_prepare_write(struct file *file, struct page *page,
1851 unsigned from, unsigned to)
1852{
Chris Masona52d9a82007-08-27 16:49:44 -04001853 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1854 page->mapping->host, page, from, to,
1855 btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001856}
1857
Chris Mason9ebefb182007-06-15 13:50:00 -04001858int btrfs_readpage(struct file *file, struct page *page)
1859{
Chris Masona52d9a82007-08-27 16:49:44 -04001860 struct extent_map_tree *tree;
1861 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1862 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001863}
Chris Mason39279cc2007-06-12 06:35:45 -04001864static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1865{
Chris Masona52d9a82007-08-27 16:49:44 -04001866 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001867
1868
1869 if (current->flags & PF_MEMALLOC) {
1870 redirty_page_for_writepage(wbc, page);
1871 unlock_page(page);
1872 return 0;
1873 }
Chris Masona52d9a82007-08-27 16:49:44 -04001874 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1875 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1876}
Chris Mason39279cc2007-06-12 06:35:45 -04001877
Chris Masonb293f02e2007-11-01 19:45:34 -04001878static int btrfs_writepages(struct address_space *mapping,
1879 struct writeback_control *wbc)
1880{
1881 struct extent_map_tree *tree;
1882 tree = &BTRFS_I(mapping->host)->extent_tree;
1883 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1884}
1885
Chris Mason3ab2fb52007-11-08 10:59:22 -05001886static int
1887btrfs_readpages(struct file *file, struct address_space *mapping,
1888 struct list_head *pages, unsigned nr_pages)
1889{
1890 struct extent_map_tree *tree;
1891 tree = &BTRFS_I(mapping->host)->extent_tree;
1892 return extent_readpages(tree, mapping, pages, nr_pages,
1893 btrfs_get_extent);
1894}
1895
Chris Masona52d9a82007-08-27 16:49:44 -04001896static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1897{
1898 struct extent_map_tree *tree;
1899 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001900
Chris Masona52d9a82007-08-27 16:49:44 -04001901 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1902 ret = try_release_extent_mapping(tree, page);
1903 if (ret == 1) {
1904 ClearPagePrivate(page);
1905 set_page_private(page, 0);
1906 page_cache_release(page);
1907 }
1908 return ret;
1909}
Chris Mason39279cc2007-06-12 06:35:45 -04001910
Chris Masona52d9a82007-08-27 16:49:44 -04001911static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1912{
1913 struct extent_map_tree *tree;
1914
1915 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1916 extent_invalidatepage(tree, page, offset);
1917 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001918}
1919
Chris Mason9ebefb182007-06-15 13:50:00 -04001920/*
1921 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1922 * called from a page fault handler when a page is first dirtied. Hence we must
1923 * be careful to check for EOF conditions here. We set the page up correctly
1924 * for a written page which means we get ENOSPC checking when writing into
1925 * holes and correct delalloc and unwritten extent mapping on filesystems that
1926 * support these features.
1927 *
1928 * We are not allowed to take the i_mutex here so we have to play games to
1929 * protect against truncate races as the page could now be beyond EOF. Because
1930 * vmtruncate() writes the inode size before removing pages, once we have the
1931 * page lock we can determine safely if the page is beyond EOF. If it is not
1932 * beyond EOF, then the page is guaranteed safe against truncation until we
1933 * unlock the page.
1934 */
1935int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1936{
1937 struct inode *inode = vma->vm_file->f_path.dentry->d_inode;
1938 unsigned long end;
1939 loff_t size;
1940 int ret = -EINVAL;
Chris Masona52d9a82007-08-27 16:49:44 -04001941 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04001942
Chris Mason011410b2007-09-10 19:58:36 -04001943 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001944 lock_page(page);
1945 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04001946 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04001947 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001948
Chris Mason9ebefb182007-06-15 13:50:00 -04001949 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04001950 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04001951 /* page got truncated out from underneath us */
1952 goto out_unlock;
1953 }
1954
1955 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04001956 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04001957 end = size & ~PAGE_CACHE_MASK;
1958 else
1959 end = PAGE_CACHE_SIZE;
1960
Chris Masonb888db22007-08-27 16:49:44 -04001961 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04001962
1963out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04001964 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001965 unlock_page(page);
1966 return ret;
1967}
1968
Chris Mason39279cc2007-06-12 06:35:45 -04001969static void btrfs_truncate(struct inode *inode)
1970{
1971 struct btrfs_root *root = BTRFS_I(inode)->root;
1972 int ret;
1973 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001974 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001975
1976 if (!S_ISREG(inode->i_mode))
1977 return;
1978 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1979 return;
1980
1981 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1982
1983 mutex_lock(&root->fs_info->fs_mutex);
1984 trans = btrfs_start_transaction(root, 1);
1985 btrfs_set_trans_block_group(trans, inode);
1986
1987 /* FIXME, add redo link to tree so we don't leak on crash */
1988 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001989 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001990 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001991
Chris Mason39279cc2007-06-12 06:35:45 -04001992 ret = btrfs_end_transaction(trans, root);
1993 BUG_ON(ret);
1994 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001995 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001996}
1997
1998int btrfs_commit_write(struct file *file, struct page *page,
1999 unsigned from, unsigned to)
2000{
Chris Masone9906a92007-12-14 12:56:58 -05002001 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2002 struct inode *inode = page->mapping->host;
2003
2004 btrfs_cow_one_page(inode, page, PAGE_CACHE_SIZE);
2005
Chris Masone9906a92007-12-14 12:56:58 -05002006 if (pos > inode->i_size) {
2007 i_size_write(inode, pos);
2008 mark_inode_dirty(inode);
2009 }
2010 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002011}
2012
2013static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2014{
2015 struct btrfs_trans_handle *trans;
2016 struct btrfs_key key;
2017 struct btrfs_root_item root_item;
2018 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002019 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002020 struct btrfs_root *new_root;
2021 struct inode *inode;
2022 struct inode *dir;
2023 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002024 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002025 u64 objectid;
2026 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002027 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002028
2029 mutex_lock(&root->fs_info->fs_mutex);
2030 trans = btrfs_start_transaction(root, 1);
2031 BUG_ON(!trans);
2032
Chris Mason7bb86312007-12-11 09:25:06 -05002033 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2034 0, &objectid);
2035 if (ret)
2036 goto fail;
2037
2038 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2039 objectid, trans->transid, 0, 0,
2040 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002041 if (IS_ERR(leaf))
2042 return PTR_ERR(leaf);
2043
2044 btrfs_set_header_nritems(leaf, 0);
2045 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002046 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002047 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002048 btrfs_set_header_owner(leaf, objectid);
2049
Chris Mason5f39d392007-10-15 16:14:19 -04002050 write_extent_buffer(leaf, root->fs_info->fsid,
2051 (unsigned long)btrfs_header_fsid(leaf),
2052 BTRFS_FSID_SIZE);
2053 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002054
2055 inode_item = &root_item.inode;
2056 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002057 inode_item->generation = cpu_to_le64(1);
2058 inode_item->size = cpu_to_le64(3);
2059 inode_item->nlink = cpu_to_le32(1);
2060 inode_item->nblocks = cpu_to_le64(1);
2061 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002062
Chris Masondb945352007-10-15 16:15:53 -04002063 btrfs_set_root_bytenr(&root_item, leaf->start);
2064 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002065 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002066 btrfs_set_root_used(&root_item, 0);
2067
Chris Mason5eda7b52007-06-22 14:16:25 -04002068 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2069 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002070
2071 free_extent_buffer(leaf);
2072 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002073
Chris Mason39279cc2007-06-12 06:35:45 -04002074 btrfs_set_root_dirid(&root_item, new_dirid);
2075
2076 key.objectid = objectid;
2077 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002078 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2079 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2080 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002081 if (ret)
2082 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002083
2084 /*
2085 * insert the directory item
2086 */
2087 key.offset = (u64)-1;
2088 dir = root->fs_info->sb->s_root->d_inode;
2089 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2090 name, namelen, dir->i_ino, &key,
2091 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002092 if (ret)
2093 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002094
Chris Mason39544012007-12-12 14:38:19 -05002095 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2096 name, namelen, objectid,
2097 root->fs_info->sb->s_root->d_inode->i_ino);
2098 if (ret)
2099 goto fail;
2100
Chris Mason39279cc2007-06-12 06:35:45 -04002101 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002102 if (ret)
2103 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002104
Josef Bacik58176a92007-08-29 15:47:34 -04002105 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002106 BUG_ON(!new_root);
2107
2108 trans = btrfs_start_transaction(new_root, 1);
2109 BUG_ON(!trans);
2110
2111 inode = btrfs_new_inode(trans, new_root, new_dirid,
2112 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002113 if (IS_ERR(inode))
2114 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002115 inode->i_op = &btrfs_dir_inode_operations;
2116 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002117 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002118
Chris Mason39544012007-12-12 14:38:19 -05002119 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2120 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002121 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002122 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002123 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002124 if (ret)
2125 goto fail;
2126fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002127 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002128 err = btrfs_commit_transaction(trans, root);
2129 if (err && !ret)
2130 ret = err;
2131fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002132 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002133 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002134 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002135}
2136
2137static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2138{
2139 struct btrfs_trans_handle *trans;
2140 struct btrfs_key key;
2141 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002142 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04002143 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002144 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002145 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002146 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002147
2148 if (!root->ref_cows)
2149 return -EINVAL;
2150
Chris Mason011410b2007-09-10 19:58:36 -04002151 down_write(&root->snap_sem);
2152 freeze_bdev(root->fs_info->sb->s_bdev);
2153 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2154
Chris Mason39279cc2007-06-12 06:35:45 -04002155 mutex_lock(&root->fs_info->fs_mutex);
2156 trans = btrfs_start_transaction(root, 1);
2157 BUG_ON(!trans);
2158
2159 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002160 if (ret)
2161 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002162
2163 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2164 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002165 if (ret)
2166 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002167
2168 memcpy(&new_root_item, &root->root_item,
2169 sizeof(new_root_item));
2170
2171 key.objectid = objectid;
2172 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002173 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Masonbe20aa92007-12-17 20:14:01 -05002174
Yan96919752007-12-04 13:20:20 -05002175 extent_buffer_get(root->node);
Chris Mason83df7c12007-08-27 16:49:44 -04002176 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Yan96919752007-12-04 13:20:20 -05002177 free_extent_buffer(tmp);
Chris Masonbe20aa92007-12-17 20:14:01 -05002178
2179 btrfs_copy_root(trans, root, root->node, &tmp, objectid);
2180
2181 btrfs_set_root_bytenr(&new_root_item, tmp->start);
2182 btrfs_set_root_level(&new_root_item, btrfs_header_level(tmp));
Chris Mason39279cc2007-06-12 06:35:45 -04002183 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2184 &new_root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05002185 free_extent_buffer(tmp);
Chris Mason54aa1f42007-06-22 14:16:25 -04002186 if (ret)
2187 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002188
2189 /*
2190 * insert the directory item
2191 */
2192 key.offset = (u64)-1;
2193 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2194 name, namelen,
2195 root->fs_info->sb->s_root->d_inode->i_ino,
2196 &key, BTRFS_FT_DIR);
2197
Chris Mason54aa1f42007-06-22 14:16:25 -04002198 if (ret)
2199 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002200
Chris Mason39544012007-12-12 14:38:19 -05002201 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2202 name, namelen, objectid,
2203 root->fs_info->sb->s_root->d_inode->i_ino);
2204
2205 if (ret)
2206 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002207fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002208 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002209 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002210
Chris Mason54aa1f42007-06-22 14:16:25 -04002211 if (err && !ret)
2212 ret = err;
Chris Mason5f39d392007-10-15 16:14:19 -04002213
Chris Mason39279cc2007-06-12 06:35:45 -04002214 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002215 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002216 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002217 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002218}
2219
Chris Mason86479a02007-09-10 19:58:16 -04002220static unsigned long force_ra(struct address_space *mapping,
2221 struct file_ra_state *ra, struct file *file,
2222 pgoff_t offset, pgoff_t last_index)
2223{
2224 pgoff_t req_size;
2225
2226#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2227 req_size = last_index - offset + 1;
2228 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2229 return offset;
2230#else
2231 req_size = min(last_index - offset + 1, (pgoff_t)128);
2232 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2233 return offset + req_size;
2234#endif
2235}
2236
2237int btrfs_defrag_file(struct file *file) {
2238 struct inode *inode = file->f_path.dentry->d_inode;
2239 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2240 struct page *page;
2241 unsigned long last_index;
2242 unsigned long ra_index = 0;
2243 u64 page_start;
2244 u64 page_end;
2245 unsigned long i;
2246
2247 mutex_lock(&inode->i_mutex);
2248 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2249 for (i = 0; i <= last_index; i++) {
2250 if (i == ra_index) {
2251 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2252 file, ra_index, last_index);
2253 }
2254 page = grab_cache_page(inode->i_mapping, i);
2255 if (!page)
2256 goto out_unlock;
2257 if (!PageUptodate(page)) {
2258 btrfs_readpage(NULL, page);
2259 lock_page(page);
2260 if (!PageUptodate(page)) {
2261 unlock_page(page);
2262 page_cache_release(page);
2263 goto out_unlock;
2264 }
2265 }
Chris Mason35ebb932007-10-30 16:56:53 -04002266 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002267 page_end = page_start + PAGE_CACHE_SIZE - 1;
2268
2269 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2270 set_extent_delalloc(em_tree, page_start,
2271 page_end, GFP_NOFS);
2272 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2273 set_page_dirty(page);
2274 unlock_page(page);
2275 page_cache_release(page);
2276 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2277 }
2278
2279out_unlock:
2280 mutex_unlock(&inode->i_mutex);
2281 return 0;
2282}
2283
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002284static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2285{
2286 struct btrfs_ioctl_vol_args vol_args;
2287 struct btrfs_dir_item *di;
2288 struct btrfs_path *path;
2289 int namelen;
2290 u64 root_dirid;
2291
2292 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2293 return -EFAULT;
Chris Mason5f39d392007-10-15 16:14:19 -04002294
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002295 namelen = strlen(vol_args.name);
2296 if (namelen > BTRFS_VOL_NAME_MAX)
2297 return -EINVAL;
2298 if (strchr(vol_args.name, '/'))
2299 return -EINVAL;
2300
2301 path = btrfs_alloc_path();
2302 if (!path)
2303 return -ENOMEM;
2304
2305 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2306 mutex_lock(&root->fs_info->fs_mutex);
2307 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2308 path, root_dirid,
2309 vol_args.name, namelen, 0);
2310 mutex_unlock(&root->fs_info->fs_mutex);
2311 btrfs_free_path(path);
2312 if (di && !IS_ERR(di))
2313 return -EEXIST;
2314 if (IS_ERR(di))
2315 return PTR_ERR(di);
2316
2317 if (root == root->fs_info->tree_root)
2318 return create_subvol(root, vol_args.name, namelen);
2319 return create_snapshot(root, vol_args.name, namelen);
2320}
2321
2322static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002323{
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002324 struct inode *inode = file->f_path.dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002325 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002326
2327 switch (inode->i_mode & S_IFMT) {
2328 case S_IFDIR:
2329 mutex_lock(&root->fs_info->fs_mutex);
2330 btrfs_defrag_root(root, 0);
2331 btrfs_defrag_root(root->fs_info->extent_root, 0);
2332 mutex_unlock(&root->fs_info->fs_mutex);
2333 break;
2334 case S_IFREG:
2335 btrfs_defrag_file(file);
2336 break;
2337 }
2338
2339 return 0;
2340}
2341
2342long btrfs_ioctl(struct file *file, unsigned int
2343 cmd, unsigned long arg)
2344{
2345 struct btrfs_root *root = BTRFS_I(file->f_path.dentry->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002346
2347 switch (cmd) {
2348 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002349 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002350 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002351 return btrfs_ioctl_defrag(file);
Chris Mason39279cc2007-06-12 06:35:45 -04002352 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002353
2354 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002355}
2356
Chris Mason39279cc2007-06-12 06:35:45 -04002357/*
2358 * Called inside transaction, so use GFP_NOFS
2359 */
2360struct inode *btrfs_alloc_inode(struct super_block *sb)
2361{
2362 struct btrfs_inode *ei;
2363
2364 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2365 if (!ei)
2366 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002367 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002368 return &ei->vfs_inode;
2369}
2370
2371void btrfs_destroy_inode(struct inode *inode)
2372{
2373 WARN_ON(!list_empty(&inode->i_dentry));
2374 WARN_ON(inode->i_data.nrpages);
2375
2376 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2377}
2378
Chris Mason44ec0b72007-10-29 10:55:05 -04002379#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2380static void init_once(struct kmem_cache * cachep, void *foo)
2381#else
Chris Mason39279cc2007-06-12 06:35:45 -04002382static void init_once(void * foo, struct kmem_cache * cachep,
2383 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002384#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002385{
2386 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2387
2388 inode_init_once(&ei->vfs_inode);
2389}
2390
2391void btrfs_destroy_cachep(void)
2392{
2393 if (btrfs_inode_cachep)
2394 kmem_cache_destroy(btrfs_inode_cachep);
2395 if (btrfs_trans_handle_cachep)
2396 kmem_cache_destroy(btrfs_trans_handle_cachep);
2397 if (btrfs_transaction_cachep)
2398 kmem_cache_destroy(btrfs_transaction_cachep);
2399 if (btrfs_bit_radix_cachep)
2400 kmem_cache_destroy(btrfs_bit_radix_cachep);
2401 if (btrfs_path_cachep)
2402 kmem_cache_destroy(btrfs_path_cachep);
2403}
2404
Chris Mason86479a02007-09-10 19:58:16 -04002405struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002406 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002407#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2408 void (*ctor)(struct kmem_cache *, void *)
2409#else
Chris Mason92fee662007-07-25 12:31:35 -04002410 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002411 unsigned long)
2412#endif
2413 )
Chris Mason92fee662007-07-25 12:31:35 -04002414{
2415 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2416 SLAB_MEM_SPREAD | extra_flags), ctor
2417#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2418 ,NULL
2419#endif
2420 );
2421}
2422
Chris Mason39279cc2007-06-12 06:35:45 -04002423int btrfs_init_cachep(void)
2424{
Chris Mason86479a02007-09-10 19:58:16 -04002425 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002426 sizeof(struct btrfs_inode),
2427 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002428 if (!btrfs_inode_cachep)
2429 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002430 btrfs_trans_handle_cachep =
2431 btrfs_cache_create("btrfs_trans_handle_cache",
2432 sizeof(struct btrfs_trans_handle),
2433 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002434 if (!btrfs_trans_handle_cachep)
2435 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002436 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002437 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002438 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002439 if (!btrfs_transaction_cachep)
2440 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002441 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002442 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002443 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002444 if (!btrfs_path_cachep)
2445 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002446 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002447 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002448 if (!btrfs_bit_radix_cachep)
2449 goto fail;
2450 return 0;
2451fail:
2452 btrfs_destroy_cachep();
2453 return -ENOMEM;
2454}
2455
2456static int btrfs_getattr(struct vfsmount *mnt,
2457 struct dentry *dentry, struct kstat *stat)
2458{
2459 struct inode *inode = dentry->d_inode;
2460 generic_fillattr(inode, stat);
2461 stat->blksize = 256 * 1024;
2462 return 0;
2463}
2464
2465static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2466 struct inode * new_dir,struct dentry *new_dentry)
2467{
2468 struct btrfs_trans_handle *trans;
2469 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2470 struct inode *new_inode = new_dentry->d_inode;
2471 struct inode *old_inode = old_dentry->d_inode;
2472 struct timespec ctime = CURRENT_TIME;
2473 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002474 int ret;
2475
2476 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2477 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2478 return -ENOTEMPTY;
2479 }
Chris Mason5f39d392007-10-15 16:14:19 -04002480
Chris Mason39279cc2007-06-12 06:35:45 -04002481 mutex_lock(&root->fs_info->fs_mutex);
2482 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002483
Chris Mason39279cc2007-06-12 06:35:45 -04002484 btrfs_set_trans_block_group(trans, new_dir);
2485 path = btrfs_alloc_path();
2486 if (!path) {
2487 ret = -ENOMEM;
2488 goto out_fail;
2489 }
2490
2491 old_dentry->d_inode->i_nlink++;
2492 old_dir->i_ctime = old_dir->i_mtime = ctime;
2493 new_dir->i_ctime = new_dir->i_mtime = ctime;
2494 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002495
Chris Mason39279cc2007-06-12 06:35:45 -04002496 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2497 if (ret)
2498 goto out_fail;
2499
2500 if (new_inode) {
2501 new_inode->i_ctime = CURRENT_TIME;
2502 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2503 if (ret)
2504 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002505 }
2506 ret = btrfs_add_link(trans, new_dentry, old_inode);
2507 if (ret)
2508 goto out_fail;
2509
2510out_fail:
2511 btrfs_free_path(path);
2512 btrfs_end_transaction(trans, root);
2513 mutex_unlock(&root->fs_info->fs_mutex);
2514 return ret;
2515}
2516
2517static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2518 const char *symname)
2519{
2520 struct btrfs_trans_handle *trans;
2521 struct btrfs_root *root = BTRFS_I(dir)->root;
2522 struct btrfs_path *path;
2523 struct btrfs_key key;
2524 struct inode *inode;
2525 int err;
2526 int drop_inode = 0;
2527 u64 objectid;
2528 int name_len;
2529 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002530 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002531 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002532 struct extent_buffer *leaf;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002533 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002534
2535 name_len = strlen(symname) + 1;
2536 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2537 return -ENAMETOOLONG;
2538 mutex_lock(&root->fs_info->fs_mutex);
2539 trans = btrfs_start_transaction(root, 1);
2540 btrfs_set_trans_block_group(trans, dir);
2541
2542 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2543 if (err) {
2544 err = -ENOSPC;
2545 goto out_unlock;
2546 }
2547
2548 inode = btrfs_new_inode(trans, root, objectid,
2549 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2550 err = PTR_ERR(inode);
2551 if (IS_ERR(inode))
2552 goto out_unlock;
2553
2554 btrfs_set_trans_block_group(trans, inode);
2555 err = btrfs_add_nondir(trans, dentry, inode);
2556 if (err)
2557 drop_inode = 1;
2558 else {
2559 inode->i_mapping->a_ops = &btrfs_aops;
2560 inode->i_fop = &btrfs_file_operations;
2561 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002562 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2563 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002564 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002565 }
2566 dir->i_sb->s_dirt = 1;
2567 btrfs_update_inode_block_group(trans, inode);
2568 btrfs_update_inode_block_group(trans, dir);
2569 if (drop_inode)
2570 goto out_unlock;
2571
2572 path = btrfs_alloc_path();
2573 BUG_ON(!path);
2574 key.objectid = inode->i_ino;
2575 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002576 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2577 datasize = btrfs_file_extent_calc_inline_size(name_len);
2578 err = btrfs_insert_empty_item(trans, root, path, &key,
2579 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002580 if (err) {
2581 drop_inode = 1;
2582 goto out_unlock;
2583 }
Chris Mason5f39d392007-10-15 16:14:19 -04002584 leaf = path->nodes[0];
2585 ei = btrfs_item_ptr(leaf, path->slots[0],
2586 struct btrfs_file_extent_item);
2587 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2588 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002589 BTRFS_FILE_EXTENT_INLINE);
2590 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002591 write_extent_buffer(leaf, symname, ptr, name_len);
2592 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002593 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002594
Chris Mason39279cc2007-06-12 06:35:45 -04002595 inode->i_op = &btrfs_symlink_inode_operations;
2596 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2597 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002598 err = btrfs_update_inode(trans, root, inode);
2599 if (err)
2600 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002601
2602out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002603 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002604 btrfs_end_transaction(trans, root);
2605 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002606 if (drop_inode) {
2607 inode_dec_link_count(inode);
2608 iput(inode);
2609 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002610 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002611 return err;
2612}
2613
2614static struct inode_operations btrfs_dir_inode_operations = {
2615 .lookup = btrfs_lookup,
2616 .create = btrfs_create,
2617 .unlink = btrfs_unlink,
2618 .link = btrfs_link,
2619 .mkdir = btrfs_mkdir,
2620 .rmdir = btrfs_rmdir,
2621 .rename = btrfs_rename,
2622 .symlink = btrfs_symlink,
2623 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002624 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002625 .setxattr = generic_setxattr,
2626 .getxattr = generic_getxattr,
2627 .listxattr = btrfs_listxattr,
2628 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002629};
2630
2631static struct inode_operations btrfs_dir_ro_inode_operations = {
2632 .lookup = btrfs_lookup,
2633};
2634
2635static struct file_operations btrfs_dir_file_operations = {
2636 .llseek = generic_file_llseek,
2637 .read = generic_read_dir,
2638 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002639 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002640#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002641 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002642#endif
2643};
2644
Chris Mason07157aa2007-08-30 08:50:51 -04002645static struct extent_map_ops btrfs_extent_map_ops = {
2646 .fill_delalloc = run_delalloc_range,
2647 .writepage_io_hook = btrfs_writepage_io_hook,
2648 .readpage_io_hook = btrfs_readpage_io_hook,
2649 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2650};
2651
Chris Mason39279cc2007-06-12 06:35:45 -04002652static struct address_space_operations btrfs_aops = {
2653 .readpage = btrfs_readpage,
2654 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04002655 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002656 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002657 .sync_page = block_sync_page,
2658 .prepare_write = btrfs_prepare_write,
2659 .commit_write = btrfs_commit_write,
2660 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002661 .invalidatepage = btrfs_invalidatepage,
2662 .releasepage = btrfs_releasepage,
2663 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002664};
2665
2666static struct address_space_operations btrfs_symlink_aops = {
2667 .readpage = btrfs_readpage,
2668 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002669 .invalidatepage = btrfs_invalidatepage,
2670 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002671};
2672
2673static struct inode_operations btrfs_file_inode_operations = {
2674 .truncate = btrfs_truncate,
2675 .getattr = btrfs_getattr,
2676 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002677 .setxattr = generic_setxattr,
2678 .getxattr = generic_getxattr,
2679 .listxattr = btrfs_listxattr,
2680 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002681};
2682
Josef Bacik618e21d2007-07-11 10:18:17 -04002683static struct inode_operations btrfs_special_inode_operations = {
2684 .getattr = btrfs_getattr,
2685 .setattr = btrfs_setattr,
2686};
2687
Chris Mason39279cc2007-06-12 06:35:45 -04002688static struct inode_operations btrfs_symlink_inode_operations = {
2689 .readlink = generic_readlink,
2690 .follow_link = page_follow_link_light,
2691 .put_link = page_put_link,
2692};