blob: 6a7d9160df276a91229b6f633ab2b6289ecb4e8a [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason39279cc2007-06-12 06:35:45 -040019#include <linux/buffer_head.h>
20#include <linux/fs.h>
21#include <linux/pagemap.h>
22#include <linux/highmem.h>
23#include <linux/time.h>
24#include <linux/init.h>
25#include <linux/string.h>
26#include <linux/smp_lock.h>
27#include <linux/backing-dev.h>
28#include <linux/mpage.h>
29#include <linux/swap.h>
30#include <linux/writeback.h>
31#include <linux/statfs.h>
32#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040033#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040034#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050035#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040036#include "ctree.h"
37#include "disk-io.h"
38#include "transaction.h"
39#include "btrfs_inode.h"
40#include "ioctl.h"
41#include "print-tree.h"
42
43struct btrfs_iget_args {
44 u64 ino;
45 struct btrfs_root *root;
46};
47
48static struct inode_operations btrfs_dir_inode_operations;
49static struct inode_operations btrfs_symlink_inode_operations;
50static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040051static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040052static struct inode_operations btrfs_file_inode_operations;
53static struct address_space_operations btrfs_aops;
54static struct address_space_operations btrfs_symlink_aops;
55static struct file_operations btrfs_dir_file_operations;
Chris Mason07157aa2007-08-30 08:50:51 -040056static struct extent_map_ops btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040057
58static struct kmem_cache *btrfs_inode_cachep;
59struct kmem_cache *btrfs_trans_handle_cachep;
60struct kmem_cache *btrfs_transaction_cachep;
61struct kmem_cache *btrfs_bit_radix_cachep;
62struct kmem_cache *btrfs_path_cachep;
63
64#define S_SHIFT 12
65static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
66 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
67 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
68 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
69 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
70 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
71 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
72 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
73};
74
Chris Masonbe20aa92007-12-17 20:14:01 -050075static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -040076{
77 struct btrfs_root *root = BTRFS_I(inode)->root;
78 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -040079 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -040080 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -050081 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -040082 u64 blocksize = root->sectorsize;
Chris Masonbe20aa92007-12-17 20:14:01 -050083 struct btrfs_key ins;
84 int ret;
Chris Masonb888db22007-08-27 16:49:44 -040085
Chris Masonb888db22007-08-27 16:49:44 -040086 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -040087 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -050088 btrfs_set_trans_block_group(trans, inode);
89
Chris Masondb945352007-10-15 16:15:53 -040090 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -050091 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -040092 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -040093 start, start + num_bytes, start, &alloc_hint);
Chris Masondb945352007-10-15 16:15:53 -040094
Chris Mason179e29e2007-11-01 11:28:41 -040095 if (alloc_hint == EXTENT_MAP_INLINE)
96 goto out;
97
Chris Masonc59f8952007-12-17 20:14:04 -050098 while(num_bytes > 0) {
99 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
100 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
101 root->root_key.objectid,
102 trans->transid,
103 inode->i_ino, start, 0,
104 alloc_hint, (u64)-1, &ins, 1);
105 if (ret) {
106 WARN_ON(1);
107 goto out;
108 }
109 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
110 start, ins.objectid, ins.offset,
111 ins.offset);
112 num_bytes -= cur_alloc_size;
113 alloc_hint = ins.objectid + ins.offset;
114 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400115 }
Chris Masonb888db22007-08-27 16:49:44 -0400116out:
117 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500118 return ret;
119}
120
121static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
122{
123 u64 extent_start;
124 u64 extent_end;
125 u64 bytenr;
126 u64 cow_end;
127 struct btrfs_root *root = BTRFS_I(inode)->root;
128 struct extent_buffer *leaf;
129 int found_type;
130 struct btrfs_path *path;
131 struct btrfs_file_extent_item *item;
132 int ret;
133 int err;
134 struct btrfs_key found_key;
135
136 path = btrfs_alloc_path();
137 BUG_ON(!path);
138again:
139 ret = btrfs_lookup_file_extent(NULL, root, path,
140 inode->i_ino, start, 0);
141 if (ret < 0) {
142 btrfs_free_path(path);
143 return ret;
144 }
145
146 cow_end = end;
147 if (ret != 0) {
148 if (path->slots[0] == 0)
149 goto not_found;
150 path->slots[0]--;
151 }
152
153 leaf = path->nodes[0];
154 item = btrfs_item_ptr(leaf, path->slots[0],
155 struct btrfs_file_extent_item);
156
157 /* are we inside the extent that was found? */
158 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
159 found_type = btrfs_key_type(&found_key);
160 if (found_key.objectid != inode->i_ino ||
161 found_type != BTRFS_EXTENT_DATA_KEY) {
162 goto not_found;
163 }
164
165 found_type = btrfs_file_extent_type(leaf, item);
166 extent_start = found_key.offset;
167 if (found_type == BTRFS_FILE_EXTENT_REG) {
168 extent_end = extent_start +
169 btrfs_file_extent_num_bytes(leaf, item);
170 err = 0;
171
172 if (start < extent_start || start >= extent_end)
173 goto not_found;
174
175 cow_end = min(end, extent_end - 1);
176 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
177 if (bytenr == 0)
178 goto not_found;
179
180 bytenr += btrfs_file_extent_offset(leaf, item);
181 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
182 goto not_found;
183 }
184
185 start = extent_end;
186 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
187 goto not_found;
188 }
189loop:
190 if (start > end) {
191 btrfs_free_path(path);
192 return 0;
193 }
194 btrfs_release_path(root, path);
195 goto again;
196
197not_found:
198 cow_file_range(inode, start, cow_end);
199 start = cow_end + 1;
200 goto loop;
201}
202
203static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
204{
205 struct btrfs_root *root = BTRFS_I(inode)->root;
206 int ret;
207
208 mutex_lock(&root->fs_info->fs_mutex);
209 if (btrfs_test_opt(root, NODATACOW))
210 ret = run_delalloc_nocow(inode, start, end);
211 else
212 ret = cow_file_range(inode, start, end);
Chris Masonb888db22007-08-27 16:49:44 -0400213 mutex_unlock(&root->fs_info->fs_mutex);
214 return ret;
215}
216
Chris Mason07157aa2007-08-30 08:50:51 -0400217int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
218{
219 struct inode *inode = page->mapping->host;
220 struct btrfs_root *root = BTRFS_I(inode)->root;
221 struct btrfs_trans_handle *trans;
222 char *kaddr;
Chris Masonb6cda9b2007-12-14 15:30:32 -0500223 int ret = 0;
Chris Mason35ebb932007-10-30 16:56:53 -0400224 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason07157aa2007-08-30 08:50:51 -0400225 size_t offset = start - page_start;
226
Chris Masonb6cda9b2007-12-14 15:30:32 -0500227 if (btrfs_test_opt(root, NODATASUM))
228 return 0;
229
Chris Mason07157aa2007-08-30 08:50:51 -0400230 mutex_lock(&root->fs_info->fs_mutex);
231 trans = btrfs_start_transaction(root, 1);
232 btrfs_set_trans_block_group(trans, inode);
233 kaddr = kmap(page);
Chris Masonf578d4b2007-10-25 15:42:56 -0400234 btrfs_csum_file_block(trans, root, inode, inode->i_ino,
Chris Mason07157aa2007-08-30 08:50:51 -0400235 start, kaddr + offset, end - start + 1);
236 kunmap(page);
237 ret = btrfs_end_transaction(trans, root);
238 BUG_ON(ret);
239 mutex_unlock(&root->fs_info->fs_mutex);
240 return ret;
241}
242
243int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
244{
245 int ret = 0;
246 struct inode *inode = page->mapping->host;
247 struct btrfs_root *root = BTRFS_I(inode)->root;
248 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
249 struct btrfs_csum_item *item;
250 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400251 u32 csum;
Chris Mason07157aa2007-08-30 08:50:51 -0400252
Chris Masonb6cda9b2007-12-14 15:30:32 -0500253 if (btrfs_test_opt(root, NODATASUM))
254 return 0;
255
Chris Mason07157aa2007-08-30 08:50:51 -0400256 mutex_lock(&root->fs_info->fs_mutex);
257 path = btrfs_alloc_path();
258 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
259 if (IS_ERR(item)) {
260 ret = PTR_ERR(item);
261 /* a csum that isn't present is a preallocated region. */
262 if (ret == -ENOENT || ret == -EFBIG)
263 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400264 csum = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400265 goto out;
266 }
Chris Masonff79f812007-10-15 16:22:25 -0400267 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
268 BTRFS_CRC32_SIZE);
269 set_state_private(em_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400270out:
271 if (path)
272 btrfs_free_path(path);
273 mutex_unlock(&root->fs_info->fs_mutex);
274 return ret;
275}
276
277int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end)
278{
Chris Mason35ebb932007-10-30 16:56:53 -0400279 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400280 struct inode *inode = page->mapping->host;
Chris Mason07157aa2007-08-30 08:50:51 -0400281 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
282 char *kaddr;
283 u64 private;
284 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400285 struct btrfs_root *root = BTRFS_I(inode)->root;
286 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400287 unsigned long flags;
Chris Mason07157aa2007-08-30 08:50:51 -0400288
Chris Masonb6cda9b2007-12-14 15:30:32 -0500289 if (btrfs_test_opt(root, NODATASUM))
290 return 0;
291
Chris Mason07157aa2007-08-30 08:50:51 -0400292 ret = get_state_private(em_tree, start, &private);
Jens Axboebbf0d002007-10-19 09:23:07 -0400293 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400294 kaddr = kmap_atomic(page, KM_IRQ0);
295 if (ret) {
296 goto zeroit;
297 }
Chris Masonff79f812007-10-15 16:22:25 -0400298 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
299 btrfs_csum_final(csum, (char *)&csum);
300 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400301 goto zeroit;
302 }
303 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400304 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400305 return 0;
306
307zeroit:
308 printk("btrfs csum failed ino %lu off %llu\n",
309 page->mapping->host->i_ino, (unsigned long long)start);
Chris Masondb945352007-10-15 16:15:53 -0400310 memset(kaddr + offset, 1, end - start + 1);
311 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400312 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400313 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400314 return 0;
315}
Chris Masonb888db22007-08-27 16:49:44 -0400316
Chris Mason39279cc2007-06-12 06:35:45 -0400317void btrfs_read_locked_inode(struct inode *inode)
318{
319 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400320 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400321 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400322 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400323 struct btrfs_root *root = BTRFS_I(inode)->root;
324 struct btrfs_key location;
325 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400326 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400327 int ret;
328
329 path = btrfs_alloc_path();
330 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400331 mutex_lock(&root->fs_info->fs_mutex);
332
333 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
334 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400335 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400336 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400337
Chris Mason5f39d392007-10-15 16:14:19 -0400338 leaf = path->nodes[0];
339 inode_item = btrfs_item_ptr(leaf, path->slots[0],
340 struct btrfs_inode_item);
341
342 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
343 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
344 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
345 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
346 inode->i_size = btrfs_inode_size(leaf, inode_item);
347
348 tspec = btrfs_inode_atime(inode_item);
349 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
350 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
351
352 tspec = btrfs_inode_mtime(inode_item);
353 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
354 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
355
356 tspec = btrfs_inode_ctime(inode_item);
357 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
358 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
359
360 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
361 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400362 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400363 rdev = btrfs_inode_rdev(leaf, inode_item);
364
365 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400366 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
367 alloc_group_block);
368
369 btrfs_free_path(path);
370 inode_item = NULL;
371
372 mutex_unlock(&root->fs_info->fs_mutex);
373
374 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400375 case S_IFREG:
376 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason07157aa2007-08-30 08:50:51 -0400377 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400378 inode->i_fop = &btrfs_file_operations;
379 inode->i_op = &btrfs_file_inode_operations;
380 break;
381 case S_IFDIR:
382 inode->i_fop = &btrfs_dir_file_operations;
383 if (root == root->fs_info->tree_root)
384 inode->i_op = &btrfs_dir_ro_inode_operations;
385 else
386 inode->i_op = &btrfs_dir_inode_operations;
387 break;
388 case S_IFLNK:
389 inode->i_op = &btrfs_symlink_inode_operations;
390 inode->i_mapping->a_ops = &btrfs_symlink_aops;
391 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400392 default:
393 init_special_inode(inode, inode->i_mode, rdev);
394 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400395 }
396 return;
397
398make_bad:
399 btrfs_release_path(root, path);
400 btrfs_free_path(path);
401 mutex_unlock(&root->fs_info->fs_mutex);
402 make_bad_inode(inode);
403}
404
Chris Mason5f39d392007-10-15 16:14:19 -0400405static void fill_inode_item(struct extent_buffer *leaf,
406 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400407 struct inode *inode)
408{
Chris Mason5f39d392007-10-15 16:14:19 -0400409 btrfs_set_inode_uid(leaf, item, inode->i_uid);
410 btrfs_set_inode_gid(leaf, item, inode->i_gid);
411 btrfs_set_inode_size(leaf, item, inode->i_size);
412 btrfs_set_inode_mode(leaf, item, inode->i_mode);
413 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
414
415 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
416 inode->i_atime.tv_sec);
417 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
418 inode->i_atime.tv_nsec);
419
420 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
421 inode->i_mtime.tv_sec);
422 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
423 inode->i_mtime.tv_nsec);
424
425 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
426 inode->i_ctime.tv_sec);
427 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
428 inode->i_ctime.tv_nsec);
429
430 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
431 btrfs_set_inode_generation(leaf, item, inode->i_generation);
432 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
433 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400434 BTRFS_I(inode)->block_group->key.objectid);
435}
436
Chris Masona52d9a82007-08-27 16:49:44 -0400437int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400438 struct btrfs_root *root,
439 struct inode *inode)
440{
441 struct btrfs_inode_item *inode_item;
442 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400443 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400444 int ret;
445
446 path = btrfs_alloc_path();
447 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400448 ret = btrfs_lookup_inode(trans, root, path,
449 &BTRFS_I(inode)->location, 1);
450 if (ret) {
451 if (ret > 0)
452 ret = -ENOENT;
453 goto failed;
454 }
455
Chris Mason5f39d392007-10-15 16:14:19 -0400456 leaf = path->nodes[0];
457 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400458 struct btrfs_inode_item);
459
Chris Mason5f39d392007-10-15 16:14:19 -0400460 fill_inode_item(leaf, inode_item, inode);
461 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400462 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400463 ret = 0;
464failed:
465 btrfs_release_path(root, path);
466 btrfs_free_path(path);
467 return ret;
468}
469
470
471static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
472 struct btrfs_root *root,
473 struct inode *dir,
474 struct dentry *dentry)
475{
476 struct btrfs_path *path;
477 const char *name = dentry->d_name.name;
478 int name_len = dentry->d_name.len;
479 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400480 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400481 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400482 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400483
484 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400485 if (!path) {
486 ret = -ENOMEM;
487 goto err;
488 }
489
Chris Mason39279cc2007-06-12 06:35:45 -0400490 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
491 name, name_len, -1);
492 if (IS_ERR(di)) {
493 ret = PTR_ERR(di);
494 goto err;
495 }
496 if (!di) {
497 ret = -ENOENT;
498 goto err;
499 }
Chris Mason5f39d392007-10-15 16:14:19 -0400500 leaf = path->nodes[0];
501 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400502 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400503 if (ret)
504 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400505 btrfs_release_path(root, path);
506
507 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400508 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400509 if (IS_ERR(di)) {
510 ret = PTR_ERR(di);
511 goto err;
512 }
513 if (!di) {
514 ret = -ENOENT;
515 goto err;
516 }
517 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400518
519 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500520 ret = btrfs_del_inode_ref(trans, root, name, name_len,
521 dentry->d_inode->i_ino,
522 dentry->d_parent->d_inode->i_ino);
523 if (ret) {
524 printk("failed to delete reference to %.*s, "
525 "inode %lu parent %lu\n", name_len, name,
526 dentry->d_inode->i_ino,
527 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500528 }
Chris Mason39279cc2007-06-12 06:35:45 -0400529err:
530 btrfs_free_path(path);
531 if (!ret) {
532 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400533 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400534 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500535#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
536 dentry->d_inode->i_nlink--;
537#else
Chris Mason39279cc2007-06-12 06:35:45 -0400538 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500539#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400540 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400541 dir->i_sb->s_dirt = 1;
542 }
543 return ret;
544}
545
546static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
547{
548 struct btrfs_root *root;
549 struct btrfs_trans_handle *trans;
550 int ret;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400551 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400552
553 root = BTRFS_I(dir)->root;
554 mutex_lock(&root->fs_info->fs_mutex);
555 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400556
Chris Mason39279cc2007-06-12 06:35:45 -0400557 btrfs_set_trans_block_group(trans, dir);
558 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400559 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400560
Chris Mason39279cc2007-06-12 06:35:45 -0400561 btrfs_end_transaction(trans, root);
562 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400563 btrfs_btree_balance_dirty(root, nr);
Chris Mason5f39d392007-10-15 16:14:19 -0400564
Chris Mason39279cc2007-06-12 06:35:45 -0400565 return ret;
566}
567
568static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
569{
570 struct inode *inode = dentry->d_inode;
571 int err;
572 int ret;
573 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400574 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400575 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400576
Yan134d4512007-10-25 15:49:25 -0400577 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
578 return -ENOTEMPTY;
579
Chris Mason39279cc2007-06-12 06:35:45 -0400580 mutex_lock(&root->fs_info->fs_mutex);
581 trans = btrfs_start_transaction(root, 1);
582 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400583
584 /* now the directory is empty */
585 err = btrfs_unlink_trans(trans, root, dir, dentry);
586 if (!err) {
587 inode->i_size = 0;
588 }
Chris Mason39544012007-12-12 14:38:19 -0500589
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400590 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400591 ret = btrfs_end_transaction(trans, root);
Yan134d4512007-10-25 15:49:25 -0400592 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400593 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -0500594
Chris Mason39279cc2007-06-12 06:35:45 -0400595 if (ret && !err)
596 err = ret;
597 return err;
598}
599
600static int btrfs_free_inode(struct btrfs_trans_handle *trans,
601 struct btrfs_root *root,
602 struct inode *inode)
603{
604 struct btrfs_path *path;
605 int ret;
606
607 clear_inode(inode);
608
609 path = btrfs_alloc_path();
610 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400611 ret = btrfs_lookup_inode(trans, root, path,
612 &BTRFS_I(inode)->location, -1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400613 if (ret > 0)
614 ret = -ENOENT;
615 if (!ret)
616 ret = btrfs_del_item(trans, root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400617 btrfs_free_path(path);
618 return ret;
619}
620
621/*
Chris Mason39279cc2007-06-12 06:35:45 -0400622 * this can truncate away extent items, csum items and directory items.
623 * It starts at a high offset and removes keys until it can't find
624 * any higher than i_size.
625 *
626 * csum items that cross the new i_size are truncated to the new size
627 * as well.
628 */
629static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
630 struct btrfs_root *root,
631 struct inode *inode)
632{
633 int ret;
634 struct btrfs_path *path;
635 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400636 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400637 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400638 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400639 struct btrfs_file_extent_item *fi;
640 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400641 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400642 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500643 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500644 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400645 int found_extent;
646 int del_item;
Chris Mason179e29e2007-11-01 11:28:41 -0400647 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400648
Chris Masona52d9a82007-08-27 16:49:44 -0400649 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400650 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400651 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400652 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400653
Chris Mason39279cc2007-06-12 06:35:45 -0400654 /* FIXME, add redo link to tree so we don't leak on crash */
655 key.objectid = inode->i_ino;
656 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400657 key.type = (u8)-1;
658
Chris Mason39279cc2007-06-12 06:35:45 -0400659 while(1) {
660 btrfs_init_path(path);
661 fi = NULL;
662 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
663 if (ret < 0) {
664 goto error;
665 }
666 if (ret > 0) {
667 BUG_ON(path->slots[0] == 0);
668 path->slots[0]--;
669 }
Chris Mason5f39d392007-10-15 16:14:19 -0400670 leaf = path->nodes[0];
671 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
672 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400673
Chris Mason5f39d392007-10-15 16:14:19 -0400674 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400675 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400676
Chris Mason39279cc2007-06-12 06:35:45 -0400677 if (found_type != BTRFS_CSUM_ITEM_KEY &&
678 found_type != BTRFS_DIR_ITEM_KEY &&
679 found_type != BTRFS_DIR_INDEX_KEY &&
680 found_type != BTRFS_EXTENT_DATA_KEY)
681 break;
682
Chris Mason5f39d392007-10-15 16:14:19 -0400683 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400684 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400685 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400686 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400687 extent_type = btrfs_file_extent_type(leaf, fi);
688 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400689 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400690 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400691 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
692 struct btrfs_item *item = btrfs_item_nr(leaf,
693 path->slots[0]);
694 item_end += btrfs_file_extent_inline_len(leaf,
695 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400696 }
Yan008630c2007-11-07 13:31:09 -0500697 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400698 }
699 if (found_type == BTRFS_CSUM_ITEM_KEY) {
700 ret = btrfs_csum_truncate(trans, root, path,
701 inode->i_size);
702 BUG_ON(ret);
703 }
Yan008630c2007-11-07 13:31:09 -0500704 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400705 if (found_type == BTRFS_DIR_ITEM_KEY) {
706 found_type = BTRFS_INODE_ITEM_KEY;
707 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
708 found_type = BTRFS_CSUM_ITEM_KEY;
709 } else if (found_type) {
710 found_type--;
711 } else {
712 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400713 }
Yana61721d2007-09-17 11:08:38 -0400714 btrfs_set_key_type(&key, found_type);
Yan65555a02007-10-25 15:42:57 -0400715 btrfs_release_path(root, path);
Chris Masonb888db22007-08-27 16:49:44 -0400716 continue;
Chris Mason39279cc2007-06-12 06:35:45 -0400717 }
Chris Mason5f39d392007-10-15 16:14:19 -0400718 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400719 del_item = 1;
720 else
721 del_item = 0;
722 found_extent = 0;
723
724 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400725 if (found_type != BTRFS_EXTENT_DATA_KEY)
726 goto delete;
727
728 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400729 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400730 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400731 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400732 u64 orig_num_bytes =
733 btrfs_file_extent_num_bytes(leaf, fi);
734 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400735 found_key.offset + root->sectorsize - 1;
Chris Masondb945352007-10-15 16:15:53 -0400736 btrfs_set_file_extent_num_bytes(leaf, fi,
737 extent_num_bytes);
738 num_dec = (orig_num_bytes -
739 extent_num_bytes) >> 9;
Yanbab9fb02007-09-17 11:13:11 -0400740 if (extent_start != 0) {
741 inode->i_blocks -= num_dec;
742 }
Chris Mason5f39d392007-10-15 16:14:19 -0400743 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400744 } else {
Chris Masondb945352007-10-15 16:15:53 -0400745 extent_num_bytes =
746 btrfs_file_extent_disk_num_bytes(leaf,
747 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400748 /* FIXME blocksize != 4096 */
Chris Masondb945352007-10-15 16:15:53 -0400749 num_dec = btrfs_file_extent_num_bytes(leaf,
750 fi) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -0400751 if (extent_start != 0) {
752 found_extent = 1;
753 inode->i_blocks -= num_dec;
754 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500755 root_gen = btrfs_header_generation(leaf);
756 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400757 }
Chris Mason179e29e2007-11-01 11:28:41 -0400758 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE &&
759 !del_item) {
760 u32 newsize = inode->i_size - found_key.offset;
761 newsize = btrfs_file_extent_calc_inline_size(newsize);
762 ret = btrfs_truncate_item(trans, root, path,
763 newsize, 1);
764 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -0400765 }
Chris Mason179e29e2007-11-01 11:28:41 -0400766delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400767 if (del_item) {
768 ret = btrfs_del_item(trans, root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400769 if (ret)
770 goto error;
Chris Mason39279cc2007-06-12 06:35:45 -0400771 } else {
772 break;
773 }
774 btrfs_release_path(root, path);
775 if (found_extent) {
776 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500777 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500778 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500779 root_gen, inode->i_ino,
780 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400781 BUG_ON(ret);
782 }
783 }
784 ret = 0;
785error:
786 btrfs_release_path(root, path);
787 btrfs_free_path(path);
788 inode->i_sb->s_dirt = 1;
789 return ret;
790}
791
Chris Masonb888db22007-08-27 16:49:44 -0400792static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400793 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400794{
Chris Mason39279cc2007-06-12 06:35:45 -0400795 char *kaddr;
796 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400797 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400798 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400799 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400800
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400801 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400802
Chris Masonb888db22007-08-27 16:49:44 -0400803 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
804 set_extent_delalloc(&BTRFS_I(inode)->extent_tree, page_start,
805 page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400806 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400807 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400808 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
809 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400810 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400811 }
Chris Masonb888db22007-08-27 16:49:44 -0400812 set_page_dirty(page);
813 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400814
Chris Masona52d9a82007-08-27 16:49:44 -0400815 return ret;
816}
817
818/*
819 * taken from block_truncate_page, but does cow as it zeros out
820 * any bytes left in the last page in the file.
821 */
822static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
823{
824 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400825 struct btrfs_root *root = BTRFS_I(inode)->root;
826 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400827 pgoff_t index = from >> PAGE_CACHE_SHIFT;
828 unsigned offset = from & (PAGE_CACHE_SIZE-1);
829 struct page *page;
830 int ret = 0;
831 u64 page_start;
832
833 if ((offset & (blocksize - 1)) == 0)
834 goto out;
835
Chris Masondb945352007-10-15 16:15:53 -0400836 down_read(&root->snap_sem);
Chris Masona52d9a82007-08-27 16:49:44 -0400837 ret = -ENOMEM;
838 page = grab_cache_page(mapping, index);
839 if (!page)
840 goto out;
841 if (!PageUptodate(page)) {
842 ret = btrfs_readpage(NULL, page);
843 lock_page(page);
844 if (!PageUptodate(page)) {
845 ret = -EIO;
846 goto out;
847 }
848 }
Chris Mason35ebb932007-10-30 16:56:53 -0400849 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400850
Chris Masonb888db22007-08-27 16:49:44 -0400851 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400852
Chris Mason39279cc2007-06-12 06:35:45 -0400853 unlock_page(page);
854 page_cache_release(page);
Chris Mason011410b2007-09-10 19:58:36 -0400855 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason39279cc2007-06-12 06:35:45 -0400856out:
857 return ret;
858}
859
860static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
861{
862 struct inode *inode = dentry->d_inode;
863 int err;
864
865 err = inode_change_ok(inode, attr);
866 if (err)
867 return err;
868
869 if (S_ISREG(inode->i_mode) &&
870 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
871 struct btrfs_trans_handle *trans;
872 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason2bf5a722007-08-30 11:54:02 -0400873 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
874
Chris Mason5f39d392007-10-15 16:14:19 -0400875 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400876 u64 pos = (inode->i_size + mask) & ~mask;
Chris Mason2bf5a722007-08-30 11:54:02 -0400877 u64 block_end = attr->ia_size | mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400878 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -0400879 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400880
881 if (attr->ia_size <= pos)
882 goto out;
883
884 btrfs_truncate_page(inode->i_mapping, inode->i_size);
885
Chris Mason2bf5a722007-08-30 11:54:02 -0400886 lock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -0400887 hole_size = (attr->ia_size - pos + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -0400888
889 mutex_lock(&root->fs_info->fs_mutex);
890 trans = btrfs_start_transaction(root, 1);
891 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -0400892 err = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400893 pos, pos + hole_size, pos,
894 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -0400895
Chris Mason179e29e2007-11-01 11:28:41 -0400896 if (alloc_hint != EXTENT_MAP_INLINE) {
897 err = btrfs_insert_file_extent(trans, root,
898 inode->i_ino,
899 pos, 0, 0, hole_size);
900 }
Chris Mason39279cc2007-06-12 06:35:45 -0400901 btrfs_end_transaction(trans, root);
902 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -0400903 unlock_extent(em_tree, pos, block_end, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -0400904 if (err)
905 return err;
Chris Mason39279cc2007-06-12 06:35:45 -0400906 }
907out:
908 err = inode_setattr(inode, attr);
909
910 return err;
911}
912void btrfs_delete_inode(struct inode *inode)
913{
914 struct btrfs_trans_handle *trans;
915 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400916 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -0400917 int ret;
918
919 truncate_inode_pages(&inode->i_data, 0);
920 if (is_bad_inode(inode)) {
921 goto no_delete;
922 }
Chris Mason5f39d392007-10-15 16:14:19 -0400923
Chris Mason39279cc2007-06-12 06:35:45 -0400924 inode->i_size = 0;
925 mutex_lock(&root->fs_info->fs_mutex);
926 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400927
Chris Mason39279cc2007-06-12 06:35:45 -0400928 btrfs_set_trans_block_group(trans, inode);
929 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -0400930 if (ret)
931 goto no_delete_lock;
Josef Bacik5103e942007-11-16 11:45:54 -0500932 ret = btrfs_delete_xattrs(trans, root, inode);
933 if (ret)
934 goto no_delete_lock;
Chris Mason54aa1f42007-06-22 14:16:25 -0400935 ret = btrfs_free_inode(trans, root, inode);
936 if (ret)
937 goto no_delete_lock;
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400938 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400939
Chris Mason39279cc2007-06-12 06:35:45 -0400940 btrfs_end_transaction(trans, root);
941 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400942 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400943 return;
Chris Mason54aa1f42007-06-22 14:16:25 -0400944
945no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400946 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -0400947 btrfs_end_transaction(trans, root);
948 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400949 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400950no_delete:
951 clear_inode(inode);
952}
953
954/*
955 * this returns the key found in the dir entry in the location pointer.
956 * If no dir entries were found, location->objectid is 0.
957 */
958static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
959 struct btrfs_key *location)
960{
961 const char *name = dentry->d_name.name;
962 int namelen = dentry->d_name.len;
963 struct btrfs_dir_item *di;
964 struct btrfs_path *path;
965 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -0400966 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400967
Chris Mason39544012007-12-12 14:38:19 -0500968 if (namelen == 1 && strcmp(name, ".") == 0) {
969 location->objectid = dir->i_ino;
970 location->type = BTRFS_INODE_ITEM_KEY;
971 location->offset = 0;
972 return 0;
973 }
Chris Mason39279cc2007-06-12 06:35:45 -0400974 path = btrfs_alloc_path();
975 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -0500976
Chris Mason7a720532007-12-13 09:06:59 -0500977 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -0500978 struct btrfs_key key;
979 struct extent_buffer *leaf;
980 u32 nritems;
981 int slot;
982
983 key.objectid = dir->i_ino;
984 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
985 key.offset = 0;
986 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
987 BUG_ON(ret == 0);
988 ret = 0;
989
990 leaf = path->nodes[0];
991 slot = path->slots[0];
992 nritems = btrfs_header_nritems(leaf);
993 if (slot >= nritems)
994 goto out_err;
995
996 btrfs_item_key_to_cpu(leaf, &key, slot);
997 if (key.objectid != dir->i_ino ||
998 key.type != BTRFS_INODE_REF_KEY) {
999 goto out_err;
1000 }
1001 location->objectid = key.offset;
1002 location->type = BTRFS_INODE_ITEM_KEY;
1003 location->offset = 0;
1004 goto out;
1005 }
1006
Chris Mason39279cc2007-06-12 06:35:45 -04001007 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1008 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001009 if (IS_ERR(di))
1010 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001011 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001012 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001013 }
Chris Mason5f39d392007-10-15 16:14:19 -04001014 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001015out:
Chris Mason39279cc2007-06-12 06:35:45 -04001016 btrfs_free_path(path);
1017 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001018out_err:
1019 location->objectid = 0;
1020 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001021}
1022
1023/*
1024 * when we hit a tree root in a directory, the btrfs part of the inode
1025 * needs to be changed to reflect the root directory of the tree root. This
1026 * is kind of like crossing a mount point.
1027 */
1028static int fixup_tree_root_location(struct btrfs_root *root,
1029 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001030 struct btrfs_root **sub_root,
1031 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001032{
1033 struct btrfs_path *path;
1034 struct btrfs_root_item *ri;
1035
1036 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1037 return 0;
1038 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1039 return 0;
1040
1041 path = btrfs_alloc_path();
1042 BUG_ON(!path);
1043 mutex_lock(&root->fs_info->fs_mutex);
1044
Josef Bacik58176a92007-08-29 15:47:34 -04001045 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1046 dentry->d_name.name,
1047 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001048 if (IS_ERR(*sub_root))
1049 return PTR_ERR(*sub_root);
1050
1051 ri = &(*sub_root)->root_item;
1052 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001053 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1054 location->offset = 0;
1055
1056 btrfs_free_path(path);
1057 mutex_unlock(&root->fs_info->fs_mutex);
1058 return 0;
1059}
1060
1061static int btrfs_init_locked_inode(struct inode *inode, void *p)
1062{
1063 struct btrfs_iget_args *args = p;
1064 inode->i_ino = args->ino;
1065 BTRFS_I(inode)->root = args->root;
Chris Masonb888db22007-08-27 16:49:44 -04001066 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1067 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001068 return 0;
1069}
1070
1071static int btrfs_find_actor(struct inode *inode, void *opaque)
1072{
1073 struct btrfs_iget_args *args = opaque;
1074 return (args->ino == inode->i_ino &&
1075 args->root == BTRFS_I(inode)->root);
1076}
1077
1078struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1079 struct btrfs_root *root)
1080{
1081 struct inode *inode;
1082 struct btrfs_iget_args args;
1083 args.ino = objectid;
1084 args.root = root;
1085
1086 inode = iget5_locked(s, objectid, btrfs_find_actor,
1087 btrfs_init_locked_inode,
1088 (void *)&args);
1089 return inode;
1090}
1091
1092static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1093 struct nameidata *nd)
1094{
1095 struct inode * inode;
1096 struct btrfs_inode *bi = BTRFS_I(dir);
1097 struct btrfs_root *root = bi->root;
1098 struct btrfs_root *sub_root = root;
1099 struct btrfs_key location;
1100 int ret;
1101
1102 if (dentry->d_name.len > BTRFS_NAME_LEN)
1103 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001104
Chris Mason39279cc2007-06-12 06:35:45 -04001105 mutex_lock(&root->fs_info->fs_mutex);
1106 ret = btrfs_inode_by_name(dir, dentry, &location);
1107 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001108
Chris Mason39279cc2007-06-12 06:35:45 -04001109 if (ret < 0)
1110 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001111
Chris Mason39279cc2007-06-12 06:35:45 -04001112 inode = NULL;
1113 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001114 ret = fixup_tree_root_location(root, &location, &sub_root,
1115 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001116 if (ret < 0)
1117 return ERR_PTR(ret);
1118 if (ret > 0)
1119 return ERR_PTR(-ENOENT);
1120 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1121 sub_root);
1122 if (!inode)
1123 return ERR_PTR(-EACCES);
1124 if (inode->i_state & I_NEW) {
1125 /* the inode and parent dir are two different roots */
1126 if (sub_root != root) {
1127 igrab(inode);
1128 sub_root->inode = inode;
1129 }
1130 BTRFS_I(inode)->root = sub_root;
1131 memcpy(&BTRFS_I(inode)->location, &location,
1132 sizeof(location));
1133 btrfs_read_locked_inode(inode);
1134 unlock_new_inode(inode);
1135 }
1136 }
1137 return d_splice_alias(inode, dentry);
1138}
1139
Chris Mason39279cc2007-06-12 06:35:45 -04001140static unsigned char btrfs_filetype_table[] = {
1141 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1142};
1143
1144static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1145{
Chris Mason6da6aba2007-12-18 16:15:09 -05001146 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001147 struct btrfs_root *root = BTRFS_I(inode)->root;
1148 struct btrfs_item *item;
1149 struct btrfs_dir_item *di;
1150 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001151 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001152 struct btrfs_path *path;
1153 int ret;
1154 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001155 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001156 int slot;
1157 int advance;
1158 unsigned char d_type;
1159 int over = 0;
1160 u32 di_cur;
1161 u32 di_total;
1162 u32 di_len;
1163 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001164 char tmp_name[32];
1165 char *name_ptr;
1166 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001167
1168 /* FIXME, use a real flag for deciding about the key type */
1169 if (root->fs_info->tree_root == root)
1170 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001171
Chris Mason39544012007-12-12 14:38:19 -05001172 /* special case for "." */
1173 if (filp->f_pos == 0) {
1174 over = filldir(dirent, ".", 1,
1175 1, inode->i_ino,
1176 DT_DIR);
1177 if (over)
1178 return 0;
1179 filp->f_pos = 1;
1180 }
1181
Chris Mason39279cc2007-06-12 06:35:45 -04001182 mutex_lock(&root->fs_info->fs_mutex);
1183 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001184 path = btrfs_alloc_path();
1185 path->reada = 2;
1186
1187 /* special case for .., just use the back ref */
1188 if (filp->f_pos == 1) {
1189 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1190 key.offset = 0;
1191 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1192 BUG_ON(ret == 0);
1193 leaf = path->nodes[0];
1194 slot = path->slots[0];
1195 nritems = btrfs_header_nritems(leaf);
1196 if (slot >= nritems) {
1197 btrfs_release_path(root, path);
1198 goto read_dir_items;
1199 }
1200 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1201 btrfs_release_path(root, path);
1202 if (found_key.objectid != key.objectid ||
1203 found_key.type != BTRFS_INODE_REF_KEY)
1204 goto read_dir_items;
1205 over = filldir(dirent, "..", 2,
1206 2, found_key.offset, DT_DIR);
1207 if (over)
1208 goto nopos;
1209 filp->f_pos = 2;
1210 }
1211
1212read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001213 btrfs_set_key_type(&key, key_type);
1214 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001215
Chris Mason39279cc2007-06-12 06:35:45 -04001216 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1217 if (ret < 0)
1218 goto err;
1219 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001220 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001221 leaf = path->nodes[0];
1222 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001223 slot = path->slots[0];
1224 if (advance || slot >= nritems) {
1225 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001226 ret = btrfs_next_leaf(root, path);
1227 if (ret)
1228 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001229 leaf = path->nodes[0];
1230 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001231 slot = path->slots[0];
1232 } else {
1233 slot++;
1234 path->slots[0]++;
1235 }
1236 }
1237 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001238 item = btrfs_item_nr(leaf, slot);
1239 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1240
1241 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001242 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001243 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001244 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001245 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001246 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001247
1248 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001249 advance = 1;
1250 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1251 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001252 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001253 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001254 struct btrfs_key location;
1255
1256 name_len = btrfs_dir_name_len(leaf, di);
1257 if (name_len < 32) {
1258 name_ptr = tmp_name;
1259 } else {
1260 name_ptr = kmalloc(name_len, GFP_NOFS);
1261 BUG_ON(!name_ptr);
1262 }
1263 read_extent_buffer(leaf, name_ptr,
1264 (unsigned long)(di + 1), name_len);
1265
1266 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1267 btrfs_dir_item_key_to_cpu(leaf, di, &location);
1268
1269 over = filldir(dirent, name_ptr, name_len,
1270 found_key.offset,
1271 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001272 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001273
1274 if (name_ptr != tmp_name)
1275 kfree(name_ptr);
1276
Chris Mason39279cc2007-06-12 06:35:45 -04001277 if (over)
1278 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001279 di_len = btrfs_dir_name_len(leaf, di) +
1280 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001281 di_cur += di_len;
1282 di = (struct btrfs_dir_item *)((char *)di + di_len);
1283 }
1284 }
1285 filp->f_pos++;
1286nopos:
1287 ret = 0;
1288err:
1289 btrfs_release_path(root, path);
1290 btrfs_free_path(path);
1291 mutex_unlock(&root->fs_info->fs_mutex);
1292 return ret;
1293}
1294
1295int btrfs_write_inode(struct inode *inode, int wait)
1296{
1297 struct btrfs_root *root = BTRFS_I(inode)->root;
1298 struct btrfs_trans_handle *trans;
1299 int ret = 0;
1300
1301 if (wait) {
1302 mutex_lock(&root->fs_info->fs_mutex);
1303 trans = btrfs_start_transaction(root, 1);
1304 btrfs_set_trans_block_group(trans, inode);
1305 ret = btrfs_commit_transaction(trans, root);
1306 mutex_unlock(&root->fs_info->fs_mutex);
1307 }
1308 return ret;
1309}
1310
1311/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001312 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001313 * inode changes. But, it is most likely to find the inode in cache.
1314 * FIXME, needs more benchmarking...there are no reasons other than performance
1315 * to keep or drop this code.
1316 */
1317void btrfs_dirty_inode(struct inode *inode)
1318{
1319 struct btrfs_root *root = BTRFS_I(inode)->root;
1320 struct btrfs_trans_handle *trans;
1321
1322 mutex_lock(&root->fs_info->fs_mutex);
1323 trans = btrfs_start_transaction(root, 1);
1324 btrfs_set_trans_block_group(trans, inode);
1325 btrfs_update_inode(trans, root, inode);
1326 btrfs_end_transaction(trans, root);
1327 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001328}
1329
1330static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1331 struct btrfs_root *root,
1332 u64 objectid,
1333 struct btrfs_block_group_cache *group,
1334 int mode)
1335{
1336 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001337 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001338 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001339 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001340 int ret;
1341 int owner;
1342
Chris Mason5f39d392007-10-15 16:14:19 -04001343 path = btrfs_alloc_path();
1344 BUG_ON(!path);
1345
Chris Mason39279cc2007-06-12 06:35:45 -04001346 inode = new_inode(root->fs_info->sb);
1347 if (!inode)
1348 return ERR_PTR(-ENOMEM);
1349
Chris Masonb888db22007-08-27 16:49:44 -04001350 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1351 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001352 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001353
Chris Mason39279cc2007-06-12 06:35:45 -04001354 if (mode & S_IFDIR)
1355 owner = 0;
1356 else
1357 owner = 1;
1358 group = btrfs_find_block_group(root, group, 0, 0, owner);
1359 BTRFS_I(inode)->block_group = group;
1360
Chris Mason5f39d392007-10-15 16:14:19 -04001361 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
1362 if (ret)
1363 goto fail;
1364
Chris Mason39279cc2007-06-12 06:35:45 -04001365 inode->i_uid = current->fsuid;
1366 inode->i_gid = current->fsgid;
1367 inode->i_mode = mode;
1368 inode->i_ino = objectid;
1369 inode->i_blocks = 0;
1370 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001371 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1372 struct btrfs_inode_item);
1373 fill_inode_item(path->nodes[0], inode_item, inode);
1374 btrfs_mark_buffer_dirty(path->nodes[0]);
1375 btrfs_free_path(path);
1376
Chris Mason39279cc2007-06-12 06:35:45 -04001377 location = &BTRFS_I(inode)->location;
1378 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001379 location->offset = 0;
1380 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1381
Chris Mason39279cc2007-06-12 06:35:45 -04001382 insert_inode_hash(inode);
1383 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001384fail:
1385 btrfs_free_path(path);
1386 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001387}
1388
1389static inline u8 btrfs_inode_type(struct inode *inode)
1390{
1391 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1392}
1393
1394static int btrfs_add_link(struct btrfs_trans_handle *trans,
1395 struct dentry *dentry, struct inode *inode)
1396{
1397 int ret;
1398 struct btrfs_key key;
1399 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001400 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001401
Chris Mason39279cc2007-06-12 06:35:45 -04001402 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001403 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1404 key.offset = 0;
1405
1406 ret = btrfs_insert_dir_item(trans, root,
1407 dentry->d_name.name, dentry->d_name.len,
1408 dentry->d_parent->d_inode->i_ino,
1409 &key, btrfs_inode_type(inode));
1410 if (ret == 0) {
Chris Mason76fea002007-12-13 09:06:01 -05001411 ret = btrfs_insert_inode_ref(trans, root,
1412 dentry->d_name.name,
1413 dentry->d_name.len,
1414 inode->i_ino,
1415 dentry->d_parent->d_inode->i_ino);
Chris Mason79c44582007-06-25 10:09:33 -04001416 parent_inode = dentry->d_parent->d_inode;
1417 parent_inode->i_size += dentry->d_name.len * 2;
1418 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001419 ret = btrfs_update_inode(trans, root,
1420 dentry->d_parent->d_inode);
1421 }
1422 return ret;
1423}
1424
1425static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1426 struct dentry *dentry, struct inode *inode)
1427{
1428 int err = btrfs_add_link(trans, dentry, inode);
1429 if (!err) {
1430 d_instantiate(dentry, inode);
1431 return 0;
1432 }
1433 if (err > 0)
1434 err = -EEXIST;
1435 return err;
1436}
1437
Josef Bacik618e21d2007-07-11 10:18:17 -04001438static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1439 int mode, dev_t rdev)
1440{
1441 struct btrfs_trans_handle *trans;
1442 struct btrfs_root *root = BTRFS_I(dir)->root;
1443 struct inode *inode;
1444 int err;
1445 int drop_inode = 0;
1446 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001447 unsigned long nr;
Josef Bacik618e21d2007-07-11 10:18:17 -04001448
1449 if (!new_valid_dev(rdev))
1450 return -EINVAL;
1451
1452 mutex_lock(&root->fs_info->fs_mutex);
1453 trans = btrfs_start_transaction(root, 1);
1454 btrfs_set_trans_block_group(trans, dir);
1455
1456 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1457 if (err) {
1458 err = -ENOSPC;
1459 goto out_unlock;
1460 }
1461
1462 inode = btrfs_new_inode(trans, root, objectid,
1463 BTRFS_I(dir)->block_group, mode);
1464 err = PTR_ERR(inode);
1465 if (IS_ERR(inode))
1466 goto out_unlock;
1467
1468 btrfs_set_trans_block_group(trans, inode);
1469 err = btrfs_add_nondir(trans, dentry, inode);
1470 if (err)
1471 drop_inode = 1;
1472 else {
1473 inode->i_op = &btrfs_special_inode_operations;
1474 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001475 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001476 }
1477 dir->i_sb->s_dirt = 1;
1478 btrfs_update_inode_block_group(trans, inode);
1479 btrfs_update_inode_block_group(trans, dir);
1480out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001481 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001482 btrfs_end_transaction(trans, root);
1483 mutex_unlock(&root->fs_info->fs_mutex);
1484
1485 if (drop_inode) {
1486 inode_dec_link_count(inode);
1487 iput(inode);
1488 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001489 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001490 return err;
1491}
1492
Chris Mason39279cc2007-06-12 06:35:45 -04001493static int btrfs_create(struct inode *dir, struct dentry *dentry,
1494 int mode, struct nameidata *nd)
1495{
1496 struct btrfs_trans_handle *trans;
1497 struct btrfs_root *root = BTRFS_I(dir)->root;
1498 struct inode *inode;
1499 int err;
1500 int drop_inode = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001501 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001502 u64 objectid;
1503
1504 mutex_lock(&root->fs_info->fs_mutex);
1505 trans = btrfs_start_transaction(root, 1);
1506 btrfs_set_trans_block_group(trans, dir);
1507
1508 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1509 if (err) {
1510 err = -ENOSPC;
1511 goto out_unlock;
1512 }
1513
1514 inode = btrfs_new_inode(trans, root, objectid,
1515 BTRFS_I(dir)->block_group, mode);
1516 err = PTR_ERR(inode);
1517 if (IS_ERR(inode))
1518 goto out_unlock;
1519
1520 btrfs_set_trans_block_group(trans, inode);
1521 err = btrfs_add_nondir(trans, dentry, inode);
1522 if (err)
1523 drop_inode = 1;
1524 else {
1525 inode->i_mapping->a_ops = &btrfs_aops;
1526 inode->i_fop = &btrfs_file_operations;
1527 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04001528 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
1529 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04001530 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001531 }
1532 dir->i_sb->s_dirt = 1;
1533 btrfs_update_inode_block_group(trans, inode);
1534 btrfs_update_inode_block_group(trans, dir);
1535out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001536 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001537 btrfs_end_transaction(trans, root);
1538 mutex_unlock(&root->fs_info->fs_mutex);
1539
1540 if (drop_inode) {
1541 inode_dec_link_count(inode);
1542 iput(inode);
1543 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001544 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001545 return err;
1546}
1547
1548static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1549 struct dentry *dentry)
1550{
1551 struct btrfs_trans_handle *trans;
1552 struct btrfs_root *root = BTRFS_I(dir)->root;
1553 struct inode *inode = old_dentry->d_inode;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001554 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001555 int err;
1556 int drop_inode = 0;
1557
1558 if (inode->i_nlink == 0)
1559 return -ENOENT;
1560
Chris Mason6da6aba2007-12-18 16:15:09 -05001561#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1562 inode->i_nlink++;
1563#else
Chris Mason39279cc2007-06-12 06:35:45 -04001564 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001565#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001566 mutex_lock(&root->fs_info->fs_mutex);
1567 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001568
Chris Mason39279cc2007-06-12 06:35:45 -04001569 btrfs_set_trans_block_group(trans, dir);
1570 atomic_inc(&inode->i_count);
1571 err = btrfs_add_nondir(trans, dentry, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001572
Chris Mason39279cc2007-06-12 06:35:45 -04001573 if (err)
1574 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001575
Chris Mason39279cc2007-06-12 06:35:45 -04001576 dir->i_sb->s_dirt = 1;
1577 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001578 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001579
Chris Mason54aa1f42007-06-22 14:16:25 -04001580 if (err)
1581 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001582
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001583 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001584 btrfs_end_transaction(trans, root);
1585 mutex_unlock(&root->fs_info->fs_mutex);
1586
1587 if (drop_inode) {
1588 inode_dec_link_count(inode);
1589 iput(inode);
1590 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001591 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001592 return err;
1593}
1594
Chris Mason39279cc2007-06-12 06:35:45 -04001595static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1596{
1597 struct inode *inode;
1598 struct btrfs_trans_handle *trans;
1599 struct btrfs_root *root = BTRFS_I(dir)->root;
1600 int err = 0;
1601 int drop_on_err = 0;
1602 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001603 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001604
1605 mutex_lock(&root->fs_info->fs_mutex);
1606 trans = btrfs_start_transaction(root, 1);
1607 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001608
Chris Mason39279cc2007-06-12 06:35:45 -04001609 if (IS_ERR(trans)) {
1610 err = PTR_ERR(trans);
1611 goto out_unlock;
1612 }
1613
1614 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1615 if (err) {
1616 err = -ENOSPC;
1617 goto out_unlock;
1618 }
1619
1620 inode = btrfs_new_inode(trans, root, objectid,
1621 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1622 if (IS_ERR(inode)) {
1623 err = PTR_ERR(inode);
1624 goto out_fail;
1625 }
Chris Mason5f39d392007-10-15 16:14:19 -04001626
Chris Mason39279cc2007-06-12 06:35:45 -04001627 drop_on_err = 1;
1628 inode->i_op = &btrfs_dir_inode_operations;
1629 inode->i_fop = &btrfs_dir_file_operations;
1630 btrfs_set_trans_block_group(trans, inode);
1631
Chris Mason39544012007-12-12 14:38:19 -05001632 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001633 err = btrfs_update_inode(trans, root, inode);
1634 if (err)
1635 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001636
Chris Mason39279cc2007-06-12 06:35:45 -04001637 err = btrfs_add_link(trans, dentry, inode);
1638 if (err)
1639 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001640
Chris Mason39279cc2007-06-12 06:35:45 -04001641 d_instantiate(dentry, inode);
1642 drop_on_err = 0;
1643 dir->i_sb->s_dirt = 1;
1644 btrfs_update_inode_block_group(trans, inode);
1645 btrfs_update_inode_block_group(trans, dir);
1646
1647out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001648 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001649 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001650
Chris Mason39279cc2007-06-12 06:35:45 -04001651out_unlock:
1652 mutex_unlock(&root->fs_info->fs_mutex);
1653 if (drop_on_err)
1654 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001655 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001656 return err;
1657}
1658
Chris Masona52d9a82007-08-27 16:49:44 -04001659struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1660 size_t page_offset, u64 start, u64 end,
1661 int create)
1662{
1663 int ret;
1664 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001665 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001666 u64 extent_start = 0;
1667 u64 extent_end = 0;
1668 u64 objectid = inode->i_ino;
1669 u32 found_type;
1670 int failed_insert = 0;
1671 struct btrfs_path *path;
1672 struct btrfs_root *root = BTRFS_I(inode)->root;
1673 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001674 struct extent_buffer *leaf;
1675 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001676 struct extent_map *em = NULL;
1677 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1678 struct btrfs_trans_handle *trans = NULL;
1679
1680 path = btrfs_alloc_path();
1681 BUG_ON(!path);
1682 mutex_lock(&root->fs_info->fs_mutex);
1683
1684again:
1685 em = lookup_extent_mapping(em_tree, start, end);
1686 if (em) {
1687 goto out;
1688 }
1689 if (!em) {
1690 em = alloc_extent_map(GFP_NOFS);
1691 if (!em) {
1692 err = -ENOMEM;
1693 goto out;
1694 }
Chris Mason5f39d392007-10-15 16:14:19 -04001695 em->start = EXTENT_MAP_HOLE;
1696 em->end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001697 }
1698 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001699 ret = btrfs_lookup_file_extent(trans, root, path,
1700 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001701 if (ret < 0) {
1702 err = ret;
1703 goto out;
1704 }
1705
1706 if (ret != 0) {
1707 if (path->slots[0] == 0)
1708 goto not_found;
1709 path->slots[0]--;
1710 }
1711
Chris Mason5f39d392007-10-15 16:14:19 -04001712 leaf = path->nodes[0];
1713 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001714 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001715 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001716 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1717 found_type = btrfs_key_type(&found_key);
1718 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001719 found_type != BTRFS_EXTENT_DATA_KEY) {
1720 goto not_found;
1721 }
1722
Chris Mason5f39d392007-10-15 16:14:19 -04001723 found_type = btrfs_file_extent_type(leaf, item);
1724 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001725 if (found_type == BTRFS_FILE_EXTENT_REG) {
1726 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001727 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001728 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001729 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001730 em->start = start;
1731 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001732 if (end < extent_start)
1733 goto not_found;
Chris Masona52d9a82007-08-27 16:49:44 -04001734 em->end = extent_end - 1;
1735 } else {
1736 em->end = end;
1737 }
1738 goto not_found_em;
1739 }
Chris Masondb945352007-10-15 16:15:53 -04001740 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
1741 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04001742 em->start = extent_start;
1743 em->end = extent_end - 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001744 em->block_start = EXTENT_MAP_HOLE;
1745 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001746 goto insert;
1747 }
Chris Masondb945352007-10-15 16:15:53 -04001748 bytenr += btrfs_file_extent_offset(leaf, item);
1749 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001750 em->block_end = em->block_start +
Chris Masondb945352007-10-15 16:15:53 -04001751 btrfs_file_extent_num_bytes(leaf, item) - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04001752 em->start = extent_start;
1753 em->end = extent_end - 1;
1754 goto insert;
1755 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001756 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04001757 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04001758 size_t size;
1759 size_t extent_offset;
1760 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04001761
Chris Mason5f39d392007-10-15 16:14:19 -04001762 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
1763 path->slots[0]));
Yan689f9342007-10-29 11:41:07 -04001764 extent_end = (extent_start + size - 1) |
Chris Masondb945352007-10-15 16:15:53 -04001765 ((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04001766 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001767 em->start = start;
1768 if (start < extent_start) {
Chris Masonb888db22007-08-27 16:49:44 -04001769 if (end < extent_start)
1770 goto not_found;
Chris Mason50b78c22007-09-20 14:14:42 -04001771 em->end = extent_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001772 } else {
1773 em->end = end;
1774 }
1775 goto not_found_em;
1776 }
1777 em->block_start = EXTENT_MAP_INLINE;
1778 em->block_end = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04001779
1780 if (!page) {
1781 em->start = extent_start;
1782 em->end = extent_start + size - 1;
1783 goto out;
1784 }
1785
Chris Mason35ebb932007-10-30 16:56:53 -04001786 extent_offset = ((u64)page->index << PAGE_CACHE_SHIFT) -
Yan689f9342007-10-29 11:41:07 -04001787 extent_start + page_offset;
1788 copy_size = min_t(u64, PAGE_CACHE_SIZE - page_offset,
1789 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04001790 em->start = extent_start + extent_offset;
1791 em->end = (em->start + copy_size -1) |
1792 ((u64)root->sectorsize -1);
Yan689f9342007-10-29 11:41:07 -04001793 map = kmap(page);
1794 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04001795 if (create == 0 && !PageUptodate(page)) {
1796 read_extent_buffer(leaf, map + page_offset, ptr,
1797 copy_size);
1798 flush_dcache_page(page);
1799 } else if (create && PageUptodate(page)) {
1800 if (!trans) {
1801 kunmap(page);
1802 free_extent_map(em);
1803 em = NULL;
1804 btrfs_release_path(root, path);
1805 trans = btrfs_start_transaction(root, 1);
1806 goto again;
1807 }
1808 write_extent_buffer(leaf, map + page_offset, ptr,
1809 copy_size);
1810 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04001811 }
Chris Masona52d9a82007-08-27 16:49:44 -04001812 kunmap(page);
Chris Mason3326d1b2007-10-15 16:18:25 -04001813 set_extent_uptodate(em_tree, em->start, em->end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001814 goto insert;
1815 } else {
1816 printk("unkknown found_type %d\n", found_type);
1817 WARN_ON(1);
1818 }
1819not_found:
1820 em->start = start;
1821 em->end = end;
1822not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04001823 em->block_start = EXTENT_MAP_HOLE;
1824 em->block_end = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04001825insert:
1826 btrfs_release_path(root, path);
1827 if (em->start > start || em->end < start) {
Chris Masonb888db22007-08-27 16:49:44 -04001828 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->end, start, end);
Chris Masona52d9a82007-08-27 16:49:44 -04001829 err = -EIO;
1830 goto out;
1831 }
1832 ret = add_extent_mapping(em_tree, em);
1833 if (ret == -EEXIST) {
1834 free_extent_map(em);
Chris Mason2bf5a722007-08-30 11:54:02 -04001835 em = NULL;
Chris Mason6da6aba2007-12-18 16:15:09 -05001836 if (0 && failed_insert == 1) {
1837 btrfs_drop_extent_cache(inode, start, end);
1838 }
Chris Masona52d9a82007-08-27 16:49:44 -04001839 failed_insert++;
1840 if (failed_insert > 5) {
1841 printk("failing to insert %Lu %Lu\n", start, end);
1842 err = -EIO;
1843 goto out;
1844 }
Chris Masona52d9a82007-08-27 16:49:44 -04001845 goto again;
1846 }
1847 err = 0;
1848out:
1849 btrfs_free_path(path);
1850 if (trans) {
1851 ret = btrfs_end_transaction(trans, root);
1852 if (!err)
1853 err = ret;
1854 }
1855 mutex_unlock(&root->fs_info->fs_mutex);
1856 if (err) {
1857 free_extent_map(em);
1858 WARN_ON(1);
1859 return ERR_PTR(err);
1860 }
1861 return em;
1862}
1863
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001864static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04001865{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04001866 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001867}
1868
1869static int btrfs_prepare_write(struct file *file, struct page *page,
1870 unsigned from, unsigned to)
1871{
Chris Masona52d9a82007-08-27 16:49:44 -04001872 return extent_prepare_write(&BTRFS_I(page->mapping->host)->extent_tree,
1873 page->mapping->host, page, from, to,
1874 btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001875}
1876
Chris Mason9ebefb182007-06-15 13:50:00 -04001877int btrfs_readpage(struct file *file, struct page *page)
1878{
Chris Masona52d9a82007-08-27 16:49:44 -04001879 struct extent_map_tree *tree;
1880 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1881 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04001882}
Chris Mason39279cc2007-06-12 06:35:45 -04001883static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
1884{
Chris Masona52d9a82007-08-27 16:49:44 -04001885 struct extent_map_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04001886
1887
1888 if (current->flags & PF_MEMALLOC) {
1889 redirty_page_for_writepage(wbc, page);
1890 unlock_page(page);
1891 return 0;
1892 }
Chris Masona52d9a82007-08-27 16:49:44 -04001893 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1894 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
1895}
Chris Mason39279cc2007-06-12 06:35:45 -04001896
Chris Masonb293f022007-11-01 19:45:34 -04001897static int btrfs_writepages(struct address_space *mapping,
1898 struct writeback_control *wbc)
1899{
1900 struct extent_map_tree *tree;
1901 tree = &BTRFS_I(mapping->host)->extent_tree;
1902 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
1903}
1904
Chris Mason3ab2fb52007-11-08 10:59:22 -05001905static int
1906btrfs_readpages(struct file *file, struct address_space *mapping,
1907 struct list_head *pages, unsigned nr_pages)
1908{
1909 struct extent_map_tree *tree;
1910 tree = &BTRFS_I(mapping->host)->extent_tree;
1911 return extent_readpages(tree, mapping, pages, nr_pages,
1912 btrfs_get_extent);
1913}
1914
Chris Masona52d9a82007-08-27 16:49:44 -04001915static int btrfs_releasepage(struct page *page, gfp_t unused_gfp_flags)
1916{
1917 struct extent_map_tree *tree;
1918 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04001919
Chris Masona52d9a82007-08-27 16:49:44 -04001920 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1921 ret = try_release_extent_mapping(tree, page);
1922 if (ret == 1) {
1923 ClearPagePrivate(page);
1924 set_page_private(page, 0);
1925 page_cache_release(page);
1926 }
1927 return ret;
1928}
Chris Mason39279cc2007-06-12 06:35:45 -04001929
Chris Masona52d9a82007-08-27 16:49:44 -04001930static void btrfs_invalidatepage(struct page *page, unsigned long offset)
1931{
1932 struct extent_map_tree *tree;
1933
1934 tree = &BTRFS_I(page->mapping->host)->extent_tree;
1935 extent_invalidatepage(tree, page, offset);
1936 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001937}
1938
Chris Mason9ebefb182007-06-15 13:50:00 -04001939/*
1940 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
1941 * called from a page fault handler when a page is first dirtied. Hence we must
1942 * be careful to check for EOF conditions here. We set the page up correctly
1943 * for a written page which means we get ENOSPC checking when writing into
1944 * holes and correct delalloc and unwritten extent mapping on filesystems that
1945 * support these features.
1946 *
1947 * We are not allowed to take the i_mutex here so we have to play games to
1948 * protect against truncate races as the page could now be beyond EOF. Because
1949 * vmtruncate() writes the inode size before removing pages, once we have the
1950 * page lock we can determine safely if the page is beyond EOF. If it is not
1951 * beyond EOF, then the page is guaranteed safe against truncation until we
1952 * unlock the page.
1953 */
1954int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1955{
Chris Mason6da6aba2007-12-18 16:15:09 -05001956 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason9ebefb182007-06-15 13:50:00 -04001957 unsigned long end;
1958 loff_t size;
1959 int ret = -EINVAL;
Chris Masona52d9a82007-08-27 16:49:44 -04001960 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04001961
Chris Mason011410b2007-09-10 19:58:36 -04001962 down_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001963 lock_page(page);
1964 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04001965 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04001966 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001967
Chris Mason9ebefb182007-06-15 13:50:00 -04001968 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04001969 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04001970 /* page got truncated out from underneath us */
1971 goto out_unlock;
1972 }
1973
1974 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04001975 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04001976 end = size & ~PAGE_CACHE_MASK;
1977 else
1978 end = PAGE_CACHE_SIZE;
1979
Chris Masonb888db22007-08-27 16:49:44 -04001980 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04001981
1982out_unlock:
Chris Mason011410b2007-09-10 19:58:36 -04001983 up_read(&BTRFS_I(inode)->root->snap_sem);
Chris Mason9ebefb182007-06-15 13:50:00 -04001984 unlock_page(page);
1985 return ret;
1986}
1987
Chris Mason39279cc2007-06-12 06:35:45 -04001988static void btrfs_truncate(struct inode *inode)
1989{
1990 struct btrfs_root *root = BTRFS_I(inode)->root;
1991 int ret;
1992 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001993 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001994
1995 if (!S_ISREG(inode->i_mode))
1996 return;
1997 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1998 return;
1999
2000 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2001
2002 mutex_lock(&root->fs_info->fs_mutex);
2003 trans = btrfs_start_transaction(root, 1);
2004 btrfs_set_trans_block_group(trans, inode);
2005
2006 /* FIXME, add redo link to tree so we don't leak on crash */
2007 ret = btrfs_truncate_in_trans(trans, root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002008 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002009 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002010
Chris Mason39279cc2007-06-12 06:35:45 -04002011 ret = btrfs_end_transaction(trans, root);
2012 BUG_ON(ret);
2013 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002014 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002015}
2016
2017int btrfs_commit_write(struct file *file, struct page *page,
2018 unsigned from, unsigned to)
2019{
Chris Masone9906a92007-12-14 12:56:58 -05002020 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
2021 struct inode *inode = page->mapping->host;
2022
2023 btrfs_cow_one_page(inode, page, PAGE_CACHE_SIZE);
2024
Chris Masone9906a92007-12-14 12:56:58 -05002025 if (pos > inode->i_size) {
2026 i_size_write(inode, pos);
2027 mark_inode_dirty(inode);
2028 }
2029 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002030}
2031
2032static int create_subvol(struct btrfs_root *root, char *name, int namelen)
2033{
2034 struct btrfs_trans_handle *trans;
2035 struct btrfs_key key;
2036 struct btrfs_root_item root_item;
2037 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002038 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002039 struct btrfs_root *new_root;
2040 struct inode *inode;
2041 struct inode *dir;
2042 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002043 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002044 u64 objectid;
2045 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002046 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002047
2048 mutex_lock(&root->fs_info->fs_mutex);
2049 trans = btrfs_start_transaction(root, 1);
2050 BUG_ON(!trans);
2051
Chris Mason7bb86312007-12-11 09:25:06 -05002052 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2053 0, &objectid);
2054 if (ret)
2055 goto fail;
2056
2057 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2058 objectid, trans->transid, 0, 0,
2059 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002060 if (IS_ERR(leaf))
2061 return PTR_ERR(leaf);
2062
2063 btrfs_set_header_nritems(leaf, 0);
2064 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002065 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002066 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002067 btrfs_set_header_owner(leaf, objectid);
2068
Chris Mason5f39d392007-10-15 16:14:19 -04002069 write_extent_buffer(leaf, root->fs_info->fsid,
2070 (unsigned long)btrfs_header_fsid(leaf),
2071 BTRFS_FSID_SIZE);
2072 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002073
2074 inode_item = &root_item.inode;
2075 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002076 inode_item->generation = cpu_to_le64(1);
2077 inode_item->size = cpu_to_le64(3);
2078 inode_item->nlink = cpu_to_le32(1);
2079 inode_item->nblocks = cpu_to_le64(1);
2080 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002081
Chris Masondb945352007-10-15 16:15:53 -04002082 btrfs_set_root_bytenr(&root_item, leaf->start);
2083 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002084 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002085 btrfs_set_root_used(&root_item, 0);
2086
Chris Mason5eda7b52007-06-22 14:16:25 -04002087 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2088 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002089
2090 free_extent_buffer(leaf);
2091 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002092
Chris Mason39279cc2007-06-12 06:35:45 -04002093 btrfs_set_root_dirid(&root_item, new_dirid);
2094
2095 key.objectid = objectid;
2096 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002097 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2098 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2099 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002100 if (ret)
2101 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002102
2103 /*
2104 * insert the directory item
2105 */
2106 key.offset = (u64)-1;
2107 dir = root->fs_info->sb->s_root->d_inode;
2108 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2109 name, namelen, dir->i_ino, &key,
2110 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002111 if (ret)
2112 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002113
Chris Mason39544012007-12-12 14:38:19 -05002114 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2115 name, namelen, objectid,
2116 root->fs_info->sb->s_root->d_inode->i_ino);
2117 if (ret)
2118 goto fail;
2119
Chris Mason39279cc2007-06-12 06:35:45 -04002120 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002121 if (ret)
2122 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002123
Josef Bacik58176a92007-08-29 15:47:34 -04002124 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002125 BUG_ON(!new_root);
2126
2127 trans = btrfs_start_transaction(new_root, 1);
2128 BUG_ON(!trans);
2129
2130 inode = btrfs_new_inode(trans, new_root, new_dirid,
2131 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002132 if (IS_ERR(inode))
2133 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002134 inode->i_op = &btrfs_dir_inode_operations;
2135 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002136 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002137
Chris Mason39544012007-12-12 14:38:19 -05002138 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2139 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002140 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002141 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002142 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002143 if (ret)
2144 goto fail;
2145fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002146 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002147 err = btrfs_commit_transaction(trans, root);
2148 if (err && !ret)
2149 ret = err;
2150fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002151 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002152 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002153 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002154}
2155
2156static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2157{
2158 struct btrfs_trans_handle *trans;
2159 struct btrfs_key key;
2160 struct btrfs_root_item new_root_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002161 struct extent_buffer *tmp;
Chris Mason39279cc2007-06-12 06:35:45 -04002162 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002163 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002164 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002165 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002166
2167 if (!root->ref_cows)
2168 return -EINVAL;
2169
Chris Mason011410b2007-09-10 19:58:36 -04002170 down_write(&root->snap_sem);
2171 freeze_bdev(root->fs_info->sb->s_bdev);
2172 thaw_bdev(root->fs_info->sb->s_bdev, root->fs_info->sb);
2173
Chris Mason39279cc2007-06-12 06:35:45 -04002174 mutex_lock(&root->fs_info->fs_mutex);
2175 trans = btrfs_start_transaction(root, 1);
2176 BUG_ON(!trans);
2177
2178 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002179 if (ret)
2180 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002181
2182 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2183 0, &objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -04002184 if (ret)
2185 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002186
2187 memcpy(&new_root_item, &root->root_item,
2188 sizeof(new_root_item));
2189
2190 key.objectid = objectid;
2191 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002192 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
Chris Masonbe20aa92007-12-17 20:14:01 -05002193
Yan96919752007-12-04 13:20:20 -05002194 extent_buffer_get(root->node);
Chris Mason83df7c12007-08-27 16:49:44 -04002195 btrfs_cow_block(trans, root, root->node, NULL, 0, &tmp);
Yan96919752007-12-04 13:20:20 -05002196 free_extent_buffer(tmp);
Chris Masonbe20aa92007-12-17 20:14:01 -05002197
2198 btrfs_copy_root(trans, root, root->node, &tmp, objectid);
2199
2200 btrfs_set_root_bytenr(&new_root_item, tmp->start);
2201 btrfs_set_root_level(&new_root_item, btrfs_header_level(tmp));
Chris Mason39279cc2007-06-12 06:35:45 -04002202 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2203 &new_root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05002204 free_extent_buffer(tmp);
Chris Mason54aa1f42007-06-22 14:16:25 -04002205 if (ret)
2206 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002207
2208 /*
2209 * insert the directory item
2210 */
2211 key.offset = (u64)-1;
2212 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2213 name, namelen,
2214 root->fs_info->sb->s_root->d_inode->i_ino,
2215 &key, BTRFS_FT_DIR);
2216
Chris Mason54aa1f42007-06-22 14:16:25 -04002217 if (ret)
2218 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002219
Chris Mason39544012007-12-12 14:38:19 -05002220 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2221 name, namelen, objectid,
2222 root->fs_info->sb->s_root->d_inode->i_ino);
2223
2224 if (ret)
2225 goto fail;
Chris Mason54aa1f42007-06-22 14:16:25 -04002226fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002227 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04002228 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002229
Chris Mason54aa1f42007-06-22 14:16:25 -04002230 if (err && !ret)
2231 ret = err;
Chris Mason5f39d392007-10-15 16:14:19 -04002232
Chris Mason39279cc2007-06-12 06:35:45 -04002233 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason011410b2007-09-10 19:58:36 -04002234 up_write(&root->snap_sem);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002235 btrfs_btree_balance_dirty(root, nr);
Chris Mason54aa1f42007-06-22 14:16:25 -04002236 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002237}
2238
Chris Mason86479a02007-09-10 19:58:16 -04002239static unsigned long force_ra(struct address_space *mapping,
2240 struct file_ra_state *ra, struct file *file,
2241 pgoff_t offset, pgoff_t last_index)
2242{
2243 pgoff_t req_size;
2244
2245#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2246 req_size = last_index - offset + 1;
2247 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2248 return offset;
2249#else
2250 req_size = min(last_index - offset + 1, (pgoff_t)128);
2251 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2252 return offset + req_size;
2253#endif
2254}
2255
2256int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002257 struct inode *inode = fdentry(file)->d_inode;
Chris Mason86479a02007-09-10 19:58:16 -04002258 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2259 struct page *page;
2260 unsigned long last_index;
2261 unsigned long ra_index = 0;
2262 u64 page_start;
2263 u64 page_end;
2264 unsigned long i;
2265
2266 mutex_lock(&inode->i_mutex);
2267 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2268 for (i = 0; i <= last_index; i++) {
2269 if (i == ra_index) {
2270 ra_index = force_ra(inode->i_mapping, &file->f_ra,
2271 file, ra_index, last_index);
2272 }
2273 page = grab_cache_page(inode->i_mapping, i);
2274 if (!page)
2275 goto out_unlock;
2276 if (!PageUptodate(page)) {
2277 btrfs_readpage(NULL, page);
2278 lock_page(page);
2279 if (!PageUptodate(page)) {
2280 unlock_page(page);
2281 page_cache_release(page);
2282 goto out_unlock;
2283 }
2284 }
Chris Mason35ebb932007-10-30 16:56:53 -04002285 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002286 page_end = page_start + PAGE_CACHE_SIZE - 1;
2287
2288 lock_extent(em_tree, page_start, page_end, GFP_NOFS);
2289 set_extent_delalloc(em_tree, page_start,
2290 page_end, GFP_NOFS);
2291 unlock_extent(em_tree, page_start, page_end, GFP_NOFS);
2292 set_page_dirty(page);
2293 unlock_page(page);
2294 page_cache_release(page);
2295 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2296 }
2297
2298out_unlock:
2299 mutex_unlock(&inode->i_mutex);
2300 return 0;
2301}
2302
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002303static int btrfs_ioctl_snap_create(struct btrfs_root *root, void __user *arg)
2304{
2305 struct btrfs_ioctl_vol_args vol_args;
2306 struct btrfs_dir_item *di;
2307 struct btrfs_path *path;
2308 int namelen;
2309 u64 root_dirid;
2310
2311 if (copy_from_user(&vol_args, arg, sizeof(vol_args)))
2312 return -EFAULT;
Chris Mason5f39d392007-10-15 16:14:19 -04002313
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002314 namelen = strlen(vol_args.name);
2315 if (namelen > BTRFS_VOL_NAME_MAX)
2316 return -EINVAL;
2317 if (strchr(vol_args.name, '/'))
2318 return -EINVAL;
2319
2320 path = btrfs_alloc_path();
2321 if (!path)
2322 return -ENOMEM;
2323
2324 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2325 mutex_lock(&root->fs_info->fs_mutex);
2326 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2327 path, root_dirid,
2328 vol_args.name, namelen, 0);
2329 mutex_unlock(&root->fs_info->fs_mutex);
2330 btrfs_free_path(path);
2331 if (di && !IS_ERR(di))
2332 return -EEXIST;
2333 if (IS_ERR(di))
2334 return PTR_ERR(di);
2335
2336 if (root == root->fs_info->tree_root)
2337 return create_subvol(root, vol_args.name, namelen);
2338 return create_snapshot(root, vol_args.name, namelen);
2339}
2340
2341static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002342{
Chris Mason6da6aba2007-12-18 16:15:09 -05002343 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002344 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002345
2346 switch (inode->i_mode & S_IFMT) {
2347 case S_IFDIR:
2348 mutex_lock(&root->fs_info->fs_mutex);
2349 btrfs_defrag_root(root, 0);
2350 btrfs_defrag_root(root->fs_info->extent_root, 0);
2351 mutex_unlock(&root->fs_info->fs_mutex);
2352 break;
2353 case S_IFREG:
2354 btrfs_defrag_file(file);
2355 break;
2356 }
2357
2358 return 0;
2359}
2360
2361long btrfs_ioctl(struct file *file, unsigned int
2362 cmd, unsigned long arg)
2363{
Chris Mason6da6aba2007-12-18 16:15:09 -05002364 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002365
2366 switch (cmd) {
2367 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002368 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002369 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002370 return btrfs_ioctl_defrag(file);
Chris Mason39279cc2007-06-12 06:35:45 -04002371 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002372
2373 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002374}
2375
Chris Mason39279cc2007-06-12 06:35:45 -04002376/*
2377 * Called inside transaction, so use GFP_NOFS
2378 */
2379struct inode *btrfs_alloc_inode(struct super_block *sb)
2380{
2381 struct btrfs_inode *ei;
2382
2383 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2384 if (!ei)
2385 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002386 ei->last_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002387 return &ei->vfs_inode;
2388}
2389
2390void btrfs_destroy_inode(struct inode *inode)
2391{
2392 WARN_ON(!list_empty(&inode->i_dentry));
2393 WARN_ON(inode->i_data.nrpages);
2394
2395 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2396}
2397
Chris Mason44ec0b72007-10-29 10:55:05 -04002398#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2399static void init_once(struct kmem_cache * cachep, void *foo)
2400#else
Chris Mason39279cc2007-06-12 06:35:45 -04002401static void init_once(void * foo, struct kmem_cache * cachep,
2402 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002403#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002404{
2405 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2406
2407 inode_init_once(&ei->vfs_inode);
2408}
2409
2410void btrfs_destroy_cachep(void)
2411{
2412 if (btrfs_inode_cachep)
2413 kmem_cache_destroy(btrfs_inode_cachep);
2414 if (btrfs_trans_handle_cachep)
2415 kmem_cache_destroy(btrfs_trans_handle_cachep);
2416 if (btrfs_transaction_cachep)
2417 kmem_cache_destroy(btrfs_transaction_cachep);
2418 if (btrfs_bit_radix_cachep)
2419 kmem_cache_destroy(btrfs_bit_radix_cachep);
2420 if (btrfs_path_cachep)
2421 kmem_cache_destroy(btrfs_path_cachep);
2422}
2423
Chris Mason86479a02007-09-10 19:58:16 -04002424struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002425 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002426#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2427 void (*ctor)(struct kmem_cache *, void *)
2428#else
Chris Mason92fee662007-07-25 12:31:35 -04002429 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002430 unsigned long)
2431#endif
2432 )
Chris Mason92fee662007-07-25 12:31:35 -04002433{
2434 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2435 SLAB_MEM_SPREAD | extra_flags), ctor
2436#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2437 ,NULL
2438#endif
2439 );
2440}
2441
Chris Mason39279cc2007-06-12 06:35:45 -04002442int btrfs_init_cachep(void)
2443{
Chris Mason86479a02007-09-10 19:58:16 -04002444 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002445 sizeof(struct btrfs_inode),
2446 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002447 if (!btrfs_inode_cachep)
2448 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002449 btrfs_trans_handle_cachep =
2450 btrfs_cache_create("btrfs_trans_handle_cache",
2451 sizeof(struct btrfs_trans_handle),
2452 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002453 if (!btrfs_trans_handle_cachep)
2454 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002455 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002456 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002457 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002458 if (!btrfs_transaction_cachep)
2459 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002460 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002461 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002462 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002463 if (!btrfs_path_cachep)
2464 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002465 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002466 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002467 if (!btrfs_bit_radix_cachep)
2468 goto fail;
2469 return 0;
2470fail:
2471 btrfs_destroy_cachep();
2472 return -ENOMEM;
2473}
2474
2475static int btrfs_getattr(struct vfsmount *mnt,
2476 struct dentry *dentry, struct kstat *stat)
2477{
2478 struct inode *inode = dentry->d_inode;
2479 generic_fillattr(inode, stat);
2480 stat->blksize = 256 * 1024;
2481 return 0;
2482}
2483
2484static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2485 struct inode * new_dir,struct dentry *new_dentry)
2486{
2487 struct btrfs_trans_handle *trans;
2488 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2489 struct inode *new_inode = new_dentry->d_inode;
2490 struct inode *old_inode = old_dentry->d_inode;
2491 struct timespec ctime = CURRENT_TIME;
2492 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002493 int ret;
2494
2495 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2496 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2497 return -ENOTEMPTY;
2498 }
Chris Mason5f39d392007-10-15 16:14:19 -04002499
Chris Mason39279cc2007-06-12 06:35:45 -04002500 mutex_lock(&root->fs_info->fs_mutex);
2501 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002502
Chris Mason39279cc2007-06-12 06:35:45 -04002503 btrfs_set_trans_block_group(trans, new_dir);
2504 path = btrfs_alloc_path();
2505 if (!path) {
2506 ret = -ENOMEM;
2507 goto out_fail;
2508 }
2509
2510 old_dentry->d_inode->i_nlink++;
2511 old_dir->i_ctime = old_dir->i_mtime = ctime;
2512 new_dir->i_ctime = new_dir->i_mtime = ctime;
2513 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002514
Chris Mason39279cc2007-06-12 06:35:45 -04002515 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2516 if (ret)
2517 goto out_fail;
2518
2519 if (new_inode) {
2520 new_inode->i_ctime = CURRENT_TIME;
2521 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2522 if (ret)
2523 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002524 }
2525 ret = btrfs_add_link(trans, new_dentry, old_inode);
2526 if (ret)
2527 goto out_fail;
2528
2529out_fail:
2530 btrfs_free_path(path);
2531 btrfs_end_transaction(trans, root);
2532 mutex_unlock(&root->fs_info->fs_mutex);
2533 return ret;
2534}
2535
2536static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2537 const char *symname)
2538{
2539 struct btrfs_trans_handle *trans;
2540 struct btrfs_root *root = BTRFS_I(dir)->root;
2541 struct btrfs_path *path;
2542 struct btrfs_key key;
2543 struct inode *inode;
2544 int err;
2545 int drop_inode = 0;
2546 u64 objectid;
2547 int name_len;
2548 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002549 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002550 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002551 struct extent_buffer *leaf;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002552 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002553
2554 name_len = strlen(symname) + 1;
2555 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2556 return -ENAMETOOLONG;
2557 mutex_lock(&root->fs_info->fs_mutex);
2558 trans = btrfs_start_transaction(root, 1);
2559 btrfs_set_trans_block_group(trans, dir);
2560
2561 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2562 if (err) {
2563 err = -ENOSPC;
2564 goto out_unlock;
2565 }
2566
2567 inode = btrfs_new_inode(trans, root, objectid,
2568 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2569 err = PTR_ERR(inode);
2570 if (IS_ERR(inode))
2571 goto out_unlock;
2572
2573 btrfs_set_trans_block_group(trans, inode);
2574 err = btrfs_add_nondir(trans, dentry, inode);
2575 if (err)
2576 drop_inode = 1;
2577 else {
2578 inode->i_mapping->a_ops = &btrfs_aops;
2579 inode->i_fop = &btrfs_file_operations;
2580 inode->i_op = &btrfs_file_inode_operations;
Chris Masona52d9a82007-08-27 16:49:44 -04002581 extent_map_tree_init(&BTRFS_I(inode)->extent_tree,
2582 inode->i_mapping, GFP_NOFS);
Chris Mason07157aa2007-08-30 08:50:51 -04002583 BTRFS_I(inode)->extent_tree.ops = &btrfs_extent_map_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002584 }
2585 dir->i_sb->s_dirt = 1;
2586 btrfs_update_inode_block_group(trans, inode);
2587 btrfs_update_inode_block_group(trans, dir);
2588 if (drop_inode)
2589 goto out_unlock;
2590
2591 path = btrfs_alloc_path();
2592 BUG_ON(!path);
2593 key.objectid = inode->i_ino;
2594 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002595 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2596 datasize = btrfs_file_extent_calc_inline_size(name_len);
2597 err = btrfs_insert_empty_item(trans, root, path, &key,
2598 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002599 if (err) {
2600 drop_inode = 1;
2601 goto out_unlock;
2602 }
Chris Mason5f39d392007-10-15 16:14:19 -04002603 leaf = path->nodes[0];
2604 ei = btrfs_item_ptr(leaf, path->slots[0],
2605 struct btrfs_file_extent_item);
2606 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2607 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002608 BTRFS_FILE_EXTENT_INLINE);
2609 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002610 write_extent_buffer(leaf, symname, ptr, name_len);
2611 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002612 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002613
Chris Mason39279cc2007-06-12 06:35:45 -04002614 inode->i_op = &btrfs_symlink_inode_operations;
2615 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2616 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002617 err = btrfs_update_inode(trans, root, inode);
2618 if (err)
2619 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002620
2621out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002622 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002623 btrfs_end_transaction(trans, root);
2624 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002625 if (drop_inode) {
2626 inode_dec_link_count(inode);
2627 iput(inode);
2628 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002629 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002630 return err;
2631}
2632
2633static struct inode_operations btrfs_dir_inode_operations = {
2634 .lookup = btrfs_lookup,
2635 .create = btrfs_create,
2636 .unlink = btrfs_unlink,
2637 .link = btrfs_link,
2638 .mkdir = btrfs_mkdir,
2639 .rmdir = btrfs_rmdir,
2640 .rename = btrfs_rename,
2641 .symlink = btrfs_symlink,
2642 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002643 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002644 .setxattr = generic_setxattr,
2645 .getxattr = generic_getxattr,
2646 .listxattr = btrfs_listxattr,
2647 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002648};
2649
2650static struct inode_operations btrfs_dir_ro_inode_operations = {
2651 .lookup = btrfs_lookup,
2652};
2653
2654static struct file_operations btrfs_dir_file_operations = {
2655 .llseek = generic_file_llseek,
2656 .read = generic_read_dir,
2657 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002658 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002659#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002660 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002661#endif
2662};
2663
Chris Mason07157aa2007-08-30 08:50:51 -04002664static struct extent_map_ops btrfs_extent_map_ops = {
2665 .fill_delalloc = run_delalloc_range,
2666 .writepage_io_hook = btrfs_writepage_io_hook,
2667 .readpage_io_hook = btrfs_readpage_io_hook,
2668 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
2669};
2670
Chris Mason39279cc2007-06-12 06:35:45 -04002671static struct address_space_operations btrfs_aops = {
2672 .readpage = btrfs_readpage,
2673 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002674 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002675 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002676 .sync_page = block_sync_page,
2677 .prepare_write = btrfs_prepare_write,
2678 .commit_write = btrfs_commit_write,
2679 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04002680 .invalidatepage = btrfs_invalidatepage,
2681 .releasepage = btrfs_releasepage,
2682 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002683};
2684
2685static struct address_space_operations btrfs_symlink_aops = {
2686 .readpage = btrfs_readpage,
2687 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002688 .invalidatepage = btrfs_invalidatepage,
2689 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002690};
2691
2692static struct inode_operations btrfs_file_inode_operations = {
2693 .truncate = btrfs_truncate,
2694 .getattr = btrfs_getattr,
2695 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002696 .setxattr = generic_setxattr,
2697 .getxattr = generic_getxattr,
2698 .listxattr = btrfs_listxattr,
2699 .removexattr = generic_removexattr,
Chris Mason39279cc2007-06-12 06:35:45 -04002700};
2701
Josef Bacik618e21d2007-07-11 10:18:17 -04002702static struct inode_operations btrfs_special_inode_operations = {
2703 .getattr = btrfs_getattr,
2704 .setattr = btrfs_setattr,
2705};
2706
Chris Mason39279cc2007-06-12 06:35:45 -04002707static struct inode_operations btrfs_symlink_inode_operations = {
2708 .readlink = generic_readlink,
2709 .follow_link = page_follow_link_light,
2710 .put_link = page_put_link,
2711};