blob: db60d85598ce21ac8b738fa65620c27ff2cd3935 [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 Masonb888db22007-08-27 16:49:44 -0400100{
101 struct btrfs_root *root = BTRFS_I(inode)->root;
102 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-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 Masonb888db22007-08-27 16:49:44 -0400111
Chris Masonb888db22007-08-27 16:49:44 -0400112 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-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 Masonb888db22007-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 Masonb888db22007-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 Masonb888db22007-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 Masonb888db22007-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;
304 struct btrfs_device *dev;
305 u64 logical = bio->bi_sector << 9;
306 u64 physical;
307 u64 length = 0;
308 u64 map_length;
309 struct bio_vec *bvec;
310 int i;
311 int ret;
312
313 bio_for_each_segment(bvec, bio, i) {
314 length += bvec->bv_len;
315 }
316 map_tree = &root->fs_info->mapping_tree;
317 map_length = length;
318 ret = btrfs_map_block(map_tree, logical, &physical, &map_length, &dev);
319 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 Mason0b86a832008-03-24 15:01:56 -0400331 if (rw != WRITE) {
332 goto mapit;
333 }
Chris Mason065631f2008-02-20 12:07:25 -0500334
335 if (btrfs_test_opt(root, NODATASUM) ||
Chris Mason0b86a832008-03-24 15:01:56 -0400336 btrfs_test_flag(inode, NODATASUM)) {
337 goto mapit;
338 }
Chris Mason065631f2008-02-20 12:07:25 -0500339
340 mutex_lock(&root->fs_info->fs_mutex);
341 trans = btrfs_start_transaction(root, 1);
342 btrfs_set_trans_block_group(trans, inode);
343 btrfs_csum_file_blocks(trans, root, inode, bio);
344 ret = btrfs_end_transaction(trans, root);
345 BUG_ON(ret);
346 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -0400347mapit:
348 return btrfs_map_bio(root, rw, bio);
Chris Mason065631f2008-02-20 12:07:25 -0500349}
Chris Mason6885f302008-02-20 16:11:05 -0500350
Chris Mason07157aa2007-08-30 08:50:51 -0400351int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
352{
353 int ret = 0;
354 struct inode *inode = page->mapping->host;
355 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500356 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400357 struct btrfs_csum_item *item;
358 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400359 u32 csum;
Yanb98b6762008-01-08 15:54:37 -0500360 if (btrfs_test_opt(root, NODATASUM) ||
361 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500362 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400363 mutex_lock(&root->fs_info->fs_mutex);
364 path = btrfs_alloc_path();
365 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
366 if (IS_ERR(item)) {
367 ret = PTR_ERR(item);
368 /* a csum that isn't present is a preallocated region. */
369 if (ret == -ENOENT || ret == -EFBIG)
370 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400371 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500372 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400373 goto out;
374 }
Chris Masonff79f812007-10-15 16:22:25 -0400375 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
376 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500377 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400378out:
379 if (path)
380 btrfs_free_path(path);
381 mutex_unlock(&root->fs_info->fs_mutex);
382 return ret;
383}
384
Chris Mason70dec802008-01-29 09:59:12 -0500385int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
386 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400387{
Chris Mason35ebb932007-10-30 16:56:53 -0400388 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400389 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500390 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400391 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500392 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400393 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400394 struct btrfs_root *root = BTRFS_I(inode)->root;
395 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400396 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500397
Yanb98b6762008-01-08 15:54:37 -0500398 if (btrfs_test_opt(root, NODATASUM) ||
399 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500400 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500401 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500402 private = state->private;
403 ret = 0;
404 } else {
405 ret = get_state_private(io_tree, start, &private);
406 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400407 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400408 kaddr = kmap_atomic(page, KM_IRQ0);
409 if (ret) {
410 goto zeroit;
411 }
Chris Masonff79f812007-10-15 16:22:25 -0400412 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
413 btrfs_csum_final(csum, (char *)&csum);
414 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400415 goto zeroit;
416 }
417 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400418 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400419 return 0;
420
421zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500422 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
423 page->mapping->host->i_ino, (unsigned long long)start, csum,
424 private);
Chris Masondb945352007-10-15 16:15:53 -0400425 memset(kaddr + offset, 1, end - start + 1);
426 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400427 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400428 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400429 return 0;
430}
Chris Masonb888db22007-08-27 16:49:44 -0400431
Chris Mason39279cc2007-06-12 06:35:45 -0400432void btrfs_read_locked_inode(struct inode *inode)
433{
434 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400435 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400436 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400437 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400438 struct btrfs_root *root = BTRFS_I(inode)->root;
439 struct btrfs_key location;
440 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400441 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400442 int ret;
443
444 path = btrfs_alloc_path();
445 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400446 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400447 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500448
Chris Mason39279cc2007-06-12 06:35:45 -0400449 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400450 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400451 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400452
Chris Mason5f39d392007-10-15 16:14:19 -0400453 leaf = path->nodes[0];
454 inode_item = btrfs_item_ptr(leaf, path->slots[0],
455 struct btrfs_inode_item);
456
457 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
458 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
459 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
460 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
461 inode->i_size = btrfs_inode_size(leaf, inode_item);
462
463 tspec = btrfs_inode_atime(inode_item);
464 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
465 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
466
467 tspec = btrfs_inode_mtime(inode_item);
468 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
469 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
470
471 tspec = btrfs_inode_ctime(inode_item);
472 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
473 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
474
475 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
476 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400477 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400478 rdev = btrfs_inode_rdev(leaf, inode_item);
479
480 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400481 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
482 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500483 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500484 if (!BTRFS_I(inode)->block_group) {
485 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400486 NULL, 0,
487 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500488 }
Chris Mason39279cc2007-06-12 06:35:45 -0400489 btrfs_free_path(path);
490 inode_item = NULL;
491
492 mutex_unlock(&root->fs_info->fs_mutex);
493
494 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400495 case S_IFREG:
496 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -0500497 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400498 inode->i_fop = &btrfs_file_operations;
499 inode->i_op = &btrfs_file_inode_operations;
500 break;
501 case S_IFDIR:
502 inode->i_fop = &btrfs_dir_file_operations;
503 if (root == root->fs_info->tree_root)
504 inode->i_op = &btrfs_dir_ro_inode_operations;
505 else
506 inode->i_op = &btrfs_dir_inode_operations;
507 break;
508 case S_IFLNK:
509 inode->i_op = &btrfs_symlink_inode_operations;
510 inode->i_mapping->a_ops = &btrfs_symlink_aops;
511 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400512 default:
513 init_special_inode(inode, inode->i_mode, rdev);
514 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400515 }
516 return;
517
518make_bad:
519 btrfs_release_path(root, path);
520 btrfs_free_path(path);
521 mutex_unlock(&root->fs_info->fs_mutex);
522 make_bad_inode(inode);
523}
524
Chris Mason5f39d392007-10-15 16:14:19 -0400525static void fill_inode_item(struct extent_buffer *leaf,
526 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400527 struct inode *inode)
528{
Chris Mason5f39d392007-10-15 16:14:19 -0400529 btrfs_set_inode_uid(leaf, item, inode->i_uid);
530 btrfs_set_inode_gid(leaf, item, inode->i_gid);
531 btrfs_set_inode_size(leaf, item, inode->i_size);
532 btrfs_set_inode_mode(leaf, item, inode->i_mode);
533 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
534
535 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
536 inode->i_atime.tv_sec);
537 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
538 inode->i_atime.tv_nsec);
539
540 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
541 inode->i_mtime.tv_sec);
542 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
543 inode->i_mtime.tv_nsec);
544
545 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
546 inode->i_ctime.tv_sec);
547 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
548 inode->i_ctime.tv_nsec);
549
550 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
551 btrfs_set_inode_generation(leaf, item, inode->i_generation);
552 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500553 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400554 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400555 BTRFS_I(inode)->block_group->key.objectid);
556}
557
Chris Masona52d9a82007-08-27 16:49:44 -0400558int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400559 struct btrfs_root *root,
560 struct inode *inode)
561{
562 struct btrfs_inode_item *inode_item;
563 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400564 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400565 int ret;
566
567 path = btrfs_alloc_path();
568 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400569 ret = btrfs_lookup_inode(trans, root, path,
570 &BTRFS_I(inode)->location, 1);
571 if (ret) {
572 if (ret > 0)
573 ret = -ENOENT;
574 goto failed;
575 }
576
Chris Mason5f39d392007-10-15 16:14:19 -0400577 leaf = path->nodes[0];
578 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400579 struct btrfs_inode_item);
580
Chris Mason5f39d392007-10-15 16:14:19 -0400581 fill_inode_item(leaf, inode_item, inode);
582 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400583 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400584 ret = 0;
585failed:
586 btrfs_release_path(root, path);
587 btrfs_free_path(path);
588 return ret;
589}
590
591
592static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
593 struct btrfs_root *root,
594 struct inode *dir,
595 struct dentry *dentry)
596{
597 struct btrfs_path *path;
598 const char *name = dentry->d_name.name;
599 int name_len = dentry->d_name.len;
600 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400601 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400602 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400603 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400604
605 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400606 if (!path) {
607 ret = -ENOMEM;
608 goto err;
609 }
610
Chris Mason39279cc2007-06-12 06:35:45 -0400611 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
612 name, name_len, -1);
613 if (IS_ERR(di)) {
614 ret = PTR_ERR(di);
615 goto err;
616 }
617 if (!di) {
618 ret = -ENOENT;
619 goto err;
620 }
Chris Mason5f39d392007-10-15 16:14:19 -0400621 leaf = path->nodes[0];
622 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400623 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400624 if (ret)
625 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400626 btrfs_release_path(root, path);
627
628 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400629 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400630 if (IS_ERR(di)) {
631 ret = PTR_ERR(di);
632 goto err;
633 }
634 if (!di) {
635 ret = -ENOENT;
636 goto err;
637 }
638 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400639
640 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500641 ret = btrfs_del_inode_ref(trans, root, name, name_len,
642 dentry->d_inode->i_ino,
643 dentry->d_parent->d_inode->i_ino);
644 if (ret) {
645 printk("failed to delete reference to %.*s, "
646 "inode %lu parent %lu\n", name_len, name,
647 dentry->d_inode->i_ino,
648 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500649 }
Chris Mason39279cc2007-06-12 06:35:45 -0400650err:
651 btrfs_free_path(path);
652 if (!ret) {
653 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400654 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400655 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500656#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
657 dentry->d_inode->i_nlink--;
658#else
Chris Mason39279cc2007-06-12 06:35:45 -0400659 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500660#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400661 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400662 dir->i_sb->s_dirt = 1;
663 }
664 return ret;
665}
666
667static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
668{
669 struct btrfs_root *root;
670 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500671 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400672 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500673 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400674
675 root = BTRFS_I(dir)->root;
676 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500677
678 ret = btrfs_check_free_space(root, 1, 1);
679 if (ret)
680 goto fail;
681
Chris Mason39279cc2007-06-12 06:35:45 -0400682 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400683
Chris Mason39279cc2007-06-12 06:35:45 -0400684 btrfs_set_trans_block_group(trans, dir);
685 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400686 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400687
Chris Mason2da98f02008-01-16 11:44:43 -0500688 if (inode->i_nlink == 0) {
689 int found;
690 /* if the inode isn't linked anywhere,
691 * we don't need to worry about
692 * data=ordered
693 */
694 found = btrfs_del_ordered_inode(inode);
695 if (found == 1) {
696 atomic_dec(&inode->i_count);
697 }
698 }
699
Chris Mason39279cc2007-06-12 06:35:45 -0400700 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500701fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400702 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400703 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500704 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400705 return ret;
706}
707
708static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
709{
710 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500711 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400712 int ret;
713 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400714 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500715 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400716
Yan134d4512007-10-25 15:49:25 -0400717 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
718 return -ENOTEMPTY;
719
Chris Mason39279cc2007-06-12 06:35:45 -0400720 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500721 ret = btrfs_check_free_space(root, 1, 1);
722 if (ret)
723 goto fail;
724
Chris Mason39279cc2007-06-12 06:35:45 -0400725 trans = btrfs_start_transaction(root, 1);
726 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400727
728 /* now the directory is empty */
729 err = btrfs_unlink_trans(trans, root, dir, dentry);
730 if (!err) {
731 inode->i_size = 0;
732 }
Chris Mason39544012007-12-12 14:38:19 -0500733
Chris Masond3c2fdc2007-09-17 10:58:06 -0400734 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400735 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500736fail:
Yan134d4512007-10-25 15:49:25 -0400737 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400738 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500739 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500740
Chris Mason39279cc2007-06-12 06:35:45 -0400741 if (ret && !err)
742 err = ret;
743 return err;
744}
745
Chris Mason39279cc2007-06-12 06:35:45 -0400746/*
Chris Mason39279cc2007-06-12 06:35:45 -0400747 * this can truncate away extent items, csum items and directory items.
748 * It starts at a high offset and removes keys until it can't find
749 * any higher than i_size.
750 *
751 * csum items that cross the new i_size are truncated to the new size
752 * as well.
753 */
754static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
755 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500756 struct inode *inode,
757 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400758{
759 int ret;
760 struct btrfs_path *path;
761 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400762 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400763 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400764 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400765 struct btrfs_file_extent_item *fi;
766 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400767 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400768 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500769 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500770 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400771 int found_extent;
772 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500773 int pending_del_nr = 0;
774 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400775 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400776
Chris Masona52d9a82007-08-27 16:49:44 -0400777 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400778 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400779 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400780 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400781
Chris Mason39279cc2007-06-12 06:35:45 -0400782 /* FIXME, add redo link to tree so we don't leak on crash */
783 key.objectid = inode->i_ino;
784 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400785 key.type = (u8)-1;
786
Chris Mason85e21ba2008-01-29 15:11:36 -0500787 btrfs_init_path(path);
788search_again:
789 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
790 if (ret < 0) {
791 goto error;
792 }
793 if (ret > 0) {
794 BUG_ON(path->slots[0] == 0);
795 path->slots[0]--;
796 }
797
Chris Mason39279cc2007-06-12 06:35:45 -0400798 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400799 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400800 leaf = path->nodes[0];
801 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
802 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400803
Chris Mason5f39d392007-10-15 16:14:19 -0400804 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400805 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400806
Chris Mason85e21ba2008-01-29 15:11:36 -0500807 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400808 break;
809
Chris Mason5f39d392007-10-15 16:14:19 -0400810 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400811 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400812 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400813 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400814 extent_type = btrfs_file_extent_type(leaf, fi);
815 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400816 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400817 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400818 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
819 struct btrfs_item *item = btrfs_item_nr(leaf,
820 path->slots[0]);
821 item_end += btrfs_file_extent_inline_len(leaf,
822 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400823 }
Yan008630c2007-11-07 13:31:09 -0500824 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400825 }
826 if (found_type == BTRFS_CSUM_ITEM_KEY) {
827 ret = btrfs_csum_truncate(trans, root, path,
828 inode->i_size);
829 BUG_ON(ret);
830 }
Yan008630c2007-11-07 13:31:09 -0500831 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400832 if (found_type == BTRFS_DIR_ITEM_KEY) {
833 found_type = BTRFS_INODE_ITEM_KEY;
834 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
835 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500836 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
837 found_type = BTRFS_XATTR_ITEM_KEY;
838 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
839 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400840 } else if (found_type) {
841 found_type--;
842 } else {
843 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400844 }
Yana61721d2007-09-17 11:08:38 -0400845 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500846 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400847 }
Chris Mason5f39d392007-10-15 16:14:19 -0400848 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400849 del_item = 1;
850 else
851 del_item = 0;
852 found_extent = 0;
853
854 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400855 if (found_type != BTRFS_EXTENT_DATA_KEY)
856 goto delete;
857
858 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400859 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400860 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400861 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400862 u64 orig_num_bytes =
863 btrfs_file_extent_num_bytes(leaf, fi);
864 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400865 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -0500866 extent_num_bytes = extent_num_bytes &
867 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400868 btrfs_set_file_extent_num_bytes(leaf, fi,
869 extent_num_bytes);
870 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -0500871 extent_num_bytes);
872 if (extent_start != 0)
873 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -0400874 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400875 } else {
Chris Masondb945352007-10-15 16:15:53 -0400876 extent_num_bytes =
877 btrfs_file_extent_disk_num_bytes(leaf,
878 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400879 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -0500880 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400881 if (extent_start != 0) {
882 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -0500883 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -0400884 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500885 root_gen = btrfs_header_generation(leaf);
886 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400887 }
Chris Mason90692182008-02-08 13:49:28 -0500888 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
889 if (!del_item) {
890 u32 newsize = inode->i_size - found_key.offset;
891 dec_i_blocks(inode, item_end + 1 -
892 found_key.offset - newsize);
893 newsize =
894 btrfs_file_extent_calc_inline_size(newsize);
895 ret = btrfs_truncate_item(trans, root, path,
896 newsize, 1);
897 BUG_ON(ret);
898 } else {
899 dec_i_blocks(inode, item_end + 1 -
900 found_key.offset);
901 }
Chris Mason39279cc2007-06-12 06:35:45 -0400902 }
Chris Mason179e29e2007-11-01 11:28:41 -0400903delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400904 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -0500905 if (!pending_del_nr) {
906 /* no pending yet, add ourselves */
907 pending_del_slot = path->slots[0];
908 pending_del_nr = 1;
909 } else if (pending_del_nr &&
910 path->slots[0] + 1 == pending_del_slot) {
911 /* hop on the pending chunk */
912 pending_del_nr++;
913 pending_del_slot = path->slots[0];
914 } else {
915 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
916 }
Chris Mason39279cc2007-06-12 06:35:45 -0400917 } else {
918 break;
919 }
Chris Mason39279cc2007-06-12 06:35:45 -0400920 if (found_extent) {
921 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500922 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500923 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500924 root_gen, inode->i_ino,
925 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400926 BUG_ON(ret);
927 }
Chris Mason85e21ba2008-01-29 15:11:36 -0500928next:
929 if (path->slots[0] == 0) {
930 if (pending_del_nr)
931 goto del_pending;
932 btrfs_release_path(root, path);
933 goto search_again;
934 }
935
936 path->slots[0]--;
937 if (pending_del_nr &&
938 path->slots[0] + 1 != pending_del_slot) {
939 struct btrfs_key debug;
940del_pending:
941 btrfs_item_key_to_cpu(path->nodes[0], &debug,
942 pending_del_slot);
943 ret = btrfs_del_items(trans, root, path,
944 pending_del_slot,
945 pending_del_nr);
946 BUG_ON(ret);
947 pending_del_nr = 0;
948 btrfs_release_path(root, path);
949 goto search_again;
950 }
Chris Mason39279cc2007-06-12 06:35:45 -0400951 }
952 ret = 0;
953error:
Chris Mason85e21ba2008-01-29 15:11:36 -0500954 if (pending_del_nr) {
955 ret = btrfs_del_items(trans, root, path, pending_del_slot,
956 pending_del_nr);
957 }
Chris Mason39279cc2007-06-12 06:35:45 -0400958 btrfs_release_path(root, path);
959 btrfs_free_path(path);
960 inode->i_sb->s_dirt = 1;
961 return ret;
962}
963
Chris Masonb888db22007-08-27 16:49:44 -0400964static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400965 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400966{
Chris Mason39279cc2007-06-12 06:35:45 -0400967 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -0500968 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400969 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400970 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500971 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400972
Chris Mason190662b2007-12-18 16:25:45 -0500973 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400974 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400975
Chris Masond1310b22008-01-24 16:13:08 -0500976 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500977 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -0400978 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500979
Chris Masona52d9a82007-08-27 16:49:44 -0400980 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400981 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400982 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
983 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400984 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400985 }
Chris Masonb888db22007-08-27 16:49:44 -0400986 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -0500987 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400988
Chris Masona52d9a82007-08-27 16:49:44 -0400989 return ret;
990}
991
992/*
993 * taken from block_truncate_page, but does cow as it zeros out
994 * any bytes left in the last page in the file.
995 */
996static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
997{
998 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400999 struct btrfs_root *root = BTRFS_I(inode)->root;
1000 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001001 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1002 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1003 struct page *page;
1004 int ret = 0;
1005 u64 page_start;
1006
1007 if ((offset & (blocksize - 1)) == 0)
1008 goto out;
1009
1010 ret = -ENOMEM;
1011 page = grab_cache_page(mapping, index);
1012 if (!page)
1013 goto out;
1014 if (!PageUptodate(page)) {
1015 ret = btrfs_readpage(NULL, page);
1016 lock_page(page);
1017 if (!PageUptodate(page)) {
1018 ret = -EIO;
1019 goto out;
1020 }
1021 }
Chris Mason35ebb932007-10-30 16:56:53 -04001022 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001023
Chris Masonb888db22007-08-27 16:49:44 -04001024 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001025
Chris Mason39279cc2007-06-12 06:35:45 -04001026 unlock_page(page);
1027 page_cache_release(page);
1028out:
1029 return ret;
1030}
1031
1032static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1033{
1034 struct inode *inode = dentry->d_inode;
1035 int err;
1036
1037 err = inode_change_ok(inode, attr);
1038 if (err)
1039 return err;
1040
1041 if (S_ISREG(inode->i_mode) &&
1042 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1043 struct btrfs_trans_handle *trans;
1044 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001045 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001046
Chris Mason5f39d392007-10-15 16:14:19 -04001047 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001048 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001049 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001050 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001051 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001052
Chris Mason1b0f7c22008-01-30 14:33:02 -05001053 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001054 goto out;
1055
Chris Mason1832a6d2007-12-21 16:27:21 -05001056 mutex_lock(&root->fs_info->fs_mutex);
1057 err = btrfs_check_free_space(root, 1, 0);
1058 mutex_unlock(&root->fs_info->fs_mutex);
1059 if (err)
1060 goto fail;
1061
Chris Mason39279cc2007-06-12 06:35:45 -04001062 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1063
Chris Mason1b0f7c22008-01-30 14:33:02 -05001064 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001065 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001066
1067 mutex_lock(&root->fs_info->fs_mutex);
1068 trans = btrfs_start_transaction(root, 1);
1069 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001070 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001071 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001072 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001073
Chris Mason179e29e2007-11-01 11:28:41 -04001074 if (alloc_hint != EXTENT_MAP_INLINE) {
1075 err = btrfs_insert_file_extent(trans, root,
1076 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001077 hole_start, 0, 0,
1078 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001079 btrfs_drop_extent_cache(inode, hole_start,
1080 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001081 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001082 }
Chris Mason39279cc2007-06-12 06:35:45 -04001083 btrfs_end_transaction(trans, root);
1084 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001085 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001086 if (err)
1087 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001088 }
1089out:
1090 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001091fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001092 return err;
1093}
Chris Mason61295eb2008-01-14 16:24:38 -05001094
Chris Mason2da98f02008-01-16 11:44:43 -05001095void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001096{
Chris Mason2da98f02008-01-16 11:44:43 -05001097 int ret;
1098
1099 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001100 return;
1101 }
Chris Mason2da98f02008-01-16 11:44:43 -05001102
1103 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1104 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1105 return;
1106
1107 ret = btrfs_del_ordered_inode(inode);
1108 if (ret == 1) {
1109 atomic_dec(&inode->i_count);
1110 }
Chris Mason61295eb2008-01-14 16:24:38 -05001111}
1112
Chris Mason39279cc2007-06-12 06:35:45 -04001113void btrfs_delete_inode(struct inode *inode)
1114{
1115 struct btrfs_trans_handle *trans;
1116 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001117 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001118 int ret;
1119
1120 truncate_inode_pages(&inode->i_data, 0);
1121 if (is_bad_inode(inode)) {
1122 goto no_delete;
1123 }
Chris Mason5f39d392007-10-15 16:14:19 -04001124
Chris Mason39279cc2007-06-12 06:35:45 -04001125 inode->i_size = 0;
1126 mutex_lock(&root->fs_info->fs_mutex);
1127 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001128
Chris Mason39279cc2007-06-12 06:35:45 -04001129 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001130 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001131 if (ret)
1132 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001133
Chris Masond3c2fdc2007-09-17 10:58:06 -04001134 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001135 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001136
Chris Mason39279cc2007-06-12 06:35:45 -04001137 btrfs_end_transaction(trans, root);
1138 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001139 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001140 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001141 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001142
1143no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001144 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001145 btrfs_end_transaction(trans, root);
1146 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001147 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001148 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001149no_delete:
1150 clear_inode(inode);
1151}
1152
1153/*
1154 * this returns the key found in the dir entry in the location pointer.
1155 * If no dir entries were found, location->objectid is 0.
1156 */
1157static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1158 struct btrfs_key *location)
1159{
1160 const char *name = dentry->d_name.name;
1161 int namelen = dentry->d_name.len;
1162 struct btrfs_dir_item *di;
1163 struct btrfs_path *path;
1164 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001165 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001166
Chris Mason39544012007-12-12 14:38:19 -05001167 if (namelen == 1 && strcmp(name, ".") == 0) {
1168 location->objectid = dir->i_ino;
1169 location->type = BTRFS_INODE_ITEM_KEY;
1170 location->offset = 0;
1171 return 0;
1172 }
Chris Mason39279cc2007-06-12 06:35:45 -04001173 path = btrfs_alloc_path();
1174 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001175
Chris Mason7a720532007-12-13 09:06:59 -05001176 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001177 struct btrfs_key key;
1178 struct extent_buffer *leaf;
1179 u32 nritems;
1180 int slot;
1181
1182 key.objectid = dir->i_ino;
1183 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1184 key.offset = 0;
1185 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1186 BUG_ON(ret == 0);
1187 ret = 0;
1188
1189 leaf = path->nodes[0];
1190 slot = path->slots[0];
1191 nritems = btrfs_header_nritems(leaf);
1192 if (slot >= nritems)
1193 goto out_err;
1194
1195 btrfs_item_key_to_cpu(leaf, &key, slot);
1196 if (key.objectid != dir->i_ino ||
1197 key.type != BTRFS_INODE_REF_KEY) {
1198 goto out_err;
1199 }
1200 location->objectid = key.offset;
1201 location->type = BTRFS_INODE_ITEM_KEY;
1202 location->offset = 0;
1203 goto out;
1204 }
1205
Chris Mason39279cc2007-06-12 06:35:45 -04001206 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1207 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001208 if (IS_ERR(di))
1209 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001210 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001211 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001212 }
Chris Mason5f39d392007-10-15 16:14:19 -04001213 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001214out:
Chris Mason39279cc2007-06-12 06:35:45 -04001215 btrfs_free_path(path);
1216 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001217out_err:
1218 location->objectid = 0;
1219 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001220}
1221
1222/*
1223 * when we hit a tree root in a directory, the btrfs part of the inode
1224 * needs to be changed to reflect the root directory of the tree root. This
1225 * is kind of like crossing a mount point.
1226 */
1227static int fixup_tree_root_location(struct btrfs_root *root,
1228 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001229 struct btrfs_root **sub_root,
1230 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001231{
1232 struct btrfs_path *path;
1233 struct btrfs_root_item *ri;
1234
1235 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1236 return 0;
1237 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1238 return 0;
1239
1240 path = btrfs_alloc_path();
1241 BUG_ON(!path);
1242 mutex_lock(&root->fs_info->fs_mutex);
1243
Josef Bacik58176a92007-08-29 15:47:34 -04001244 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1245 dentry->d_name.name,
1246 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001247 if (IS_ERR(*sub_root))
1248 return PTR_ERR(*sub_root);
1249
1250 ri = &(*sub_root)->root_item;
1251 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001252 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1253 location->offset = 0;
1254
1255 btrfs_free_path(path);
1256 mutex_unlock(&root->fs_info->fs_mutex);
1257 return 0;
1258}
1259
1260static int btrfs_init_locked_inode(struct inode *inode, void *p)
1261{
1262 struct btrfs_iget_args *args = p;
1263 inode->i_ino = args->ino;
1264 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001265 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001266 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1267 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001268 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001269 return 0;
1270}
1271
1272static int btrfs_find_actor(struct inode *inode, void *opaque)
1273{
1274 struct btrfs_iget_args *args = opaque;
1275 return (args->ino == inode->i_ino &&
1276 args->root == BTRFS_I(inode)->root);
1277}
1278
Chris Masondc17ff82008-01-08 15:46:30 -05001279struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1280 u64 root_objectid)
1281{
1282 struct btrfs_iget_args args;
1283 args.ino = objectid;
1284 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1285
1286 if (!args.root)
1287 return NULL;
1288
1289 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1290}
1291
Chris Mason39279cc2007-06-12 06:35:45 -04001292struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1293 struct btrfs_root *root)
1294{
1295 struct inode *inode;
1296 struct btrfs_iget_args args;
1297 args.ino = objectid;
1298 args.root = root;
1299
1300 inode = iget5_locked(s, objectid, btrfs_find_actor,
1301 btrfs_init_locked_inode,
1302 (void *)&args);
1303 return inode;
1304}
1305
1306static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1307 struct nameidata *nd)
1308{
1309 struct inode * inode;
1310 struct btrfs_inode *bi = BTRFS_I(dir);
1311 struct btrfs_root *root = bi->root;
1312 struct btrfs_root *sub_root = root;
1313 struct btrfs_key location;
1314 int ret;
1315
1316 if (dentry->d_name.len > BTRFS_NAME_LEN)
1317 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001318
Chris Mason39279cc2007-06-12 06:35:45 -04001319 mutex_lock(&root->fs_info->fs_mutex);
1320 ret = btrfs_inode_by_name(dir, dentry, &location);
1321 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001322
Chris Mason39279cc2007-06-12 06:35:45 -04001323 if (ret < 0)
1324 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001325
Chris Mason39279cc2007-06-12 06:35:45 -04001326 inode = NULL;
1327 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001328 ret = fixup_tree_root_location(root, &location, &sub_root,
1329 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001330 if (ret < 0)
1331 return ERR_PTR(ret);
1332 if (ret > 0)
1333 return ERR_PTR(-ENOENT);
1334 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1335 sub_root);
1336 if (!inode)
1337 return ERR_PTR(-EACCES);
1338 if (inode->i_state & I_NEW) {
1339 /* the inode and parent dir are two different roots */
1340 if (sub_root != root) {
1341 igrab(inode);
1342 sub_root->inode = inode;
1343 }
1344 BTRFS_I(inode)->root = sub_root;
1345 memcpy(&BTRFS_I(inode)->location, &location,
1346 sizeof(location));
1347 btrfs_read_locked_inode(inode);
1348 unlock_new_inode(inode);
1349 }
1350 }
1351 return d_splice_alias(inode, dentry);
1352}
1353
Chris Mason39279cc2007-06-12 06:35:45 -04001354static unsigned char btrfs_filetype_table[] = {
1355 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1356};
1357
1358static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1359{
Chris Mason6da6aba2007-12-18 16:15:09 -05001360 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001361 struct btrfs_root *root = BTRFS_I(inode)->root;
1362 struct btrfs_item *item;
1363 struct btrfs_dir_item *di;
1364 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001365 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001366 struct btrfs_path *path;
1367 int ret;
1368 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001369 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001370 int slot;
1371 int advance;
1372 unsigned char d_type;
1373 int over = 0;
1374 u32 di_cur;
1375 u32 di_total;
1376 u32 di_len;
1377 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001378 char tmp_name[32];
1379 char *name_ptr;
1380 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001381
1382 /* FIXME, use a real flag for deciding about the key type */
1383 if (root->fs_info->tree_root == root)
1384 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001385
Chris Mason39544012007-12-12 14:38:19 -05001386 /* special case for "." */
1387 if (filp->f_pos == 0) {
1388 over = filldir(dirent, ".", 1,
1389 1, inode->i_ino,
1390 DT_DIR);
1391 if (over)
1392 return 0;
1393 filp->f_pos = 1;
1394 }
1395
Chris Mason39279cc2007-06-12 06:35:45 -04001396 mutex_lock(&root->fs_info->fs_mutex);
1397 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001398 path = btrfs_alloc_path();
1399 path->reada = 2;
1400
1401 /* special case for .., just use the back ref */
1402 if (filp->f_pos == 1) {
1403 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1404 key.offset = 0;
1405 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1406 BUG_ON(ret == 0);
1407 leaf = path->nodes[0];
1408 slot = path->slots[0];
1409 nritems = btrfs_header_nritems(leaf);
1410 if (slot >= nritems) {
1411 btrfs_release_path(root, path);
1412 goto read_dir_items;
1413 }
1414 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1415 btrfs_release_path(root, path);
1416 if (found_key.objectid != key.objectid ||
1417 found_key.type != BTRFS_INODE_REF_KEY)
1418 goto read_dir_items;
1419 over = filldir(dirent, "..", 2,
1420 2, found_key.offset, DT_DIR);
1421 if (over)
1422 goto nopos;
1423 filp->f_pos = 2;
1424 }
1425
1426read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001427 btrfs_set_key_type(&key, key_type);
1428 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001429
Chris Mason39279cc2007-06-12 06:35:45 -04001430 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1431 if (ret < 0)
1432 goto err;
1433 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001434 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001435 leaf = path->nodes[0];
1436 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001437 slot = path->slots[0];
1438 if (advance || slot >= nritems) {
1439 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001440 ret = btrfs_next_leaf(root, path);
1441 if (ret)
1442 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001443 leaf = path->nodes[0];
1444 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001445 slot = path->slots[0];
1446 } else {
1447 slot++;
1448 path->slots[0]++;
1449 }
1450 }
1451 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001452 item = btrfs_item_nr(leaf, slot);
1453 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1454
1455 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001456 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001457 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001458 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001459 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001460 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001461
1462 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001463 advance = 1;
1464 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1465 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001466 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001467 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001468 struct btrfs_key location;
1469
1470 name_len = btrfs_dir_name_len(leaf, di);
1471 if (name_len < 32) {
1472 name_ptr = tmp_name;
1473 } else {
1474 name_ptr = kmalloc(name_len, GFP_NOFS);
1475 BUG_ON(!name_ptr);
1476 }
1477 read_extent_buffer(leaf, name_ptr,
1478 (unsigned long)(di + 1), name_len);
1479
1480 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1481 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001482 over = filldir(dirent, name_ptr, name_len,
1483 found_key.offset,
1484 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001485 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001486
1487 if (name_ptr != tmp_name)
1488 kfree(name_ptr);
1489
Chris Mason39279cc2007-06-12 06:35:45 -04001490 if (over)
1491 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001492 di_len = btrfs_dir_name_len(leaf, di) +
1493 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001494 di_cur += di_len;
1495 di = (struct btrfs_dir_item *)((char *)di + di_len);
1496 }
1497 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001498 if (key_type == BTRFS_DIR_INDEX_KEY)
1499 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1500 else
1501 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001502nopos:
1503 ret = 0;
1504err:
1505 btrfs_release_path(root, path);
1506 btrfs_free_path(path);
1507 mutex_unlock(&root->fs_info->fs_mutex);
1508 return ret;
1509}
1510
1511int btrfs_write_inode(struct inode *inode, int wait)
1512{
1513 struct btrfs_root *root = BTRFS_I(inode)->root;
1514 struct btrfs_trans_handle *trans;
1515 int ret = 0;
1516
1517 if (wait) {
1518 mutex_lock(&root->fs_info->fs_mutex);
1519 trans = btrfs_start_transaction(root, 1);
1520 btrfs_set_trans_block_group(trans, inode);
1521 ret = btrfs_commit_transaction(trans, root);
1522 mutex_unlock(&root->fs_info->fs_mutex);
1523 }
1524 return ret;
1525}
1526
1527/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001528 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001529 * inode changes. But, it is most likely to find the inode in cache.
1530 * FIXME, needs more benchmarking...there are no reasons other than performance
1531 * to keep or drop this code.
1532 */
1533void btrfs_dirty_inode(struct inode *inode)
1534{
1535 struct btrfs_root *root = BTRFS_I(inode)->root;
1536 struct btrfs_trans_handle *trans;
1537
1538 mutex_lock(&root->fs_info->fs_mutex);
1539 trans = btrfs_start_transaction(root, 1);
1540 btrfs_set_trans_block_group(trans, inode);
1541 btrfs_update_inode(trans, root, inode);
1542 btrfs_end_transaction(trans, root);
1543 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001544}
1545
1546static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1547 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001548 const char *name, int name_len,
1549 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001550 u64 objectid,
1551 struct btrfs_block_group_cache *group,
1552 int mode)
1553{
1554 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001555 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001556 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001557 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001558 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001559 struct btrfs_inode_ref *ref;
1560 struct btrfs_key key[2];
1561 u32 sizes[2];
1562 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001563 int ret;
1564 int owner;
1565
Chris Mason5f39d392007-10-15 16:14:19 -04001566 path = btrfs_alloc_path();
1567 BUG_ON(!path);
1568
Chris Mason39279cc2007-06-12 06:35:45 -04001569 inode = new_inode(root->fs_info->sb);
1570 if (!inode)
1571 return ERR_PTR(-ENOMEM);
1572
Chris Masond1310b22008-01-24 16:13:08 -05001573 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1574 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001575 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001576 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001577 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001578
Chris Mason39279cc2007-06-12 06:35:45 -04001579 if (mode & S_IFDIR)
1580 owner = 0;
1581 else
1582 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001583 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001584 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001585 if (!new_inode_group) {
1586 printk("find_block group failed\n");
1587 new_inode_group = group;
1588 }
1589 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001590 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001591
1592 key[0].objectid = objectid;
1593 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1594 key[0].offset = 0;
1595
1596 key[1].objectid = objectid;
1597 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1598 key[1].offset = ref_objectid;
1599
1600 sizes[0] = sizeof(struct btrfs_inode_item);
1601 sizes[1] = name_len + sizeof(*ref);
1602
1603 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1604 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001605 goto fail;
1606
Chris Mason9c583092008-01-29 15:15:18 -05001607 if (objectid > root->highest_inode)
1608 root->highest_inode = objectid;
1609
Chris Mason39279cc2007-06-12 06:35:45 -04001610 inode->i_uid = current->fsuid;
1611 inode->i_gid = current->fsgid;
1612 inode->i_mode = mode;
1613 inode->i_ino = objectid;
1614 inode->i_blocks = 0;
1615 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001616 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1617 struct btrfs_inode_item);
1618 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001619
1620 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1621 struct btrfs_inode_ref);
1622 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1623 ptr = (unsigned long)(ref + 1);
1624 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1625
Chris Mason5f39d392007-10-15 16:14:19 -04001626 btrfs_mark_buffer_dirty(path->nodes[0]);
1627 btrfs_free_path(path);
1628
Chris Mason39279cc2007-06-12 06:35:45 -04001629 location = &BTRFS_I(inode)->location;
1630 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001631 location->offset = 0;
1632 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1633
Chris Mason39279cc2007-06-12 06:35:45 -04001634 insert_inode_hash(inode);
1635 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001636fail:
1637 btrfs_free_path(path);
1638 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001639}
1640
1641static inline u8 btrfs_inode_type(struct inode *inode)
1642{
1643 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1644}
1645
1646static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001647 struct dentry *dentry, struct inode *inode,
1648 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001649{
1650 int ret;
1651 struct btrfs_key key;
1652 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001653 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001654
Chris Mason39279cc2007-06-12 06:35:45 -04001655 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001656 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1657 key.offset = 0;
1658
1659 ret = btrfs_insert_dir_item(trans, root,
1660 dentry->d_name.name, dentry->d_name.len,
1661 dentry->d_parent->d_inode->i_ino,
1662 &key, btrfs_inode_type(inode));
1663 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001664 if (add_backref) {
1665 ret = btrfs_insert_inode_ref(trans, root,
1666 dentry->d_name.name,
1667 dentry->d_name.len,
1668 inode->i_ino,
1669 dentry->d_parent->d_inode->i_ino);
1670 }
Chris Mason79c44582007-06-25 10:09:33 -04001671 parent_inode = dentry->d_parent->d_inode;
1672 parent_inode->i_size += dentry->d_name.len * 2;
1673 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001674 ret = btrfs_update_inode(trans, root,
1675 dentry->d_parent->d_inode);
1676 }
1677 return ret;
1678}
1679
1680static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001681 struct dentry *dentry, struct inode *inode,
1682 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001683{
Chris Mason9c583092008-01-29 15:15:18 -05001684 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001685 if (!err) {
1686 d_instantiate(dentry, inode);
1687 return 0;
1688 }
1689 if (err > 0)
1690 err = -EEXIST;
1691 return err;
1692}
1693
Josef Bacik618e21d2007-07-11 10:18:17 -04001694static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1695 int mode, dev_t rdev)
1696{
1697 struct btrfs_trans_handle *trans;
1698 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001699 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001700 int err;
1701 int drop_inode = 0;
1702 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001703 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001704
1705 if (!new_valid_dev(rdev))
1706 return -EINVAL;
1707
1708 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001709 err = btrfs_check_free_space(root, 1, 0);
1710 if (err)
1711 goto fail;
1712
Josef Bacik618e21d2007-07-11 10:18:17 -04001713 trans = btrfs_start_transaction(root, 1);
1714 btrfs_set_trans_block_group(trans, dir);
1715
1716 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1717 if (err) {
1718 err = -ENOSPC;
1719 goto out_unlock;
1720 }
1721
Chris Mason9c583092008-01-29 15:15:18 -05001722 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1723 dentry->d_name.len,
1724 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001725 BTRFS_I(dir)->block_group, mode);
1726 err = PTR_ERR(inode);
1727 if (IS_ERR(inode))
1728 goto out_unlock;
1729
1730 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001731 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001732 if (err)
1733 drop_inode = 1;
1734 else {
1735 inode->i_op = &btrfs_special_inode_operations;
1736 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001737 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001738 }
1739 dir->i_sb->s_dirt = 1;
1740 btrfs_update_inode_block_group(trans, inode);
1741 btrfs_update_inode_block_group(trans, dir);
1742out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001743 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001744 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001745fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001746 mutex_unlock(&root->fs_info->fs_mutex);
1747
1748 if (drop_inode) {
1749 inode_dec_link_count(inode);
1750 iput(inode);
1751 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001752 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001753 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001754 return err;
1755}
1756
Chris Mason39279cc2007-06-12 06:35:45 -04001757static int btrfs_create(struct inode *dir, struct dentry *dentry,
1758 int mode, struct nameidata *nd)
1759{
1760 struct btrfs_trans_handle *trans;
1761 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001762 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001763 int err;
1764 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001765 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001766 u64 objectid;
1767
1768 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001769 err = btrfs_check_free_space(root, 1, 0);
1770 if (err)
1771 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001772 trans = btrfs_start_transaction(root, 1);
1773 btrfs_set_trans_block_group(trans, dir);
1774
1775 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1776 if (err) {
1777 err = -ENOSPC;
1778 goto out_unlock;
1779 }
1780
Chris Mason9c583092008-01-29 15:15:18 -05001781 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1782 dentry->d_name.len,
1783 dentry->d_parent->d_inode->i_ino,
1784 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001785 err = PTR_ERR(inode);
1786 if (IS_ERR(inode))
1787 goto out_unlock;
1788
1789 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001790 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001791 if (err)
1792 drop_inode = 1;
1793 else {
1794 inode->i_mapping->a_ops = &btrfs_aops;
1795 inode->i_fop = &btrfs_file_operations;
1796 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001797 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1798 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001799 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001800 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001801 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001802 }
1803 dir->i_sb->s_dirt = 1;
1804 btrfs_update_inode_block_group(trans, inode);
1805 btrfs_update_inode_block_group(trans, dir);
1806out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001807 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001808 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001809fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001810 mutex_unlock(&root->fs_info->fs_mutex);
1811
1812 if (drop_inode) {
1813 inode_dec_link_count(inode);
1814 iput(inode);
1815 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001816 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001817 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001818 return err;
1819}
1820
1821static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1822 struct dentry *dentry)
1823{
1824 struct btrfs_trans_handle *trans;
1825 struct btrfs_root *root = BTRFS_I(dir)->root;
1826 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001827 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001828 int err;
1829 int drop_inode = 0;
1830
1831 if (inode->i_nlink == 0)
1832 return -ENOENT;
1833
Chris Mason6da6aba2007-12-18 16:15:09 -05001834#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1835 inode->i_nlink++;
1836#else
Chris Mason39279cc2007-06-12 06:35:45 -04001837 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001838#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001839 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001840 err = btrfs_check_free_space(root, 1, 0);
1841 if (err)
1842 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001843 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001844
Chris Mason39279cc2007-06-12 06:35:45 -04001845 btrfs_set_trans_block_group(trans, dir);
1846 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05001847 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001848
Chris Mason39279cc2007-06-12 06:35:45 -04001849 if (err)
1850 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001851
Chris Mason39279cc2007-06-12 06:35:45 -04001852 dir->i_sb->s_dirt = 1;
1853 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001854 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001855
Chris Mason54aa1f42007-06-22 14:16:25 -04001856 if (err)
1857 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001858
Chris Masond3c2fdc2007-09-17 10:58:06 -04001859 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001860 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001861fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001862 mutex_unlock(&root->fs_info->fs_mutex);
1863
1864 if (drop_inode) {
1865 inode_dec_link_count(inode);
1866 iput(inode);
1867 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001868 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001869 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001870 return err;
1871}
1872
Chris Mason39279cc2007-06-12 06:35:45 -04001873static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1874{
1875 struct inode *inode;
1876 struct btrfs_trans_handle *trans;
1877 struct btrfs_root *root = BTRFS_I(dir)->root;
1878 int err = 0;
1879 int drop_on_err = 0;
1880 u64 objectid;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001881 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001882
1883 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001884 err = btrfs_check_free_space(root, 1, 0);
1885 if (err)
1886 goto out_unlock;
1887
Chris Mason39279cc2007-06-12 06:35:45 -04001888 trans = btrfs_start_transaction(root, 1);
1889 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001890
Chris Mason39279cc2007-06-12 06:35:45 -04001891 if (IS_ERR(trans)) {
1892 err = PTR_ERR(trans);
1893 goto out_unlock;
1894 }
1895
1896 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1897 if (err) {
1898 err = -ENOSPC;
1899 goto out_unlock;
1900 }
1901
Chris Mason9c583092008-01-29 15:15:18 -05001902 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1903 dentry->d_name.len,
1904 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001905 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1906 if (IS_ERR(inode)) {
1907 err = PTR_ERR(inode);
1908 goto out_fail;
1909 }
Chris Mason5f39d392007-10-15 16:14:19 -04001910
Chris Mason39279cc2007-06-12 06:35:45 -04001911 drop_on_err = 1;
1912 inode->i_op = &btrfs_dir_inode_operations;
1913 inode->i_fop = &btrfs_dir_file_operations;
1914 btrfs_set_trans_block_group(trans, inode);
1915
Chris Mason39544012007-12-12 14:38:19 -05001916 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001917 err = btrfs_update_inode(trans, root, inode);
1918 if (err)
1919 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001920
Chris Mason9c583092008-01-29 15:15:18 -05001921 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001922 if (err)
1923 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001924
Chris Mason39279cc2007-06-12 06:35:45 -04001925 d_instantiate(dentry, inode);
1926 drop_on_err = 0;
1927 dir->i_sb->s_dirt = 1;
1928 btrfs_update_inode_block_group(trans, inode);
1929 btrfs_update_inode_block_group(trans, dir);
1930
1931out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001932 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001933 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001934
Chris Mason39279cc2007-06-12 06:35:45 -04001935out_unlock:
1936 mutex_unlock(&root->fs_info->fs_mutex);
1937 if (drop_on_err)
1938 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001939 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001940 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001941 return err;
1942}
1943
Chris Masona52d9a82007-08-27 16:49:44 -04001944struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05001945 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04001946 int create)
1947{
1948 int ret;
1949 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001950 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001951 u64 extent_start = 0;
1952 u64 extent_end = 0;
1953 u64 objectid = inode->i_ino;
1954 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04001955 struct btrfs_path *path;
1956 struct btrfs_root *root = BTRFS_I(inode)->root;
1957 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001958 struct extent_buffer *leaf;
1959 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001960 struct extent_map *em = NULL;
1961 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05001962 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04001963 struct btrfs_trans_handle *trans = NULL;
1964
1965 path = btrfs_alloc_path();
1966 BUG_ON(!path);
1967 mutex_lock(&root->fs_info->fs_mutex);
1968
1969again:
Chris Masond1310b22008-01-24 16:13:08 -05001970 spin_lock(&em_tree->lock);
1971 em = lookup_extent_mapping(em_tree, start, len);
1972 spin_unlock(&em_tree->lock);
1973
Chris Masona52d9a82007-08-27 16:49:44 -04001974 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001975 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05001976 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1977 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05001978 WARN_ON(1);
1979 }
Chris Mason70dec802008-01-29 09:59:12 -05001980 if (em->block_start == EXTENT_MAP_INLINE && page)
1981 free_extent_map(em);
1982 else
1983 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001984 }
Chris Masond1310b22008-01-24 16:13:08 -05001985 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001986 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05001987 err = -ENOMEM;
1988 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001989 }
Chris Masond1310b22008-01-24 16:13:08 -05001990
1991 em->start = EXTENT_MAP_HOLE;
1992 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04001993 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001994 ret = btrfs_lookup_file_extent(trans, root, path,
1995 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001996 if (ret < 0) {
1997 err = ret;
1998 goto out;
1999 }
2000
2001 if (ret != 0) {
2002 if (path->slots[0] == 0)
2003 goto not_found;
2004 path->slots[0]--;
2005 }
2006
Chris Mason5f39d392007-10-15 16:14:19 -04002007 leaf = path->nodes[0];
2008 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002009 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002010 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002011 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2012 found_type = btrfs_key_type(&found_key);
2013 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002014 found_type != BTRFS_EXTENT_DATA_KEY) {
2015 goto not_found;
2016 }
2017
Chris Mason5f39d392007-10-15 16:14:19 -04002018 found_type = btrfs_file_extent_type(leaf, item);
2019 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002020 if (found_type == BTRFS_FILE_EXTENT_REG) {
2021 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002022 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002023 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002024 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002025 em->start = start;
2026 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002027 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002028 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002029 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002030 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002031 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002032 }
2033 goto not_found_em;
2034 }
Chris Masondb945352007-10-15 16:15:53 -04002035 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2036 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002037 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002038 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002039 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002040 goto insert;
2041 }
Chris Masondb945352007-10-15 16:15:53 -04002042 bytenr += btrfs_file_extent_offset(leaf, item);
2043 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002044 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002045 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002046 goto insert;
2047 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002048 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002049 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002050 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002051 size_t size;
2052 size_t extent_offset;
2053 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002054
Chris Mason5f39d392007-10-15 16:14:19 -04002055 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2056 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002057 extent_end = (extent_start + size + root->sectorsize - 1) &
2058 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002059 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002060 em->start = start;
2061 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002062 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002063 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002064 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002065 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002066 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002067 }
2068 goto not_found_em;
2069 }
2070 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002071
2072 if (!page) {
2073 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002074 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002075 goto out;
2076 }
2077
Chris Mason70dec802008-01-29 09:59:12 -05002078 page_start = page_offset(page) + pg_offset;
2079 extent_offset = page_start - extent_start;
2080 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002081 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002082 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002083 em->len = (copy_size + root->sectorsize - 1) &
2084 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002085 map = kmap(page);
2086 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002087 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002088 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002089 copy_size);
2090 flush_dcache_page(page);
2091 } else if (create && PageUptodate(page)) {
2092 if (!trans) {
2093 kunmap(page);
2094 free_extent_map(em);
2095 em = NULL;
2096 btrfs_release_path(root, path);
2097 trans = btrfs_start_transaction(root, 1);
2098 goto again;
2099 }
Chris Mason70dec802008-01-29 09:59:12 -05002100 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002101 copy_size);
2102 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002103 }
Chris Masona52d9a82007-08-27 16:49:44 -04002104 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002105 set_extent_uptodate(io_tree, em->start,
2106 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002107 goto insert;
2108 } else {
2109 printk("unkknown found_type %d\n", found_type);
2110 WARN_ON(1);
2111 }
2112not_found:
2113 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002114 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002115not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002116 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002117insert:
2118 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002119 if (em->start > start || extent_map_end(em) <= start) {
2120 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002121 err = -EIO;
2122 goto out;
2123 }
Chris Masond1310b22008-01-24 16:13:08 -05002124
2125 err = 0;
2126 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002127 ret = add_extent_mapping(em_tree, em);
2128 if (ret == -EEXIST) {
2129 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002130 em = lookup_extent_mapping(em_tree, start, len);
2131 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002132 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002133 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002134 }
Chris Masona52d9a82007-08-27 16:49:44 -04002135 }
Chris Masond1310b22008-01-24 16:13:08 -05002136 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002137out:
2138 btrfs_free_path(path);
2139 if (trans) {
2140 ret = btrfs_end_transaction(trans, root);
2141 if (!err)
2142 err = ret;
2143 }
2144 mutex_unlock(&root->fs_info->fs_mutex);
2145 if (err) {
2146 free_extent_map(em);
2147 WARN_ON(1);
2148 return ERR_PTR(err);
2149 }
2150 return em;
2151}
2152
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002153static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002154{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002155 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002156}
2157
Chris Mason9ebefb182007-06-15 13:50:00 -04002158int btrfs_readpage(struct file *file, struct page *page)
2159{
Chris Masond1310b22008-01-24 16:13:08 -05002160 struct extent_io_tree *tree;
2161 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002162 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002163}
Chris Mason1832a6d2007-12-21 16:27:21 -05002164
Chris Mason39279cc2007-06-12 06:35:45 -04002165static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2166{
Chris Masond1310b22008-01-24 16:13:08 -05002167 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002168
2169
2170 if (current->flags & PF_MEMALLOC) {
2171 redirty_page_for_writepage(wbc, page);
2172 unlock_page(page);
2173 return 0;
2174 }
Chris Masond1310b22008-01-24 16:13:08 -05002175 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002176 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2177}
Chris Mason39279cc2007-06-12 06:35:45 -04002178
Chris Masonb293f022007-11-01 19:45:34 -04002179static int btrfs_writepages(struct address_space *mapping,
2180 struct writeback_control *wbc)
2181{
Chris Masond1310b22008-01-24 16:13:08 -05002182 struct extent_io_tree *tree;
2183 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002184 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2185}
2186
Chris Mason3ab2fb52007-11-08 10:59:22 -05002187static int
2188btrfs_readpages(struct file *file, struct address_space *mapping,
2189 struct list_head *pages, unsigned nr_pages)
2190{
Chris Masond1310b22008-01-24 16:13:08 -05002191 struct extent_io_tree *tree;
2192 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002193 return extent_readpages(tree, mapping, pages, nr_pages,
2194 btrfs_get_extent);
2195}
2196
Chris Mason70dec802008-01-29 09:59:12 -05002197static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002198{
Chris Masond1310b22008-01-24 16:13:08 -05002199 struct extent_io_tree *tree;
2200 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002201 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002202
Chris Masond1310b22008-01-24 16:13:08 -05002203 tree = &BTRFS_I(page->mapping->host)->io_tree;
2204 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002205 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002206 if (ret == 1) {
2207 ClearPagePrivate(page);
2208 set_page_private(page, 0);
2209 page_cache_release(page);
2210 }
2211 return ret;
2212}
Chris Mason39279cc2007-06-12 06:35:45 -04002213
Chris Masona52d9a82007-08-27 16:49:44 -04002214static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2215{
Chris Masond1310b22008-01-24 16:13:08 -05002216 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002217
Chris Masond1310b22008-01-24 16:13:08 -05002218 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002219 extent_invalidatepage(tree, page, offset);
2220 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002221}
2222
Chris Mason9ebefb182007-06-15 13:50:00 -04002223/*
2224 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2225 * called from a page fault handler when a page is first dirtied. Hence we must
2226 * be careful to check for EOF conditions here. We set the page up correctly
2227 * for a written page which means we get ENOSPC checking when writing into
2228 * holes and correct delalloc and unwritten extent mapping on filesystems that
2229 * support these features.
2230 *
2231 * We are not allowed to take the i_mutex here so we have to play games to
2232 * protect against truncate races as the page could now be beyond EOF. Because
2233 * vmtruncate() writes the inode size before removing pages, once we have the
2234 * page lock we can determine safely if the page is beyond EOF. If it is not
2235 * beyond EOF, then the page is guaranteed safe against truncation until we
2236 * unlock the page.
2237 */
2238int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2239{
Chris Mason6da6aba2007-12-18 16:15:09 -05002240 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002241 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002242 unsigned long end;
2243 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002244 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002245 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002246
Chris Mason1832a6d2007-12-21 16:27:21 -05002247 mutex_lock(&root->fs_info->fs_mutex);
2248 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002249 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002250 if (ret)
2251 goto out;
2252
2253 ret = -EINVAL;
2254
Chris Mason9ebefb182007-06-15 13:50:00 -04002255 lock_page(page);
2256 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002257 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002258 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002259
Chris Mason9ebefb182007-06-15 13:50:00 -04002260 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002261 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002262 /* page got truncated out from underneath us */
2263 goto out_unlock;
2264 }
2265
2266 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002267 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002268 end = size & ~PAGE_CACHE_MASK;
2269 else
2270 end = PAGE_CACHE_SIZE;
2271
Chris Masonb888db22007-08-27 16:49:44 -04002272 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002273
2274out_unlock:
2275 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002276out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002277 return ret;
2278}
2279
Chris Mason39279cc2007-06-12 06:35:45 -04002280static void btrfs_truncate(struct inode *inode)
2281{
2282 struct btrfs_root *root = BTRFS_I(inode)->root;
2283 int ret;
2284 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002285 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002286
2287 if (!S_ISREG(inode->i_mode))
2288 return;
2289 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2290 return;
2291
2292 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2293
2294 mutex_lock(&root->fs_info->fs_mutex);
2295 trans = btrfs_start_transaction(root, 1);
2296 btrfs_set_trans_block_group(trans, inode);
2297
2298 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002299 ret = btrfs_truncate_in_trans(trans, root, inode,
2300 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002301 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002302 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002303
Chris Mason39279cc2007-06-12 06:35:45 -04002304 ret = btrfs_end_transaction(trans, root);
2305 BUG_ON(ret);
2306 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002307 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002308 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002309}
2310
Chris Mason4313b392008-01-03 09:08:48 -05002311static int noinline create_subvol(struct btrfs_root *root, char *name,
2312 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002313{
2314 struct btrfs_trans_handle *trans;
2315 struct btrfs_key key;
2316 struct btrfs_root_item root_item;
2317 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002318 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002319 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002320 struct inode *inode;
2321 struct inode *dir;
2322 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002323 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002324 u64 objectid;
2325 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002326 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002327
2328 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002329 ret = btrfs_check_free_space(root, 1, 0);
2330 if (ret)
2331 goto fail_commit;
2332
Chris Mason39279cc2007-06-12 06:35:45 -04002333 trans = btrfs_start_transaction(root, 1);
2334 BUG_ON(!trans);
2335
Chris Mason7bb86312007-12-11 09:25:06 -05002336 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2337 0, &objectid);
2338 if (ret)
2339 goto fail;
2340
2341 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2342 objectid, trans->transid, 0, 0,
2343 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002344 if (IS_ERR(leaf))
2345 return PTR_ERR(leaf);
2346
2347 btrfs_set_header_nritems(leaf, 0);
2348 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002349 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002350 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002351 btrfs_set_header_owner(leaf, objectid);
2352
Chris Mason5f39d392007-10-15 16:14:19 -04002353 write_extent_buffer(leaf, root->fs_info->fsid,
2354 (unsigned long)btrfs_header_fsid(leaf),
2355 BTRFS_FSID_SIZE);
2356 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002357
2358 inode_item = &root_item.inode;
2359 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002360 inode_item->generation = cpu_to_le64(1);
2361 inode_item->size = cpu_to_le64(3);
2362 inode_item->nlink = cpu_to_le32(1);
2363 inode_item->nblocks = cpu_to_le64(1);
2364 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002365
Chris Masondb945352007-10-15 16:15:53 -04002366 btrfs_set_root_bytenr(&root_item, leaf->start);
2367 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002368 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002369 btrfs_set_root_used(&root_item, 0);
2370
Chris Mason5eda7b52007-06-22 14:16:25 -04002371 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2372 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002373
2374 free_extent_buffer(leaf);
2375 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002376
Chris Mason39279cc2007-06-12 06:35:45 -04002377 btrfs_set_root_dirid(&root_item, new_dirid);
2378
2379 key.objectid = objectid;
2380 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002381 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2382 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2383 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002384 if (ret)
2385 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002386
2387 /*
2388 * insert the directory item
2389 */
2390 key.offset = (u64)-1;
2391 dir = root->fs_info->sb->s_root->d_inode;
2392 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2393 name, namelen, dir->i_ino, &key,
2394 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002395 if (ret)
2396 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002397
Chris Mason39544012007-12-12 14:38:19 -05002398 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2399 name, namelen, objectid,
2400 root->fs_info->sb->s_root->d_inode->i_ino);
2401 if (ret)
2402 goto fail;
2403
Chris Mason39279cc2007-06-12 06:35:45 -04002404 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002405 if (ret)
2406 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002407
Josef Bacik58176a92007-08-29 15:47:34 -04002408 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002409 BUG_ON(!new_root);
2410
2411 trans = btrfs_start_transaction(new_root, 1);
2412 BUG_ON(!trans);
2413
Chris Mason9c583092008-01-29 15:15:18 -05002414 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2415 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002416 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002417 if (IS_ERR(inode))
2418 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002419 inode->i_op = &btrfs_dir_inode_operations;
2420 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002421 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002422
Chris Mason39544012007-12-12 14:38:19 -05002423 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2424 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002425 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002426 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002427 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002428 if (ret)
2429 goto fail;
2430fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002431 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002432 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002433 if (err && !ret)
2434 ret = err;
2435fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002436 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002437 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002438 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002439 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002440}
2441
2442static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2443{
Chris Mason3063d292008-01-08 15:46:30 -05002444 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002445 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002446 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002447 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002448 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002449
2450 if (!root->ref_cows)
2451 return -EINVAL;
2452
2453 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002454 ret = btrfs_check_free_space(root, 1, 0);
2455 if (ret)
2456 goto fail_unlock;
2457
Chris Mason3063d292008-01-08 15:46:30 -05002458 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2459 if (!pending_snapshot) {
2460 ret = -ENOMEM;
2461 goto fail_unlock;
2462 }
Yanfb4bc1e2008-01-17 11:59:51 -05002463 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002464 if (!pending_snapshot->name) {
2465 ret = -ENOMEM;
2466 kfree(pending_snapshot);
2467 goto fail_unlock;
2468 }
Yanfb4bc1e2008-01-17 11:59:51 -05002469 memcpy(pending_snapshot->name, name, namelen);
2470 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002471 trans = btrfs_start_transaction(root, 1);
2472 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002473 pending_snapshot->root = root;
2474 list_add(&pending_snapshot->list,
2475 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002476 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002477 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002478
Chris Mason1832a6d2007-12-21 16:27:21 -05002479fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002480 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002481 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002482 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002483 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002484}
2485
Chris Masonedbd8d42007-12-21 16:27:24 -05002486unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002487 struct file_ra_state *ra, struct file *file,
2488 pgoff_t offset, pgoff_t last_index)
2489{
2490 pgoff_t req_size;
2491
2492#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2493 req_size = last_index - offset + 1;
2494 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2495 return offset;
2496#else
2497 req_size = min(last_index - offset + 1, (pgoff_t)128);
2498 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2499 return offset + req_size;
2500#endif
2501}
2502
2503int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002504 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002505 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002506 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002507 struct page *page;
2508 unsigned long last_index;
2509 unsigned long ra_index = 0;
2510 u64 page_start;
2511 u64 page_end;
2512 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002513 int ret;
2514
2515 mutex_lock(&root->fs_info->fs_mutex);
2516 ret = btrfs_check_free_space(root, inode->i_size, 0);
2517 mutex_unlock(&root->fs_info->fs_mutex);
2518 if (ret)
2519 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002520
2521 mutex_lock(&inode->i_mutex);
2522 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2523 for (i = 0; i <= last_index; i++) {
2524 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002525 ra_index = btrfs_force_ra(inode->i_mapping,
2526 &file->f_ra,
2527 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002528 }
2529 page = grab_cache_page(inode->i_mapping, i);
2530 if (!page)
2531 goto out_unlock;
2532 if (!PageUptodate(page)) {
2533 btrfs_readpage(NULL, page);
2534 lock_page(page);
2535 if (!PageUptodate(page)) {
2536 unlock_page(page);
2537 page_cache_release(page);
2538 goto out_unlock;
2539 }
2540 }
Chris Mason35ebb932007-10-30 16:56:53 -04002541 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002542 page_end = page_start + PAGE_CACHE_SIZE - 1;
2543
Chris Masond1310b22008-01-24 16:13:08 -05002544 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002545 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002546 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002547
Chris Masond1310b22008-01-24 16:13:08 -05002548 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002549 set_page_dirty(page);
2550 unlock_page(page);
2551 page_cache_release(page);
2552 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2553 }
2554
2555out_unlock:
2556 mutex_unlock(&inode->i_mutex);
2557 return 0;
2558}
2559
Chris Masonedbd8d42007-12-21 16:27:24 -05002560static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2561{
2562 u64 new_size;
2563 u64 old_size;
2564 struct btrfs_ioctl_vol_args *vol_args;
2565 struct btrfs_trans_handle *trans;
2566 char *sizestr;
2567 int ret = 0;
2568 int namelen;
2569 int mod = 0;
2570
2571 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2572
2573 if (!vol_args)
2574 return -ENOMEM;
2575
2576 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2577 ret = -EFAULT;
2578 goto out;
2579 }
2580 namelen = strlen(vol_args->name);
2581 if (namelen > BTRFS_VOL_NAME_MAX) {
2582 ret = -EINVAL;
2583 goto out;
2584 }
2585
2586 sizestr = vol_args->name;
2587 if (!strcmp(sizestr, "max"))
2588 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2589 else {
2590 if (sizestr[0] == '-') {
2591 mod = -1;
2592 sizestr++;
2593 } else if (sizestr[0] == '+') {
2594 mod = 1;
2595 sizestr++;
2596 }
2597 new_size = btrfs_parse_size(sizestr);
2598 if (new_size == 0) {
2599 ret = -EINVAL;
2600 goto out;
2601 }
2602 }
2603
2604 mutex_lock(&root->fs_info->fs_mutex);
2605 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2606
2607 if (mod < 0) {
2608 if (new_size > old_size) {
2609 ret = -EINVAL;
2610 goto out_unlock;
2611 }
2612 new_size = old_size - new_size;
2613 } else if (mod > 0) {
2614 new_size = old_size + new_size;
2615 }
2616
2617 if (new_size < 256 * 1024 * 1024) {
2618 ret = -EINVAL;
2619 goto out_unlock;
2620 }
2621 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2622 ret = -EFBIG;
2623 goto out_unlock;
2624 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002625
2626 do_div(new_size, root->sectorsize);
2627 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002628
2629printk("new size is %Lu\n", new_size);
2630 if (new_size > old_size) {
2631 trans = btrfs_start_transaction(root, 1);
2632 ret = btrfs_grow_extent_tree(trans, root, new_size);
2633 btrfs_commit_transaction(trans, root);
2634 } else {
2635 ret = btrfs_shrink_extent_tree(root, new_size);
2636 }
2637
2638out_unlock:
2639 mutex_unlock(&root->fs_info->fs_mutex);
2640out:
2641 kfree(vol_args);
2642 return ret;
2643}
2644
Chris Mason4313b392008-01-03 09:08:48 -05002645static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2646 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002647{
Chris Mason4aec2b52007-12-18 16:25:45 -05002648 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002649 struct btrfs_dir_item *di;
2650 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002651 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002652 int namelen;
2653 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002654
Chris Mason4aec2b52007-12-18 16:25:45 -05002655 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002656
Chris Mason4aec2b52007-12-18 16:25:45 -05002657 if (!vol_args)
2658 return -ENOMEM;
2659
2660 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2661 ret = -EFAULT;
2662 goto out;
2663 }
2664
2665 namelen = strlen(vol_args->name);
2666 if (namelen > BTRFS_VOL_NAME_MAX) {
2667 ret = -EINVAL;
2668 goto out;
2669 }
2670 if (strchr(vol_args->name, '/')) {
2671 ret = -EINVAL;
2672 goto out;
2673 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002674
2675 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002676 if (!path) {
2677 ret = -ENOMEM;
2678 goto out;
2679 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002680
2681 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2682 mutex_lock(&root->fs_info->fs_mutex);
2683 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2684 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002685 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002686 mutex_unlock(&root->fs_info->fs_mutex);
2687 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002688
2689 if (di && !IS_ERR(di)) {
2690 ret = -EEXIST;
2691 goto out;
2692 }
2693
2694 if (IS_ERR(di)) {
2695 ret = PTR_ERR(di);
2696 goto out;
2697 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002698
2699 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002700 ret = create_subvol(root, vol_args->name, namelen);
2701 else
2702 ret = create_snapshot(root, vol_args->name, namelen);
2703out:
2704 kfree(vol_args);
2705 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002706}
2707
2708static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002709{
Chris Mason6da6aba2007-12-18 16:15:09 -05002710 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002711 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002712
2713 switch (inode->i_mode & S_IFMT) {
2714 case S_IFDIR:
2715 mutex_lock(&root->fs_info->fs_mutex);
2716 btrfs_defrag_root(root, 0);
2717 btrfs_defrag_root(root->fs_info->extent_root, 0);
2718 mutex_unlock(&root->fs_info->fs_mutex);
2719 break;
2720 case S_IFREG:
2721 btrfs_defrag_file(file);
2722 break;
2723 }
2724
2725 return 0;
2726}
2727
2728long btrfs_ioctl(struct file *file, unsigned int
2729 cmd, unsigned long arg)
2730{
Chris Mason6da6aba2007-12-18 16:15:09 -05002731 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002732
2733 switch (cmd) {
2734 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002735 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002736 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002737 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002738 case BTRFS_IOC_RESIZE:
2739 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002740 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002741
2742 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002743}
2744
Chris Mason39279cc2007-06-12 06:35:45 -04002745/*
2746 * Called inside transaction, so use GFP_NOFS
2747 */
2748struct inode *btrfs_alloc_inode(struct super_block *sb)
2749{
2750 struct btrfs_inode *ei;
2751
2752 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2753 if (!ei)
2754 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002755 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002756 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002757 return &ei->vfs_inode;
2758}
2759
2760void btrfs_destroy_inode(struct inode *inode)
2761{
2762 WARN_ON(!list_empty(&inode->i_dentry));
2763 WARN_ON(inode->i_data.nrpages);
2764
Chris Mason8c416c92008-01-14 15:10:26 -05002765 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002766 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2767}
2768
Chris Mason44ec0b72007-10-29 10:55:05 -04002769#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2770static void init_once(struct kmem_cache * cachep, void *foo)
2771#else
Chris Mason39279cc2007-06-12 06:35:45 -04002772static void init_once(void * foo, struct kmem_cache * cachep,
2773 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002774#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002775{
2776 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2777
2778 inode_init_once(&ei->vfs_inode);
2779}
2780
2781void btrfs_destroy_cachep(void)
2782{
2783 if (btrfs_inode_cachep)
2784 kmem_cache_destroy(btrfs_inode_cachep);
2785 if (btrfs_trans_handle_cachep)
2786 kmem_cache_destroy(btrfs_trans_handle_cachep);
2787 if (btrfs_transaction_cachep)
2788 kmem_cache_destroy(btrfs_transaction_cachep);
2789 if (btrfs_bit_radix_cachep)
2790 kmem_cache_destroy(btrfs_bit_radix_cachep);
2791 if (btrfs_path_cachep)
2792 kmem_cache_destroy(btrfs_path_cachep);
2793}
2794
Chris Mason86479a02007-09-10 19:58:16 -04002795struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002796 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002797#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2798 void (*ctor)(struct kmem_cache *, void *)
2799#else
Chris Mason92fee662007-07-25 12:31:35 -04002800 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002801 unsigned long)
2802#endif
2803 )
Chris Mason92fee662007-07-25 12:31:35 -04002804{
2805 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2806 SLAB_MEM_SPREAD | extra_flags), ctor
2807#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2808 ,NULL
2809#endif
2810 );
2811}
2812
Chris Mason39279cc2007-06-12 06:35:45 -04002813int btrfs_init_cachep(void)
2814{
Chris Mason86479a02007-09-10 19:58:16 -04002815 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002816 sizeof(struct btrfs_inode),
2817 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002818 if (!btrfs_inode_cachep)
2819 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002820 btrfs_trans_handle_cachep =
2821 btrfs_cache_create("btrfs_trans_handle_cache",
2822 sizeof(struct btrfs_trans_handle),
2823 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002824 if (!btrfs_trans_handle_cachep)
2825 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002826 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002827 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002828 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002829 if (!btrfs_transaction_cachep)
2830 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002831 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002832 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002833 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002834 if (!btrfs_path_cachep)
2835 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002836 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002837 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002838 if (!btrfs_bit_radix_cachep)
2839 goto fail;
2840 return 0;
2841fail:
2842 btrfs_destroy_cachep();
2843 return -ENOMEM;
2844}
2845
2846static int btrfs_getattr(struct vfsmount *mnt,
2847 struct dentry *dentry, struct kstat *stat)
2848{
2849 struct inode *inode = dentry->d_inode;
2850 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002851 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05002852 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04002853 return 0;
2854}
2855
2856static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2857 struct inode * new_dir,struct dentry *new_dentry)
2858{
2859 struct btrfs_trans_handle *trans;
2860 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2861 struct inode *new_inode = new_dentry->d_inode;
2862 struct inode *old_inode = old_dentry->d_inode;
2863 struct timespec ctime = CURRENT_TIME;
2864 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002865 int ret;
2866
2867 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2868 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2869 return -ENOTEMPTY;
2870 }
Chris Mason5f39d392007-10-15 16:14:19 -04002871
Chris Mason39279cc2007-06-12 06:35:45 -04002872 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002873 ret = btrfs_check_free_space(root, 1, 0);
2874 if (ret)
2875 goto out_unlock;
2876
Chris Mason39279cc2007-06-12 06:35:45 -04002877 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002878
Chris Mason39279cc2007-06-12 06:35:45 -04002879 btrfs_set_trans_block_group(trans, new_dir);
2880 path = btrfs_alloc_path();
2881 if (!path) {
2882 ret = -ENOMEM;
2883 goto out_fail;
2884 }
2885
2886 old_dentry->d_inode->i_nlink++;
2887 old_dir->i_ctime = old_dir->i_mtime = ctime;
2888 new_dir->i_ctime = new_dir->i_mtime = ctime;
2889 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002890
Chris Mason39279cc2007-06-12 06:35:45 -04002891 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2892 if (ret)
2893 goto out_fail;
2894
2895 if (new_inode) {
2896 new_inode->i_ctime = CURRENT_TIME;
2897 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2898 if (ret)
2899 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002900 }
Chris Mason9c583092008-01-29 15:15:18 -05002901 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002902 if (ret)
2903 goto out_fail;
2904
2905out_fail:
2906 btrfs_free_path(path);
2907 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002908out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002909 mutex_unlock(&root->fs_info->fs_mutex);
2910 return ret;
2911}
2912
2913static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2914 const char *symname)
2915{
2916 struct btrfs_trans_handle *trans;
2917 struct btrfs_root *root = BTRFS_I(dir)->root;
2918 struct btrfs_path *path;
2919 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002920 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002921 int err;
2922 int drop_inode = 0;
2923 u64 objectid;
2924 int name_len;
2925 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002926 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002927 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002928 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002929 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002930
2931 name_len = strlen(symname) + 1;
2932 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2933 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002934
Chris Mason39279cc2007-06-12 06:35:45 -04002935 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002936 err = btrfs_check_free_space(root, 1, 0);
2937 if (err)
2938 goto out_fail;
2939
Chris Mason39279cc2007-06-12 06:35:45 -04002940 trans = btrfs_start_transaction(root, 1);
2941 btrfs_set_trans_block_group(trans, dir);
2942
2943 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2944 if (err) {
2945 err = -ENOSPC;
2946 goto out_unlock;
2947 }
2948
Chris Mason9c583092008-01-29 15:15:18 -05002949 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2950 dentry->d_name.len,
2951 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002952 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2953 err = PTR_ERR(inode);
2954 if (IS_ERR(inode))
2955 goto out_unlock;
2956
2957 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002958 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002959 if (err)
2960 drop_inode = 1;
2961 else {
2962 inode->i_mapping->a_ops = &btrfs_aops;
2963 inode->i_fop = &btrfs_file_operations;
2964 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002965 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2966 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002967 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05002968 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002969 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002970 }
2971 dir->i_sb->s_dirt = 1;
2972 btrfs_update_inode_block_group(trans, inode);
2973 btrfs_update_inode_block_group(trans, dir);
2974 if (drop_inode)
2975 goto out_unlock;
2976
2977 path = btrfs_alloc_path();
2978 BUG_ON(!path);
2979 key.objectid = inode->i_ino;
2980 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002981 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2982 datasize = btrfs_file_extent_calc_inline_size(name_len);
2983 err = btrfs_insert_empty_item(trans, root, path, &key,
2984 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002985 if (err) {
2986 drop_inode = 1;
2987 goto out_unlock;
2988 }
Chris Mason5f39d392007-10-15 16:14:19 -04002989 leaf = path->nodes[0];
2990 ei = btrfs_item_ptr(leaf, path->slots[0],
2991 struct btrfs_file_extent_item);
2992 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2993 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002994 BTRFS_FILE_EXTENT_INLINE);
2995 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002996 write_extent_buffer(leaf, symname, ptr, name_len);
2997 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002998 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002999
Chris Mason39279cc2007-06-12 06:35:45 -04003000 inode->i_op = &btrfs_symlink_inode_operations;
3001 inode->i_mapping->a_ops = &btrfs_symlink_aops;
3002 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04003003 err = btrfs_update_inode(trans, root, inode);
3004 if (err)
3005 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003006
3007out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003008 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04003009 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003010out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003011 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04003012 if (drop_inode) {
3013 inode_dec_link_count(inode);
3014 iput(inode);
3015 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003016 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05003017 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003018 return err;
3019}
Yanfdebe2b2008-01-14 13:26:08 -05003020static int btrfs_permission(struct inode *inode, int mask,
3021 struct nameidata *nd)
3022{
3023 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3024 return -EACCES;
3025 return generic_permission(inode, mask, NULL);
3026}
Chris Mason39279cc2007-06-12 06:35:45 -04003027
3028static struct inode_operations btrfs_dir_inode_operations = {
3029 .lookup = btrfs_lookup,
3030 .create = btrfs_create,
3031 .unlink = btrfs_unlink,
3032 .link = btrfs_link,
3033 .mkdir = btrfs_mkdir,
3034 .rmdir = btrfs_rmdir,
3035 .rename = btrfs_rename,
3036 .symlink = btrfs_symlink,
3037 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003038 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003039 .setxattr = generic_setxattr,
3040 .getxattr = generic_getxattr,
3041 .listxattr = btrfs_listxattr,
3042 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003043 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003044};
Chris Mason39279cc2007-06-12 06:35:45 -04003045static struct inode_operations btrfs_dir_ro_inode_operations = {
3046 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003047 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003048};
Chris Mason39279cc2007-06-12 06:35:45 -04003049static struct file_operations btrfs_dir_file_operations = {
3050 .llseek = generic_file_llseek,
3051 .read = generic_read_dir,
3052 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003053 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003054#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003055 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003056#endif
3057};
3058
Chris Masond1310b22008-01-24 16:13:08 -05003059static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003060 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003061 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003062 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003063 .readpage_io_hook = btrfs_readpage_io_hook,
3064 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003065 .set_bit_hook = btrfs_set_bit_hook,
3066 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003067};
3068
Chris Mason39279cc2007-06-12 06:35:45 -04003069static struct address_space_operations btrfs_aops = {
3070 .readpage = btrfs_readpage,
3071 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003072 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003073 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003074 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003075 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04003076 .invalidatepage = btrfs_invalidatepage,
3077 .releasepage = btrfs_releasepage,
3078 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003079};
3080
3081static struct address_space_operations btrfs_symlink_aops = {
3082 .readpage = btrfs_readpage,
3083 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003084 .invalidatepage = btrfs_invalidatepage,
3085 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003086};
3087
3088static struct inode_operations btrfs_file_inode_operations = {
3089 .truncate = btrfs_truncate,
3090 .getattr = btrfs_getattr,
3091 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003092 .setxattr = generic_setxattr,
3093 .getxattr = generic_getxattr,
3094 .listxattr = btrfs_listxattr,
3095 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003096 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003097};
Josef Bacik618e21d2007-07-11 10:18:17 -04003098static struct inode_operations btrfs_special_inode_operations = {
3099 .getattr = btrfs_getattr,
3100 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003101 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003102};
Chris Mason39279cc2007-06-12 06:35:45 -04003103static struct inode_operations btrfs_symlink_inode_operations = {
3104 .readlink = generic_readlink,
3105 .follow_link = page_follow_link_light,
3106 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003107 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003108};