blob: e1ef1acdb3503fda709239b482f70e54d9b19eca [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 Mason065631f2008-02-20 12:07:25 -050019#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040020#include <linux/buffer_head.h>
21#include <linux/fs.h>
22#include <linux/pagemap.h>
23#include <linux/highmem.h>
24#include <linux/time.h>
25#include <linux/init.h>
26#include <linux/string.h>
27#include <linux/smp_lock.h>
28#include <linux/backing-dev.h>
29#include <linux/mpage.h>
30#include <linux/swap.h>
31#include <linux/writeback.h>
32#include <linux/statfs.h>
33#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040035#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040037#include "ctree.h"
38#include "disk-io.h"
39#include "transaction.h"
40#include "btrfs_inode.h"
41#include "ioctl.h"
42#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040043#include "volumes.h"
Chris Mason39279cc2007-06-12 06:35:45 -040044
45struct btrfs_iget_args {
46 u64 ino;
47 struct btrfs_root *root;
48};
49
50static struct inode_operations btrfs_dir_inode_operations;
51static struct inode_operations btrfs_symlink_inode_operations;
52static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040053static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040054static struct inode_operations btrfs_file_inode_operations;
55static struct address_space_operations btrfs_aops;
56static struct address_space_operations btrfs_symlink_aops;
57static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050058static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040059
60static struct kmem_cache *btrfs_inode_cachep;
61struct kmem_cache *btrfs_trans_handle_cachep;
62struct kmem_cache *btrfs_transaction_cachep;
63struct kmem_cache *btrfs_bit_radix_cachep;
64struct kmem_cache *btrfs_path_cachep;
65
66#define S_SHIFT 12
67static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
68 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
69 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
70 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
71 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
72 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
73 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
74 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
75};
76
Chris Mason1832a6d2007-12-21 16:27:21 -050077int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
78 int for_del)
79{
80 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
81 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
82 u64 thresh;
83 int ret = 0;
84
85 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050086 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050087 else
Chris Masonf9ef6602008-01-03 09:22:38 -050088 thresh = total * 85;
89
90 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050091
92 spin_lock(&root->fs_info->delalloc_lock);
93 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
94 ret = -ENOSPC;
95 spin_unlock(&root->fs_info->delalloc_lock);
96 return ret;
97}
98
Chris Masonbe20aa92007-12-17 20:14:01 -050099static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db2b2007-08-27 16:49:44 -0400100{
101 struct btrfs_root *root = BTRFS_I(inode)->root;
102 struct btrfs_trans_handle *trans;
Chris Masonb888db2b2007-08-27 16:49:44 -0400103 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400104 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500105 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400106 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500107 u64 orig_start = start;
108 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500109 struct btrfs_key ins;
110 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -0400111
Chris Masonb888db2b2007-08-27 16:49:44 -0400112 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db2b2007-08-27 16:49:44 -0400113 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500114 btrfs_set_trans_block_group(trans, inode);
115
Chris Masondb945352007-10-15 16:15:53 -0400116 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500117 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db2b2007-08-27 16:49:44 -0400118 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400119 start, start + num_bytes, start, &alloc_hint);
Chris Masond1310b22008-01-24 16:13:08 -0500120 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400121
Chris Mason179e29e2007-11-01 11:28:41 -0400122 if (alloc_hint == EXTENT_MAP_INLINE)
123 goto out;
124
Chris Masonc59f8952007-12-17 20:14:04 -0500125 while(num_bytes > 0) {
126 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
127 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
128 root->root_key.objectid,
129 trans->transid,
130 inode->i_ino, start, 0,
131 alloc_hint, (u64)-1, &ins, 1);
132 if (ret) {
133 WARN_ON(1);
134 goto out;
135 }
136 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
137 start, ins.objectid, ins.offset,
138 ins.offset);
Chris Mason90692182008-02-08 13:49:28 -0500139 inode->i_blocks += ins.offset >> 9;
Chris Mason5f564062008-01-22 16:47:59 -0500140 btrfs_check_file(root, inode);
Chris Masonc59f8952007-12-17 20:14:04 -0500141 num_bytes -= cur_alloc_size;
142 alloc_hint = ins.objectid + ins.offset;
143 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400144 }
Chris Masond1310b22008-01-24 16:13:08 -0500145 btrfs_drop_extent_cache(inode, orig_start,
146 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500147 btrfs_add_ordered_inode(inode);
Chris Mason90692182008-02-08 13:49:28 -0500148 btrfs_update_inode(trans, root, inode);
Chris Masonb888db2b2007-08-27 16:49:44 -0400149out:
150 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500151 return ret;
152}
153
154static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
155{
156 u64 extent_start;
157 u64 extent_end;
158 u64 bytenr;
159 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500160 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500161 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500162 struct btrfs_root *root = BTRFS_I(inode)->root;
163 struct extent_buffer *leaf;
164 int found_type;
165 struct btrfs_path *path;
166 struct btrfs_file_extent_item *item;
167 int ret;
168 int err;
169 struct btrfs_key found_key;
170
Chris Masonc31f8832008-01-08 15:46:31 -0500171 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500172 path = btrfs_alloc_path();
173 BUG_ON(!path);
174again:
175 ret = btrfs_lookup_file_extent(NULL, root, path,
176 inode->i_ino, start, 0);
177 if (ret < 0) {
178 btrfs_free_path(path);
179 return ret;
180 }
181
182 cow_end = end;
183 if (ret != 0) {
184 if (path->slots[0] == 0)
185 goto not_found;
186 path->slots[0]--;
187 }
188
189 leaf = path->nodes[0];
190 item = btrfs_item_ptr(leaf, path->slots[0],
191 struct btrfs_file_extent_item);
192
193 /* are we inside the extent that was found? */
194 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
195 found_type = btrfs_key_type(&found_key);
196 if (found_key.objectid != inode->i_ino ||
197 found_type != BTRFS_EXTENT_DATA_KEY) {
198 goto not_found;
199 }
200
201 found_type = btrfs_file_extent_type(leaf, item);
202 extent_start = found_key.offset;
203 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500204 u64 extent_num_bytes;
205
206 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
207 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500208 err = 0;
209
Chris Mason1832a6d2007-12-21 16:27:21 -0500210 if (loops && start != extent_start)
211 goto not_found;
212
Chris Masonbe20aa92007-12-17 20:14:01 -0500213 if (start < extent_start || start >= extent_end)
214 goto not_found;
215
216 cow_end = min(end, extent_end - 1);
217 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
218 if (bytenr == 0)
219 goto not_found;
220
Chris Masonc31f8832008-01-08 15:46:31 -0500221 /*
222 * we may be called by the resizer, make sure we're inside
223 * the limits of the FS
224 */
225 if (bytenr + extent_num_bytes > total_fs_bytes)
226 goto not_found;
227
Chris Masonbe20aa92007-12-17 20:14:01 -0500228 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
229 goto not_found;
230 }
231
232 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500233 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500234 goto not_found;
235 }
236loop:
237 if (start > end) {
238 btrfs_free_path(path);
239 return 0;
240 }
241 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500242 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500243 goto again;
244
245not_found:
246 cow_file_range(inode, start, cow_end);
247 start = cow_end + 1;
248 goto loop;
249}
250
251static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
252{
253 struct btrfs_root *root = BTRFS_I(inode)->root;
254 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500255 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500256 if (btrfs_test_opt(root, NODATACOW) ||
257 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500258 ret = run_delalloc_nocow(inode, start, end);
259 else
260 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500261
Chris Masonb888db2b2007-08-27 16:49:44 -0400262 mutex_unlock(&root->fs_info->fs_mutex);
263 return ret;
264}
265
Chris Mason291d6732008-01-29 15:55:23 -0500266int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500267 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500268{
Chris Masonb0c68f82008-01-31 11:05:37 -0500269 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500270 struct btrfs_root *root = BTRFS_I(inode)->root;
271 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -0500272 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500273 root->fs_info->delalloc_bytes += end - start + 1;
274 spin_unlock(&root->fs_info->delalloc_lock);
275 }
276 return 0;
277}
278
279int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500280 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500281{
Chris Masonb0c68f82008-01-31 11:05:37 -0500282 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500283 struct btrfs_root *root = BTRFS_I(inode)->root;
284 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -0500285 if (end - start + 1 > root->fs_info->delalloc_bytes) {
286 printk("warning: delalloc account %Lu %Lu\n",
287 end - start + 1, root->fs_info->delalloc_bytes);
288 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500289 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500290 } else {
291 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500292 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500293 }
Chris Mason291d6732008-01-29 15:55:23 -0500294 spin_unlock(&root->fs_info->delalloc_lock);
295 }
296 return 0;
297}
298
Chris Mason239b14b2008-03-24 15:02:07 -0400299int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
300 size_t size, struct bio *bio)
301{
302 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
303 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400304 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400305 u64 length = 0;
306 u64 map_length;
307 struct bio_vec *bvec;
308 int i;
309 int ret;
310
311 bio_for_each_segment(bvec, bio, i) {
312 length += bvec->bv_len;
313 }
314 map_tree = &root->fs_info->mapping_tree;
315 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400316 ret = btrfs_map_block(map_tree, READ, logical,
317 &map_length, NULL);
318
Chris Mason239b14b2008-03-24 15:02:07 -0400319 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400320 return 1;
321 }
322 return 0;
323}
324
Chris Mason0b86a832008-03-24 15:01:56 -0400325int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
Chris Mason065631f2008-02-20 12:07:25 -0500326{
Chris Mason065631f2008-02-20 12:07:25 -0500327 struct btrfs_root *root = BTRFS_I(inode)->root;
328 struct btrfs_trans_handle *trans;
329 int ret = 0;
330
Chris Mason22c59942008-04-09 16:28:12 -0400331 if (!(rw & (1 << BIO_RW))) {
332 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
333 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400334 goto mapit;
335 }
Chris Mason065631f2008-02-20 12:07:25 -0500336
337 if (btrfs_test_opt(root, NODATASUM) ||
Chris Mason0b86a832008-03-24 15:01:56 -0400338 btrfs_test_flag(inode, NODATASUM)) {
339 goto mapit;
340 }
Chris Mason065631f2008-02-20 12:07:25 -0500341
342 mutex_lock(&root->fs_info->fs_mutex);
343 trans = btrfs_start_transaction(root, 1);
344 btrfs_set_trans_block_group(trans, inode);
345 btrfs_csum_file_blocks(trans, root, inode, bio);
346 ret = btrfs_end_transaction(trans, root);
347 BUG_ON(ret);
348 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -0400349mapit:
350 return btrfs_map_bio(root, rw, bio);
Chris Mason065631f2008-02-20 12:07:25 -0500351}
Chris Mason6885f302008-02-20 16:11:05 -0500352
Chris Mason07157aa2007-08-30 08:50:51 -0400353int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
354{
355 int ret = 0;
356 struct inode *inode = page->mapping->host;
357 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500358 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400359 struct btrfs_csum_item *item;
360 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400361 u32 csum;
Yanb98b6762008-01-08 15:54:37 -0500362 if (btrfs_test_opt(root, NODATASUM) ||
363 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500364 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400365 mutex_lock(&root->fs_info->fs_mutex);
366 path = btrfs_alloc_path();
367 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
368 if (IS_ERR(item)) {
369 ret = PTR_ERR(item);
370 /* a csum that isn't present is a preallocated region. */
371 if (ret == -ENOENT || ret == -EFBIG)
372 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400373 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500374 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400375 goto out;
376 }
Chris Masonff79f812007-10-15 16:22:25 -0400377 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
378 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500379 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400380out:
381 if (path)
382 btrfs_free_path(path);
383 mutex_unlock(&root->fs_info->fs_mutex);
384 return ret;
385}
386
Chris Mason70dec802008-01-29 09:59:12 -0500387int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
388 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400389{
Chris Mason35ebb932007-10-30 16:56:53 -0400390 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400391 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500392 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400393 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500394 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400395 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400396 struct btrfs_root *root = BTRFS_I(inode)->root;
397 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400398 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500399
Yanb98b6762008-01-08 15:54:37 -0500400 if (btrfs_test_opt(root, NODATASUM) ||
401 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500402 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500403 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500404 private = state->private;
405 ret = 0;
406 } else {
407 ret = get_state_private(io_tree, start, &private);
408 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400409 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400410 kaddr = kmap_atomic(page, KM_IRQ0);
411 if (ret) {
412 goto zeroit;
413 }
Chris Masonff79f812007-10-15 16:22:25 -0400414 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
415 btrfs_csum_final(csum, (char *)&csum);
416 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400417 goto zeroit;
418 }
419 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400420 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400421 return 0;
422
423zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500424 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
425 page->mapping->host->i_ino, (unsigned long long)start, csum,
426 private);
Chris Masondb945352007-10-15 16:15:53 -0400427 memset(kaddr + offset, 1, end - start + 1);
428 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400429 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400430 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400431 return 0;
432}
Chris Masonb888db2b2007-08-27 16:49:44 -0400433
Chris Mason39279cc2007-06-12 06:35:45 -0400434void btrfs_read_locked_inode(struct inode *inode)
435{
436 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400437 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400438 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400439 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400440 struct btrfs_root *root = BTRFS_I(inode)->root;
441 struct btrfs_key location;
442 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400443 u32 rdev;
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 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400449 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500450
Chris Mason39279cc2007-06-12 06:35:45 -0400451 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400452 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400453 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400454
Chris Mason5f39d392007-10-15 16:14:19 -0400455 leaf = path->nodes[0];
456 inode_item = btrfs_item_ptr(leaf, path->slots[0],
457 struct btrfs_inode_item);
458
459 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
460 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
461 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
462 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
463 inode->i_size = btrfs_inode_size(leaf, inode_item);
464
465 tspec = btrfs_inode_atime(inode_item);
466 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
467 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
468
469 tspec = btrfs_inode_mtime(inode_item);
470 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
471 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
472
473 tspec = btrfs_inode_ctime(inode_item);
474 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
475 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
476
477 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
478 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400479 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400480 rdev = btrfs_inode_rdev(leaf, inode_item);
481
482 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400483 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
484 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500485 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500486 if (!BTRFS_I(inode)->block_group) {
487 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400488 NULL, 0,
489 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500490 }
Chris Mason39279cc2007-06-12 06:35:45 -0400491 btrfs_free_path(path);
492 inode_item = NULL;
493
494 mutex_unlock(&root->fs_info->fs_mutex);
495
496 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400497 case S_IFREG:
498 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400499 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500500 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400501 inode->i_fop = &btrfs_file_operations;
502 inode->i_op = &btrfs_file_inode_operations;
503 break;
504 case S_IFDIR:
505 inode->i_fop = &btrfs_dir_file_operations;
506 if (root == root->fs_info->tree_root)
507 inode->i_op = &btrfs_dir_ro_inode_operations;
508 else
509 inode->i_op = &btrfs_dir_inode_operations;
510 break;
511 case S_IFLNK:
512 inode->i_op = &btrfs_symlink_inode_operations;
513 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400514 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400515 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400516 default:
517 init_special_inode(inode, inode->i_mode, rdev);
518 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400519 }
520 return;
521
522make_bad:
523 btrfs_release_path(root, path);
524 btrfs_free_path(path);
525 mutex_unlock(&root->fs_info->fs_mutex);
526 make_bad_inode(inode);
527}
528
Chris Mason5f39d392007-10-15 16:14:19 -0400529static void fill_inode_item(struct extent_buffer *leaf,
530 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400531 struct inode *inode)
532{
Chris Mason5f39d392007-10-15 16:14:19 -0400533 btrfs_set_inode_uid(leaf, item, inode->i_uid);
534 btrfs_set_inode_gid(leaf, item, inode->i_gid);
535 btrfs_set_inode_size(leaf, item, inode->i_size);
536 btrfs_set_inode_mode(leaf, item, inode->i_mode);
537 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
538
539 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
540 inode->i_atime.tv_sec);
541 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
542 inode->i_atime.tv_nsec);
543
544 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
545 inode->i_mtime.tv_sec);
546 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
547 inode->i_mtime.tv_nsec);
548
549 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
550 inode->i_ctime.tv_sec);
551 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
552 inode->i_ctime.tv_nsec);
553
554 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
555 btrfs_set_inode_generation(leaf, item, inode->i_generation);
556 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500557 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400558 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400559 BTRFS_I(inode)->block_group->key.objectid);
560}
561
Chris Masona52d9a82007-08-27 16:49:44 -0400562int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400563 struct btrfs_root *root,
564 struct inode *inode)
565{
566 struct btrfs_inode_item *inode_item;
567 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400568 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400569 int ret;
570
571 path = btrfs_alloc_path();
572 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400573 ret = btrfs_lookup_inode(trans, root, path,
574 &BTRFS_I(inode)->location, 1);
575 if (ret) {
576 if (ret > 0)
577 ret = -ENOENT;
578 goto failed;
579 }
580
Chris Mason5f39d392007-10-15 16:14:19 -0400581 leaf = path->nodes[0];
582 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400583 struct btrfs_inode_item);
584
Chris Mason5f39d392007-10-15 16:14:19 -0400585 fill_inode_item(leaf, inode_item, inode);
586 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400587 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400588 ret = 0;
589failed:
590 btrfs_release_path(root, path);
591 btrfs_free_path(path);
592 return ret;
593}
594
595
596static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
597 struct btrfs_root *root,
598 struct inode *dir,
599 struct dentry *dentry)
600{
601 struct btrfs_path *path;
602 const char *name = dentry->d_name.name;
603 int name_len = dentry->d_name.len;
604 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400605 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400606 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400607 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400608
609 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400610 if (!path) {
611 ret = -ENOMEM;
612 goto err;
613 }
614
Chris Mason39279cc2007-06-12 06:35:45 -0400615 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
616 name, name_len, -1);
617 if (IS_ERR(di)) {
618 ret = PTR_ERR(di);
619 goto err;
620 }
621 if (!di) {
622 ret = -ENOENT;
623 goto err;
624 }
Chris Mason5f39d392007-10-15 16:14:19 -0400625 leaf = path->nodes[0];
626 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400627 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400628 if (ret)
629 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400630 btrfs_release_path(root, path);
631
632 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400633 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400634 if (IS_ERR(di)) {
635 ret = PTR_ERR(di);
636 goto err;
637 }
638 if (!di) {
639 ret = -ENOENT;
640 goto err;
641 }
642 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400643
644 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500645 ret = btrfs_del_inode_ref(trans, root, name, name_len,
646 dentry->d_inode->i_ino,
647 dentry->d_parent->d_inode->i_ino);
648 if (ret) {
649 printk("failed to delete reference to %.*s, "
650 "inode %lu parent %lu\n", name_len, name,
651 dentry->d_inode->i_ino,
652 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500653 }
Chris Mason39279cc2007-06-12 06:35:45 -0400654err:
655 btrfs_free_path(path);
656 if (!ret) {
657 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400658 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400659 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500660#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
661 dentry->d_inode->i_nlink--;
662#else
Chris Mason39279cc2007-06-12 06:35:45 -0400663 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500664#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400665 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400666 dir->i_sb->s_dirt = 1;
667 }
668 return ret;
669}
670
671static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
672{
673 struct btrfs_root *root;
674 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500675 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400676 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500677 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400678
679 root = BTRFS_I(dir)->root;
680 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500681
682 ret = btrfs_check_free_space(root, 1, 1);
683 if (ret)
684 goto fail;
685
Chris Mason39279cc2007-06-12 06:35:45 -0400686 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400687
Chris Mason39279cc2007-06-12 06:35:45 -0400688 btrfs_set_trans_block_group(trans, dir);
689 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400690 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400691
Chris Mason2da98f02008-01-16 11:44:43 -0500692 if (inode->i_nlink == 0) {
693 int found;
694 /* if the inode isn't linked anywhere,
695 * we don't need to worry about
696 * data=ordered
697 */
698 found = btrfs_del_ordered_inode(inode);
699 if (found == 1) {
700 atomic_dec(&inode->i_count);
701 }
702 }
703
Chris Mason39279cc2007-06-12 06:35:45 -0400704 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500705fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400706 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400707 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500708 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400709 return ret;
710}
711
712static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
713{
714 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500715 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400716 int ret;
717 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400718 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500719 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400720
Yan134d4512007-10-25 15:49:25 -0400721 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
722 return -ENOTEMPTY;
723
Chris Mason39279cc2007-06-12 06:35:45 -0400724 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500725 ret = btrfs_check_free_space(root, 1, 1);
726 if (ret)
727 goto fail;
728
Chris Mason39279cc2007-06-12 06:35:45 -0400729 trans = btrfs_start_transaction(root, 1);
730 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400731
732 /* now the directory is empty */
733 err = btrfs_unlink_trans(trans, root, dir, dentry);
734 if (!err) {
735 inode->i_size = 0;
736 }
Chris Mason39544012007-12-12 14:38:19 -0500737
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400738 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400739 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500740fail:
Yan134d4512007-10-25 15:49:25 -0400741 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400742 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500743 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500744
Chris Mason39279cc2007-06-12 06:35:45 -0400745 if (ret && !err)
746 err = ret;
747 return err;
748}
749
Chris Mason39279cc2007-06-12 06:35:45 -0400750/*
Chris Mason39279cc2007-06-12 06:35:45 -0400751 * this can truncate away extent items, csum items and directory items.
752 * It starts at a high offset and removes keys until it can't find
753 * any higher than i_size.
754 *
755 * csum items that cross the new i_size are truncated to the new size
756 * as well.
757 */
758static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
759 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500760 struct inode *inode,
761 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400762{
763 int ret;
764 struct btrfs_path *path;
765 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400766 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400767 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400768 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400769 struct btrfs_file_extent_item *fi;
770 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400771 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400772 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500773 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500774 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400775 int found_extent;
776 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500777 int pending_del_nr = 0;
778 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400779 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400780
Chris Masona52d9a82007-08-27 16:49:44 -0400781 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400782 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400783 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400784 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400785
Chris Mason39279cc2007-06-12 06:35:45 -0400786 /* FIXME, add redo link to tree so we don't leak on crash */
787 key.objectid = inode->i_ino;
788 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400789 key.type = (u8)-1;
790
Chris Mason85e21ba2008-01-29 15:11:36 -0500791 btrfs_init_path(path);
792search_again:
793 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
794 if (ret < 0) {
795 goto error;
796 }
797 if (ret > 0) {
798 BUG_ON(path->slots[0] == 0);
799 path->slots[0]--;
800 }
801
Chris Mason39279cc2007-06-12 06:35:45 -0400802 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400803 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400804 leaf = path->nodes[0];
805 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
806 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400807
Chris Mason5f39d392007-10-15 16:14:19 -0400808 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400809 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400810
Chris Mason85e21ba2008-01-29 15:11:36 -0500811 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400812 break;
813
Chris Mason5f39d392007-10-15 16:14:19 -0400814 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400815 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400816 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400817 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400818 extent_type = btrfs_file_extent_type(leaf, fi);
819 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400820 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400821 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400822 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
823 struct btrfs_item *item = btrfs_item_nr(leaf,
824 path->slots[0]);
825 item_end += btrfs_file_extent_inline_len(leaf,
826 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400827 }
Yan008630c2007-11-07 13:31:09 -0500828 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400829 }
830 if (found_type == BTRFS_CSUM_ITEM_KEY) {
831 ret = btrfs_csum_truncate(trans, root, path,
832 inode->i_size);
833 BUG_ON(ret);
834 }
Yan008630c2007-11-07 13:31:09 -0500835 if (item_end < inode->i_size) {
Chris Masonb888db2b2007-08-27 16:49:44 -0400836 if (found_type == BTRFS_DIR_ITEM_KEY) {
837 found_type = BTRFS_INODE_ITEM_KEY;
838 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
839 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500840 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
841 found_type = BTRFS_XATTR_ITEM_KEY;
842 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
843 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db2b2007-08-27 16:49:44 -0400844 } else if (found_type) {
845 found_type--;
846 } else {
847 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400848 }
Yana61721d2007-09-17 11:08:38 -0400849 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500850 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400851 }
Chris Mason5f39d392007-10-15 16:14:19 -0400852 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400853 del_item = 1;
854 else
855 del_item = 0;
856 found_extent = 0;
857
858 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400859 if (found_type != BTRFS_EXTENT_DATA_KEY)
860 goto delete;
861
862 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400863 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400864 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400865 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400866 u64 orig_num_bytes =
867 btrfs_file_extent_num_bytes(leaf, fi);
868 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400869 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -0500870 extent_num_bytes = extent_num_bytes &
871 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400872 btrfs_set_file_extent_num_bytes(leaf, fi,
873 extent_num_bytes);
874 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -0500875 extent_num_bytes);
876 if (extent_start != 0)
877 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -0400878 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400879 } else {
Chris Masondb945352007-10-15 16:15:53 -0400880 extent_num_bytes =
881 btrfs_file_extent_disk_num_bytes(leaf,
882 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400883 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -0500884 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400885 if (extent_start != 0) {
886 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -0500887 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -0400888 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500889 root_gen = btrfs_header_generation(leaf);
890 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400891 }
Chris Mason90692182008-02-08 13:49:28 -0500892 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
893 if (!del_item) {
894 u32 newsize = inode->i_size - found_key.offset;
895 dec_i_blocks(inode, item_end + 1 -
896 found_key.offset - newsize);
897 newsize =
898 btrfs_file_extent_calc_inline_size(newsize);
899 ret = btrfs_truncate_item(trans, root, path,
900 newsize, 1);
901 BUG_ON(ret);
902 } else {
903 dec_i_blocks(inode, item_end + 1 -
904 found_key.offset);
905 }
Chris Mason39279cc2007-06-12 06:35:45 -0400906 }
Chris Mason179e29e2007-11-01 11:28:41 -0400907delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400908 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -0500909 if (!pending_del_nr) {
910 /* no pending yet, add ourselves */
911 pending_del_slot = path->slots[0];
912 pending_del_nr = 1;
913 } else if (pending_del_nr &&
914 path->slots[0] + 1 == pending_del_slot) {
915 /* hop on the pending chunk */
916 pending_del_nr++;
917 pending_del_slot = path->slots[0];
918 } else {
919 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
920 }
Chris Mason39279cc2007-06-12 06:35:45 -0400921 } else {
922 break;
923 }
Chris Mason39279cc2007-06-12 06:35:45 -0400924 if (found_extent) {
925 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500926 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500927 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500928 root_gen, inode->i_ino,
929 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400930 BUG_ON(ret);
931 }
Chris Mason85e21ba2008-01-29 15:11:36 -0500932next:
933 if (path->slots[0] == 0) {
934 if (pending_del_nr)
935 goto del_pending;
936 btrfs_release_path(root, path);
937 goto search_again;
938 }
939
940 path->slots[0]--;
941 if (pending_del_nr &&
942 path->slots[0] + 1 != pending_del_slot) {
943 struct btrfs_key debug;
944del_pending:
945 btrfs_item_key_to_cpu(path->nodes[0], &debug,
946 pending_del_slot);
947 ret = btrfs_del_items(trans, root, path,
948 pending_del_slot,
949 pending_del_nr);
950 BUG_ON(ret);
951 pending_del_nr = 0;
952 btrfs_release_path(root, path);
953 goto search_again;
954 }
Chris Mason39279cc2007-06-12 06:35:45 -0400955 }
956 ret = 0;
957error:
Chris Mason85e21ba2008-01-29 15:11:36 -0500958 if (pending_del_nr) {
959 ret = btrfs_del_items(trans, root, path, pending_del_slot,
960 pending_del_nr);
961 }
Chris Mason39279cc2007-06-12 06:35:45 -0400962 btrfs_release_path(root, path);
963 btrfs_free_path(path);
964 inode->i_sb->s_dirt = 1;
965 return ret;
966}
967
Chris Masonb888db2b2007-08-27 16:49:44 -0400968static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400969 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400970{
Chris Mason39279cc2007-06-12 06:35:45 -0400971 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -0500972 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400973 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db2b2007-08-27 16:49:44 -0400974 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500975 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400976
Chris Mason190662b2007-12-18 16:25:45 -0500977 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400978 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400979
Chris Masond1310b22008-01-24 16:13:08 -0500980 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500981 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db2b2007-08-27 16:49:44 -0400982 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500983
Chris Masona52d9a82007-08-27 16:49:44 -0400984 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db2b2007-08-27 16:49:44 -0400985 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400986 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
987 flush_dcache_page(page);
Chris Masonb888db2b2007-08-27 16:49:44 -0400988 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400989 }
Chris Masonb888db2b2007-08-27 16:49:44 -0400990 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -0500991 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400992
Chris Masona52d9a82007-08-27 16:49:44 -0400993 return ret;
994}
995
996/*
997 * taken from block_truncate_page, but does cow as it zeros out
998 * any bytes left in the last page in the file.
999 */
1000static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1001{
1002 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001003 struct btrfs_root *root = BTRFS_I(inode)->root;
1004 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001005 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1006 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1007 struct page *page;
1008 int ret = 0;
1009 u64 page_start;
1010
1011 if ((offset & (blocksize - 1)) == 0)
1012 goto out;
1013
1014 ret = -ENOMEM;
1015 page = grab_cache_page(mapping, index);
1016 if (!page)
1017 goto out;
1018 if (!PageUptodate(page)) {
1019 ret = btrfs_readpage(NULL, page);
1020 lock_page(page);
1021 if (!PageUptodate(page)) {
1022 ret = -EIO;
1023 goto out;
1024 }
1025 }
Chris Mason35ebb932007-10-30 16:56:53 -04001026 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001027
Chris Masonb888db2b2007-08-27 16:49:44 -04001028 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001029
Chris Mason39279cc2007-06-12 06:35:45 -04001030 unlock_page(page);
1031 page_cache_release(page);
1032out:
1033 return ret;
1034}
1035
1036static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1037{
1038 struct inode *inode = dentry->d_inode;
1039 int err;
1040
1041 err = inode_change_ok(inode, attr);
1042 if (err)
1043 return err;
1044
1045 if (S_ISREG(inode->i_mode) &&
1046 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1047 struct btrfs_trans_handle *trans;
1048 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001049 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001050
Chris Mason5f39d392007-10-15 16:14:19 -04001051 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001052 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001053 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001054 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001055 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001056
Chris Mason1b0f7c22008-01-30 14:33:02 -05001057 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001058 goto out;
1059
Chris Mason1832a6d2007-12-21 16:27:21 -05001060 mutex_lock(&root->fs_info->fs_mutex);
1061 err = btrfs_check_free_space(root, 1, 0);
1062 mutex_unlock(&root->fs_info->fs_mutex);
1063 if (err)
1064 goto fail;
1065
Chris Mason39279cc2007-06-12 06:35:45 -04001066 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1067
Chris Mason1b0f7c22008-01-30 14:33:02 -05001068 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001069 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001070
1071 mutex_lock(&root->fs_info->fs_mutex);
1072 trans = btrfs_start_transaction(root, 1);
1073 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001074 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001075 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001076 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001077
Chris Mason179e29e2007-11-01 11:28:41 -04001078 if (alloc_hint != EXTENT_MAP_INLINE) {
1079 err = btrfs_insert_file_extent(trans, root,
1080 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001081 hole_start, 0, 0,
1082 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001083 btrfs_drop_extent_cache(inode, hole_start,
1084 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001085 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001086 }
Chris Mason39279cc2007-06-12 06:35:45 -04001087 btrfs_end_transaction(trans, root);
1088 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001089 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001090 if (err)
1091 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001092 }
1093out:
1094 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001095fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001096 return err;
1097}
Chris Mason61295eb2008-01-14 16:24:38 -05001098
Chris Mason2da98f02008-01-16 11:44:43 -05001099void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001100{
Chris Mason2da98f02008-01-16 11:44:43 -05001101 int ret;
1102
1103 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001104 return;
1105 }
Chris Mason2da98f02008-01-16 11:44:43 -05001106
1107 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1108 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1109 return;
1110
1111 ret = btrfs_del_ordered_inode(inode);
1112 if (ret == 1) {
1113 atomic_dec(&inode->i_count);
1114 }
Chris Mason61295eb2008-01-14 16:24:38 -05001115}
1116
Chris Mason39279cc2007-06-12 06:35:45 -04001117void btrfs_delete_inode(struct inode *inode)
1118{
1119 struct btrfs_trans_handle *trans;
1120 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001121 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001122 int ret;
1123
1124 truncate_inode_pages(&inode->i_data, 0);
1125 if (is_bad_inode(inode)) {
1126 goto no_delete;
1127 }
Chris Mason5f39d392007-10-15 16:14:19 -04001128
Chris Mason39279cc2007-06-12 06:35:45 -04001129 inode->i_size = 0;
1130 mutex_lock(&root->fs_info->fs_mutex);
1131 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001132
Chris Mason39279cc2007-06-12 06:35:45 -04001133 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001134 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001135 if (ret)
1136 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001137
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001138 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001139 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001140
Chris Mason39279cc2007-06-12 06:35:45 -04001141 btrfs_end_transaction(trans, root);
1142 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001143 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001144 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001145 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001146
1147no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001148 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001149 btrfs_end_transaction(trans, root);
1150 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001151 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001152 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001153no_delete:
1154 clear_inode(inode);
1155}
1156
1157/*
1158 * this returns the key found in the dir entry in the location pointer.
1159 * If no dir entries were found, location->objectid is 0.
1160 */
1161static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1162 struct btrfs_key *location)
1163{
1164 const char *name = dentry->d_name.name;
1165 int namelen = dentry->d_name.len;
1166 struct btrfs_dir_item *di;
1167 struct btrfs_path *path;
1168 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001169 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001170
Chris Mason39544012007-12-12 14:38:19 -05001171 if (namelen == 1 && strcmp(name, ".") == 0) {
1172 location->objectid = dir->i_ino;
1173 location->type = BTRFS_INODE_ITEM_KEY;
1174 location->offset = 0;
1175 return 0;
1176 }
Chris Mason39279cc2007-06-12 06:35:45 -04001177 path = btrfs_alloc_path();
1178 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001179
Chris Mason7a720532007-12-13 09:06:59 -05001180 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001181 struct btrfs_key key;
1182 struct extent_buffer *leaf;
1183 u32 nritems;
1184 int slot;
1185
1186 key.objectid = dir->i_ino;
1187 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1188 key.offset = 0;
1189 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1190 BUG_ON(ret == 0);
1191 ret = 0;
1192
1193 leaf = path->nodes[0];
1194 slot = path->slots[0];
1195 nritems = btrfs_header_nritems(leaf);
1196 if (slot >= nritems)
1197 goto out_err;
1198
1199 btrfs_item_key_to_cpu(leaf, &key, slot);
1200 if (key.objectid != dir->i_ino ||
1201 key.type != BTRFS_INODE_REF_KEY) {
1202 goto out_err;
1203 }
1204 location->objectid = key.offset;
1205 location->type = BTRFS_INODE_ITEM_KEY;
1206 location->offset = 0;
1207 goto out;
1208 }
1209
Chris Mason39279cc2007-06-12 06:35:45 -04001210 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1211 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001212 if (IS_ERR(di))
1213 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001214 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001215 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001216 }
Chris Mason5f39d392007-10-15 16:14:19 -04001217 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001218out:
Chris Mason39279cc2007-06-12 06:35:45 -04001219 btrfs_free_path(path);
1220 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001221out_err:
1222 location->objectid = 0;
1223 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001224}
1225
1226/*
1227 * when we hit a tree root in a directory, the btrfs part of the inode
1228 * needs to be changed to reflect the root directory of the tree root. This
1229 * is kind of like crossing a mount point.
1230 */
1231static int fixup_tree_root_location(struct btrfs_root *root,
1232 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001233 struct btrfs_root **sub_root,
1234 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001235{
1236 struct btrfs_path *path;
1237 struct btrfs_root_item *ri;
1238
1239 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1240 return 0;
1241 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1242 return 0;
1243
1244 path = btrfs_alloc_path();
1245 BUG_ON(!path);
1246 mutex_lock(&root->fs_info->fs_mutex);
1247
Josef Bacik58176a92007-08-29 15:47:34 -04001248 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1249 dentry->d_name.name,
1250 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001251 if (IS_ERR(*sub_root))
1252 return PTR_ERR(*sub_root);
1253
1254 ri = &(*sub_root)->root_item;
1255 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001256 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1257 location->offset = 0;
1258
1259 btrfs_free_path(path);
1260 mutex_unlock(&root->fs_info->fs_mutex);
1261 return 0;
1262}
1263
1264static int btrfs_init_locked_inode(struct inode *inode, void *p)
1265{
1266 struct btrfs_iget_args *args = p;
1267 inode->i_ino = args->ino;
1268 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001269 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001270 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1271 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db2b2007-08-27 16:49:44 -04001272 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001273 return 0;
1274}
1275
1276static int btrfs_find_actor(struct inode *inode, void *opaque)
1277{
1278 struct btrfs_iget_args *args = opaque;
1279 return (args->ino == inode->i_ino &&
1280 args->root == BTRFS_I(inode)->root);
1281}
1282
Chris Masondc17ff82008-01-08 15:46:30 -05001283struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1284 u64 root_objectid)
1285{
1286 struct btrfs_iget_args args;
1287 args.ino = objectid;
1288 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1289
1290 if (!args.root)
1291 return NULL;
1292
1293 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1294}
1295
Chris Mason39279cc2007-06-12 06:35:45 -04001296struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1297 struct btrfs_root *root)
1298{
1299 struct inode *inode;
1300 struct btrfs_iget_args args;
1301 args.ino = objectid;
1302 args.root = root;
1303
1304 inode = iget5_locked(s, objectid, btrfs_find_actor,
1305 btrfs_init_locked_inode,
1306 (void *)&args);
1307 return inode;
1308}
1309
1310static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1311 struct nameidata *nd)
1312{
1313 struct inode * inode;
1314 struct btrfs_inode *bi = BTRFS_I(dir);
1315 struct btrfs_root *root = bi->root;
1316 struct btrfs_root *sub_root = root;
1317 struct btrfs_key location;
1318 int ret;
1319
1320 if (dentry->d_name.len > BTRFS_NAME_LEN)
1321 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001322
Chris Mason39279cc2007-06-12 06:35:45 -04001323 mutex_lock(&root->fs_info->fs_mutex);
1324 ret = btrfs_inode_by_name(dir, dentry, &location);
1325 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001326
Chris Mason39279cc2007-06-12 06:35:45 -04001327 if (ret < 0)
1328 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001329
Chris Mason39279cc2007-06-12 06:35:45 -04001330 inode = NULL;
1331 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001332 ret = fixup_tree_root_location(root, &location, &sub_root,
1333 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001334 if (ret < 0)
1335 return ERR_PTR(ret);
1336 if (ret > 0)
1337 return ERR_PTR(-ENOENT);
1338 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1339 sub_root);
1340 if (!inode)
1341 return ERR_PTR(-EACCES);
1342 if (inode->i_state & I_NEW) {
1343 /* the inode and parent dir are two different roots */
1344 if (sub_root != root) {
1345 igrab(inode);
1346 sub_root->inode = inode;
1347 }
1348 BTRFS_I(inode)->root = sub_root;
1349 memcpy(&BTRFS_I(inode)->location, &location,
1350 sizeof(location));
1351 btrfs_read_locked_inode(inode);
1352 unlock_new_inode(inode);
1353 }
1354 }
1355 return d_splice_alias(inode, dentry);
1356}
1357
Chris Mason39279cc2007-06-12 06:35:45 -04001358static unsigned char btrfs_filetype_table[] = {
1359 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1360};
1361
1362static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1363{
Chris Mason6da6aba2007-12-18 16:15:09 -05001364 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001365 struct btrfs_root *root = BTRFS_I(inode)->root;
1366 struct btrfs_item *item;
1367 struct btrfs_dir_item *di;
1368 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001369 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001370 struct btrfs_path *path;
1371 int ret;
1372 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001373 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001374 int slot;
1375 int advance;
1376 unsigned char d_type;
1377 int over = 0;
1378 u32 di_cur;
1379 u32 di_total;
1380 u32 di_len;
1381 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001382 char tmp_name[32];
1383 char *name_ptr;
1384 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001385
1386 /* FIXME, use a real flag for deciding about the key type */
1387 if (root->fs_info->tree_root == root)
1388 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001389
Chris Mason39544012007-12-12 14:38:19 -05001390 /* special case for "." */
1391 if (filp->f_pos == 0) {
1392 over = filldir(dirent, ".", 1,
1393 1, inode->i_ino,
1394 DT_DIR);
1395 if (over)
1396 return 0;
1397 filp->f_pos = 1;
1398 }
1399
Chris Mason39279cc2007-06-12 06:35:45 -04001400 mutex_lock(&root->fs_info->fs_mutex);
1401 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001402 path = btrfs_alloc_path();
1403 path->reada = 2;
1404
1405 /* special case for .., just use the back ref */
1406 if (filp->f_pos == 1) {
1407 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1408 key.offset = 0;
1409 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1410 BUG_ON(ret == 0);
1411 leaf = path->nodes[0];
1412 slot = path->slots[0];
1413 nritems = btrfs_header_nritems(leaf);
1414 if (slot >= nritems) {
1415 btrfs_release_path(root, path);
1416 goto read_dir_items;
1417 }
1418 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1419 btrfs_release_path(root, path);
1420 if (found_key.objectid != key.objectid ||
1421 found_key.type != BTRFS_INODE_REF_KEY)
1422 goto read_dir_items;
1423 over = filldir(dirent, "..", 2,
1424 2, found_key.offset, DT_DIR);
1425 if (over)
1426 goto nopos;
1427 filp->f_pos = 2;
1428 }
1429
1430read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001431 btrfs_set_key_type(&key, key_type);
1432 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001433
Chris Mason39279cc2007-06-12 06:35:45 -04001434 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1435 if (ret < 0)
1436 goto err;
1437 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001438 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001439 leaf = path->nodes[0];
1440 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001441 slot = path->slots[0];
1442 if (advance || slot >= nritems) {
1443 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001444 ret = btrfs_next_leaf(root, path);
1445 if (ret)
1446 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001447 leaf = path->nodes[0];
1448 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001449 slot = path->slots[0];
1450 } else {
1451 slot++;
1452 path->slots[0]++;
1453 }
1454 }
1455 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001456 item = btrfs_item_nr(leaf, slot);
1457 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1458
1459 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001460 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001461 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001462 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001463 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001464 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001465
1466 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001467 advance = 1;
1468 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1469 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001470 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001471 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001472 struct btrfs_key location;
1473
1474 name_len = btrfs_dir_name_len(leaf, di);
1475 if (name_len < 32) {
1476 name_ptr = tmp_name;
1477 } else {
1478 name_ptr = kmalloc(name_len, GFP_NOFS);
1479 BUG_ON(!name_ptr);
1480 }
1481 read_extent_buffer(leaf, name_ptr,
1482 (unsigned long)(di + 1), name_len);
1483
1484 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1485 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001486 over = filldir(dirent, name_ptr, name_len,
1487 found_key.offset,
1488 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001489 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001490
1491 if (name_ptr != tmp_name)
1492 kfree(name_ptr);
1493
Chris Mason39279cc2007-06-12 06:35:45 -04001494 if (over)
1495 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001496 di_len = btrfs_dir_name_len(leaf, di) +
1497 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001498 di_cur += di_len;
1499 di = (struct btrfs_dir_item *)((char *)di + di_len);
1500 }
1501 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001502 if (key_type == BTRFS_DIR_INDEX_KEY)
1503 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1504 else
1505 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001506nopos:
1507 ret = 0;
1508err:
1509 btrfs_release_path(root, path);
1510 btrfs_free_path(path);
1511 mutex_unlock(&root->fs_info->fs_mutex);
1512 return ret;
1513}
1514
1515int btrfs_write_inode(struct inode *inode, int wait)
1516{
1517 struct btrfs_root *root = BTRFS_I(inode)->root;
1518 struct btrfs_trans_handle *trans;
1519 int ret = 0;
1520
1521 if (wait) {
1522 mutex_lock(&root->fs_info->fs_mutex);
1523 trans = btrfs_start_transaction(root, 1);
1524 btrfs_set_trans_block_group(trans, inode);
1525 ret = btrfs_commit_transaction(trans, root);
1526 mutex_unlock(&root->fs_info->fs_mutex);
1527 }
1528 return ret;
1529}
1530
1531/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001532 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001533 * inode changes. But, it is most likely to find the inode in cache.
1534 * FIXME, needs more benchmarking...there are no reasons other than performance
1535 * to keep or drop this code.
1536 */
1537void btrfs_dirty_inode(struct inode *inode)
1538{
1539 struct btrfs_root *root = BTRFS_I(inode)->root;
1540 struct btrfs_trans_handle *trans;
1541
1542 mutex_lock(&root->fs_info->fs_mutex);
1543 trans = btrfs_start_transaction(root, 1);
1544 btrfs_set_trans_block_group(trans, inode);
1545 btrfs_update_inode(trans, root, inode);
1546 btrfs_end_transaction(trans, root);
1547 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001548}
1549
1550static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1551 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001552 const char *name, int name_len,
1553 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001554 u64 objectid,
1555 struct btrfs_block_group_cache *group,
1556 int mode)
1557{
1558 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001559 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001560 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001561 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001562 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001563 struct btrfs_inode_ref *ref;
1564 struct btrfs_key key[2];
1565 u32 sizes[2];
1566 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001567 int ret;
1568 int owner;
1569
Chris Mason5f39d392007-10-15 16:14:19 -04001570 path = btrfs_alloc_path();
1571 BUG_ON(!path);
1572
Chris Mason39279cc2007-06-12 06:35:45 -04001573 inode = new_inode(root->fs_info->sb);
1574 if (!inode)
1575 return ERR_PTR(-ENOMEM);
1576
Chris Masond1310b22008-01-24 16:13:08 -05001577 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1578 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db2b2007-08-27 16:49:44 -04001579 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001580 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001581 BTRFS_I(inode)->root = root;
Chris Masonb888db2b2007-08-27 16:49:44 -04001582
Chris Mason39279cc2007-06-12 06:35:45 -04001583 if (mode & S_IFDIR)
1584 owner = 0;
1585 else
1586 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001587 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001588 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001589 if (!new_inode_group) {
1590 printk("find_block group failed\n");
1591 new_inode_group = group;
1592 }
1593 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001594 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001595
1596 key[0].objectid = objectid;
1597 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1598 key[0].offset = 0;
1599
1600 key[1].objectid = objectid;
1601 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1602 key[1].offset = ref_objectid;
1603
1604 sizes[0] = sizeof(struct btrfs_inode_item);
1605 sizes[1] = name_len + sizeof(*ref);
1606
1607 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1608 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001609 goto fail;
1610
Chris Mason9c583092008-01-29 15:15:18 -05001611 if (objectid > root->highest_inode)
1612 root->highest_inode = objectid;
1613
Chris Mason39279cc2007-06-12 06:35:45 -04001614 inode->i_uid = current->fsuid;
1615 inode->i_gid = current->fsgid;
1616 inode->i_mode = mode;
1617 inode->i_ino = objectid;
1618 inode->i_blocks = 0;
1619 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001620 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1621 struct btrfs_inode_item);
1622 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001623
1624 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1625 struct btrfs_inode_ref);
1626 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1627 ptr = (unsigned long)(ref + 1);
1628 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1629
Chris Mason5f39d392007-10-15 16:14:19 -04001630 btrfs_mark_buffer_dirty(path->nodes[0]);
1631 btrfs_free_path(path);
1632
Chris Mason39279cc2007-06-12 06:35:45 -04001633 location = &BTRFS_I(inode)->location;
1634 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001635 location->offset = 0;
1636 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1637
Chris Mason39279cc2007-06-12 06:35:45 -04001638 insert_inode_hash(inode);
1639 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001640fail:
1641 btrfs_free_path(path);
1642 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001643}
1644
1645static inline u8 btrfs_inode_type(struct inode *inode)
1646{
1647 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1648}
1649
1650static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001651 struct dentry *dentry, struct inode *inode,
1652 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001653{
1654 int ret;
1655 struct btrfs_key key;
1656 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001657 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001658
Chris Mason39279cc2007-06-12 06:35:45 -04001659 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001660 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1661 key.offset = 0;
1662
1663 ret = btrfs_insert_dir_item(trans, root,
1664 dentry->d_name.name, dentry->d_name.len,
1665 dentry->d_parent->d_inode->i_ino,
1666 &key, btrfs_inode_type(inode));
1667 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001668 if (add_backref) {
1669 ret = btrfs_insert_inode_ref(trans, root,
1670 dentry->d_name.name,
1671 dentry->d_name.len,
1672 inode->i_ino,
1673 dentry->d_parent->d_inode->i_ino);
1674 }
Chris Mason79c44582007-06-25 10:09:33 -04001675 parent_inode = dentry->d_parent->d_inode;
1676 parent_inode->i_size += dentry->d_name.len * 2;
1677 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001678 ret = btrfs_update_inode(trans, root,
1679 dentry->d_parent->d_inode);
1680 }
1681 return ret;
1682}
1683
1684static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001685 struct dentry *dentry, struct inode *inode,
1686 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001687{
Chris Mason9c583092008-01-29 15:15:18 -05001688 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001689 if (!err) {
1690 d_instantiate(dentry, inode);
1691 return 0;
1692 }
1693 if (err > 0)
1694 err = -EEXIST;
1695 return err;
1696}
1697
Josef Bacik618e21d2007-07-11 10:18:17 -04001698static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1699 int mode, dev_t rdev)
1700{
1701 struct btrfs_trans_handle *trans;
1702 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001703 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001704 int err;
1705 int drop_inode = 0;
1706 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001707 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001708
1709 if (!new_valid_dev(rdev))
1710 return -EINVAL;
1711
1712 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001713 err = btrfs_check_free_space(root, 1, 0);
1714 if (err)
1715 goto fail;
1716
Josef Bacik618e21d2007-07-11 10:18:17 -04001717 trans = btrfs_start_transaction(root, 1);
1718 btrfs_set_trans_block_group(trans, dir);
1719
1720 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1721 if (err) {
1722 err = -ENOSPC;
1723 goto out_unlock;
1724 }
1725
Chris Mason9c583092008-01-29 15:15:18 -05001726 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1727 dentry->d_name.len,
1728 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001729 BTRFS_I(dir)->block_group, mode);
1730 err = PTR_ERR(inode);
1731 if (IS_ERR(inode))
1732 goto out_unlock;
1733
1734 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001735 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001736 if (err)
1737 drop_inode = 1;
1738 else {
1739 inode->i_op = &btrfs_special_inode_operations;
1740 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001741 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001742 }
1743 dir->i_sb->s_dirt = 1;
1744 btrfs_update_inode_block_group(trans, inode);
1745 btrfs_update_inode_block_group(trans, dir);
1746out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001747 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001748 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001749fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001750 mutex_unlock(&root->fs_info->fs_mutex);
1751
1752 if (drop_inode) {
1753 inode_dec_link_count(inode);
1754 iput(inode);
1755 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001756 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001757 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001758 return err;
1759}
1760
Chris Mason39279cc2007-06-12 06:35:45 -04001761static int btrfs_create(struct inode *dir, struct dentry *dentry,
1762 int mode, struct nameidata *nd)
1763{
1764 struct btrfs_trans_handle *trans;
1765 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001766 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001767 int err;
1768 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001769 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001770 u64 objectid;
1771
1772 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001773 err = btrfs_check_free_space(root, 1, 0);
1774 if (err)
1775 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001776 trans = btrfs_start_transaction(root, 1);
1777 btrfs_set_trans_block_group(trans, dir);
1778
1779 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1780 if (err) {
1781 err = -ENOSPC;
1782 goto out_unlock;
1783 }
1784
Chris Mason9c583092008-01-29 15:15:18 -05001785 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1786 dentry->d_name.len,
1787 dentry->d_parent->d_inode->i_ino,
1788 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001789 err = PTR_ERR(inode);
1790 if (IS_ERR(inode))
1791 goto out_unlock;
1792
1793 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001794 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001795 if (err)
1796 drop_inode = 1;
1797 else {
1798 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001799 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001800 inode->i_fop = &btrfs_file_operations;
1801 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001802 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1803 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001804 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001805 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001806 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001807 }
1808 dir->i_sb->s_dirt = 1;
1809 btrfs_update_inode_block_group(trans, inode);
1810 btrfs_update_inode_block_group(trans, dir);
1811out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001812 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001813 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001814fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001815 mutex_unlock(&root->fs_info->fs_mutex);
1816
1817 if (drop_inode) {
1818 inode_dec_link_count(inode);
1819 iput(inode);
1820 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001821 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001822 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001823 return err;
1824}
1825
1826static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1827 struct dentry *dentry)
1828{
1829 struct btrfs_trans_handle *trans;
1830 struct btrfs_root *root = BTRFS_I(dir)->root;
1831 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001832 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001833 int err;
1834 int drop_inode = 0;
1835
1836 if (inode->i_nlink == 0)
1837 return -ENOENT;
1838
Chris Mason6da6aba2007-12-18 16:15:09 -05001839#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1840 inode->i_nlink++;
1841#else
Chris Mason39279cc2007-06-12 06:35:45 -04001842 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001843#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001844 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001845 err = btrfs_check_free_space(root, 1, 0);
1846 if (err)
1847 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001848 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001849
Chris Mason39279cc2007-06-12 06:35:45 -04001850 btrfs_set_trans_block_group(trans, dir);
1851 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05001852 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001853
Chris Mason39279cc2007-06-12 06:35:45 -04001854 if (err)
1855 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001856
Chris Mason39279cc2007-06-12 06:35:45 -04001857 dir->i_sb->s_dirt = 1;
1858 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001859 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001860
Chris Mason54aa1f42007-06-22 14:16:25 -04001861 if (err)
1862 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001863
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001864 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001865 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001866fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001867 mutex_unlock(&root->fs_info->fs_mutex);
1868
1869 if (drop_inode) {
1870 inode_dec_link_count(inode);
1871 iput(inode);
1872 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001873 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001874 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001875 return err;
1876}
1877
Chris Mason39279cc2007-06-12 06:35:45 -04001878static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1879{
1880 struct inode *inode;
1881 struct btrfs_trans_handle *trans;
1882 struct btrfs_root *root = BTRFS_I(dir)->root;
1883 int err = 0;
1884 int drop_on_err = 0;
1885 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001886 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001887
1888 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001889 err = btrfs_check_free_space(root, 1, 0);
1890 if (err)
1891 goto out_unlock;
1892
Chris Mason39279cc2007-06-12 06:35:45 -04001893 trans = btrfs_start_transaction(root, 1);
1894 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001895
Chris Mason39279cc2007-06-12 06:35:45 -04001896 if (IS_ERR(trans)) {
1897 err = PTR_ERR(trans);
1898 goto out_unlock;
1899 }
1900
1901 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1902 if (err) {
1903 err = -ENOSPC;
1904 goto out_unlock;
1905 }
1906
Chris Mason9c583092008-01-29 15:15:18 -05001907 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1908 dentry->d_name.len,
1909 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001910 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1911 if (IS_ERR(inode)) {
1912 err = PTR_ERR(inode);
1913 goto out_fail;
1914 }
Chris Mason5f39d392007-10-15 16:14:19 -04001915
Chris Mason39279cc2007-06-12 06:35:45 -04001916 drop_on_err = 1;
1917 inode->i_op = &btrfs_dir_inode_operations;
1918 inode->i_fop = &btrfs_dir_file_operations;
1919 btrfs_set_trans_block_group(trans, inode);
1920
Chris Mason39544012007-12-12 14:38:19 -05001921 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001922 err = btrfs_update_inode(trans, root, inode);
1923 if (err)
1924 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001925
Chris Mason9c583092008-01-29 15:15:18 -05001926 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001927 if (err)
1928 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001929
Chris Mason39279cc2007-06-12 06:35:45 -04001930 d_instantiate(dentry, inode);
1931 drop_on_err = 0;
1932 dir->i_sb->s_dirt = 1;
1933 btrfs_update_inode_block_group(trans, inode);
1934 btrfs_update_inode_block_group(trans, dir);
1935
1936out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001937 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001938 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001939
Chris Mason39279cc2007-06-12 06:35:45 -04001940out_unlock:
1941 mutex_unlock(&root->fs_info->fs_mutex);
1942 if (drop_on_err)
1943 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001944 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001945 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001946 return err;
1947}
1948
Chris Masona52d9a82007-08-27 16:49:44 -04001949struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05001950 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04001951 int create)
1952{
1953 int ret;
1954 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001955 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001956 u64 extent_start = 0;
1957 u64 extent_end = 0;
1958 u64 objectid = inode->i_ino;
1959 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04001960 struct btrfs_path *path;
1961 struct btrfs_root *root = BTRFS_I(inode)->root;
1962 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001963 struct extent_buffer *leaf;
1964 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001965 struct extent_map *em = NULL;
1966 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05001967 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04001968 struct btrfs_trans_handle *trans = NULL;
1969
1970 path = btrfs_alloc_path();
1971 BUG_ON(!path);
1972 mutex_lock(&root->fs_info->fs_mutex);
1973
1974again:
Chris Masond1310b22008-01-24 16:13:08 -05001975 spin_lock(&em_tree->lock);
1976 em = lookup_extent_mapping(em_tree, start, len);
1977 spin_unlock(&em_tree->lock);
1978
Chris Masona52d9a82007-08-27 16:49:44 -04001979 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001980 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05001981 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1982 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05001983 WARN_ON(1);
1984 }
Chris Mason70dec802008-01-29 09:59:12 -05001985 if (em->block_start == EXTENT_MAP_INLINE && page)
1986 free_extent_map(em);
1987 else
1988 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001989 }
Chris Masond1310b22008-01-24 16:13:08 -05001990 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001991 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05001992 err = -ENOMEM;
1993 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001994 }
Chris Masond1310b22008-01-24 16:13:08 -05001995
1996 em->start = EXTENT_MAP_HOLE;
1997 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04001998 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001999 ret = btrfs_lookup_file_extent(trans, root, path,
2000 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002001 if (ret < 0) {
2002 err = ret;
2003 goto out;
2004 }
2005
2006 if (ret != 0) {
2007 if (path->slots[0] == 0)
2008 goto not_found;
2009 path->slots[0]--;
2010 }
2011
Chris Mason5f39d392007-10-15 16:14:19 -04002012 leaf = path->nodes[0];
2013 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002014 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002015 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002016 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2017 found_type = btrfs_key_type(&found_key);
2018 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002019 found_type != BTRFS_EXTENT_DATA_KEY) {
2020 goto not_found;
2021 }
2022
Chris Mason5f39d392007-10-15 16:14:19 -04002023 found_type = btrfs_file_extent_type(leaf, item);
2024 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002025 if (found_type == BTRFS_FILE_EXTENT_REG) {
2026 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002027 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002028 err = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -04002029 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002030 em->start = start;
2031 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002032 if (start + len <= extent_start)
Chris Masonb888db2b2007-08-27 16:49:44 -04002033 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002034 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002035 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002036 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002037 }
2038 goto not_found_em;
2039 }
Chris Masondb945352007-10-15 16:15:53 -04002040 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2041 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002042 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002043 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002044 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002045 goto insert;
2046 }
Chris Masondb945352007-10-15 16:15:53 -04002047 bytenr += btrfs_file_extent_offset(leaf, item);
2048 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002049 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002050 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002051 goto insert;
2052 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002053 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002054 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002055 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002056 size_t size;
2057 size_t extent_offset;
2058 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002059
Chris Mason5f39d392007-10-15 16:14:19 -04002060 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2061 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002062 extent_end = (extent_start + size + root->sectorsize - 1) &
2063 ~((u64)root->sectorsize - 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04002064 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002065 em->start = start;
2066 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002067 if (start + len <= extent_start)
Chris Masonb888db2b2007-08-27 16:49:44 -04002068 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002069 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002070 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002071 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002072 }
2073 goto not_found_em;
2074 }
2075 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002076
2077 if (!page) {
2078 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002079 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002080 goto out;
2081 }
2082
Chris Mason70dec802008-01-29 09:59:12 -05002083 page_start = page_offset(page) + pg_offset;
2084 extent_offset = page_start - extent_start;
2085 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002086 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002087 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002088 em->len = (copy_size + root->sectorsize - 1) &
2089 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002090 map = kmap(page);
2091 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002092 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002093 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002094 copy_size);
2095 flush_dcache_page(page);
2096 } else if (create && PageUptodate(page)) {
2097 if (!trans) {
2098 kunmap(page);
2099 free_extent_map(em);
2100 em = NULL;
2101 btrfs_release_path(root, path);
2102 trans = btrfs_start_transaction(root, 1);
2103 goto again;
2104 }
Chris Mason70dec802008-01-29 09:59:12 -05002105 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002106 copy_size);
2107 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002108 }
Chris Masona52d9a82007-08-27 16:49:44 -04002109 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002110 set_extent_uptodate(io_tree, em->start,
2111 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002112 goto insert;
2113 } else {
2114 printk("unkknown found_type %d\n", found_type);
2115 WARN_ON(1);
2116 }
2117not_found:
2118 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002119 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002120not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002121 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002122insert:
2123 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002124 if (em->start > start || extent_map_end(em) <= start) {
2125 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002126 err = -EIO;
2127 goto out;
2128 }
Chris Masond1310b22008-01-24 16:13:08 -05002129
2130 err = 0;
2131 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002132 ret = add_extent_mapping(em_tree, em);
2133 if (ret == -EEXIST) {
2134 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002135 em = lookup_extent_mapping(em_tree, start, len);
2136 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002137 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002138 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002139 }
Chris Masona52d9a82007-08-27 16:49:44 -04002140 }
Chris Masond1310b22008-01-24 16:13:08 -05002141 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002142out:
2143 btrfs_free_path(path);
2144 if (trans) {
2145 ret = btrfs_end_transaction(trans, root);
2146 if (!err)
2147 err = ret;
2148 }
2149 mutex_unlock(&root->fs_info->fs_mutex);
2150 if (err) {
2151 free_extent_map(em);
2152 WARN_ON(1);
2153 return ERR_PTR(err);
2154 }
2155 return em;
2156}
2157
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002158static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002159{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002160 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002161}
2162
Chris Mason9ebefb182007-06-15 13:50:00 -04002163int btrfs_readpage(struct file *file, struct page *page)
2164{
Chris Masond1310b22008-01-24 16:13:08 -05002165 struct extent_io_tree *tree;
2166 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002167 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002168}
Chris Mason1832a6d2007-12-21 16:27:21 -05002169
Chris Mason39279cc2007-06-12 06:35:45 -04002170static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2171{
Chris Masond1310b22008-01-24 16:13:08 -05002172 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04002173
2174
2175 if (current->flags & PF_MEMALLOC) {
2176 redirty_page_for_writepage(wbc, page);
2177 unlock_page(page);
2178 return 0;
2179 }
Chris Masond1310b22008-01-24 16:13:08 -05002180 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002181 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2182}
Chris Mason39279cc2007-06-12 06:35:45 -04002183
Chris Masonb293f02e2007-11-01 19:45:34 -04002184static int btrfs_writepages(struct address_space *mapping,
2185 struct writeback_control *wbc)
2186{
Chris Masond1310b22008-01-24 16:13:08 -05002187 struct extent_io_tree *tree;
2188 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04002189 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2190}
2191
Chris Mason3ab2fb52007-11-08 10:59:22 -05002192static int
2193btrfs_readpages(struct file *file, struct address_space *mapping,
2194 struct list_head *pages, unsigned nr_pages)
2195{
Chris Masond1310b22008-01-24 16:13:08 -05002196 struct extent_io_tree *tree;
2197 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002198 return extent_readpages(tree, mapping, pages, nr_pages,
2199 btrfs_get_extent);
2200}
2201
Chris Mason70dec802008-01-29 09:59:12 -05002202static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002203{
Chris Masond1310b22008-01-24 16:13:08 -05002204 struct extent_io_tree *tree;
2205 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002206 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002207
Chris Masond1310b22008-01-24 16:13:08 -05002208 tree = &BTRFS_I(page->mapping->host)->io_tree;
2209 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002210 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002211 if (ret == 1) {
2212 ClearPagePrivate(page);
2213 set_page_private(page, 0);
2214 page_cache_release(page);
2215 }
2216 return ret;
2217}
Chris Mason39279cc2007-06-12 06:35:45 -04002218
Chris Masona52d9a82007-08-27 16:49:44 -04002219static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2220{
Chris Masond1310b22008-01-24 16:13:08 -05002221 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002222
Chris Masond1310b22008-01-24 16:13:08 -05002223 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002224 extent_invalidatepage(tree, page, offset);
2225 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002226}
2227
Chris Mason9ebefb182007-06-15 13:50:00 -04002228/*
2229 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2230 * called from a page fault handler when a page is first dirtied. Hence we must
2231 * be careful to check for EOF conditions here. We set the page up correctly
2232 * for a written page which means we get ENOSPC checking when writing into
2233 * holes and correct delalloc and unwritten extent mapping on filesystems that
2234 * support these features.
2235 *
2236 * We are not allowed to take the i_mutex here so we have to play games to
2237 * protect against truncate races as the page could now be beyond EOF. Because
2238 * vmtruncate() writes the inode size before removing pages, once we have the
2239 * page lock we can determine safely if the page is beyond EOF. If it is not
2240 * beyond EOF, then the page is guaranteed safe against truncation until we
2241 * unlock the page.
2242 */
2243int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2244{
Chris Mason6da6aba2007-12-18 16:15:09 -05002245 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002246 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002247 unsigned long end;
2248 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002249 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002250 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002251
Chris Mason1832a6d2007-12-21 16:27:21 -05002252 mutex_lock(&root->fs_info->fs_mutex);
2253 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002254 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002255 if (ret)
2256 goto out;
2257
2258 ret = -EINVAL;
2259
Chris Mason9ebefb182007-06-15 13:50:00 -04002260 lock_page(page);
2261 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002262 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002263 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002264
Chris Mason9ebefb182007-06-15 13:50:00 -04002265 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002266 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002267 /* page got truncated out from underneath us */
2268 goto out_unlock;
2269 }
2270
2271 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002272 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002273 end = size & ~PAGE_CACHE_MASK;
2274 else
2275 end = PAGE_CACHE_SIZE;
2276
Chris Masonb888db2b2007-08-27 16:49:44 -04002277 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002278
2279out_unlock:
2280 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002281out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002282 return ret;
2283}
2284
Chris Mason39279cc2007-06-12 06:35:45 -04002285static void btrfs_truncate(struct inode *inode)
2286{
2287 struct btrfs_root *root = BTRFS_I(inode)->root;
2288 int ret;
2289 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002290 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002291
2292 if (!S_ISREG(inode->i_mode))
2293 return;
2294 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2295 return;
2296
2297 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2298
2299 mutex_lock(&root->fs_info->fs_mutex);
2300 trans = btrfs_start_transaction(root, 1);
2301 btrfs_set_trans_block_group(trans, inode);
2302
2303 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002304 ret = btrfs_truncate_in_trans(trans, root, inode,
2305 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002306 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002307 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002308
Chris Mason39279cc2007-06-12 06:35:45 -04002309 ret = btrfs_end_transaction(trans, root);
2310 BUG_ON(ret);
2311 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002312 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002313 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002314}
2315
Chris Mason4313b392008-01-03 09:08:48 -05002316static int noinline create_subvol(struct btrfs_root *root, char *name,
2317 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002318{
2319 struct btrfs_trans_handle *trans;
2320 struct btrfs_key key;
2321 struct btrfs_root_item root_item;
2322 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002323 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002324 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002325 struct inode *inode;
2326 struct inode *dir;
2327 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002328 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002329 u64 objectid;
2330 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002331 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002332
2333 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002334 ret = btrfs_check_free_space(root, 1, 0);
2335 if (ret)
2336 goto fail_commit;
2337
Chris Mason39279cc2007-06-12 06:35:45 -04002338 trans = btrfs_start_transaction(root, 1);
2339 BUG_ON(!trans);
2340
Chris Mason7bb86312007-12-11 09:25:06 -05002341 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2342 0, &objectid);
2343 if (ret)
2344 goto fail;
2345
2346 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2347 objectid, trans->transid, 0, 0,
2348 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002349 if (IS_ERR(leaf))
2350 return PTR_ERR(leaf);
2351
2352 btrfs_set_header_nritems(leaf, 0);
2353 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002354 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002355 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002356 btrfs_set_header_owner(leaf, objectid);
2357
Chris Mason5f39d392007-10-15 16:14:19 -04002358 write_extent_buffer(leaf, root->fs_info->fsid,
2359 (unsigned long)btrfs_header_fsid(leaf),
2360 BTRFS_FSID_SIZE);
2361 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002362
2363 inode_item = &root_item.inode;
2364 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002365 inode_item->generation = cpu_to_le64(1);
2366 inode_item->size = cpu_to_le64(3);
2367 inode_item->nlink = cpu_to_le32(1);
2368 inode_item->nblocks = cpu_to_le64(1);
2369 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002370
Chris Masondb945352007-10-15 16:15:53 -04002371 btrfs_set_root_bytenr(&root_item, leaf->start);
2372 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002373 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002374 btrfs_set_root_used(&root_item, 0);
2375
Chris Mason5eda7b52007-06-22 14:16:25 -04002376 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2377 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002378
2379 free_extent_buffer(leaf);
2380 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002381
Chris Mason39279cc2007-06-12 06:35:45 -04002382 btrfs_set_root_dirid(&root_item, new_dirid);
2383
2384 key.objectid = objectid;
2385 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002386 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2387 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2388 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002389 if (ret)
2390 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002391
2392 /*
2393 * insert the directory item
2394 */
2395 key.offset = (u64)-1;
2396 dir = root->fs_info->sb->s_root->d_inode;
2397 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2398 name, namelen, dir->i_ino, &key,
2399 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002400 if (ret)
2401 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002402
Chris Mason39544012007-12-12 14:38:19 -05002403 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2404 name, namelen, objectid,
2405 root->fs_info->sb->s_root->d_inode->i_ino);
2406 if (ret)
2407 goto fail;
2408
Chris Mason39279cc2007-06-12 06:35:45 -04002409 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002410 if (ret)
2411 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002412
Josef Bacik58176a92007-08-29 15:47:34 -04002413 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002414 BUG_ON(!new_root);
2415
2416 trans = btrfs_start_transaction(new_root, 1);
2417 BUG_ON(!trans);
2418
Chris Mason9c583092008-01-29 15:15:18 -05002419 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2420 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002421 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002422 if (IS_ERR(inode))
2423 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002424 inode->i_op = &btrfs_dir_inode_operations;
2425 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002426 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002427
Chris Mason39544012007-12-12 14:38:19 -05002428 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2429 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002430 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002431 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002432 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002433 if (ret)
2434 goto fail;
2435fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002436 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002437 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002438 if (err && !ret)
2439 ret = err;
2440fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002441 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002442 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002443 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002444 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002445}
2446
2447static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2448{
Chris Mason3063d292008-01-08 15:46:30 -05002449 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002450 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002451 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002452 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002453 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002454
2455 if (!root->ref_cows)
2456 return -EINVAL;
2457
2458 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002459 ret = btrfs_check_free_space(root, 1, 0);
2460 if (ret)
2461 goto fail_unlock;
2462
Chris Mason3063d292008-01-08 15:46:30 -05002463 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2464 if (!pending_snapshot) {
2465 ret = -ENOMEM;
2466 goto fail_unlock;
2467 }
Yanfb4bc1e2008-01-17 11:59:51 -05002468 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002469 if (!pending_snapshot->name) {
2470 ret = -ENOMEM;
2471 kfree(pending_snapshot);
2472 goto fail_unlock;
2473 }
Yanfb4bc1e2008-01-17 11:59:51 -05002474 memcpy(pending_snapshot->name, name, namelen);
2475 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002476 trans = btrfs_start_transaction(root, 1);
2477 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002478 pending_snapshot->root = root;
2479 list_add(&pending_snapshot->list,
2480 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002481 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002482 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002483
Chris Mason1832a6d2007-12-21 16:27:21 -05002484fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002485 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002486 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002487 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002488 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002489}
2490
Chris Masonedbd8d42007-12-21 16:27:24 -05002491unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002492 struct file_ra_state *ra, struct file *file,
2493 pgoff_t offset, pgoff_t last_index)
2494{
2495 pgoff_t req_size;
2496
2497#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2498 req_size = last_index - offset + 1;
2499 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2500 return offset;
2501#else
2502 req_size = min(last_index - offset + 1, (pgoff_t)128);
2503 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2504 return offset + req_size;
2505#endif
2506}
2507
2508int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002509 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002510 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002511 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002512 struct page *page;
2513 unsigned long last_index;
2514 unsigned long ra_index = 0;
2515 u64 page_start;
2516 u64 page_end;
2517 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002518 int ret;
2519
2520 mutex_lock(&root->fs_info->fs_mutex);
2521 ret = btrfs_check_free_space(root, inode->i_size, 0);
2522 mutex_unlock(&root->fs_info->fs_mutex);
2523 if (ret)
2524 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002525
2526 mutex_lock(&inode->i_mutex);
2527 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2528 for (i = 0; i <= last_index; i++) {
2529 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002530 ra_index = btrfs_force_ra(inode->i_mapping,
2531 &file->f_ra,
2532 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002533 }
2534 page = grab_cache_page(inode->i_mapping, i);
2535 if (!page)
2536 goto out_unlock;
2537 if (!PageUptodate(page)) {
2538 btrfs_readpage(NULL, page);
2539 lock_page(page);
2540 if (!PageUptodate(page)) {
2541 unlock_page(page);
2542 page_cache_release(page);
2543 goto out_unlock;
2544 }
2545 }
Chris Mason35ebb932007-10-30 16:56:53 -04002546 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002547 page_end = page_start + PAGE_CACHE_SIZE - 1;
2548
Chris Masond1310b22008-01-24 16:13:08 -05002549 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002550 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002551 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002552
Chris Masond1310b22008-01-24 16:13:08 -05002553 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002554 set_page_dirty(page);
2555 unlock_page(page);
2556 page_cache_release(page);
2557 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2558 }
2559
2560out_unlock:
2561 mutex_unlock(&inode->i_mutex);
2562 return 0;
2563}
2564
Chris Masonedbd8d42007-12-21 16:27:24 -05002565static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2566{
2567 u64 new_size;
2568 u64 old_size;
2569 struct btrfs_ioctl_vol_args *vol_args;
2570 struct btrfs_trans_handle *trans;
2571 char *sizestr;
2572 int ret = 0;
2573 int namelen;
2574 int mod = 0;
2575
2576 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2577
2578 if (!vol_args)
2579 return -ENOMEM;
2580
2581 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2582 ret = -EFAULT;
2583 goto out;
2584 }
2585 namelen = strlen(vol_args->name);
2586 if (namelen > BTRFS_VOL_NAME_MAX) {
2587 ret = -EINVAL;
2588 goto out;
2589 }
2590
2591 sizestr = vol_args->name;
2592 if (!strcmp(sizestr, "max"))
2593 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2594 else {
2595 if (sizestr[0] == '-') {
2596 mod = -1;
2597 sizestr++;
2598 } else if (sizestr[0] == '+') {
2599 mod = 1;
2600 sizestr++;
2601 }
2602 new_size = btrfs_parse_size(sizestr);
2603 if (new_size == 0) {
2604 ret = -EINVAL;
2605 goto out;
2606 }
2607 }
2608
2609 mutex_lock(&root->fs_info->fs_mutex);
2610 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2611
2612 if (mod < 0) {
2613 if (new_size > old_size) {
2614 ret = -EINVAL;
2615 goto out_unlock;
2616 }
2617 new_size = old_size - new_size;
2618 } else if (mod > 0) {
2619 new_size = old_size + new_size;
2620 }
2621
2622 if (new_size < 256 * 1024 * 1024) {
2623 ret = -EINVAL;
2624 goto out_unlock;
2625 }
2626 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2627 ret = -EFBIG;
2628 goto out_unlock;
2629 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002630
2631 do_div(new_size, root->sectorsize);
2632 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002633
2634printk("new size is %Lu\n", new_size);
2635 if (new_size > old_size) {
2636 trans = btrfs_start_transaction(root, 1);
2637 ret = btrfs_grow_extent_tree(trans, root, new_size);
2638 btrfs_commit_transaction(trans, root);
2639 } else {
2640 ret = btrfs_shrink_extent_tree(root, new_size);
2641 }
2642
2643out_unlock:
2644 mutex_unlock(&root->fs_info->fs_mutex);
2645out:
2646 kfree(vol_args);
2647 return ret;
2648}
2649
Chris Mason4313b392008-01-03 09:08:48 -05002650static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2651 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002652{
Chris Mason4aec2b52007-12-18 16:25:45 -05002653 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002654 struct btrfs_dir_item *di;
2655 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002656 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002657 int namelen;
2658 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002659
Chris Mason4aec2b52007-12-18 16:25:45 -05002660 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002661
Chris Mason4aec2b52007-12-18 16:25:45 -05002662 if (!vol_args)
2663 return -ENOMEM;
2664
2665 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2666 ret = -EFAULT;
2667 goto out;
2668 }
2669
2670 namelen = strlen(vol_args->name);
2671 if (namelen > BTRFS_VOL_NAME_MAX) {
2672 ret = -EINVAL;
2673 goto out;
2674 }
2675 if (strchr(vol_args->name, '/')) {
2676 ret = -EINVAL;
2677 goto out;
2678 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002679
2680 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002681 if (!path) {
2682 ret = -ENOMEM;
2683 goto out;
2684 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002685
2686 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2687 mutex_lock(&root->fs_info->fs_mutex);
2688 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2689 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002690 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002691 mutex_unlock(&root->fs_info->fs_mutex);
2692 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002693
2694 if (di && !IS_ERR(di)) {
2695 ret = -EEXIST;
2696 goto out;
2697 }
2698
2699 if (IS_ERR(di)) {
2700 ret = PTR_ERR(di);
2701 goto out;
2702 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002703
2704 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002705 ret = create_subvol(root, vol_args->name, namelen);
2706 else
2707 ret = create_snapshot(root, vol_args->name, namelen);
2708out:
2709 kfree(vol_args);
2710 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002711}
2712
2713static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002714{
Chris Mason6da6aba2007-12-18 16:15:09 -05002715 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002716 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002717
2718 switch (inode->i_mode & S_IFMT) {
2719 case S_IFDIR:
2720 mutex_lock(&root->fs_info->fs_mutex);
2721 btrfs_defrag_root(root, 0);
2722 btrfs_defrag_root(root->fs_info->extent_root, 0);
2723 mutex_unlock(&root->fs_info->fs_mutex);
2724 break;
2725 case S_IFREG:
2726 btrfs_defrag_file(file);
2727 break;
2728 }
2729
2730 return 0;
2731}
2732
2733long btrfs_ioctl(struct file *file, unsigned int
2734 cmd, unsigned long arg)
2735{
Chris Mason6da6aba2007-12-18 16:15:09 -05002736 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002737
2738 switch (cmd) {
2739 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002740 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002741 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002742 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002743 case BTRFS_IOC_RESIZE:
2744 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002745 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002746
2747 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002748}
2749
Chris Mason39279cc2007-06-12 06:35:45 -04002750/*
2751 * Called inside transaction, so use GFP_NOFS
2752 */
2753struct inode *btrfs_alloc_inode(struct super_block *sb)
2754{
2755 struct btrfs_inode *ei;
2756
2757 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2758 if (!ei)
2759 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002760 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002761 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002762 return &ei->vfs_inode;
2763}
2764
2765void btrfs_destroy_inode(struct inode *inode)
2766{
2767 WARN_ON(!list_empty(&inode->i_dentry));
2768 WARN_ON(inode->i_data.nrpages);
2769
Chris Mason8c416c92008-01-14 15:10:26 -05002770 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002771 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2772}
2773
Chris Mason44ec0b72007-10-29 10:55:05 -04002774#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2775static void init_once(struct kmem_cache * cachep, void *foo)
2776#else
Chris Mason39279cc2007-06-12 06:35:45 -04002777static void init_once(void * foo, struct kmem_cache * cachep,
2778 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002779#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002780{
2781 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2782
2783 inode_init_once(&ei->vfs_inode);
2784}
2785
2786void btrfs_destroy_cachep(void)
2787{
2788 if (btrfs_inode_cachep)
2789 kmem_cache_destroy(btrfs_inode_cachep);
2790 if (btrfs_trans_handle_cachep)
2791 kmem_cache_destroy(btrfs_trans_handle_cachep);
2792 if (btrfs_transaction_cachep)
2793 kmem_cache_destroy(btrfs_transaction_cachep);
2794 if (btrfs_bit_radix_cachep)
2795 kmem_cache_destroy(btrfs_bit_radix_cachep);
2796 if (btrfs_path_cachep)
2797 kmem_cache_destroy(btrfs_path_cachep);
2798}
2799
Chris Mason86479a02007-09-10 19:58:16 -04002800struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002801 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002802#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2803 void (*ctor)(struct kmem_cache *, void *)
2804#else
Chris Mason92fee662007-07-25 12:31:35 -04002805 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002806 unsigned long)
2807#endif
2808 )
Chris Mason92fee662007-07-25 12:31:35 -04002809{
2810 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2811 SLAB_MEM_SPREAD | extra_flags), ctor
2812#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2813 ,NULL
2814#endif
2815 );
2816}
2817
Chris Mason39279cc2007-06-12 06:35:45 -04002818int btrfs_init_cachep(void)
2819{
Chris Mason86479a02007-09-10 19:58:16 -04002820 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002821 sizeof(struct btrfs_inode),
2822 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002823 if (!btrfs_inode_cachep)
2824 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002825 btrfs_trans_handle_cachep =
2826 btrfs_cache_create("btrfs_trans_handle_cache",
2827 sizeof(struct btrfs_trans_handle),
2828 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002829 if (!btrfs_trans_handle_cachep)
2830 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002831 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002832 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002833 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002834 if (!btrfs_transaction_cachep)
2835 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002836 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002837 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002838 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002839 if (!btrfs_path_cachep)
2840 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002841 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002842 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002843 if (!btrfs_bit_radix_cachep)
2844 goto fail;
2845 return 0;
2846fail:
2847 btrfs_destroy_cachep();
2848 return -ENOMEM;
2849}
2850
2851static int btrfs_getattr(struct vfsmount *mnt,
2852 struct dentry *dentry, struct kstat *stat)
2853{
2854 struct inode *inode = dentry->d_inode;
2855 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002856 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05002857 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04002858 return 0;
2859}
2860
2861static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2862 struct inode * new_dir,struct dentry *new_dentry)
2863{
2864 struct btrfs_trans_handle *trans;
2865 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2866 struct inode *new_inode = new_dentry->d_inode;
2867 struct inode *old_inode = old_dentry->d_inode;
2868 struct timespec ctime = CURRENT_TIME;
2869 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002870 int ret;
2871
2872 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2873 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2874 return -ENOTEMPTY;
2875 }
Chris Mason5f39d392007-10-15 16:14:19 -04002876
Chris Mason39279cc2007-06-12 06:35:45 -04002877 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002878 ret = btrfs_check_free_space(root, 1, 0);
2879 if (ret)
2880 goto out_unlock;
2881
Chris Mason39279cc2007-06-12 06:35:45 -04002882 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002883
Chris Mason39279cc2007-06-12 06:35:45 -04002884 btrfs_set_trans_block_group(trans, new_dir);
2885 path = btrfs_alloc_path();
2886 if (!path) {
2887 ret = -ENOMEM;
2888 goto out_fail;
2889 }
2890
2891 old_dentry->d_inode->i_nlink++;
2892 old_dir->i_ctime = old_dir->i_mtime = ctime;
2893 new_dir->i_ctime = new_dir->i_mtime = ctime;
2894 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002895
Chris Mason39279cc2007-06-12 06:35:45 -04002896 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2897 if (ret)
2898 goto out_fail;
2899
2900 if (new_inode) {
2901 new_inode->i_ctime = CURRENT_TIME;
2902 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2903 if (ret)
2904 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002905 }
Chris Mason9c583092008-01-29 15:15:18 -05002906 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002907 if (ret)
2908 goto out_fail;
2909
2910out_fail:
2911 btrfs_free_path(path);
2912 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002913out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002914 mutex_unlock(&root->fs_info->fs_mutex);
2915 return ret;
2916}
2917
2918static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2919 const char *symname)
2920{
2921 struct btrfs_trans_handle *trans;
2922 struct btrfs_root *root = BTRFS_I(dir)->root;
2923 struct btrfs_path *path;
2924 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002925 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002926 int err;
2927 int drop_inode = 0;
2928 u64 objectid;
2929 int name_len;
2930 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002931 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002932 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002933 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002934 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002935
2936 name_len = strlen(symname) + 1;
2937 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2938 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002939
Chris Mason39279cc2007-06-12 06:35:45 -04002940 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002941 err = btrfs_check_free_space(root, 1, 0);
2942 if (err)
2943 goto out_fail;
2944
Chris Mason39279cc2007-06-12 06:35:45 -04002945 trans = btrfs_start_transaction(root, 1);
2946 btrfs_set_trans_block_group(trans, dir);
2947
2948 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2949 if (err) {
2950 err = -ENOSPC;
2951 goto out_unlock;
2952 }
2953
Chris Mason9c583092008-01-29 15:15:18 -05002954 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2955 dentry->d_name.len,
2956 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002957 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2958 err = PTR_ERR(inode);
2959 if (IS_ERR(inode))
2960 goto out_unlock;
2961
2962 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002963 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002964 if (err)
2965 drop_inode = 1;
2966 else {
2967 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002968 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002969 inode->i_fop = &btrfs_file_operations;
2970 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002971 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2972 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002973 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05002974 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002975 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002976 }
2977 dir->i_sb->s_dirt = 1;
2978 btrfs_update_inode_block_group(trans, inode);
2979 btrfs_update_inode_block_group(trans, dir);
2980 if (drop_inode)
2981 goto out_unlock;
2982
2983 path = btrfs_alloc_path();
2984 BUG_ON(!path);
2985 key.objectid = inode->i_ino;
2986 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002987 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2988 datasize = btrfs_file_extent_calc_inline_size(name_len);
2989 err = btrfs_insert_empty_item(trans, root, path, &key,
2990 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002991 if (err) {
2992 drop_inode = 1;
2993 goto out_unlock;
2994 }
Chris Mason5f39d392007-10-15 16:14:19 -04002995 leaf = path->nodes[0];
2996 ei = btrfs_item_ptr(leaf, path->slots[0],
2997 struct btrfs_file_extent_item);
2998 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2999 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003000 BTRFS_FILE_EXTENT_INLINE);
3001 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003002 write_extent_buffer(leaf, symname, ptr, name_len);
3003 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003004 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003005
Chris Mason39279cc2007-06-12 06:35:45 -04003006 inode->i_op = &btrfs_symlink_inode_operations;
3007 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003008 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003009 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04003010 err = btrfs_update_inode(trans, root, inode);
3011 if (err)
3012 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003013
3014out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003015 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04003016 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003017out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003018 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04003019 if (drop_inode) {
3020 inode_dec_link_count(inode);
3021 iput(inode);
3022 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003023 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05003024 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003025 return err;
3026}
Yanfdebe2b2008-01-14 13:26:08 -05003027static int btrfs_permission(struct inode *inode, int mask,
3028 struct nameidata *nd)
3029{
3030 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3031 return -EACCES;
3032 return generic_permission(inode, mask, NULL);
3033}
Chris Mason39279cc2007-06-12 06:35:45 -04003034
3035static struct inode_operations btrfs_dir_inode_operations = {
3036 .lookup = btrfs_lookup,
3037 .create = btrfs_create,
3038 .unlink = btrfs_unlink,
3039 .link = btrfs_link,
3040 .mkdir = btrfs_mkdir,
3041 .rmdir = btrfs_rmdir,
3042 .rename = btrfs_rename,
3043 .symlink = btrfs_symlink,
3044 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003045 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003046 .setxattr = generic_setxattr,
3047 .getxattr = generic_getxattr,
3048 .listxattr = btrfs_listxattr,
3049 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003050 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003051};
Chris Mason39279cc2007-06-12 06:35:45 -04003052static struct inode_operations btrfs_dir_ro_inode_operations = {
3053 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003054 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003055};
Chris Mason39279cc2007-06-12 06:35:45 -04003056static struct file_operations btrfs_dir_file_operations = {
3057 .llseek = generic_file_llseek,
3058 .read = generic_read_dir,
3059 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003060 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003061#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003062 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003063#endif
3064};
3065
Chris Masond1310b22008-01-24 16:13:08 -05003066static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003067 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003068 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003069 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003070 .readpage_io_hook = btrfs_readpage_io_hook,
3071 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003072 .set_bit_hook = btrfs_set_bit_hook,
3073 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003074};
3075
Chris Mason39279cc2007-06-12 06:35:45 -04003076static struct address_space_operations btrfs_aops = {
3077 .readpage = btrfs_readpage,
3078 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04003079 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003080 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003081 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003082 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04003083 .invalidatepage = btrfs_invalidatepage,
3084 .releasepage = btrfs_releasepage,
3085 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003086};
3087
3088static struct address_space_operations btrfs_symlink_aops = {
3089 .readpage = btrfs_readpage,
3090 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003091 .invalidatepage = btrfs_invalidatepage,
3092 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003093};
3094
3095static struct inode_operations btrfs_file_inode_operations = {
3096 .truncate = btrfs_truncate,
3097 .getattr = btrfs_getattr,
3098 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003099 .setxattr = generic_setxattr,
3100 .getxattr = generic_getxattr,
3101 .listxattr = btrfs_listxattr,
3102 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003103 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003104};
Josef Bacik618e21d2007-07-11 10:18:17 -04003105static struct inode_operations btrfs_special_inode_operations = {
3106 .getattr = btrfs_getattr,
3107 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003108 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003109};
Chris Mason39279cc2007-06-12 06:35:45 -04003110static struct inode_operations btrfs_symlink_inode_operations = {
3111 .readlink = generic_readlink,
3112 .follow_link = page_follow_link_light,
3113 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003114 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003115};