blob: 5632ea760077e7e26af2548e2e14e0f4eefcf79e [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,
Chris Mason98d20f62008-04-14 09:46:10 -0400128 root->sectorsize,
Chris Masonc59f8952007-12-17 20:14:04 -0500129 root->root_key.objectid,
130 trans->transid,
131 inode->i_ino, start, 0,
132 alloc_hint, (u64)-1, &ins, 1);
133 if (ret) {
134 WARN_ON(1);
135 goto out;
136 }
Chris Mason98d20f62008-04-14 09:46:10 -0400137 cur_alloc_size = ins.offset;
Chris Masonc59f8952007-12-17 20:14:04 -0500138 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
139 start, ins.objectid, ins.offset,
140 ins.offset);
Chris Mason90692182008-02-08 13:49:28 -0500141 inode->i_blocks += ins.offset >> 9;
Chris Mason5f564062008-01-22 16:47:59 -0500142 btrfs_check_file(root, inode);
Chris Masonc59f8952007-12-17 20:14:04 -0500143 num_bytes -= cur_alloc_size;
144 alloc_hint = ins.objectid + ins.offset;
145 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400146 }
Chris Masond1310b22008-01-24 16:13:08 -0500147 btrfs_drop_extent_cache(inode, orig_start,
148 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500149 btrfs_add_ordered_inode(inode);
Chris Mason90692182008-02-08 13:49:28 -0500150 btrfs_update_inode(trans, root, inode);
Chris Masonb888db22007-08-27 16:49:44 -0400151out:
152 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500153 return ret;
154}
155
156static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
157{
158 u64 extent_start;
159 u64 extent_end;
160 u64 bytenr;
161 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500162 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500163 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500164 struct btrfs_root *root = BTRFS_I(inode)->root;
165 struct extent_buffer *leaf;
166 int found_type;
167 struct btrfs_path *path;
168 struct btrfs_file_extent_item *item;
169 int ret;
170 int err;
171 struct btrfs_key found_key;
172
Chris Masonc31f8832008-01-08 15:46:31 -0500173 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500174 path = btrfs_alloc_path();
175 BUG_ON(!path);
176again:
177 ret = btrfs_lookup_file_extent(NULL, root, path,
178 inode->i_ino, start, 0);
179 if (ret < 0) {
180 btrfs_free_path(path);
181 return ret;
182 }
183
184 cow_end = end;
185 if (ret != 0) {
186 if (path->slots[0] == 0)
187 goto not_found;
188 path->slots[0]--;
189 }
190
191 leaf = path->nodes[0];
192 item = btrfs_item_ptr(leaf, path->slots[0],
193 struct btrfs_file_extent_item);
194
195 /* are we inside the extent that was found? */
196 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
197 found_type = btrfs_key_type(&found_key);
198 if (found_key.objectid != inode->i_ino ||
199 found_type != BTRFS_EXTENT_DATA_KEY) {
200 goto not_found;
201 }
202
203 found_type = btrfs_file_extent_type(leaf, item);
204 extent_start = found_key.offset;
205 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500206 u64 extent_num_bytes;
207
208 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
209 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500210 err = 0;
211
Chris Mason1832a6d2007-12-21 16:27:21 -0500212 if (loops && start != extent_start)
213 goto not_found;
214
Chris Masonbe20aa92007-12-17 20:14:01 -0500215 if (start < extent_start || start >= extent_end)
216 goto not_found;
217
218 cow_end = min(end, extent_end - 1);
219 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
220 if (bytenr == 0)
221 goto not_found;
222
Chris Masonc31f8832008-01-08 15:46:31 -0500223 /*
224 * we may be called by the resizer, make sure we're inside
225 * the limits of the FS
226 */
227 if (bytenr + extent_num_bytes > total_fs_bytes)
228 goto not_found;
229
Chris Masonbe20aa92007-12-17 20:14:01 -0500230 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
231 goto not_found;
232 }
233
234 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500235 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500236 goto not_found;
237 }
238loop:
239 if (start > end) {
240 btrfs_free_path(path);
241 return 0;
242 }
243 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500244 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500245 goto again;
246
247not_found:
248 cow_file_range(inode, start, cow_end);
249 start = cow_end + 1;
250 goto loop;
251}
252
253static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
254{
255 struct btrfs_root *root = BTRFS_I(inode)->root;
256 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500257 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500258 if (btrfs_test_opt(root, NODATACOW) ||
259 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500260 ret = run_delalloc_nocow(inode, start, end);
261 else
262 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500263
Chris Masonb888db22007-08-27 16:49:44 -0400264 mutex_unlock(&root->fs_info->fs_mutex);
265 return ret;
266}
267
Chris Mason291d6732008-01-29 15:55:23 -0500268int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500269 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500270{
Chris Masonb0c68f82008-01-31 11:05:37 -0500271 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500272 struct btrfs_root *root = BTRFS_I(inode)->root;
273 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -0500274 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500275 root->fs_info->delalloc_bytes += end - start + 1;
276 spin_unlock(&root->fs_info->delalloc_lock);
277 }
278 return 0;
279}
280
281int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500282 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500283{
Chris Masonb0c68f82008-01-31 11:05:37 -0500284 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500285 struct btrfs_root *root = BTRFS_I(inode)->root;
286 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -0500287 if (end - start + 1 > root->fs_info->delalloc_bytes) {
288 printk("warning: delalloc account %Lu %Lu\n",
289 end - start + 1, root->fs_info->delalloc_bytes);
290 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500291 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500292 } else {
293 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500294 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500295 }
Chris Mason291d6732008-01-29 15:55:23 -0500296 spin_unlock(&root->fs_info->delalloc_lock);
297 }
298 return 0;
299}
300
Chris Mason239b14b2008-03-24 15:02:07 -0400301int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
302 size_t size, struct bio *bio)
303{
304 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
305 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400306 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400307 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;
Chris Masoncea9e442008-04-09 16:28:12 -0400318 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400319 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400320
Chris Mason239b14b2008-03-24 15:02:07 -0400321 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400322 return 1;
323 }
324 return 0;
325}
326
Chris Mason44b8bd72008-04-16 11:14:51 -0400327int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400328 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500329{
Chris Mason065631f2008-02-20 12:07:25 -0500330 struct btrfs_root *root = BTRFS_I(inode)->root;
331 struct btrfs_trans_handle *trans;
332 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400333 char *sums = NULL;
334
335 ret = btrfs_csum_one_bio(root, bio, &sums);
336 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500337
Chris Mason44b8bd72008-04-16 11:14:51 -0400338 mutex_lock(&root->fs_info->fs_mutex);
339 trans = btrfs_start_transaction(root, 1);
Chris Masone0156402008-04-16 11:15:20 -0400340
Chris Mason44b8bd72008-04-16 11:14:51 -0400341 btrfs_set_trans_block_group(trans, inode);
Chris Masone0156402008-04-16 11:15:20 -0400342 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
343
Chris Mason44b8bd72008-04-16 11:14:51 -0400344 ret = btrfs_end_transaction(trans, root);
345 BUG_ON(ret);
346 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0156402008-04-16 11:15:20 -0400347
348 kfree(sums);
349
Chris Mason44b8bd72008-04-16 11:14:51 -0400350 return btrfs_map_bio(root, rw, bio, mirror_num);
351}
352
353int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
354 int mirror_num)
355{
356 struct btrfs_root *root = BTRFS_I(inode)->root;
357 int ret = 0;
358
Chris Mason22c59942008-04-09 16:28:12 -0400359 if (!(rw & (1 << BIO_RW))) {
360 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
361 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400362 goto mapit;
363 }
Chris Mason065631f2008-02-20 12:07:25 -0500364
365 if (btrfs_test_opt(root, NODATASUM) ||
Chris Mason0b86a832008-03-24 15:01:56 -0400366 btrfs_test_flag(inode, NODATASUM)) {
367 goto mapit;
368 }
Chris Mason065631f2008-02-20 12:07:25 -0500369
Chris Mason44b8bd72008-04-16 11:14:51 -0400370 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
371 inode, rw, bio, mirror_num,
372 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400373mapit:
Chris Masonf1885912008-04-09 16:28:12 -0400374 return btrfs_map_bio(root, rw, bio, mirror_num);
Chris Mason065631f2008-02-20 12:07:25 -0500375}
Chris Mason6885f302008-02-20 16:11:05 -0500376
Chris Mason07157aa2007-08-30 08:50:51 -0400377int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
378{
379 int ret = 0;
380 struct inode *inode = page->mapping->host;
381 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500382 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400383 struct btrfs_csum_item *item;
384 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400385 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400386
Yanb98b6762008-01-08 15:54:37 -0500387 if (btrfs_test_opt(root, NODATASUM) ||
388 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500389 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400390
Chris Mason07157aa2007-08-30 08:50:51 -0400391 mutex_lock(&root->fs_info->fs_mutex);
392 path = btrfs_alloc_path();
393 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
394 if (IS_ERR(item)) {
395 ret = PTR_ERR(item);
396 /* a csum that isn't present is a preallocated region. */
397 if (ret == -ENOENT || ret == -EFBIG)
398 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400399 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500400 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400401 goto out;
402 }
Chris Masonff79f812007-10-15 16:22:25 -0400403 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
404 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500405 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400406out:
407 if (path)
408 btrfs_free_path(path);
409 mutex_unlock(&root->fs_info->fs_mutex);
410 return ret;
411}
412
Chris Mason7e383262008-04-09 16:28:12 -0400413struct io_failure_record {
414 struct page *page;
415 u64 start;
416 u64 len;
417 u64 logical;
418 int last_mirror;
419};
420
421int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
422 struct page *page, u64 start, u64 end,
423 struct extent_state *state)
424{
425 struct io_failure_record *failrec = NULL;
426 u64 private;
427 struct extent_map *em;
428 struct inode *inode = page->mapping->host;
429 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
430 struct bio *bio;
431 int num_copies;
432 int ret;
433 u64 logical;
434
435 ret = get_state_private(failure_tree, start, &private);
436 if (ret) {
437 size_t pg_offset = start - page_offset(page);
438 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
439 if (!failrec)
440 return -ENOMEM;
441 failrec->start = start;
442 failrec->len = end - start + 1;
443 failrec->last_mirror = 0;
444
445 em = btrfs_get_extent(inode, NULL, pg_offset, start,
446 failrec->len, 0);
447
448 if (!em || IS_ERR(em)) {
449 kfree(failrec);
450 return -EIO;
451 }
452 logical = start - em->start;
453 logical = em->block_start + logical;
454 failrec->logical = logical;
455 free_extent_map(em);
456 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
457 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400458 set_state_private(failure_tree, start,
459 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400460 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400461 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400462 }
463 num_copies = btrfs_num_copies(
464 &BTRFS_I(inode)->root->fs_info->mapping_tree,
465 failrec->logical, failrec->len);
466 failrec->last_mirror++;
467 if (!state) {
468 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
469 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
470 failrec->start,
471 EXTENT_LOCKED);
472 if (state && state->start != failrec->start)
473 state = NULL;
474 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
475 }
476 if (!state || failrec->last_mirror > num_copies) {
477 set_state_private(failure_tree, failrec->start, 0);
478 clear_extent_bits(failure_tree, failrec->start,
479 failrec->start + failrec->len - 1,
480 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
481 kfree(failrec);
482 return -EIO;
483 }
484 bio = bio_alloc(GFP_NOFS, 1);
485 bio->bi_private = state;
486 bio->bi_end_io = failed_bio->bi_end_io;
487 bio->bi_sector = failrec->logical >> 9;
488 bio->bi_bdev = failed_bio->bi_bdev;
489 bio_add_page(bio, page, failrec->len, start - page_offset(page));
490 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
491 return 0;
492}
493
Chris Mason70dec802008-01-29 09:59:12 -0500494int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
495 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400496{
Chris Mason35ebb932007-10-30 16:56:53 -0400497 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400498 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500499 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400500 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500501 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400502 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400503 struct btrfs_root *root = BTRFS_I(inode)->root;
504 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400505 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500506
Yanb98b6762008-01-08 15:54:37 -0500507 if (btrfs_test_opt(root, NODATASUM) ||
508 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500509 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500510 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500511 private = state->private;
512 ret = 0;
513 } else {
514 ret = get_state_private(io_tree, start, &private);
515 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400516 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400517 kaddr = kmap_atomic(page, KM_IRQ0);
518 if (ret) {
519 goto zeroit;
520 }
Chris Masonff79f812007-10-15 16:22:25 -0400521 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
522 btrfs_csum_final(csum, (char *)&csum);
523 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400524 goto zeroit;
525 }
526 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400527 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400528
529 /* if the io failure tree for this inode is non-empty,
530 * check to see if we've recovered from a failed IO
531 */
532 private = 0;
533 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
534 (u64)-1, 1, EXTENT_DIRTY)) {
535 u64 private_failure;
536 struct io_failure_record *failure;
537 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
538 start, &private_failure);
539 if (ret == 0) {
Chris Mason587f7702008-04-11 12:16:46 -0400540 failure = (struct io_failure_record *)(unsigned long)
541 private_failure;
Chris Mason7e383262008-04-09 16:28:12 -0400542 set_state_private(&BTRFS_I(inode)->io_failure_tree,
543 failure->start, 0);
544 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
545 failure->start,
546 failure->start + failure->len - 1,
547 EXTENT_DIRTY | EXTENT_LOCKED,
548 GFP_NOFS);
549 kfree(failure);
550 }
551 }
Chris Mason07157aa2007-08-30 08:50:51 -0400552 return 0;
553
554zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500555 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
556 page->mapping->host->i_ino, (unsigned long long)start, csum,
557 private);
Chris Masondb945352007-10-15 16:15:53 -0400558 memset(kaddr + offset, 1, end - start + 1);
559 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400560 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400561 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400562 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400563}
Chris Masonb888db22007-08-27 16:49:44 -0400564
Chris Mason39279cc2007-06-12 06:35:45 -0400565void btrfs_read_locked_inode(struct inode *inode)
566{
567 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400568 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400569 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400570 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400571 struct btrfs_root *root = BTRFS_I(inode)->root;
572 struct btrfs_key location;
573 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400574 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400575 int ret;
576
577 path = btrfs_alloc_path();
578 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400579 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400580 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500581
Chris Mason39279cc2007-06-12 06:35:45 -0400582 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400583 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400584 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400585
Chris Mason5f39d392007-10-15 16:14:19 -0400586 leaf = path->nodes[0];
587 inode_item = btrfs_item_ptr(leaf, path->slots[0],
588 struct btrfs_inode_item);
589
590 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
591 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
592 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
593 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
594 inode->i_size = btrfs_inode_size(leaf, inode_item);
595
596 tspec = btrfs_inode_atime(inode_item);
597 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
598 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
599
600 tspec = btrfs_inode_mtime(inode_item);
601 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
602 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
603
604 tspec = btrfs_inode_ctime(inode_item);
605 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
606 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
607
608 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
609 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400610 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400611 rdev = btrfs_inode_rdev(leaf, inode_item);
612
613 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400614 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
615 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500616 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500617 if (!BTRFS_I(inode)->block_group) {
618 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400619 NULL, 0,
620 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500621 }
Chris Mason39279cc2007-06-12 06:35:45 -0400622 btrfs_free_path(path);
623 inode_item = NULL;
624
625 mutex_unlock(&root->fs_info->fs_mutex);
626
627 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400628 case S_IFREG:
629 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400630 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500631 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400632 inode->i_fop = &btrfs_file_operations;
633 inode->i_op = &btrfs_file_inode_operations;
634 break;
635 case S_IFDIR:
636 inode->i_fop = &btrfs_dir_file_operations;
637 if (root == root->fs_info->tree_root)
638 inode->i_op = &btrfs_dir_ro_inode_operations;
639 else
640 inode->i_op = &btrfs_dir_inode_operations;
641 break;
642 case S_IFLNK:
643 inode->i_op = &btrfs_symlink_inode_operations;
644 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400645 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400646 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400647 default:
648 init_special_inode(inode, inode->i_mode, rdev);
649 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400650 }
651 return;
652
653make_bad:
654 btrfs_release_path(root, path);
655 btrfs_free_path(path);
656 mutex_unlock(&root->fs_info->fs_mutex);
657 make_bad_inode(inode);
658}
659
Chris Mason5f39d392007-10-15 16:14:19 -0400660static void fill_inode_item(struct extent_buffer *leaf,
661 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400662 struct inode *inode)
663{
Chris Mason5f39d392007-10-15 16:14:19 -0400664 btrfs_set_inode_uid(leaf, item, inode->i_uid);
665 btrfs_set_inode_gid(leaf, item, inode->i_gid);
666 btrfs_set_inode_size(leaf, item, inode->i_size);
667 btrfs_set_inode_mode(leaf, item, inode->i_mode);
668 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
669
670 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
671 inode->i_atime.tv_sec);
672 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
673 inode->i_atime.tv_nsec);
674
675 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
676 inode->i_mtime.tv_sec);
677 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
678 inode->i_mtime.tv_nsec);
679
680 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
681 inode->i_ctime.tv_sec);
682 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
683 inode->i_ctime.tv_nsec);
684
685 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
686 btrfs_set_inode_generation(leaf, item, inode->i_generation);
687 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500688 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400689 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400690 BTRFS_I(inode)->block_group->key.objectid);
691}
692
Chris Masona52d9a82007-08-27 16:49:44 -0400693int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400694 struct btrfs_root *root,
695 struct inode *inode)
696{
697 struct btrfs_inode_item *inode_item;
698 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400699 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400700 int ret;
701
702 path = btrfs_alloc_path();
703 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400704 ret = btrfs_lookup_inode(trans, root, path,
705 &BTRFS_I(inode)->location, 1);
706 if (ret) {
707 if (ret > 0)
708 ret = -ENOENT;
709 goto failed;
710 }
711
Chris Mason5f39d392007-10-15 16:14:19 -0400712 leaf = path->nodes[0];
713 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400714 struct btrfs_inode_item);
715
Chris Mason5f39d392007-10-15 16:14:19 -0400716 fill_inode_item(leaf, inode_item, inode);
717 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400718 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400719 ret = 0;
720failed:
721 btrfs_release_path(root, path);
722 btrfs_free_path(path);
723 return ret;
724}
725
726
727static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
728 struct btrfs_root *root,
729 struct inode *dir,
730 struct dentry *dentry)
731{
732 struct btrfs_path *path;
733 const char *name = dentry->d_name.name;
734 int name_len = dentry->d_name.len;
735 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400736 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400737 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400738 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400739
740 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400741 if (!path) {
742 ret = -ENOMEM;
743 goto err;
744 }
745
Chris Mason39279cc2007-06-12 06:35:45 -0400746 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
747 name, name_len, -1);
748 if (IS_ERR(di)) {
749 ret = PTR_ERR(di);
750 goto err;
751 }
752 if (!di) {
753 ret = -ENOENT;
754 goto err;
755 }
Chris Mason5f39d392007-10-15 16:14:19 -0400756 leaf = path->nodes[0];
757 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400758 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400759 if (ret)
760 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400761 btrfs_release_path(root, path);
762
763 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400764 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400765 if (IS_ERR(di)) {
766 ret = PTR_ERR(di);
767 goto err;
768 }
769 if (!di) {
770 ret = -ENOENT;
771 goto err;
772 }
773 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400774
775 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500776 ret = btrfs_del_inode_ref(trans, root, name, name_len,
777 dentry->d_inode->i_ino,
778 dentry->d_parent->d_inode->i_ino);
779 if (ret) {
780 printk("failed to delete reference to %.*s, "
781 "inode %lu parent %lu\n", name_len, name,
782 dentry->d_inode->i_ino,
783 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500784 }
Chris Mason39279cc2007-06-12 06:35:45 -0400785err:
786 btrfs_free_path(path);
787 if (!ret) {
788 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400789 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400790 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500791#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
792 dentry->d_inode->i_nlink--;
793#else
Chris Mason39279cc2007-06-12 06:35:45 -0400794 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500795#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400796 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400797 dir->i_sb->s_dirt = 1;
798 }
799 return ret;
800}
801
802static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
803{
804 struct btrfs_root *root;
805 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500806 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400807 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500808 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400809
810 root = BTRFS_I(dir)->root;
811 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500812
813 ret = btrfs_check_free_space(root, 1, 1);
814 if (ret)
815 goto fail;
816
Chris Mason39279cc2007-06-12 06:35:45 -0400817 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400818
Chris Mason39279cc2007-06-12 06:35:45 -0400819 btrfs_set_trans_block_group(trans, dir);
820 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400821 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400822
Chris Mason2da98f02008-01-16 11:44:43 -0500823 if (inode->i_nlink == 0) {
824 int found;
825 /* if the inode isn't linked anywhere,
826 * we don't need to worry about
827 * data=ordered
828 */
829 found = btrfs_del_ordered_inode(inode);
830 if (found == 1) {
831 atomic_dec(&inode->i_count);
832 }
833 }
834
Chris Mason39279cc2007-06-12 06:35:45 -0400835 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500836fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400837 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400838 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500839 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400840 return ret;
841}
842
843static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
844{
845 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500846 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400847 int ret;
848 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400849 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500850 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400851
Yan134d4512007-10-25 15:49:25 -0400852 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
853 return -ENOTEMPTY;
854
Chris Mason39279cc2007-06-12 06:35:45 -0400855 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500856 ret = btrfs_check_free_space(root, 1, 1);
857 if (ret)
858 goto fail;
859
Chris Mason39279cc2007-06-12 06:35:45 -0400860 trans = btrfs_start_transaction(root, 1);
861 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400862
863 /* now the directory is empty */
864 err = btrfs_unlink_trans(trans, root, dir, dentry);
865 if (!err) {
866 inode->i_size = 0;
867 }
Chris Mason39544012007-12-12 14:38:19 -0500868
Chris Masond3c2fdc2007-09-17 10:58:06 -0400869 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400870 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500871fail:
Yan134d4512007-10-25 15:49:25 -0400872 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400873 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500874 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500875
Chris Mason39279cc2007-06-12 06:35:45 -0400876 if (ret && !err)
877 err = ret;
878 return err;
879}
880
Chris Mason39279cc2007-06-12 06:35:45 -0400881/*
Chris Mason39279cc2007-06-12 06:35:45 -0400882 * this can truncate away extent items, csum items and directory items.
883 * It starts at a high offset and removes keys until it can't find
884 * any higher than i_size.
885 *
886 * csum items that cross the new i_size are truncated to the new size
887 * as well.
888 */
889static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
890 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500891 struct inode *inode,
892 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400893{
894 int ret;
895 struct btrfs_path *path;
896 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400897 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400898 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400899 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400900 struct btrfs_file_extent_item *fi;
901 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400902 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400903 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500904 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500905 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400906 int found_extent;
907 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500908 int pending_del_nr = 0;
909 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400910 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400911
Chris Masona52d9a82007-08-27 16:49:44 -0400912 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400913 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400914 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400915 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400916
Chris Mason39279cc2007-06-12 06:35:45 -0400917 /* FIXME, add redo link to tree so we don't leak on crash */
918 key.objectid = inode->i_ino;
919 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400920 key.type = (u8)-1;
921
Chris Mason85e21ba2008-01-29 15:11:36 -0500922 btrfs_init_path(path);
923search_again:
924 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
925 if (ret < 0) {
926 goto error;
927 }
928 if (ret > 0) {
929 BUG_ON(path->slots[0] == 0);
930 path->slots[0]--;
931 }
932
Chris Mason39279cc2007-06-12 06:35:45 -0400933 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400934 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400935 leaf = path->nodes[0];
936 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
937 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400938
Chris Mason5f39d392007-10-15 16:14:19 -0400939 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400940 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400941
Chris Mason85e21ba2008-01-29 15:11:36 -0500942 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400943 break;
944
Chris Mason5f39d392007-10-15 16:14:19 -0400945 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400946 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400947 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400948 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400949 extent_type = btrfs_file_extent_type(leaf, fi);
950 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400951 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400952 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400953 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
954 struct btrfs_item *item = btrfs_item_nr(leaf,
955 path->slots[0]);
956 item_end += btrfs_file_extent_inline_len(leaf,
957 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400958 }
Yan008630c2007-11-07 13:31:09 -0500959 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400960 }
961 if (found_type == BTRFS_CSUM_ITEM_KEY) {
962 ret = btrfs_csum_truncate(trans, root, path,
963 inode->i_size);
964 BUG_ON(ret);
965 }
Yan008630c2007-11-07 13:31:09 -0500966 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400967 if (found_type == BTRFS_DIR_ITEM_KEY) {
968 found_type = BTRFS_INODE_ITEM_KEY;
969 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
970 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500971 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
972 found_type = BTRFS_XATTR_ITEM_KEY;
973 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
974 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400975 } else if (found_type) {
976 found_type--;
977 } else {
978 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400979 }
Yana61721d2007-09-17 11:08:38 -0400980 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500981 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400982 }
Chris Mason5f39d392007-10-15 16:14:19 -0400983 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400984 del_item = 1;
985 else
986 del_item = 0;
987 found_extent = 0;
988
989 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400990 if (found_type != BTRFS_EXTENT_DATA_KEY)
991 goto delete;
992
993 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400994 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400995 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400996 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400997 u64 orig_num_bytes =
998 btrfs_file_extent_num_bytes(leaf, fi);
999 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001000 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -05001001 extent_num_bytes = extent_num_bytes &
1002 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001003 btrfs_set_file_extent_num_bytes(leaf, fi,
1004 extent_num_bytes);
1005 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001006 extent_num_bytes);
1007 if (extent_start != 0)
1008 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001009 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001010 } else {
Chris Masondb945352007-10-15 16:15:53 -04001011 extent_num_bytes =
1012 btrfs_file_extent_disk_num_bytes(leaf,
1013 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001014 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001015 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001016 if (extent_start != 0) {
1017 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001018 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001019 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001020 root_gen = btrfs_header_generation(leaf);
1021 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001022 }
Chris Mason90692182008-02-08 13:49:28 -05001023 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1024 if (!del_item) {
1025 u32 newsize = inode->i_size - found_key.offset;
1026 dec_i_blocks(inode, item_end + 1 -
1027 found_key.offset - newsize);
1028 newsize =
1029 btrfs_file_extent_calc_inline_size(newsize);
1030 ret = btrfs_truncate_item(trans, root, path,
1031 newsize, 1);
1032 BUG_ON(ret);
1033 } else {
1034 dec_i_blocks(inode, item_end + 1 -
1035 found_key.offset);
1036 }
Chris Mason39279cc2007-06-12 06:35:45 -04001037 }
Chris Mason179e29e2007-11-01 11:28:41 -04001038delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001039 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001040 if (!pending_del_nr) {
1041 /* no pending yet, add ourselves */
1042 pending_del_slot = path->slots[0];
1043 pending_del_nr = 1;
1044 } else if (pending_del_nr &&
1045 path->slots[0] + 1 == pending_del_slot) {
1046 /* hop on the pending chunk */
1047 pending_del_nr++;
1048 pending_del_slot = path->slots[0];
1049 } else {
1050 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1051 }
Chris Mason39279cc2007-06-12 06:35:45 -04001052 } else {
1053 break;
1054 }
Chris Mason39279cc2007-06-12 06:35:45 -04001055 if (found_extent) {
1056 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001057 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001058 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001059 root_gen, inode->i_ino,
1060 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001061 BUG_ON(ret);
1062 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001063next:
1064 if (path->slots[0] == 0) {
1065 if (pending_del_nr)
1066 goto del_pending;
1067 btrfs_release_path(root, path);
1068 goto search_again;
1069 }
1070
1071 path->slots[0]--;
1072 if (pending_del_nr &&
1073 path->slots[0] + 1 != pending_del_slot) {
1074 struct btrfs_key debug;
1075del_pending:
1076 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1077 pending_del_slot);
1078 ret = btrfs_del_items(trans, root, path,
1079 pending_del_slot,
1080 pending_del_nr);
1081 BUG_ON(ret);
1082 pending_del_nr = 0;
1083 btrfs_release_path(root, path);
1084 goto search_again;
1085 }
Chris Mason39279cc2007-06-12 06:35:45 -04001086 }
1087 ret = 0;
1088error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001089 if (pending_del_nr) {
1090 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1091 pending_del_nr);
1092 }
Chris Mason39279cc2007-06-12 06:35:45 -04001093 btrfs_release_path(root, path);
1094 btrfs_free_path(path);
1095 inode->i_sb->s_dirt = 1;
1096 return ret;
1097}
1098
Chris Masonb888db22007-08-27 16:49:44 -04001099static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -04001100 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001101{
Chris Mason39279cc2007-06-12 06:35:45 -04001102 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -05001103 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001104 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001105 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -05001106 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001107
Chris Mason190662b2007-12-18 16:25:45 -05001108 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001109 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001110
Chris Masond1310b22008-01-24 16:13:08 -05001111 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001112 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -04001113 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -05001114
Chris Masona52d9a82007-08-27 16:49:44 -04001115 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -04001116 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001117 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1118 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -04001119 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001120 }
Chris Masonb888db22007-08-27 16:49:44 -04001121 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -05001122 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001123
Chris Masona52d9a82007-08-27 16:49:44 -04001124 return ret;
1125}
1126
1127/*
1128 * taken from block_truncate_page, but does cow as it zeros out
1129 * any bytes left in the last page in the file.
1130 */
1131static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1132{
1133 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001134 struct btrfs_root *root = BTRFS_I(inode)->root;
1135 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001136 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1137 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1138 struct page *page;
1139 int ret = 0;
1140 u64 page_start;
1141
1142 if ((offset & (blocksize - 1)) == 0)
1143 goto out;
1144
1145 ret = -ENOMEM;
1146 page = grab_cache_page(mapping, index);
1147 if (!page)
1148 goto out;
1149 if (!PageUptodate(page)) {
1150 ret = btrfs_readpage(NULL, page);
1151 lock_page(page);
1152 if (!PageUptodate(page)) {
1153 ret = -EIO;
1154 goto out;
1155 }
1156 }
Chris Mason35ebb932007-10-30 16:56:53 -04001157 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001158
Chris Masonb888db22007-08-27 16:49:44 -04001159 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001160
Chris Mason39279cc2007-06-12 06:35:45 -04001161 unlock_page(page);
1162 page_cache_release(page);
1163out:
1164 return ret;
1165}
1166
1167static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1168{
1169 struct inode *inode = dentry->d_inode;
1170 int err;
1171
1172 err = inode_change_ok(inode, attr);
1173 if (err)
1174 return err;
1175
1176 if (S_ISREG(inode->i_mode) &&
1177 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1178 struct btrfs_trans_handle *trans;
1179 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001180 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001181
Chris Mason5f39d392007-10-15 16:14:19 -04001182 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001183 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001184 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001185 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001186 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001187
Chris Mason1b0f7c22008-01-30 14:33:02 -05001188 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001189 goto out;
1190
Chris Mason1832a6d2007-12-21 16:27:21 -05001191 mutex_lock(&root->fs_info->fs_mutex);
1192 err = btrfs_check_free_space(root, 1, 0);
1193 mutex_unlock(&root->fs_info->fs_mutex);
1194 if (err)
1195 goto fail;
1196
Chris Mason39279cc2007-06-12 06:35:45 -04001197 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1198
Chris Mason1b0f7c22008-01-30 14:33:02 -05001199 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001200 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001201
1202 mutex_lock(&root->fs_info->fs_mutex);
1203 trans = btrfs_start_transaction(root, 1);
1204 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001205 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001206 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001207 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001208
Chris Mason179e29e2007-11-01 11:28:41 -04001209 if (alloc_hint != EXTENT_MAP_INLINE) {
1210 err = btrfs_insert_file_extent(trans, root,
1211 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001212 hole_start, 0, 0,
1213 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001214 btrfs_drop_extent_cache(inode, hole_start,
1215 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001216 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001217 }
Chris Mason39279cc2007-06-12 06:35:45 -04001218 btrfs_end_transaction(trans, root);
1219 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001220 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001221 if (err)
1222 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001223 }
1224out:
1225 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001226fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001227 return err;
1228}
Chris Mason61295eb2008-01-14 16:24:38 -05001229
Chris Mason2da98f02008-01-16 11:44:43 -05001230void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001231{
Chris Mason2da98f02008-01-16 11:44:43 -05001232 int ret;
1233
1234 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001235 return;
1236 }
Chris Mason2da98f02008-01-16 11:44:43 -05001237
1238 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1239 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1240 return;
1241
1242 ret = btrfs_del_ordered_inode(inode);
1243 if (ret == 1) {
1244 atomic_dec(&inode->i_count);
1245 }
Chris Mason61295eb2008-01-14 16:24:38 -05001246}
1247
Chris Mason39279cc2007-06-12 06:35:45 -04001248void btrfs_delete_inode(struct inode *inode)
1249{
1250 struct btrfs_trans_handle *trans;
1251 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001252 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001253 int ret;
1254
1255 truncate_inode_pages(&inode->i_data, 0);
1256 if (is_bad_inode(inode)) {
1257 goto no_delete;
1258 }
Chris Mason5f39d392007-10-15 16:14:19 -04001259
Chris Mason39279cc2007-06-12 06:35:45 -04001260 inode->i_size = 0;
1261 mutex_lock(&root->fs_info->fs_mutex);
1262 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001263
Chris Mason39279cc2007-06-12 06:35:45 -04001264 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001265 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001266 if (ret)
1267 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001268
Chris Masond3c2fdc2007-09-17 10:58:06 -04001269 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001270 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001271
Chris Mason39279cc2007-06-12 06:35:45 -04001272 btrfs_end_transaction(trans, root);
1273 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001274 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001275 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001276 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001277
1278no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001279 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001280 btrfs_end_transaction(trans, root);
1281 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001282 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001283 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001284no_delete:
1285 clear_inode(inode);
1286}
1287
1288/*
1289 * this returns the key found in the dir entry in the location pointer.
1290 * If no dir entries were found, location->objectid is 0.
1291 */
1292static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1293 struct btrfs_key *location)
1294{
1295 const char *name = dentry->d_name.name;
1296 int namelen = dentry->d_name.len;
1297 struct btrfs_dir_item *di;
1298 struct btrfs_path *path;
1299 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001300 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001301
Chris Mason39544012007-12-12 14:38:19 -05001302 if (namelen == 1 && strcmp(name, ".") == 0) {
1303 location->objectid = dir->i_ino;
1304 location->type = BTRFS_INODE_ITEM_KEY;
1305 location->offset = 0;
1306 return 0;
1307 }
Chris Mason39279cc2007-06-12 06:35:45 -04001308 path = btrfs_alloc_path();
1309 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001310
Chris Mason7a720532007-12-13 09:06:59 -05001311 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001312 struct btrfs_key key;
1313 struct extent_buffer *leaf;
1314 u32 nritems;
1315 int slot;
1316
1317 key.objectid = dir->i_ino;
1318 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1319 key.offset = 0;
1320 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1321 BUG_ON(ret == 0);
1322 ret = 0;
1323
1324 leaf = path->nodes[0];
1325 slot = path->slots[0];
1326 nritems = btrfs_header_nritems(leaf);
1327 if (slot >= nritems)
1328 goto out_err;
1329
1330 btrfs_item_key_to_cpu(leaf, &key, slot);
1331 if (key.objectid != dir->i_ino ||
1332 key.type != BTRFS_INODE_REF_KEY) {
1333 goto out_err;
1334 }
1335 location->objectid = key.offset;
1336 location->type = BTRFS_INODE_ITEM_KEY;
1337 location->offset = 0;
1338 goto out;
1339 }
1340
Chris Mason39279cc2007-06-12 06:35:45 -04001341 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1342 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001343 if (IS_ERR(di))
1344 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001345 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001346 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001347 }
Chris Mason5f39d392007-10-15 16:14:19 -04001348 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001349out:
Chris Mason39279cc2007-06-12 06:35:45 -04001350 btrfs_free_path(path);
1351 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001352out_err:
1353 location->objectid = 0;
1354 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001355}
1356
1357/*
1358 * when we hit a tree root in a directory, the btrfs part of the inode
1359 * needs to be changed to reflect the root directory of the tree root. This
1360 * is kind of like crossing a mount point.
1361 */
1362static int fixup_tree_root_location(struct btrfs_root *root,
1363 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001364 struct btrfs_root **sub_root,
1365 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001366{
1367 struct btrfs_path *path;
1368 struct btrfs_root_item *ri;
1369
1370 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1371 return 0;
1372 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1373 return 0;
1374
1375 path = btrfs_alloc_path();
1376 BUG_ON(!path);
1377 mutex_lock(&root->fs_info->fs_mutex);
1378
Josef Bacik58176a92007-08-29 15:47:34 -04001379 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1380 dentry->d_name.name,
1381 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001382 if (IS_ERR(*sub_root))
1383 return PTR_ERR(*sub_root);
1384
1385 ri = &(*sub_root)->root_item;
1386 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001387 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1388 location->offset = 0;
1389
1390 btrfs_free_path(path);
1391 mutex_unlock(&root->fs_info->fs_mutex);
1392 return 0;
1393}
1394
1395static int btrfs_init_locked_inode(struct inode *inode, void *p)
1396{
1397 struct btrfs_iget_args *args = p;
1398 inode->i_ino = args->ino;
1399 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001400 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001401 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1402 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001403 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001404 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1405 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001406 return 0;
1407}
1408
1409static int btrfs_find_actor(struct inode *inode, void *opaque)
1410{
1411 struct btrfs_iget_args *args = opaque;
1412 return (args->ino == inode->i_ino &&
1413 args->root == BTRFS_I(inode)->root);
1414}
1415
Chris Masondc17ff82008-01-08 15:46:30 -05001416struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1417 u64 root_objectid)
1418{
1419 struct btrfs_iget_args args;
1420 args.ino = objectid;
1421 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1422
1423 if (!args.root)
1424 return NULL;
1425
1426 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1427}
1428
Chris Mason39279cc2007-06-12 06:35:45 -04001429struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1430 struct btrfs_root *root)
1431{
1432 struct inode *inode;
1433 struct btrfs_iget_args args;
1434 args.ino = objectid;
1435 args.root = root;
1436
1437 inode = iget5_locked(s, objectid, btrfs_find_actor,
1438 btrfs_init_locked_inode,
1439 (void *)&args);
1440 return inode;
1441}
1442
1443static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1444 struct nameidata *nd)
1445{
1446 struct inode * inode;
1447 struct btrfs_inode *bi = BTRFS_I(dir);
1448 struct btrfs_root *root = bi->root;
1449 struct btrfs_root *sub_root = root;
1450 struct btrfs_key location;
1451 int ret;
1452
1453 if (dentry->d_name.len > BTRFS_NAME_LEN)
1454 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001455
Chris Mason39279cc2007-06-12 06:35:45 -04001456 mutex_lock(&root->fs_info->fs_mutex);
1457 ret = btrfs_inode_by_name(dir, dentry, &location);
1458 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001459
Chris Mason39279cc2007-06-12 06:35:45 -04001460 if (ret < 0)
1461 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001462
Chris Mason39279cc2007-06-12 06:35:45 -04001463 inode = NULL;
1464 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001465 ret = fixup_tree_root_location(root, &location, &sub_root,
1466 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001467 if (ret < 0)
1468 return ERR_PTR(ret);
1469 if (ret > 0)
1470 return ERR_PTR(-ENOENT);
1471 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1472 sub_root);
1473 if (!inode)
1474 return ERR_PTR(-EACCES);
1475 if (inode->i_state & I_NEW) {
1476 /* the inode and parent dir are two different roots */
1477 if (sub_root != root) {
1478 igrab(inode);
1479 sub_root->inode = inode;
1480 }
1481 BTRFS_I(inode)->root = sub_root;
1482 memcpy(&BTRFS_I(inode)->location, &location,
1483 sizeof(location));
1484 btrfs_read_locked_inode(inode);
1485 unlock_new_inode(inode);
1486 }
1487 }
1488 return d_splice_alias(inode, dentry);
1489}
1490
Chris Mason39279cc2007-06-12 06:35:45 -04001491static unsigned char btrfs_filetype_table[] = {
1492 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1493};
1494
1495static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1496{
Chris Mason6da6aba2007-12-18 16:15:09 -05001497 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001498 struct btrfs_root *root = BTRFS_I(inode)->root;
1499 struct btrfs_item *item;
1500 struct btrfs_dir_item *di;
1501 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001502 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001503 struct btrfs_path *path;
1504 int ret;
1505 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001506 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001507 int slot;
1508 int advance;
1509 unsigned char d_type;
1510 int over = 0;
1511 u32 di_cur;
1512 u32 di_total;
1513 u32 di_len;
1514 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001515 char tmp_name[32];
1516 char *name_ptr;
1517 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001518
1519 /* FIXME, use a real flag for deciding about the key type */
1520 if (root->fs_info->tree_root == root)
1521 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001522
Chris Mason39544012007-12-12 14:38:19 -05001523 /* special case for "." */
1524 if (filp->f_pos == 0) {
1525 over = filldir(dirent, ".", 1,
1526 1, inode->i_ino,
1527 DT_DIR);
1528 if (over)
1529 return 0;
1530 filp->f_pos = 1;
1531 }
1532
Chris Mason39279cc2007-06-12 06:35:45 -04001533 mutex_lock(&root->fs_info->fs_mutex);
1534 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001535 path = btrfs_alloc_path();
1536 path->reada = 2;
1537
1538 /* special case for .., just use the back ref */
1539 if (filp->f_pos == 1) {
1540 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1541 key.offset = 0;
1542 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1543 BUG_ON(ret == 0);
1544 leaf = path->nodes[0];
1545 slot = path->slots[0];
1546 nritems = btrfs_header_nritems(leaf);
1547 if (slot >= nritems) {
1548 btrfs_release_path(root, path);
1549 goto read_dir_items;
1550 }
1551 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1552 btrfs_release_path(root, path);
1553 if (found_key.objectid != key.objectid ||
1554 found_key.type != BTRFS_INODE_REF_KEY)
1555 goto read_dir_items;
1556 over = filldir(dirent, "..", 2,
1557 2, found_key.offset, DT_DIR);
1558 if (over)
1559 goto nopos;
1560 filp->f_pos = 2;
1561 }
1562
1563read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001564 btrfs_set_key_type(&key, key_type);
1565 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001566
Chris Mason39279cc2007-06-12 06:35:45 -04001567 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1568 if (ret < 0)
1569 goto err;
1570 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001571 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001572 leaf = path->nodes[0];
1573 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001574 slot = path->slots[0];
1575 if (advance || slot >= nritems) {
1576 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001577 ret = btrfs_next_leaf(root, path);
1578 if (ret)
1579 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001580 leaf = path->nodes[0];
1581 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001582 slot = path->slots[0];
1583 } else {
1584 slot++;
1585 path->slots[0]++;
1586 }
1587 }
1588 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001589 item = btrfs_item_nr(leaf, slot);
1590 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1591
1592 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001593 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001594 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001595 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001596 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001597 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001598
1599 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001600 advance = 1;
1601 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1602 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001603 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001604 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001605 struct btrfs_key location;
1606
1607 name_len = btrfs_dir_name_len(leaf, di);
1608 if (name_len < 32) {
1609 name_ptr = tmp_name;
1610 } else {
1611 name_ptr = kmalloc(name_len, GFP_NOFS);
1612 BUG_ON(!name_ptr);
1613 }
1614 read_extent_buffer(leaf, name_ptr,
1615 (unsigned long)(di + 1), name_len);
1616
1617 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1618 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001619 over = filldir(dirent, name_ptr, name_len,
1620 found_key.offset,
1621 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001622 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001623
1624 if (name_ptr != tmp_name)
1625 kfree(name_ptr);
1626
Chris Mason39279cc2007-06-12 06:35:45 -04001627 if (over)
1628 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001629 di_len = btrfs_dir_name_len(leaf, di) +
1630 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001631 di_cur += di_len;
1632 di = (struct btrfs_dir_item *)((char *)di + di_len);
1633 }
1634 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001635 if (key_type == BTRFS_DIR_INDEX_KEY)
1636 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1637 else
1638 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001639nopos:
1640 ret = 0;
1641err:
1642 btrfs_release_path(root, path);
1643 btrfs_free_path(path);
1644 mutex_unlock(&root->fs_info->fs_mutex);
1645 return ret;
1646}
1647
1648int btrfs_write_inode(struct inode *inode, int wait)
1649{
1650 struct btrfs_root *root = BTRFS_I(inode)->root;
1651 struct btrfs_trans_handle *trans;
1652 int ret = 0;
1653
1654 if (wait) {
1655 mutex_lock(&root->fs_info->fs_mutex);
1656 trans = btrfs_start_transaction(root, 1);
1657 btrfs_set_trans_block_group(trans, inode);
1658 ret = btrfs_commit_transaction(trans, root);
1659 mutex_unlock(&root->fs_info->fs_mutex);
1660 }
1661 return ret;
1662}
1663
1664/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001665 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001666 * inode changes. But, it is most likely to find the inode in cache.
1667 * FIXME, needs more benchmarking...there are no reasons other than performance
1668 * to keep or drop this code.
1669 */
1670void btrfs_dirty_inode(struct inode *inode)
1671{
1672 struct btrfs_root *root = BTRFS_I(inode)->root;
1673 struct btrfs_trans_handle *trans;
1674
1675 mutex_lock(&root->fs_info->fs_mutex);
1676 trans = btrfs_start_transaction(root, 1);
1677 btrfs_set_trans_block_group(trans, inode);
1678 btrfs_update_inode(trans, root, inode);
1679 btrfs_end_transaction(trans, root);
1680 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001681}
1682
1683static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1684 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001685 const char *name, int name_len,
1686 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001687 u64 objectid,
1688 struct btrfs_block_group_cache *group,
1689 int mode)
1690{
1691 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001692 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001693 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001694 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001695 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001696 struct btrfs_inode_ref *ref;
1697 struct btrfs_key key[2];
1698 u32 sizes[2];
1699 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001700 int ret;
1701 int owner;
1702
Chris Mason5f39d392007-10-15 16:14:19 -04001703 path = btrfs_alloc_path();
1704 BUG_ON(!path);
1705
Chris Mason39279cc2007-06-12 06:35:45 -04001706 inode = new_inode(root->fs_info->sb);
1707 if (!inode)
1708 return ERR_PTR(-ENOMEM);
1709
Chris Masond1310b22008-01-24 16:13:08 -05001710 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1711 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001712 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001713 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1714 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001715 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001716 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001717
Chris Mason39279cc2007-06-12 06:35:45 -04001718 if (mode & S_IFDIR)
1719 owner = 0;
1720 else
1721 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001722 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001723 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001724 if (!new_inode_group) {
1725 printk("find_block group failed\n");
1726 new_inode_group = group;
1727 }
1728 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001729 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001730
1731 key[0].objectid = objectid;
1732 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1733 key[0].offset = 0;
1734
1735 key[1].objectid = objectid;
1736 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1737 key[1].offset = ref_objectid;
1738
1739 sizes[0] = sizeof(struct btrfs_inode_item);
1740 sizes[1] = name_len + sizeof(*ref);
1741
1742 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1743 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001744 goto fail;
1745
Chris Mason9c583092008-01-29 15:15:18 -05001746 if (objectid > root->highest_inode)
1747 root->highest_inode = objectid;
1748
Chris Mason39279cc2007-06-12 06:35:45 -04001749 inode->i_uid = current->fsuid;
1750 inode->i_gid = current->fsgid;
1751 inode->i_mode = mode;
1752 inode->i_ino = objectid;
1753 inode->i_blocks = 0;
1754 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001755 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1756 struct btrfs_inode_item);
1757 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001758
1759 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1760 struct btrfs_inode_ref);
1761 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1762 ptr = (unsigned long)(ref + 1);
1763 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1764
Chris Mason5f39d392007-10-15 16:14:19 -04001765 btrfs_mark_buffer_dirty(path->nodes[0]);
1766 btrfs_free_path(path);
1767
Chris Mason39279cc2007-06-12 06:35:45 -04001768 location = &BTRFS_I(inode)->location;
1769 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001770 location->offset = 0;
1771 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1772
Chris Mason39279cc2007-06-12 06:35:45 -04001773 insert_inode_hash(inode);
1774 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001775fail:
1776 btrfs_free_path(path);
1777 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001778}
1779
1780static inline u8 btrfs_inode_type(struct inode *inode)
1781{
1782 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1783}
1784
1785static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001786 struct dentry *dentry, struct inode *inode,
1787 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001788{
1789 int ret;
1790 struct btrfs_key key;
1791 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001792 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001793
Chris Mason39279cc2007-06-12 06:35:45 -04001794 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001795 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1796 key.offset = 0;
1797
1798 ret = btrfs_insert_dir_item(trans, root,
1799 dentry->d_name.name, dentry->d_name.len,
1800 dentry->d_parent->d_inode->i_ino,
1801 &key, btrfs_inode_type(inode));
1802 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001803 if (add_backref) {
1804 ret = btrfs_insert_inode_ref(trans, root,
1805 dentry->d_name.name,
1806 dentry->d_name.len,
1807 inode->i_ino,
1808 dentry->d_parent->d_inode->i_ino);
1809 }
Chris Mason79c44582007-06-25 10:09:33 -04001810 parent_inode = dentry->d_parent->d_inode;
1811 parent_inode->i_size += dentry->d_name.len * 2;
1812 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001813 ret = btrfs_update_inode(trans, root,
1814 dentry->d_parent->d_inode);
1815 }
1816 return ret;
1817}
1818
1819static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001820 struct dentry *dentry, struct inode *inode,
1821 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001822{
Chris Mason9c583092008-01-29 15:15:18 -05001823 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001824 if (!err) {
1825 d_instantiate(dentry, inode);
1826 return 0;
1827 }
1828 if (err > 0)
1829 err = -EEXIST;
1830 return err;
1831}
1832
Josef Bacik618e21d2007-07-11 10:18:17 -04001833static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1834 int mode, dev_t rdev)
1835{
1836 struct btrfs_trans_handle *trans;
1837 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001838 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001839 int err;
1840 int drop_inode = 0;
1841 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001842 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001843
1844 if (!new_valid_dev(rdev))
1845 return -EINVAL;
1846
1847 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001848 err = btrfs_check_free_space(root, 1, 0);
1849 if (err)
1850 goto fail;
1851
Josef Bacik618e21d2007-07-11 10:18:17 -04001852 trans = btrfs_start_transaction(root, 1);
1853 btrfs_set_trans_block_group(trans, dir);
1854
1855 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1856 if (err) {
1857 err = -ENOSPC;
1858 goto out_unlock;
1859 }
1860
Chris Mason9c583092008-01-29 15:15:18 -05001861 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1862 dentry->d_name.len,
1863 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001864 BTRFS_I(dir)->block_group, mode);
1865 err = PTR_ERR(inode);
1866 if (IS_ERR(inode))
1867 goto out_unlock;
1868
1869 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001870 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001871 if (err)
1872 drop_inode = 1;
1873 else {
1874 inode->i_op = &btrfs_special_inode_operations;
1875 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001876 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001877 }
1878 dir->i_sb->s_dirt = 1;
1879 btrfs_update_inode_block_group(trans, inode);
1880 btrfs_update_inode_block_group(trans, dir);
1881out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001882 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001883 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001884fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001885 mutex_unlock(&root->fs_info->fs_mutex);
1886
1887 if (drop_inode) {
1888 inode_dec_link_count(inode);
1889 iput(inode);
1890 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001891 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001892 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001893 return err;
1894}
1895
Chris Mason39279cc2007-06-12 06:35:45 -04001896static int btrfs_create(struct inode *dir, struct dentry *dentry,
1897 int mode, struct nameidata *nd)
1898{
1899 struct btrfs_trans_handle *trans;
1900 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001901 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001902 int err;
1903 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001904 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001905 u64 objectid;
1906
1907 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001908 err = btrfs_check_free_space(root, 1, 0);
1909 if (err)
1910 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001911 trans = btrfs_start_transaction(root, 1);
1912 btrfs_set_trans_block_group(trans, dir);
1913
1914 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1915 if (err) {
1916 err = -ENOSPC;
1917 goto out_unlock;
1918 }
1919
Chris Mason9c583092008-01-29 15:15:18 -05001920 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1921 dentry->d_name.len,
1922 dentry->d_parent->d_inode->i_ino,
1923 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001924 err = PTR_ERR(inode);
1925 if (IS_ERR(inode))
1926 goto out_unlock;
1927
1928 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001929 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001930 if (err)
1931 drop_inode = 1;
1932 else {
1933 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001934 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001935 inode->i_fop = &btrfs_file_operations;
1936 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001937 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1938 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001939 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001940 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1941 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001942 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001943 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001944 }
1945 dir->i_sb->s_dirt = 1;
1946 btrfs_update_inode_block_group(trans, inode);
1947 btrfs_update_inode_block_group(trans, dir);
1948out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001949 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001950 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001951fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001952 mutex_unlock(&root->fs_info->fs_mutex);
1953
1954 if (drop_inode) {
1955 inode_dec_link_count(inode);
1956 iput(inode);
1957 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001958 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001959 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001960 return err;
1961}
1962
1963static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1964 struct dentry *dentry)
1965{
1966 struct btrfs_trans_handle *trans;
1967 struct btrfs_root *root = BTRFS_I(dir)->root;
1968 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001969 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001970 int err;
1971 int drop_inode = 0;
1972
1973 if (inode->i_nlink == 0)
1974 return -ENOENT;
1975
Chris Mason6da6aba2007-12-18 16:15:09 -05001976#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1977 inode->i_nlink++;
1978#else
Chris Mason39279cc2007-06-12 06:35:45 -04001979 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001980#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001981 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001982 err = btrfs_check_free_space(root, 1, 0);
1983 if (err)
1984 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001985 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001986
Chris Mason39279cc2007-06-12 06:35:45 -04001987 btrfs_set_trans_block_group(trans, dir);
1988 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05001989 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001990
Chris Mason39279cc2007-06-12 06:35:45 -04001991 if (err)
1992 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001993
Chris Mason39279cc2007-06-12 06:35:45 -04001994 dir->i_sb->s_dirt = 1;
1995 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001996 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001997
Chris Mason54aa1f42007-06-22 14:16:25 -04001998 if (err)
1999 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002000
Chris Masond3c2fdc2007-09-17 10:58:06 -04002001 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002002 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002003fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002004 mutex_unlock(&root->fs_info->fs_mutex);
2005
2006 if (drop_inode) {
2007 inode_dec_link_count(inode);
2008 iput(inode);
2009 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002010 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002011 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002012 return err;
2013}
2014
Chris Mason39279cc2007-06-12 06:35:45 -04002015static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2016{
2017 struct inode *inode;
2018 struct btrfs_trans_handle *trans;
2019 struct btrfs_root *root = BTRFS_I(dir)->root;
2020 int err = 0;
2021 int drop_on_err = 0;
2022 u64 objectid;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002023 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002024
2025 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002026 err = btrfs_check_free_space(root, 1, 0);
2027 if (err)
2028 goto out_unlock;
2029
Chris Mason39279cc2007-06-12 06:35:45 -04002030 trans = btrfs_start_transaction(root, 1);
2031 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002032
Chris Mason39279cc2007-06-12 06:35:45 -04002033 if (IS_ERR(trans)) {
2034 err = PTR_ERR(trans);
2035 goto out_unlock;
2036 }
2037
2038 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2039 if (err) {
2040 err = -ENOSPC;
2041 goto out_unlock;
2042 }
2043
Chris Mason9c583092008-01-29 15:15:18 -05002044 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2045 dentry->d_name.len,
2046 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002047 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2048 if (IS_ERR(inode)) {
2049 err = PTR_ERR(inode);
2050 goto out_fail;
2051 }
Chris Mason5f39d392007-10-15 16:14:19 -04002052
Chris Mason39279cc2007-06-12 06:35:45 -04002053 drop_on_err = 1;
2054 inode->i_op = &btrfs_dir_inode_operations;
2055 inode->i_fop = &btrfs_dir_file_operations;
2056 btrfs_set_trans_block_group(trans, inode);
2057
Chris Mason39544012007-12-12 14:38:19 -05002058 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002059 err = btrfs_update_inode(trans, root, inode);
2060 if (err)
2061 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002062
Chris Mason9c583092008-01-29 15:15:18 -05002063 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002064 if (err)
2065 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002066
Chris Mason39279cc2007-06-12 06:35:45 -04002067 d_instantiate(dentry, inode);
2068 drop_on_err = 0;
2069 dir->i_sb->s_dirt = 1;
2070 btrfs_update_inode_block_group(trans, inode);
2071 btrfs_update_inode_block_group(trans, dir);
2072
2073out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002074 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002075 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002076
Chris Mason39279cc2007-06-12 06:35:45 -04002077out_unlock:
2078 mutex_unlock(&root->fs_info->fs_mutex);
2079 if (drop_on_err)
2080 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002081 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002082 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002083 return err;
2084}
2085
Chris Masona52d9a82007-08-27 16:49:44 -04002086struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002087 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002088 int create)
2089{
2090 int ret;
2091 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002092 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002093 u64 extent_start = 0;
2094 u64 extent_end = 0;
2095 u64 objectid = inode->i_ino;
2096 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002097 struct btrfs_path *path;
2098 struct btrfs_root *root = BTRFS_I(inode)->root;
2099 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002100 struct extent_buffer *leaf;
2101 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002102 struct extent_map *em = NULL;
2103 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002104 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002105 struct btrfs_trans_handle *trans = NULL;
2106
2107 path = btrfs_alloc_path();
2108 BUG_ON(!path);
2109 mutex_lock(&root->fs_info->fs_mutex);
2110
2111again:
Chris Masond1310b22008-01-24 16:13:08 -05002112 spin_lock(&em_tree->lock);
2113 em = lookup_extent_mapping(em_tree, start, len);
2114 spin_unlock(&em_tree->lock);
2115
Chris Masona52d9a82007-08-27 16:49:44 -04002116 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05002117 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05002118 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
2119 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05002120 WARN_ON(1);
2121 }
Chris Mason70dec802008-01-29 09:59:12 -05002122 if (em->block_start == EXTENT_MAP_INLINE && page)
2123 free_extent_map(em);
2124 else
2125 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002126 }
Chris Masond1310b22008-01-24 16:13:08 -05002127 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002128 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002129 err = -ENOMEM;
2130 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002131 }
Chris Masond1310b22008-01-24 16:13:08 -05002132
2133 em->start = EXTENT_MAP_HOLE;
2134 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04002135 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04002136 ret = btrfs_lookup_file_extent(trans, root, path,
2137 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002138 if (ret < 0) {
2139 err = ret;
2140 goto out;
2141 }
2142
2143 if (ret != 0) {
2144 if (path->slots[0] == 0)
2145 goto not_found;
2146 path->slots[0]--;
2147 }
2148
Chris Mason5f39d392007-10-15 16:14:19 -04002149 leaf = path->nodes[0];
2150 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002151 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002152 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002153 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2154 found_type = btrfs_key_type(&found_key);
2155 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002156 found_type != BTRFS_EXTENT_DATA_KEY) {
2157 goto not_found;
2158 }
2159
Chris Mason5f39d392007-10-15 16:14:19 -04002160 found_type = btrfs_file_extent_type(leaf, item);
2161 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002162 if (found_type == BTRFS_FILE_EXTENT_REG) {
2163 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002164 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002165 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002166 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002167 em->start = start;
2168 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002169 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002170 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002171 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002172 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002173 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002174 }
2175 goto not_found_em;
2176 }
Chris Masondb945352007-10-15 16:15:53 -04002177 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2178 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002179 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002180 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002181 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002182 goto insert;
2183 }
Chris Masondb945352007-10-15 16:15:53 -04002184 bytenr += btrfs_file_extent_offset(leaf, item);
2185 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002186 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002187 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002188 goto insert;
2189 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002190 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002191 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002192 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002193 size_t size;
2194 size_t extent_offset;
2195 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002196
Chris Mason5f39d392007-10-15 16:14:19 -04002197 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2198 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002199 extent_end = (extent_start + size + root->sectorsize - 1) &
2200 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002201 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002202 em->start = start;
2203 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002204 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002205 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002206 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002207 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002208 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002209 }
2210 goto not_found_em;
2211 }
2212 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002213
2214 if (!page) {
2215 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002216 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002217 goto out;
2218 }
2219
Chris Mason70dec802008-01-29 09:59:12 -05002220 page_start = page_offset(page) + pg_offset;
2221 extent_offset = page_start - extent_start;
2222 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002223 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002224 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002225 em->len = (copy_size + root->sectorsize - 1) &
2226 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002227 map = kmap(page);
2228 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002229 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002230 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002231 copy_size);
2232 flush_dcache_page(page);
2233 } else if (create && PageUptodate(page)) {
2234 if (!trans) {
2235 kunmap(page);
2236 free_extent_map(em);
2237 em = NULL;
2238 btrfs_release_path(root, path);
2239 trans = btrfs_start_transaction(root, 1);
2240 goto again;
2241 }
Chris Mason70dec802008-01-29 09:59:12 -05002242 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002243 copy_size);
2244 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002245 }
Chris Masona52d9a82007-08-27 16:49:44 -04002246 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002247 set_extent_uptodate(io_tree, em->start,
2248 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002249 goto insert;
2250 } else {
2251 printk("unkknown found_type %d\n", found_type);
2252 WARN_ON(1);
2253 }
2254not_found:
2255 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002256 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002257not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002258 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002259insert:
2260 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002261 if (em->start > start || extent_map_end(em) <= start) {
2262 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002263 err = -EIO;
2264 goto out;
2265 }
Chris Masond1310b22008-01-24 16:13:08 -05002266
2267 err = 0;
2268 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002269 ret = add_extent_mapping(em_tree, em);
2270 if (ret == -EEXIST) {
2271 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002272 em = lookup_extent_mapping(em_tree, start, len);
2273 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002274 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002275 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002276 }
Chris Masona52d9a82007-08-27 16:49:44 -04002277 }
Chris Masond1310b22008-01-24 16:13:08 -05002278 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002279out:
2280 btrfs_free_path(path);
2281 if (trans) {
2282 ret = btrfs_end_transaction(trans, root);
2283 if (!err)
2284 err = ret;
2285 }
2286 mutex_unlock(&root->fs_info->fs_mutex);
2287 if (err) {
2288 free_extent_map(em);
2289 WARN_ON(1);
2290 return ERR_PTR(err);
2291 }
2292 return em;
2293}
2294
Chris Mason16432982008-04-10 10:23:21 -04002295static int btrfs_get_block(struct inode *inode, sector_t iblock,
2296 struct buffer_head *bh_result, int create)
2297{
2298 struct extent_map *em;
2299 u64 start = (u64)iblock << inode->i_blkbits;
2300 struct btrfs_multi_bio *multi = NULL;
2301 struct btrfs_root *root = BTRFS_I(inode)->root;
2302 u64 len;
2303 u64 logical;
2304 u64 map_length;
2305 int ret = 0;
2306
2307 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2308
2309 if (!em || IS_ERR(em))
2310 goto out;
2311
2312 if (em->start > start || em->start + em->len <= start)
2313 goto out;
2314
2315 if (em->block_start == EXTENT_MAP_INLINE) {
2316 ret = -EINVAL;
2317 goto out;
2318 }
2319
2320 if (em->block_start == EXTENT_MAP_HOLE ||
2321 em->block_start == EXTENT_MAP_DELALLOC) {
2322 goto out;
2323 }
2324
2325 len = em->start + em->len - start;
2326 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2327
2328 logical = start - em->start;
2329 logical = em->block_start + logical;
2330
2331 map_length = len;
2332 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2333 logical, &map_length, &multi, 0);
2334 BUG_ON(ret);
2335 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2336 bh_result->b_size = min(map_length, len);
2337 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2338 set_buffer_mapped(bh_result);
2339 kfree(multi);
2340out:
2341 free_extent_map(em);
2342 return ret;
2343}
2344
2345static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2346 const struct iovec *iov, loff_t offset,
2347 unsigned long nr_segs)
2348{
2349 struct file *file = iocb->ki_filp;
2350 struct inode *inode = file->f_mapping->host;
2351
2352 if (rw == WRITE)
2353 return -EINVAL;
2354
2355 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2356 offset, nr_segs, btrfs_get_block, NULL);
2357}
2358
Christoph Hellwigd396c6f52007-09-10 20:02:30 -04002359static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002360{
Christoph Hellwigd396c6f52007-09-10 20:02:30 -04002361 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002362}
2363
Chris Mason9ebefb182007-06-15 13:50:00 -04002364int btrfs_readpage(struct file *file, struct page *page)
2365{
Chris Masond1310b22008-01-24 16:13:08 -05002366 struct extent_io_tree *tree;
2367 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002368 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002369}
Chris Mason1832a6d2007-12-21 16:27:21 -05002370
Chris Mason39279cc2007-06-12 06:35:45 -04002371static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2372{
Chris Masond1310b22008-01-24 16:13:08 -05002373 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002374
2375
2376 if (current->flags & PF_MEMALLOC) {
2377 redirty_page_for_writepage(wbc, page);
2378 unlock_page(page);
2379 return 0;
2380 }
Chris Masond1310b22008-01-24 16:13:08 -05002381 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002382 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2383}
Chris Mason39279cc2007-06-12 06:35:45 -04002384
Chris Masonb293f022007-11-01 19:45:34 -04002385static int btrfs_writepages(struct address_space *mapping,
2386 struct writeback_control *wbc)
2387{
Chris Masond1310b22008-01-24 16:13:08 -05002388 struct extent_io_tree *tree;
2389 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002390 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2391}
2392
Chris Mason3ab2fb52007-11-08 10:59:22 -05002393static int
2394btrfs_readpages(struct file *file, struct address_space *mapping,
2395 struct list_head *pages, unsigned nr_pages)
2396{
Chris Masond1310b22008-01-24 16:13:08 -05002397 struct extent_io_tree *tree;
2398 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002399 return extent_readpages(tree, mapping, pages, nr_pages,
2400 btrfs_get_extent);
2401}
2402
Chris Mason70dec802008-01-29 09:59:12 -05002403static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002404{
Chris Masond1310b22008-01-24 16:13:08 -05002405 struct extent_io_tree *tree;
2406 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002407 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002408
Chris Masond1310b22008-01-24 16:13:08 -05002409 tree = &BTRFS_I(page->mapping->host)->io_tree;
2410 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002411 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002412 if (ret == 1) {
2413 ClearPagePrivate(page);
2414 set_page_private(page, 0);
2415 page_cache_release(page);
2416 }
2417 return ret;
2418}
Chris Mason39279cc2007-06-12 06:35:45 -04002419
Chris Masona52d9a82007-08-27 16:49:44 -04002420static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2421{
Chris Masond1310b22008-01-24 16:13:08 -05002422 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002423
Chris Masond1310b22008-01-24 16:13:08 -05002424 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002425 extent_invalidatepage(tree, page, offset);
2426 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002427}
2428
Chris Mason9ebefb182007-06-15 13:50:00 -04002429/*
2430 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2431 * called from a page fault handler when a page is first dirtied. Hence we must
2432 * be careful to check for EOF conditions here. We set the page up correctly
2433 * for a written page which means we get ENOSPC checking when writing into
2434 * holes and correct delalloc and unwritten extent mapping on filesystems that
2435 * support these features.
2436 *
2437 * We are not allowed to take the i_mutex here so we have to play games to
2438 * protect against truncate races as the page could now be beyond EOF. Because
2439 * vmtruncate() writes the inode size before removing pages, once we have the
2440 * page lock we can determine safely if the page is beyond EOF. If it is not
2441 * beyond EOF, then the page is guaranteed safe against truncation until we
2442 * unlock the page.
2443 */
2444int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2445{
Chris Mason6da6aba2007-12-18 16:15:09 -05002446 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002447 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002448 unsigned long end;
2449 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002450 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002451 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002452
Chris Mason1832a6d2007-12-21 16:27:21 -05002453 mutex_lock(&root->fs_info->fs_mutex);
2454 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002455 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002456 if (ret)
2457 goto out;
2458
2459 ret = -EINVAL;
2460
Chris Mason9ebefb182007-06-15 13:50:00 -04002461 lock_page(page);
2462 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002463 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002464 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002465
Chris Mason9ebefb182007-06-15 13:50:00 -04002466 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002467 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002468 /* page got truncated out from underneath us */
2469 goto out_unlock;
2470 }
2471
2472 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002473 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002474 end = size & ~PAGE_CACHE_MASK;
2475 else
2476 end = PAGE_CACHE_SIZE;
2477
Chris Masonb888db22007-08-27 16:49:44 -04002478 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002479
2480out_unlock:
2481 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002482out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002483 return ret;
2484}
2485
Chris Mason39279cc2007-06-12 06:35:45 -04002486static void btrfs_truncate(struct inode *inode)
2487{
2488 struct btrfs_root *root = BTRFS_I(inode)->root;
2489 int ret;
2490 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002491 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002492
2493 if (!S_ISREG(inode->i_mode))
2494 return;
2495 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2496 return;
2497
2498 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2499
2500 mutex_lock(&root->fs_info->fs_mutex);
2501 trans = btrfs_start_transaction(root, 1);
2502 btrfs_set_trans_block_group(trans, inode);
2503
2504 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002505 ret = btrfs_truncate_in_trans(trans, root, inode,
2506 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002507 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002508 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002509
Chris Mason39279cc2007-06-12 06:35:45 -04002510 ret = btrfs_end_transaction(trans, root);
2511 BUG_ON(ret);
2512 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002513 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002514 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002515}
2516
Chris Mason4313b392008-01-03 09:08:48 -05002517static int noinline create_subvol(struct btrfs_root *root, char *name,
2518 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002519{
2520 struct btrfs_trans_handle *trans;
2521 struct btrfs_key key;
2522 struct btrfs_root_item root_item;
2523 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002524 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002525 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002526 struct inode *inode;
2527 struct inode *dir;
2528 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002529 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002530 u64 objectid;
2531 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002532 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002533
2534 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002535 ret = btrfs_check_free_space(root, 1, 0);
2536 if (ret)
2537 goto fail_commit;
2538
Chris Mason39279cc2007-06-12 06:35:45 -04002539 trans = btrfs_start_transaction(root, 1);
2540 BUG_ON(!trans);
2541
Chris Mason7bb86312007-12-11 09:25:06 -05002542 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2543 0, &objectid);
2544 if (ret)
2545 goto fail;
2546
2547 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2548 objectid, trans->transid, 0, 0,
2549 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002550 if (IS_ERR(leaf))
2551 return PTR_ERR(leaf);
2552
2553 btrfs_set_header_nritems(leaf, 0);
2554 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002555 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002556 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002557 btrfs_set_header_owner(leaf, objectid);
2558
Chris Mason5f39d392007-10-15 16:14:19 -04002559 write_extent_buffer(leaf, root->fs_info->fsid,
2560 (unsigned long)btrfs_header_fsid(leaf),
2561 BTRFS_FSID_SIZE);
2562 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002563
2564 inode_item = &root_item.inode;
2565 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002566 inode_item->generation = cpu_to_le64(1);
2567 inode_item->size = cpu_to_le64(3);
2568 inode_item->nlink = cpu_to_le32(1);
2569 inode_item->nblocks = cpu_to_le64(1);
2570 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002571
Chris Masondb945352007-10-15 16:15:53 -04002572 btrfs_set_root_bytenr(&root_item, leaf->start);
2573 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002574 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002575 btrfs_set_root_used(&root_item, 0);
2576
Chris Mason5eda7b52007-06-22 14:16:25 -04002577 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2578 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002579
2580 free_extent_buffer(leaf);
2581 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002582
Chris Mason39279cc2007-06-12 06:35:45 -04002583 btrfs_set_root_dirid(&root_item, new_dirid);
2584
2585 key.objectid = objectid;
2586 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002587 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2588 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2589 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002590 if (ret)
2591 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002592
2593 /*
2594 * insert the directory item
2595 */
2596 key.offset = (u64)-1;
2597 dir = root->fs_info->sb->s_root->d_inode;
2598 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2599 name, namelen, dir->i_ino, &key,
2600 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002601 if (ret)
2602 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002603
Chris Mason39544012007-12-12 14:38:19 -05002604 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2605 name, namelen, objectid,
2606 root->fs_info->sb->s_root->d_inode->i_ino);
2607 if (ret)
2608 goto fail;
2609
Chris Mason39279cc2007-06-12 06:35:45 -04002610 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002611 if (ret)
2612 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002613
Josef Bacik58176a92007-08-29 15:47:34 -04002614 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002615 BUG_ON(!new_root);
2616
2617 trans = btrfs_start_transaction(new_root, 1);
2618 BUG_ON(!trans);
2619
Chris Mason9c583092008-01-29 15:15:18 -05002620 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2621 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002622 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002623 if (IS_ERR(inode))
2624 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002625 inode->i_op = &btrfs_dir_inode_operations;
2626 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002627 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002628
Chris Mason39544012007-12-12 14:38:19 -05002629 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2630 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002631 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002632 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002633 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002634 if (ret)
2635 goto fail;
2636fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002637 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002638 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002639 if (err && !ret)
2640 ret = err;
2641fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002642 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002643 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002644 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002645 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002646}
2647
2648static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2649{
Chris Mason3063d292008-01-08 15:46:30 -05002650 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002651 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002652 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002653 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002654 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002655
2656 if (!root->ref_cows)
2657 return -EINVAL;
2658
2659 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002660 ret = btrfs_check_free_space(root, 1, 0);
2661 if (ret)
2662 goto fail_unlock;
2663
Chris Mason3063d292008-01-08 15:46:30 -05002664 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2665 if (!pending_snapshot) {
2666 ret = -ENOMEM;
2667 goto fail_unlock;
2668 }
Yanfb4bc1e2008-01-17 11:59:51 -05002669 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002670 if (!pending_snapshot->name) {
2671 ret = -ENOMEM;
2672 kfree(pending_snapshot);
2673 goto fail_unlock;
2674 }
Yanfb4bc1e2008-01-17 11:59:51 -05002675 memcpy(pending_snapshot->name, name, namelen);
2676 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002677 trans = btrfs_start_transaction(root, 1);
2678 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002679 pending_snapshot->root = root;
2680 list_add(&pending_snapshot->list,
2681 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002682 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002683 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002684
Chris Mason1832a6d2007-12-21 16:27:21 -05002685fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002686 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002687 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002688 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002689 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002690}
2691
Chris Masonedbd8d42007-12-21 16:27:24 -05002692unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002693 struct file_ra_state *ra, struct file *file,
2694 pgoff_t offset, pgoff_t last_index)
2695{
2696 pgoff_t req_size;
2697
2698#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2699 req_size = last_index - offset + 1;
2700 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2701 return offset;
2702#else
2703 req_size = min(last_index - offset + 1, (pgoff_t)128);
2704 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2705 return offset + req_size;
2706#endif
2707}
2708
2709int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002710 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002711 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002712 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002713 struct page *page;
2714 unsigned long last_index;
2715 unsigned long ra_index = 0;
2716 u64 page_start;
2717 u64 page_end;
2718 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002719 int ret;
2720
2721 mutex_lock(&root->fs_info->fs_mutex);
2722 ret = btrfs_check_free_space(root, inode->i_size, 0);
2723 mutex_unlock(&root->fs_info->fs_mutex);
2724 if (ret)
2725 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002726
2727 mutex_lock(&inode->i_mutex);
2728 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2729 for (i = 0; i <= last_index; i++) {
2730 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002731 ra_index = btrfs_force_ra(inode->i_mapping,
2732 &file->f_ra,
2733 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002734 }
2735 page = grab_cache_page(inode->i_mapping, i);
2736 if (!page)
2737 goto out_unlock;
2738 if (!PageUptodate(page)) {
2739 btrfs_readpage(NULL, page);
2740 lock_page(page);
2741 if (!PageUptodate(page)) {
2742 unlock_page(page);
2743 page_cache_release(page);
2744 goto out_unlock;
2745 }
2746 }
Chris Mason35ebb932007-10-30 16:56:53 -04002747 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002748 page_end = page_start + PAGE_CACHE_SIZE - 1;
2749
Chris Masond1310b22008-01-24 16:13:08 -05002750 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002751 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002752 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002753
Chris Masond1310b22008-01-24 16:13:08 -05002754 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002755 set_page_dirty(page);
2756 unlock_page(page);
2757 page_cache_release(page);
2758 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2759 }
2760
2761out_unlock:
2762 mutex_unlock(&inode->i_mutex);
2763 return 0;
2764}
2765
Chris Masonedbd8d42007-12-21 16:27:24 -05002766static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2767{
2768 u64 new_size;
2769 u64 old_size;
2770 struct btrfs_ioctl_vol_args *vol_args;
2771 struct btrfs_trans_handle *trans;
2772 char *sizestr;
2773 int ret = 0;
2774 int namelen;
2775 int mod = 0;
2776
2777 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2778
2779 if (!vol_args)
2780 return -ENOMEM;
2781
2782 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2783 ret = -EFAULT;
2784 goto out;
2785 }
2786 namelen = strlen(vol_args->name);
2787 if (namelen > BTRFS_VOL_NAME_MAX) {
2788 ret = -EINVAL;
2789 goto out;
2790 }
2791
2792 sizestr = vol_args->name;
2793 if (!strcmp(sizestr, "max"))
2794 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2795 else {
2796 if (sizestr[0] == '-') {
2797 mod = -1;
2798 sizestr++;
2799 } else if (sizestr[0] == '+') {
2800 mod = 1;
2801 sizestr++;
2802 }
2803 new_size = btrfs_parse_size(sizestr);
2804 if (new_size == 0) {
2805 ret = -EINVAL;
2806 goto out;
2807 }
2808 }
2809
2810 mutex_lock(&root->fs_info->fs_mutex);
2811 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2812
2813 if (mod < 0) {
2814 if (new_size > old_size) {
2815 ret = -EINVAL;
2816 goto out_unlock;
2817 }
2818 new_size = old_size - new_size;
2819 } else if (mod > 0) {
2820 new_size = old_size + new_size;
2821 }
2822
2823 if (new_size < 256 * 1024 * 1024) {
2824 ret = -EINVAL;
2825 goto out_unlock;
2826 }
2827 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2828 ret = -EFBIG;
2829 goto out_unlock;
2830 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002831
2832 do_div(new_size, root->sectorsize);
2833 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002834
2835printk("new size is %Lu\n", new_size);
2836 if (new_size > old_size) {
2837 trans = btrfs_start_transaction(root, 1);
2838 ret = btrfs_grow_extent_tree(trans, root, new_size);
2839 btrfs_commit_transaction(trans, root);
2840 } else {
2841 ret = btrfs_shrink_extent_tree(root, new_size);
2842 }
2843
2844out_unlock:
2845 mutex_unlock(&root->fs_info->fs_mutex);
2846out:
2847 kfree(vol_args);
2848 return ret;
2849}
2850
Chris Mason4313b392008-01-03 09:08:48 -05002851static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2852 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002853{
Chris Mason4aec2b52007-12-18 16:25:45 -05002854 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002855 struct btrfs_dir_item *di;
2856 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002857 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002858 int namelen;
2859 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002860
Chris Mason4aec2b52007-12-18 16:25:45 -05002861 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002862
Chris Mason4aec2b52007-12-18 16:25:45 -05002863 if (!vol_args)
2864 return -ENOMEM;
2865
2866 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2867 ret = -EFAULT;
2868 goto out;
2869 }
2870
2871 namelen = strlen(vol_args->name);
2872 if (namelen > BTRFS_VOL_NAME_MAX) {
2873 ret = -EINVAL;
2874 goto out;
2875 }
2876 if (strchr(vol_args->name, '/')) {
2877 ret = -EINVAL;
2878 goto out;
2879 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002880
2881 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002882 if (!path) {
2883 ret = -ENOMEM;
2884 goto out;
2885 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002886
2887 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2888 mutex_lock(&root->fs_info->fs_mutex);
2889 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2890 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002891 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002892 mutex_unlock(&root->fs_info->fs_mutex);
2893 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002894
2895 if (di && !IS_ERR(di)) {
2896 ret = -EEXIST;
2897 goto out;
2898 }
2899
2900 if (IS_ERR(di)) {
2901 ret = PTR_ERR(di);
2902 goto out;
2903 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002904
2905 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002906 ret = create_subvol(root, vol_args->name, namelen);
2907 else
2908 ret = create_snapshot(root, vol_args->name, namelen);
2909out:
2910 kfree(vol_args);
2911 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002912}
2913
2914static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002915{
Chris Mason6da6aba2007-12-18 16:15:09 -05002916 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002917 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002918
2919 switch (inode->i_mode & S_IFMT) {
2920 case S_IFDIR:
2921 mutex_lock(&root->fs_info->fs_mutex);
2922 btrfs_defrag_root(root, 0);
2923 btrfs_defrag_root(root->fs_info->extent_root, 0);
2924 mutex_unlock(&root->fs_info->fs_mutex);
2925 break;
2926 case S_IFREG:
2927 btrfs_defrag_file(file);
2928 break;
2929 }
2930
2931 return 0;
2932}
2933
2934long btrfs_ioctl(struct file *file, unsigned int
2935 cmd, unsigned long arg)
2936{
Chris Mason6da6aba2007-12-18 16:15:09 -05002937 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002938
2939 switch (cmd) {
2940 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002941 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002942 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002943 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002944 case BTRFS_IOC_RESIZE:
2945 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002946 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002947
2948 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002949}
2950
Chris Mason39279cc2007-06-12 06:35:45 -04002951/*
2952 * Called inside transaction, so use GFP_NOFS
2953 */
2954struct inode *btrfs_alloc_inode(struct super_block *sb)
2955{
2956 struct btrfs_inode *ei;
2957
2958 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2959 if (!ei)
2960 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002961 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002962 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002963 return &ei->vfs_inode;
2964}
2965
2966void btrfs_destroy_inode(struct inode *inode)
2967{
2968 WARN_ON(!list_empty(&inode->i_dentry));
2969 WARN_ON(inode->i_data.nrpages);
2970
Chris Mason8c416c92008-01-14 15:10:26 -05002971 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002972 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2973}
2974
Chris Mason44ec0b72007-10-29 10:55:05 -04002975#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2976static void init_once(struct kmem_cache * cachep, void *foo)
2977#else
Chris Mason39279cc2007-06-12 06:35:45 -04002978static void init_once(void * foo, struct kmem_cache * cachep,
2979 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002980#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002981{
2982 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2983
2984 inode_init_once(&ei->vfs_inode);
2985}
2986
2987void btrfs_destroy_cachep(void)
2988{
2989 if (btrfs_inode_cachep)
2990 kmem_cache_destroy(btrfs_inode_cachep);
2991 if (btrfs_trans_handle_cachep)
2992 kmem_cache_destroy(btrfs_trans_handle_cachep);
2993 if (btrfs_transaction_cachep)
2994 kmem_cache_destroy(btrfs_transaction_cachep);
2995 if (btrfs_bit_radix_cachep)
2996 kmem_cache_destroy(btrfs_bit_radix_cachep);
2997 if (btrfs_path_cachep)
2998 kmem_cache_destroy(btrfs_path_cachep);
2999}
3000
Chris Mason86479a02007-09-10 19:58:16 -04003001struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003002 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04003003#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3004 void (*ctor)(struct kmem_cache *, void *)
3005#else
Chris Mason92fee662007-07-25 12:31:35 -04003006 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003007 unsigned long)
3008#endif
3009 )
Chris Mason92fee662007-07-25 12:31:35 -04003010{
3011 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3012 SLAB_MEM_SPREAD | extra_flags), ctor
3013#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3014 ,NULL
3015#endif
3016 );
3017}
3018
Chris Mason39279cc2007-06-12 06:35:45 -04003019int btrfs_init_cachep(void)
3020{
Chris Mason86479a02007-09-10 19:58:16 -04003021 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003022 sizeof(struct btrfs_inode),
3023 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003024 if (!btrfs_inode_cachep)
3025 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003026 btrfs_trans_handle_cachep =
3027 btrfs_cache_create("btrfs_trans_handle_cache",
3028 sizeof(struct btrfs_trans_handle),
3029 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003030 if (!btrfs_trans_handle_cachep)
3031 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003032 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003033 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003034 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003035 if (!btrfs_transaction_cachep)
3036 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003037 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003038 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003039 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003040 if (!btrfs_path_cachep)
3041 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003042 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003043 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003044 if (!btrfs_bit_radix_cachep)
3045 goto fail;
3046 return 0;
3047fail:
3048 btrfs_destroy_cachep();
3049 return -ENOMEM;
3050}
3051
3052static int btrfs_getattr(struct vfsmount *mnt,
3053 struct dentry *dentry, struct kstat *stat)
3054{
3055 struct inode *inode = dentry->d_inode;
3056 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003057 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003058 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003059 return 0;
3060}
3061
3062static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3063 struct inode * new_dir,struct dentry *new_dentry)
3064{
3065 struct btrfs_trans_handle *trans;
3066 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3067 struct inode *new_inode = new_dentry->d_inode;
3068 struct inode *old_inode = old_dentry->d_inode;
3069 struct timespec ctime = CURRENT_TIME;
3070 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003071 int ret;
3072
3073 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3074 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3075 return -ENOTEMPTY;
3076 }
Chris Mason5f39d392007-10-15 16:14:19 -04003077
Chris Mason39279cc2007-06-12 06:35:45 -04003078 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003079 ret = btrfs_check_free_space(root, 1, 0);
3080 if (ret)
3081 goto out_unlock;
3082
Chris Mason39279cc2007-06-12 06:35:45 -04003083 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003084
Chris Mason39279cc2007-06-12 06:35:45 -04003085 btrfs_set_trans_block_group(trans, new_dir);
3086 path = btrfs_alloc_path();
3087 if (!path) {
3088 ret = -ENOMEM;
3089 goto out_fail;
3090 }
3091
3092 old_dentry->d_inode->i_nlink++;
3093 old_dir->i_ctime = old_dir->i_mtime = ctime;
3094 new_dir->i_ctime = new_dir->i_mtime = ctime;
3095 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003096
Chris Mason39279cc2007-06-12 06:35:45 -04003097 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3098 if (ret)
3099 goto out_fail;
3100
3101 if (new_inode) {
3102 new_inode->i_ctime = CURRENT_TIME;
3103 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3104 if (ret)
3105 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003106 }
Chris Mason9c583092008-01-29 15:15:18 -05003107 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003108 if (ret)
3109 goto out_fail;
3110
3111out_fail:
3112 btrfs_free_path(path);
3113 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003114out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003115 mutex_unlock(&root->fs_info->fs_mutex);
3116 return ret;
3117}
3118
3119static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3120 const char *symname)
3121{
3122 struct btrfs_trans_handle *trans;
3123 struct btrfs_root *root = BTRFS_I(dir)->root;
3124 struct btrfs_path *path;
3125 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003126 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003127 int err;
3128 int drop_inode = 0;
3129 u64 objectid;
3130 int name_len;
3131 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003132 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003133 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003134 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003135 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003136
3137 name_len = strlen(symname) + 1;
3138 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3139 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003140
Chris Mason39279cc2007-06-12 06:35:45 -04003141 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003142 err = btrfs_check_free_space(root, 1, 0);
3143 if (err)
3144 goto out_fail;
3145
Chris Mason39279cc2007-06-12 06:35:45 -04003146 trans = btrfs_start_transaction(root, 1);
3147 btrfs_set_trans_block_group(trans, dir);
3148
3149 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3150 if (err) {
3151 err = -ENOSPC;
3152 goto out_unlock;
3153 }
3154
Chris Mason9c583092008-01-29 15:15:18 -05003155 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3156 dentry->d_name.len,
3157 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003158 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3159 err = PTR_ERR(inode);
3160 if (IS_ERR(inode))
3161 goto out_unlock;
3162
3163 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003164 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003165 if (err)
3166 drop_inode = 1;
3167 else {
3168 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003169 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003170 inode->i_fop = &btrfs_file_operations;
3171 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003172 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3173 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003174 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003175 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3176 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05003177 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003178 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003179 }
3180 dir->i_sb->s_dirt = 1;
3181 btrfs_update_inode_block_group(trans, inode);
3182 btrfs_update_inode_block_group(trans, dir);
3183 if (drop_inode)
3184 goto out_unlock;
3185
3186 path = btrfs_alloc_path();
3187 BUG_ON(!path);
3188 key.objectid = inode->i_ino;
3189 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003190 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3191 datasize = btrfs_file_extent_calc_inline_size(name_len);
3192 err = btrfs_insert_empty_item(trans, root, path, &key,
3193 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003194 if (err) {
3195 drop_inode = 1;
3196 goto out_unlock;
3197 }
Chris Mason5f39d392007-10-15 16:14:19 -04003198 leaf = path->nodes[0];
3199 ei = btrfs_item_ptr(leaf, path->slots[0],
3200 struct btrfs_file_extent_item);
3201 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3202 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003203 BTRFS_FILE_EXTENT_INLINE);
3204 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003205 write_extent_buffer(leaf, symname, ptr, name_len);
3206 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003207 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003208
Chris Mason39279cc2007-06-12 06:35:45 -04003209 inode->i_op = &btrfs_symlink_inode_operations;
3210 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003211 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003212 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04003213 err = btrfs_update_inode(trans, root, inode);
3214 if (err)
3215 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003216
3217out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003218 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04003219 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003220out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003221 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04003222 if (drop_inode) {
3223 inode_dec_link_count(inode);
3224 iput(inode);
3225 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003226 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05003227 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003228 return err;
3229}
Chris Mason16432982008-04-10 10:23:21 -04003230
Yanfdebe2b2008-01-14 13:26:08 -05003231static int btrfs_permission(struct inode *inode, int mask,
3232 struct nameidata *nd)
3233{
3234 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3235 return -EACCES;
3236 return generic_permission(inode, mask, NULL);
3237}
Chris Mason39279cc2007-06-12 06:35:45 -04003238
3239static struct inode_operations btrfs_dir_inode_operations = {
3240 .lookup = btrfs_lookup,
3241 .create = btrfs_create,
3242 .unlink = btrfs_unlink,
3243 .link = btrfs_link,
3244 .mkdir = btrfs_mkdir,
3245 .rmdir = btrfs_rmdir,
3246 .rename = btrfs_rename,
3247 .symlink = btrfs_symlink,
3248 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003249 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003250 .setxattr = generic_setxattr,
3251 .getxattr = generic_getxattr,
3252 .listxattr = btrfs_listxattr,
3253 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003254 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003255};
Chris Mason39279cc2007-06-12 06:35:45 -04003256static struct inode_operations btrfs_dir_ro_inode_operations = {
3257 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003258 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003259};
Chris Mason39279cc2007-06-12 06:35:45 -04003260static struct file_operations btrfs_dir_file_operations = {
3261 .llseek = generic_file_llseek,
3262 .read = generic_read_dir,
3263 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003264 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003265#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003266 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003267#endif
3268};
3269
Chris Masond1310b22008-01-24 16:13:08 -05003270static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003271 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003272 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003273 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003274 .readpage_io_hook = btrfs_readpage_io_hook,
3275 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Mason7e383262008-04-09 16:28:12 -04003276 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003277 .set_bit_hook = btrfs_set_bit_hook,
3278 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003279};
3280
Chris Mason39279cc2007-06-12 06:35:45 -04003281static struct address_space_operations btrfs_aops = {
3282 .readpage = btrfs_readpage,
3283 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003284 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003285 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003286 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003287 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003288 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003289 .invalidatepage = btrfs_invalidatepage,
3290 .releasepage = btrfs_releasepage,
3291 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003292};
3293
3294static struct address_space_operations btrfs_symlink_aops = {
3295 .readpage = btrfs_readpage,
3296 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003297 .invalidatepage = btrfs_invalidatepage,
3298 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003299};
3300
3301static struct inode_operations btrfs_file_inode_operations = {
3302 .truncate = btrfs_truncate,
3303 .getattr = btrfs_getattr,
3304 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003305 .setxattr = generic_setxattr,
3306 .getxattr = generic_getxattr,
3307 .listxattr = btrfs_listxattr,
3308 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003309 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003310};
Josef Bacik618e21d2007-07-11 10:18:17 -04003311static struct inode_operations btrfs_special_inode_operations = {
3312 .getattr = btrfs_getattr,
3313 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003314 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003315};
Chris Mason39279cc2007-06-12 06:35:45 -04003316static struct inode_operations btrfs_symlink_inode_operations = {
3317 .readlink = generic_readlink,
3318 .follow_link = page_follow_link_light,
3319 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003320 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003321};