blob: 3e1f21c632c18c43961466b2b5cd00de0cb23b39 [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;
Yanb98b6762008-01-08 15:54:37 -0500386 if (btrfs_test_opt(root, NODATASUM) ||
387 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500388 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400389 mutex_lock(&root->fs_info->fs_mutex);
390 path = btrfs_alloc_path();
391 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
392 if (IS_ERR(item)) {
393 ret = PTR_ERR(item);
394 /* a csum that isn't present is a preallocated region. */
395 if (ret == -ENOENT || ret == -EFBIG)
396 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400397 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500398 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400399 goto out;
400 }
Chris Masonff79f812007-10-15 16:22:25 -0400401 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
402 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500403 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400404out:
405 if (path)
406 btrfs_free_path(path);
407 mutex_unlock(&root->fs_info->fs_mutex);
408 return ret;
409}
410
Chris Mason7e383262008-04-09 16:28:12 -0400411struct io_failure_record {
412 struct page *page;
413 u64 start;
414 u64 len;
415 u64 logical;
416 int last_mirror;
417};
418
419int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
420 struct page *page, u64 start, u64 end,
421 struct extent_state *state)
422{
423 struct io_failure_record *failrec = NULL;
424 u64 private;
425 struct extent_map *em;
426 struct inode *inode = page->mapping->host;
427 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
428 struct bio *bio;
429 int num_copies;
430 int ret;
431 u64 logical;
432
433 ret = get_state_private(failure_tree, start, &private);
434 if (ret) {
435 size_t pg_offset = start - page_offset(page);
436 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
437 if (!failrec)
438 return -ENOMEM;
439 failrec->start = start;
440 failrec->len = end - start + 1;
441 failrec->last_mirror = 0;
442
443 em = btrfs_get_extent(inode, NULL, pg_offset, start,
444 failrec->len, 0);
445
446 if (!em || IS_ERR(em)) {
447 kfree(failrec);
448 return -EIO;
449 }
450 logical = start - em->start;
451 logical = em->block_start + logical;
452 failrec->logical = logical;
453 free_extent_map(em);
454 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
455 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400456 set_state_private(failure_tree, start,
457 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400458 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400459 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400460 }
461 num_copies = btrfs_num_copies(
462 &BTRFS_I(inode)->root->fs_info->mapping_tree,
463 failrec->logical, failrec->len);
464 failrec->last_mirror++;
465 if (!state) {
466 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
467 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
468 failrec->start,
469 EXTENT_LOCKED);
470 if (state && state->start != failrec->start)
471 state = NULL;
472 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
473 }
474 if (!state || failrec->last_mirror > num_copies) {
475 set_state_private(failure_tree, failrec->start, 0);
476 clear_extent_bits(failure_tree, failrec->start,
477 failrec->start + failrec->len - 1,
478 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
479 kfree(failrec);
480 return -EIO;
481 }
482 bio = bio_alloc(GFP_NOFS, 1);
483 bio->bi_private = state;
484 bio->bi_end_io = failed_bio->bi_end_io;
485 bio->bi_sector = failrec->logical >> 9;
486 bio->bi_bdev = failed_bio->bi_bdev;
487 bio_add_page(bio, page, failrec->len, start - page_offset(page));
488 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
489 return 0;
490}
491
Chris Mason70dec802008-01-29 09:59:12 -0500492int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
493 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400494{
Chris Mason35ebb932007-10-30 16:56:53 -0400495 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400496 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500497 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400498 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500499 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400500 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400501 struct btrfs_root *root = BTRFS_I(inode)->root;
502 u32 csum = ~(u32)0;
Jens Axboebbf0d0062007-10-19 09:23:07 -0400503 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500504
Yanb98b6762008-01-08 15:54:37 -0500505 if (btrfs_test_opt(root, NODATASUM) ||
506 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500507 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500508 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500509 private = state->private;
510 ret = 0;
511 } else {
512 ret = get_state_private(io_tree, start, &private);
513 }
Jens Axboebbf0d0062007-10-19 09:23:07 -0400514 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400515 kaddr = kmap_atomic(page, KM_IRQ0);
516 if (ret) {
517 goto zeroit;
518 }
Chris Masonff79f812007-10-15 16:22:25 -0400519 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
520 btrfs_csum_final(csum, (char *)&csum);
521 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400522 goto zeroit;
523 }
524 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -0400525 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400526
527 /* if the io failure tree for this inode is non-empty,
528 * check to see if we've recovered from a failed IO
529 */
530 private = 0;
531 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
532 (u64)-1, 1, EXTENT_DIRTY)) {
533 u64 private_failure;
534 struct io_failure_record *failure;
535 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
536 start, &private_failure);
537 if (ret == 0) {
Chris Mason587f7702008-04-11 12:16:46 -0400538 failure = (struct io_failure_record *)(unsigned long)
539 private_failure;
Chris Mason7e383262008-04-09 16:28:12 -0400540 set_state_private(&BTRFS_I(inode)->io_failure_tree,
541 failure->start, 0);
542 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
543 failure->start,
544 failure->start + failure->len - 1,
545 EXTENT_DIRTY | EXTENT_LOCKED,
546 GFP_NOFS);
547 kfree(failure);
548 }
549 }
Chris Mason07157aa2007-08-30 08:50:51 -0400550 return 0;
551
552zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500553 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
554 page->mapping->host->i_ino, (unsigned long long)start, csum,
555 private);
Chris Masondb945352007-10-15 16:15:53 -0400556 memset(kaddr + offset, 1, end - start + 1);
557 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400558 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -0400559 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400560 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400561}
Chris Masonb888db22007-08-27 16:49:44 -0400562
Chris Mason39279cc2007-06-12 06:35:45 -0400563void btrfs_read_locked_inode(struct inode *inode)
564{
565 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400566 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400567 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400568 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400569 struct btrfs_root *root = BTRFS_I(inode)->root;
570 struct btrfs_key location;
571 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400572 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400573 int ret;
574
575 path = btrfs_alloc_path();
576 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400577 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400578 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500579
Chris Mason39279cc2007-06-12 06:35:45 -0400580 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400581 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400582 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400583
Chris Mason5f39d392007-10-15 16:14:19 -0400584 leaf = path->nodes[0];
585 inode_item = btrfs_item_ptr(leaf, path->slots[0],
586 struct btrfs_inode_item);
587
588 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
589 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
590 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
591 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
592 inode->i_size = btrfs_inode_size(leaf, inode_item);
593
594 tspec = btrfs_inode_atime(inode_item);
595 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
596 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
597
598 tspec = btrfs_inode_mtime(inode_item);
599 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
600 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
601
602 tspec = btrfs_inode_ctime(inode_item);
603 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
604 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
605
606 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
607 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400608 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400609 rdev = btrfs_inode_rdev(leaf, inode_item);
610
611 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400612 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
613 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500614 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500615 if (!BTRFS_I(inode)->block_group) {
616 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400617 NULL, 0,
618 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500619 }
Chris Mason39279cc2007-06-12 06:35:45 -0400620 btrfs_free_path(path);
621 inode_item = NULL;
622
623 mutex_unlock(&root->fs_info->fs_mutex);
624
625 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400626 case S_IFREG:
627 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400628 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500629 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400630 inode->i_fop = &btrfs_file_operations;
631 inode->i_op = &btrfs_file_inode_operations;
632 break;
633 case S_IFDIR:
634 inode->i_fop = &btrfs_dir_file_operations;
635 if (root == root->fs_info->tree_root)
636 inode->i_op = &btrfs_dir_ro_inode_operations;
637 else
638 inode->i_op = &btrfs_dir_inode_operations;
639 break;
640 case S_IFLNK:
641 inode->i_op = &btrfs_symlink_inode_operations;
642 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400643 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400644 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400645 default:
646 init_special_inode(inode, inode->i_mode, rdev);
647 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400648 }
649 return;
650
651make_bad:
652 btrfs_release_path(root, path);
653 btrfs_free_path(path);
654 mutex_unlock(&root->fs_info->fs_mutex);
655 make_bad_inode(inode);
656}
657
Chris Mason5f39d392007-10-15 16:14:19 -0400658static void fill_inode_item(struct extent_buffer *leaf,
659 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400660 struct inode *inode)
661{
Chris Mason5f39d392007-10-15 16:14:19 -0400662 btrfs_set_inode_uid(leaf, item, inode->i_uid);
663 btrfs_set_inode_gid(leaf, item, inode->i_gid);
664 btrfs_set_inode_size(leaf, item, inode->i_size);
665 btrfs_set_inode_mode(leaf, item, inode->i_mode);
666 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
667
668 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
669 inode->i_atime.tv_sec);
670 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
671 inode->i_atime.tv_nsec);
672
673 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
674 inode->i_mtime.tv_sec);
675 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
676 inode->i_mtime.tv_nsec);
677
678 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
679 inode->i_ctime.tv_sec);
680 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
681 inode->i_ctime.tv_nsec);
682
683 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
684 btrfs_set_inode_generation(leaf, item, inode->i_generation);
685 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500686 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400687 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400688 BTRFS_I(inode)->block_group->key.objectid);
689}
690
Chris Masona52d9a82007-08-27 16:49:44 -0400691int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400692 struct btrfs_root *root,
693 struct inode *inode)
694{
695 struct btrfs_inode_item *inode_item;
696 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400697 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400698 int ret;
699
700 path = btrfs_alloc_path();
701 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400702 ret = btrfs_lookup_inode(trans, root, path,
703 &BTRFS_I(inode)->location, 1);
704 if (ret) {
705 if (ret > 0)
706 ret = -ENOENT;
707 goto failed;
708 }
709
Chris Mason5f39d392007-10-15 16:14:19 -0400710 leaf = path->nodes[0];
711 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400712 struct btrfs_inode_item);
713
Chris Mason5f39d392007-10-15 16:14:19 -0400714 fill_inode_item(leaf, inode_item, inode);
715 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400716 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400717 ret = 0;
718failed:
719 btrfs_release_path(root, path);
720 btrfs_free_path(path);
721 return ret;
722}
723
724
725static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
726 struct btrfs_root *root,
727 struct inode *dir,
728 struct dentry *dentry)
729{
730 struct btrfs_path *path;
731 const char *name = dentry->d_name.name;
732 int name_len = dentry->d_name.len;
733 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400734 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400735 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400736 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400737
738 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400739 if (!path) {
740 ret = -ENOMEM;
741 goto err;
742 }
743
Chris Mason39279cc2007-06-12 06:35:45 -0400744 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
745 name, name_len, -1);
746 if (IS_ERR(di)) {
747 ret = PTR_ERR(di);
748 goto err;
749 }
750 if (!di) {
751 ret = -ENOENT;
752 goto err;
753 }
Chris Mason5f39d392007-10-15 16:14:19 -0400754 leaf = path->nodes[0];
755 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400756 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400757 if (ret)
758 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400759 btrfs_release_path(root, path);
760
761 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400762 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400763 if (IS_ERR(di)) {
764 ret = PTR_ERR(di);
765 goto err;
766 }
767 if (!di) {
768 ret = -ENOENT;
769 goto err;
770 }
771 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400772
773 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500774 ret = btrfs_del_inode_ref(trans, root, name, name_len,
775 dentry->d_inode->i_ino,
776 dentry->d_parent->d_inode->i_ino);
777 if (ret) {
778 printk("failed to delete reference to %.*s, "
779 "inode %lu parent %lu\n", name_len, name,
780 dentry->d_inode->i_ino,
781 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500782 }
Chris Mason39279cc2007-06-12 06:35:45 -0400783err:
784 btrfs_free_path(path);
785 if (!ret) {
786 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400787 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400788 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500789#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
790 dentry->d_inode->i_nlink--;
791#else
Chris Mason39279cc2007-06-12 06:35:45 -0400792 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500793#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400794 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400795 dir->i_sb->s_dirt = 1;
796 }
797 return ret;
798}
799
800static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
801{
802 struct btrfs_root *root;
803 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500804 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400805 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500806 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400807
808 root = BTRFS_I(dir)->root;
809 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500810
811 ret = btrfs_check_free_space(root, 1, 1);
812 if (ret)
813 goto fail;
814
Chris Mason39279cc2007-06-12 06:35:45 -0400815 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400816
Chris Mason39279cc2007-06-12 06:35:45 -0400817 btrfs_set_trans_block_group(trans, dir);
818 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400819 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400820
Chris Mason2da98f02008-01-16 11:44:43 -0500821 if (inode->i_nlink == 0) {
822 int found;
823 /* if the inode isn't linked anywhere,
824 * we don't need to worry about
825 * data=ordered
826 */
827 found = btrfs_del_ordered_inode(inode);
828 if (found == 1) {
829 atomic_dec(&inode->i_count);
830 }
831 }
832
Chris Mason39279cc2007-06-12 06:35:45 -0400833 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500834fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400835 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400836 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500837 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400838 return ret;
839}
840
841static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
842{
843 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500844 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400845 int ret;
846 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400847 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500848 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400849
Yan134d4512007-10-25 15:49:25 -0400850 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
851 return -ENOTEMPTY;
852
Chris Mason39279cc2007-06-12 06:35:45 -0400853 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500854 ret = btrfs_check_free_space(root, 1, 1);
855 if (ret)
856 goto fail;
857
Chris Mason39279cc2007-06-12 06:35:45 -0400858 trans = btrfs_start_transaction(root, 1);
859 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400860
861 /* now the directory is empty */
862 err = btrfs_unlink_trans(trans, root, dir, dentry);
863 if (!err) {
864 inode->i_size = 0;
865 }
Chris Mason39544012007-12-12 14:38:19 -0500866
Chris Masond3c2fdc2007-09-17 10:58:06 -0400867 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400868 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500869fail:
Yan134d4512007-10-25 15:49:25 -0400870 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400871 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500872 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500873
Chris Mason39279cc2007-06-12 06:35:45 -0400874 if (ret && !err)
875 err = ret;
876 return err;
877}
878
Chris Mason39279cc2007-06-12 06:35:45 -0400879/*
Chris Mason39279cc2007-06-12 06:35:45 -0400880 * this can truncate away extent items, csum items and directory items.
881 * It starts at a high offset and removes keys until it can't find
882 * any higher than i_size.
883 *
884 * csum items that cross the new i_size are truncated to the new size
885 * as well.
886 */
887static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
888 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500889 struct inode *inode,
890 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400891{
892 int ret;
893 struct btrfs_path *path;
894 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400895 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400896 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400897 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400898 struct btrfs_file_extent_item *fi;
899 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400900 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400901 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500902 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500903 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400904 int found_extent;
905 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500906 int pending_del_nr = 0;
907 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400908 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400909
Chris Masona52d9a82007-08-27 16:49:44 -0400910 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400911 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400912 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400913 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400914
Chris Mason39279cc2007-06-12 06:35:45 -0400915 /* FIXME, add redo link to tree so we don't leak on crash */
916 key.objectid = inode->i_ino;
917 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400918 key.type = (u8)-1;
919
Chris Mason85e21ba2008-01-29 15:11:36 -0500920 btrfs_init_path(path);
921search_again:
922 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
923 if (ret < 0) {
924 goto error;
925 }
926 if (ret > 0) {
927 BUG_ON(path->slots[0] == 0);
928 path->slots[0]--;
929 }
930
Chris Mason39279cc2007-06-12 06:35:45 -0400931 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400932 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400933 leaf = path->nodes[0];
934 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
935 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400936
Chris Mason5f39d392007-10-15 16:14:19 -0400937 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400938 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400939
Chris Mason85e21ba2008-01-29 15:11:36 -0500940 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400941 break;
942
Chris Mason5f39d392007-10-15 16:14:19 -0400943 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400944 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400945 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400946 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400947 extent_type = btrfs_file_extent_type(leaf, fi);
948 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400949 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400950 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400951 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
952 struct btrfs_item *item = btrfs_item_nr(leaf,
953 path->slots[0]);
954 item_end += btrfs_file_extent_inline_len(leaf,
955 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400956 }
Yan008630c2007-11-07 13:31:09 -0500957 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400958 }
959 if (found_type == BTRFS_CSUM_ITEM_KEY) {
960 ret = btrfs_csum_truncate(trans, root, path,
961 inode->i_size);
962 BUG_ON(ret);
963 }
Yan008630c2007-11-07 13:31:09 -0500964 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400965 if (found_type == BTRFS_DIR_ITEM_KEY) {
966 found_type = BTRFS_INODE_ITEM_KEY;
967 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
968 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500969 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
970 found_type = BTRFS_XATTR_ITEM_KEY;
971 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
972 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400973 } else if (found_type) {
974 found_type--;
975 } else {
976 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400977 }
Yana61721d2007-09-17 11:08:38 -0400978 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500979 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400980 }
Chris Mason5f39d392007-10-15 16:14:19 -0400981 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400982 del_item = 1;
983 else
984 del_item = 0;
985 found_extent = 0;
986
987 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400988 if (found_type != BTRFS_EXTENT_DATA_KEY)
989 goto delete;
990
991 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400992 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400993 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400994 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400995 u64 orig_num_bytes =
996 btrfs_file_extent_num_bytes(leaf, fi);
997 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400998 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -0500999 extent_num_bytes = extent_num_bytes &
1000 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001001 btrfs_set_file_extent_num_bytes(leaf, fi,
1002 extent_num_bytes);
1003 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001004 extent_num_bytes);
1005 if (extent_start != 0)
1006 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001007 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001008 } else {
Chris Masondb945352007-10-15 16:15:53 -04001009 extent_num_bytes =
1010 btrfs_file_extent_disk_num_bytes(leaf,
1011 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001012 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001013 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001014 if (extent_start != 0) {
1015 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001016 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001017 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001018 root_gen = btrfs_header_generation(leaf);
1019 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001020 }
Chris Mason90692182008-02-08 13:49:28 -05001021 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1022 if (!del_item) {
1023 u32 newsize = inode->i_size - found_key.offset;
1024 dec_i_blocks(inode, item_end + 1 -
1025 found_key.offset - newsize);
1026 newsize =
1027 btrfs_file_extent_calc_inline_size(newsize);
1028 ret = btrfs_truncate_item(trans, root, path,
1029 newsize, 1);
1030 BUG_ON(ret);
1031 } else {
1032 dec_i_blocks(inode, item_end + 1 -
1033 found_key.offset);
1034 }
Chris Mason39279cc2007-06-12 06:35:45 -04001035 }
Chris Mason179e29e2007-11-01 11:28:41 -04001036delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001037 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001038 if (!pending_del_nr) {
1039 /* no pending yet, add ourselves */
1040 pending_del_slot = path->slots[0];
1041 pending_del_nr = 1;
1042 } else if (pending_del_nr &&
1043 path->slots[0] + 1 == pending_del_slot) {
1044 /* hop on the pending chunk */
1045 pending_del_nr++;
1046 pending_del_slot = path->slots[0];
1047 } else {
1048 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1049 }
Chris Mason39279cc2007-06-12 06:35:45 -04001050 } else {
1051 break;
1052 }
Chris Mason39279cc2007-06-12 06:35:45 -04001053 if (found_extent) {
1054 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001055 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001056 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001057 root_gen, inode->i_ino,
1058 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001059 BUG_ON(ret);
1060 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001061next:
1062 if (path->slots[0] == 0) {
1063 if (pending_del_nr)
1064 goto del_pending;
1065 btrfs_release_path(root, path);
1066 goto search_again;
1067 }
1068
1069 path->slots[0]--;
1070 if (pending_del_nr &&
1071 path->slots[0] + 1 != pending_del_slot) {
1072 struct btrfs_key debug;
1073del_pending:
1074 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1075 pending_del_slot);
1076 ret = btrfs_del_items(trans, root, path,
1077 pending_del_slot,
1078 pending_del_nr);
1079 BUG_ON(ret);
1080 pending_del_nr = 0;
1081 btrfs_release_path(root, path);
1082 goto search_again;
1083 }
Chris Mason39279cc2007-06-12 06:35:45 -04001084 }
1085 ret = 0;
1086error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001087 if (pending_del_nr) {
1088 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1089 pending_del_nr);
1090 }
Chris Mason39279cc2007-06-12 06:35:45 -04001091 btrfs_release_path(root, path);
1092 btrfs_free_path(path);
1093 inode->i_sb->s_dirt = 1;
1094 return ret;
1095}
1096
Chris Masonb888db22007-08-27 16:49:44 -04001097static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -04001098 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001099{
Chris Mason39279cc2007-06-12 06:35:45 -04001100 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -05001101 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001102 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001103 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -05001104 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001105
Chris Mason190662b2007-12-18 16:25:45 -05001106 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001107 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001108
Chris Masond1310b22008-01-24 16:13:08 -05001109 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001110 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -04001111 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -05001112
Chris Masona52d9a82007-08-27 16:49:44 -04001113 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -04001114 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001115 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1116 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -04001117 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001118 }
Chris Masonb888db22007-08-27 16:49:44 -04001119 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -05001120 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001121
Chris Masona52d9a82007-08-27 16:49:44 -04001122 return ret;
1123}
1124
1125/*
1126 * taken from block_truncate_page, but does cow as it zeros out
1127 * any bytes left in the last page in the file.
1128 */
1129static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1130{
1131 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001132 struct btrfs_root *root = BTRFS_I(inode)->root;
1133 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001134 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1135 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1136 struct page *page;
1137 int ret = 0;
1138 u64 page_start;
1139
1140 if ((offset & (blocksize - 1)) == 0)
1141 goto out;
1142
1143 ret = -ENOMEM;
1144 page = grab_cache_page(mapping, index);
1145 if (!page)
1146 goto out;
1147 if (!PageUptodate(page)) {
1148 ret = btrfs_readpage(NULL, page);
1149 lock_page(page);
1150 if (!PageUptodate(page)) {
1151 ret = -EIO;
1152 goto out;
1153 }
1154 }
Chris Mason35ebb932007-10-30 16:56:53 -04001155 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001156
Chris Masonb888db22007-08-27 16:49:44 -04001157 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001158
Chris Mason39279cc2007-06-12 06:35:45 -04001159 unlock_page(page);
1160 page_cache_release(page);
1161out:
1162 return ret;
1163}
1164
1165static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1166{
1167 struct inode *inode = dentry->d_inode;
1168 int err;
1169
1170 err = inode_change_ok(inode, attr);
1171 if (err)
1172 return err;
1173
1174 if (S_ISREG(inode->i_mode) &&
1175 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1176 struct btrfs_trans_handle *trans;
1177 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001178 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001179
Chris Mason5f39d392007-10-15 16:14:19 -04001180 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001181 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001182 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001183 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001184 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001185
Chris Mason1b0f7c22008-01-30 14:33:02 -05001186 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001187 goto out;
1188
Chris Mason1832a6d2007-12-21 16:27:21 -05001189 mutex_lock(&root->fs_info->fs_mutex);
1190 err = btrfs_check_free_space(root, 1, 0);
1191 mutex_unlock(&root->fs_info->fs_mutex);
1192 if (err)
1193 goto fail;
1194
Chris Mason39279cc2007-06-12 06:35:45 -04001195 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1196
Chris Mason1b0f7c22008-01-30 14:33:02 -05001197 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001198 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001199
1200 mutex_lock(&root->fs_info->fs_mutex);
1201 trans = btrfs_start_transaction(root, 1);
1202 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001203 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001204 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001205 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001206
Chris Mason179e29e2007-11-01 11:28:41 -04001207 if (alloc_hint != EXTENT_MAP_INLINE) {
1208 err = btrfs_insert_file_extent(trans, root,
1209 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001210 hole_start, 0, 0,
1211 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001212 btrfs_drop_extent_cache(inode, hole_start,
1213 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001214 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001215 }
Chris Mason39279cc2007-06-12 06:35:45 -04001216 btrfs_end_transaction(trans, root);
1217 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001218 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001219 if (err)
1220 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001221 }
1222out:
1223 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001224fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001225 return err;
1226}
Chris Mason61295eb2008-01-14 16:24:38 -05001227
Chris Mason2da98f02008-01-16 11:44:43 -05001228void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001229{
Chris Mason2da98f02008-01-16 11:44:43 -05001230 int ret;
1231
1232 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001233 return;
1234 }
Chris Mason2da98f02008-01-16 11:44:43 -05001235
1236 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1237 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1238 return;
1239
1240 ret = btrfs_del_ordered_inode(inode);
1241 if (ret == 1) {
1242 atomic_dec(&inode->i_count);
1243 }
Chris Mason61295eb2008-01-14 16:24:38 -05001244}
1245
Chris Mason39279cc2007-06-12 06:35:45 -04001246void btrfs_delete_inode(struct inode *inode)
1247{
1248 struct btrfs_trans_handle *trans;
1249 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001250 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001251 int ret;
1252
1253 truncate_inode_pages(&inode->i_data, 0);
1254 if (is_bad_inode(inode)) {
1255 goto no_delete;
1256 }
Chris Mason5f39d392007-10-15 16:14:19 -04001257
Chris Mason39279cc2007-06-12 06:35:45 -04001258 inode->i_size = 0;
1259 mutex_lock(&root->fs_info->fs_mutex);
1260 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001261
Chris Mason39279cc2007-06-12 06:35:45 -04001262 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001263 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001264 if (ret)
1265 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001266
Chris Masond3c2fdc2007-09-17 10:58:06 -04001267 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001268 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001269
Chris Mason39279cc2007-06-12 06:35:45 -04001270 btrfs_end_transaction(trans, root);
1271 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001272 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001273 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001274 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001275
1276no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001277 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001278 btrfs_end_transaction(trans, root);
1279 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001280 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001281 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001282no_delete:
1283 clear_inode(inode);
1284}
1285
1286/*
1287 * this returns the key found in the dir entry in the location pointer.
1288 * If no dir entries were found, location->objectid is 0.
1289 */
1290static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1291 struct btrfs_key *location)
1292{
1293 const char *name = dentry->d_name.name;
1294 int namelen = dentry->d_name.len;
1295 struct btrfs_dir_item *di;
1296 struct btrfs_path *path;
1297 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001298 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001299
Chris Mason39544012007-12-12 14:38:19 -05001300 if (namelen == 1 && strcmp(name, ".") == 0) {
1301 location->objectid = dir->i_ino;
1302 location->type = BTRFS_INODE_ITEM_KEY;
1303 location->offset = 0;
1304 return 0;
1305 }
Chris Mason39279cc2007-06-12 06:35:45 -04001306 path = btrfs_alloc_path();
1307 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001308
Chris Mason7a720532007-12-13 09:06:59 -05001309 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001310 struct btrfs_key key;
1311 struct extent_buffer *leaf;
1312 u32 nritems;
1313 int slot;
1314
1315 key.objectid = dir->i_ino;
1316 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1317 key.offset = 0;
1318 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1319 BUG_ON(ret == 0);
1320 ret = 0;
1321
1322 leaf = path->nodes[0];
1323 slot = path->slots[0];
1324 nritems = btrfs_header_nritems(leaf);
1325 if (slot >= nritems)
1326 goto out_err;
1327
1328 btrfs_item_key_to_cpu(leaf, &key, slot);
1329 if (key.objectid != dir->i_ino ||
1330 key.type != BTRFS_INODE_REF_KEY) {
1331 goto out_err;
1332 }
1333 location->objectid = key.offset;
1334 location->type = BTRFS_INODE_ITEM_KEY;
1335 location->offset = 0;
1336 goto out;
1337 }
1338
Chris Mason39279cc2007-06-12 06:35:45 -04001339 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1340 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001341 if (IS_ERR(di))
1342 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001343 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001344 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001345 }
Chris Mason5f39d392007-10-15 16:14:19 -04001346 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001347out:
Chris Mason39279cc2007-06-12 06:35:45 -04001348 btrfs_free_path(path);
1349 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001350out_err:
1351 location->objectid = 0;
1352 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001353}
1354
1355/*
1356 * when we hit a tree root in a directory, the btrfs part of the inode
1357 * needs to be changed to reflect the root directory of the tree root. This
1358 * is kind of like crossing a mount point.
1359 */
1360static int fixup_tree_root_location(struct btrfs_root *root,
1361 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001362 struct btrfs_root **sub_root,
1363 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001364{
1365 struct btrfs_path *path;
1366 struct btrfs_root_item *ri;
1367
1368 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1369 return 0;
1370 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1371 return 0;
1372
1373 path = btrfs_alloc_path();
1374 BUG_ON(!path);
1375 mutex_lock(&root->fs_info->fs_mutex);
1376
Josef Bacik58176a92007-08-29 15:47:34 -04001377 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1378 dentry->d_name.name,
1379 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001380 if (IS_ERR(*sub_root))
1381 return PTR_ERR(*sub_root);
1382
1383 ri = &(*sub_root)->root_item;
1384 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001385 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1386 location->offset = 0;
1387
1388 btrfs_free_path(path);
1389 mutex_unlock(&root->fs_info->fs_mutex);
1390 return 0;
1391}
1392
1393static int btrfs_init_locked_inode(struct inode *inode, void *p)
1394{
1395 struct btrfs_iget_args *args = p;
1396 inode->i_ino = args->ino;
1397 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001398 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001399 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1400 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001401 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001402 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1403 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001404 return 0;
1405}
1406
1407static int btrfs_find_actor(struct inode *inode, void *opaque)
1408{
1409 struct btrfs_iget_args *args = opaque;
1410 return (args->ino == inode->i_ino &&
1411 args->root == BTRFS_I(inode)->root);
1412}
1413
Chris Masondc17ff82008-01-08 15:46:30 -05001414struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1415 u64 root_objectid)
1416{
1417 struct btrfs_iget_args args;
1418 args.ino = objectid;
1419 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1420
1421 if (!args.root)
1422 return NULL;
1423
1424 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1425}
1426
Chris Mason39279cc2007-06-12 06:35:45 -04001427struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1428 struct btrfs_root *root)
1429{
1430 struct inode *inode;
1431 struct btrfs_iget_args args;
1432 args.ino = objectid;
1433 args.root = root;
1434
1435 inode = iget5_locked(s, objectid, btrfs_find_actor,
1436 btrfs_init_locked_inode,
1437 (void *)&args);
1438 return inode;
1439}
1440
1441static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1442 struct nameidata *nd)
1443{
1444 struct inode * inode;
1445 struct btrfs_inode *bi = BTRFS_I(dir);
1446 struct btrfs_root *root = bi->root;
1447 struct btrfs_root *sub_root = root;
1448 struct btrfs_key location;
1449 int ret;
1450
1451 if (dentry->d_name.len > BTRFS_NAME_LEN)
1452 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001453
Chris Mason39279cc2007-06-12 06:35:45 -04001454 mutex_lock(&root->fs_info->fs_mutex);
1455 ret = btrfs_inode_by_name(dir, dentry, &location);
1456 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001457
Chris Mason39279cc2007-06-12 06:35:45 -04001458 if (ret < 0)
1459 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001460
Chris Mason39279cc2007-06-12 06:35:45 -04001461 inode = NULL;
1462 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001463 ret = fixup_tree_root_location(root, &location, &sub_root,
1464 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001465 if (ret < 0)
1466 return ERR_PTR(ret);
1467 if (ret > 0)
1468 return ERR_PTR(-ENOENT);
1469 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1470 sub_root);
1471 if (!inode)
1472 return ERR_PTR(-EACCES);
1473 if (inode->i_state & I_NEW) {
1474 /* the inode and parent dir are two different roots */
1475 if (sub_root != root) {
1476 igrab(inode);
1477 sub_root->inode = inode;
1478 }
1479 BTRFS_I(inode)->root = sub_root;
1480 memcpy(&BTRFS_I(inode)->location, &location,
1481 sizeof(location));
1482 btrfs_read_locked_inode(inode);
1483 unlock_new_inode(inode);
1484 }
1485 }
1486 return d_splice_alias(inode, dentry);
1487}
1488
Chris Mason39279cc2007-06-12 06:35:45 -04001489static unsigned char btrfs_filetype_table[] = {
1490 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1491};
1492
1493static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1494{
Chris Mason6da6aba2007-12-18 16:15:09 -05001495 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001496 struct btrfs_root *root = BTRFS_I(inode)->root;
1497 struct btrfs_item *item;
1498 struct btrfs_dir_item *di;
1499 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001500 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001501 struct btrfs_path *path;
1502 int ret;
1503 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001504 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001505 int slot;
1506 int advance;
1507 unsigned char d_type;
1508 int over = 0;
1509 u32 di_cur;
1510 u32 di_total;
1511 u32 di_len;
1512 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001513 char tmp_name[32];
1514 char *name_ptr;
1515 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001516
1517 /* FIXME, use a real flag for deciding about the key type */
1518 if (root->fs_info->tree_root == root)
1519 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001520
Chris Mason39544012007-12-12 14:38:19 -05001521 /* special case for "." */
1522 if (filp->f_pos == 0) {
1523 over = filldir(dirent, ".", 1,
1524 1, inode->i_ino,
1525 DT_DIR);
1526 if (over)
1527 return 0;
1528 filp->f_pos = 1;
1529 }
1530
Chris Mason39279cc2007-06-12 06:35:45 -04001531 mutex_lock(&root->fs_info->fs_mutex);
1532 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001533 path = btrfs_alloc_path();
1534 path->reada = 2;
1535
1536 /* special case for .., just use the back ref */
1537 if (filp->f_pos == 1) {
1538 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1539 key.offset = 0;
1540 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1541 BUG_ON(ret == 0);
1542 leaf = path->nodes[0];
1543 slot = path->slots[0];
1544 nritems = btrfs_header_nritems(leaf);
1545 if (slot >= nritems) {
1546 btrfs_release_path(root, path);
1547 goto read_dir_items;
1548 }
1549 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1550 btrfs_release_path(root, path);
1551 if (found_key.objectid != key.objectid ||
1552 found_key.type != BTRFS_INODE_REF_KEY)
1553 goto read_dir_items;
1554 over = filldir(dirent, "..", 2,
1555 2, found_key.offset, DT_DIR);
1556 if (over)
1557 goto nopos;
1558 filp->f_pos = 2;
1559 }
1560
1561read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001562 btrfs_set_key_type(&key, key_type);
1563 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001564
Chris Mason39279cc2007-06-12 06:35:45 -04001565 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1566 if (ret < 0)
1567 goto err;
1568 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001569 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001570 leaf = path->nodes[0];
1571 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001572 slot = path->slots[0];
1573 if (advance || slot >= nritems) {
1574 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001575 ret = btrfs_next_leaf(root, path);
1576 if (ret)
1577 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001578 leaf = path->nodes[0];
1579 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001580 slot = path->slots[0];
1581 } else {
1582 slot++;
1583 path->slots[0]++;
1584 }
1585 }
1586 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001587 item = btrfs_item_nr(leaf, slot);
1588 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1589
1590 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001591 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001592 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001593 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001594 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001595 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001596
1597 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001598 advance = 1;
1599 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1600 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001601 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001602 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001603 struct btrfs_key location;
1604
1605 name_len = btrfs_dir_name_len(leaf, di);
1606 if (name_len < 32) {
1607 name_ptr = tmp_name;
1608 } else {
1609 name_ptr = kmalloc(name_len, GFP_NOFS);
1610 BUG_ON(!name_ptr);
1611 }
1612 read_extent_buffer(leaf, name_ptr,
1613 (unsigned long)(di + 1), name_len);
1614
1615 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1616 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001617 over = filldir(dirent, name_ptr, name_len,
1618 found_key.offset,
1619 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001620 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001621
1622 if (name_ptr != tmp_name)
1623 kfree(name_ptr);
1624
Chris Mason39279cc2007-06-12 06:35:45 -04001625 if (over)
1626 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001627 di_len = btrfs_dir_name_len(leaf, di) +
1628 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001629 di_cur += di_len;
1630 di = (struct btrfs_dir_item *)((char *)di + di_len);
1631 }
1632 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001633 if (key_type == BTRFS_DIR_INDEX_KEY)
1634 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1635 else
1636 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001637nopos:
1638 ret = 0;
1639err:
1640 btrfs_release_path(root, path);
1641 btrfs_free_path(path);
1642 mutex_unlock(&root->fs_info->fs_mutex);
1643 return ret;
1644}
1645
1646int btrfs_write_inode(struct inode *inode, int wait)
1647{
1648 struct btrfs_root *root = BTRFS_I(inode)->root;
1649 struct btrfs_trans_handle *trans;
1650 int ret = 0;
1651
1652 if (wait) {
1653 mutex_lock(&root->fs_info->fs_mutex);
1654 trans = btrfs_start_transaction(root, 1);
1655 btrfs_set_trans_block_group(trans, inode);
1656 ret = btrfs_commit_transaction(trans, root);
1657 mutex_unlock(&root->fs_info->fs_mutex);
1658 }
1659 return ret;
1660}
1661
1662/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001663 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001664 * inode changes. But, it is most likely to find the inode in cache.
1665 * FIXME, needs more benchmarking...there are no reasons other than performance
1666 * to keep or drop this code.
1667 */
1668void btrfs_dirty_inode(struct inode *inode)
1669{
1670 struct btrfs_root *root = BTRFS_I(inode)->root;
1671 struct btrfs_trans_handle *trans;
1672
1673 mutex_lock(&root->fs_info->fs_mutex);
1674 trans = btrfs_start_transaction(root, 1);
1675 btrfs_set_trans_block_group(trans, inode);
1676 btrfs_update_inode(trans, root, inode);
1677 btrfs_end_transaction(trans, root);
1678 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001679}
1680
1681static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1682 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001683 const char *name, int name_len,
1684 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001685 u64 objectid,
1686 struct btrfs_block_group_cache *group,
1687 int mode)
1688{
1689 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001690 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001691 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001692 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001693 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001694 struct btrfs_inode_ref *ref;
1695 struct btrfs_key key[2];
1696 u32 sizes[2];
1697 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001698 int ret;
1699 int owner;
1700
Chris Mason5f39d392007-10-15 16:14:19 -04001701 path = btrfs_alloc_path();
1702 BUG_ON(!path);
1703
Chris Mason39279cc2007-06-12 06:35:45 -04001704 inode = new_inode(root->fs_info->sb);
1705 if (!inode)
1706 return ERR_PTR(-ENOMEM);
1707
Chris Masond1310b22008-01-24 16:13:08 -05001708 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1709 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001710 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001711 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1712 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001713 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001714 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001715
Chris Mason39279cc2007-06-12 06:35:45 -04001716 if (mode & S_IFDIR)
1717 owner = 0;
1718 else
1719 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001720 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001721 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001722 if (!new_inode_group) {
1723 printk("find_block group failed\n");
1724 new_inode_group = group;
1725 }
1726 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001727 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001728
1729 key[0].objectid = objectid;
1730 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1731 key[0].offset = 0;
1732
1733 key[1].objectid = objectid;
1734 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1735 key[1].offset = ref_objectid;
1736
1737 sizes[0] = sizeof(struct btrfs_inode_item);
1738 sizes[1] = name_len + sizeof(*ref);
1739
1740 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1741 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001742 goto fail;
1743
Chris Mason9c583092008-01-29 15:15:18 -05001744 if (objectid > root->highest_inode)
1745 root->highest_inode = objectid;
1746
Chris Mason39279cc2007-06-12 06:35:45 -04001747 inode->i_uid = current->fsuid;
1748 inode->i_gid = current->fsgid;
1749 inode->i_mode = mode;
1750 inode->i_ino = objectid;
1751 inode->i_blocks = 0;
1752 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001753 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1754 struct btrfs_inode_item);
1755 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001756
1757 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1758 struct btrfs_inode_ref);
1759 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1760 ptr = (unsigned long)(ref + 1);
1761 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1762
Chris Mason5f39d392007-10-15 16:14:19 -04001763 btrfs_mark_buffer_dirty(path->nodes[0]);
1764 btrfs_free_path(path);
1765
Chris Mason39279cc2007-06-12 06:35:45 -04001766 location = &BTRFS_I(inode)->location;
1767 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001768 location->offset = 0;
1769 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1770
Chris Mason39279cc2007-06-12 06:35:45 -04001771 insert_inode_hash(inode);
1772 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001773fail:
1774 btrfs_free_path(path);
1775 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001776}
1777
1778static inline u8 btrfs_inode_type(struct inode *inode)
1779{
1780 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1781}
1782
1783static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001784 struct dentry *dentry, struct inode *inode,
1785 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001786{
1787 int ret;
1788 struct btrfs_key key;
1789 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001790 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001791
Chris Mason39279cc2007-06-12 06:35:45 -04001792 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001793 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1794 key.offset = 0;
1795
1796 ret = btrfs_insert_dir_item(trans, root,
1797 dentry->d_name.name, dentry->d_name.len,
1798 dentry->d_parent->d_inode->i_ino,
1799 &key, btrfs_inode_type(inode));
1800 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001801 if (add_backref) {
1802 ret = btrfs_insert_inode_ref(trans, root,
1803 dentry->d_name.name,
1804 dentry->d_name.len,
1805 inode->i_ino,
1806 dentry->d_parent->d_inode->i_ino);
1807 }
Chris Mason79c44582007-06-25 10:09:33 -04001808 parent_inode = dentry->d_parent->d_inode;
1809 parent_inode->i_size += dentry->d_name.len * 2;
1810 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001811 ret = btrfs_update_inode(trans, root,
1812 dentry->d_parent->d_inode);
1813 }
1814 return ret;
1815}
1816
1817static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001818 struct dentry *dentry, struct inode *inode,
1819 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001820{
Chris Mason9c583092008-01-29 15:15:18 -05001821 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001822 if (!err) {
1823 d_instantiate(dentry, inode);
1824 return 0;
1825 }
1826 if (err > 0)
1827 err = -EEXIST;
1828 return err;
1829}
1830
Josef Bacik618e21d2007-07-11 10:18:17 -04001831static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1832 int mode, dev_t rdev)
1833{
1834 struct btrfs_trans_handle *trans;
1835 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001836 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001837 int err;
1838 int drop_inode = 0;
1839 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001840 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001841
1842 if (!new_valid_dev(rdev))
1843 return -EINVAL;
1844
1845 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001846 err = btrfs_check_free_space(root, 1, 0);
1847 if (err)
1848 goto fail;
1849
Josef Bacik618e21d2007-07-11 10:18:17 -04001850 trans = btrfs_start_transaction(root, 1);
1851 btrfs_set_trans_block_group(trans, dir);
1852
1853 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1854 if (err) {
1855 err = -ENOSPC;
1856 goto out_unlock;
1857 }
1858
Chris Mason9c583092008-01-29 15:15:18 -05001859 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1860 dentry->d_name.len,
1861 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001862 BTRFS_I(dir)->block_group, mode);
1863 err = PTR_ERR(inode);
1864 if (IS_ERR(inode))
1865 goto out_unlock;
1866
1867 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001868 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001869 if (err)
1870 drop_inode = 1;
1871 else {
1872 inode->i_op = &btrfs_special_inode_operations;
1873 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001874 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001875 }
1876 dir->i_sb->s_dirt = 1;
1877 btrfs_update_inode_block_group(trans, inode);
1878 btrfs_update_inode_block_group(trans, dir);
1879out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001880 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001881 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001882fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001883 mutex_unlock(&root->fs_info->fs_mutex);
1884
1885 if (drop_inode) {
1886 inode_dec_link_count(inode);
1887 iput(inode);
1888 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001889 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001890 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001891 return err;
1892}
1893
Chris Mason39279cc2007-06-12 06:35:45 -04001894static int btrfs_create(struct inode *dir, struct dentry *dentry,
1895 int mode, struct nameidata *nd)
1896{
1897 struct btrfs_trans_handle *trans;
1898 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001899 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001900 int err;
1901 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001902 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001903 u64 objectid;
1904
1905 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001906 err = btrfs_check_free_space(root, 1, 0);
1907 if (err)
1908 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001909 trans = btrfs_start_transaction(root, 1);
1910 btrfs_set_trans_block_group(trans, dir);
1911
1912 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1913 if (err) {
1914 err = -ENOSPC;
1915 goto out_unlock;
1916 }
1917
Chris Mason9c583092008-01-29 15:15:18 -05001918 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1919 dentry->d_name.len,
1920 dentry->d_parent->d_inode->i_ino,
1921 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001922 err = PTR_ERR(inode);
1923 if (IS_ERR(inode))
1924 goto out_unlock;
1925
1926 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001927 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001928 if (err)
1929 drop_inode = 1;
1930 else {
1931 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001932 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001933 inode->i_fop = &btrfs_file_operations;
1934 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001935 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1936 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001937 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001938 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1939 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001940 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001941 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001942 }
1943 dir->i_sb->s_dirt = 1;
1944 btrfs_update_inode_block_group(trans, inode);
1945 btrfs_update_inode_block_group(trans, dir);
1946out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001947 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001948 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001949fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001950 mutex_unlock(&root->fs_info->fs_mutex);
1951
1952 if (drop_inode) {
1953 inode_dec_link_count(inode);
1954 iput(inode);
1955 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001956 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001957 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001958 return err;
1959}
1960
1961static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1962 struct dentry *dentry)
1963{
1964 struct btrfs_trans_handle *trans;
1965 struct btrfs_root *root = BTRFS_I(dir)->root;
1966 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001967 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001968 int err;
1969 int drop_inode = 0;
1970
1971 if (inode->i_nlink == 0)
1972 return -ENOENT;
1973
Chris Mason6da6aba2007-12-18 16:15:09 -05001974#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1975 inode->i_nlink++;
1976#else
Chris Mason39279cc2007-06-12 06:35:45 -04001977 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001978#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001979 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001980 err = btrfs_check_free_space(root, 1, 0);
1981 if (err)
1982 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001983 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001984
Chris Mason39279cc2007-06-12 06:35:45 -04001985 btrfs_set_trans_block_group(trans, dir);
1986 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05001987 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001988
Chris Mason39279cc2007-06-12 06:35:45 -04001989 if (err)
1990 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001991
Chris Mason39279cc2007-06-12 06:35:45 -04001992 dir->i_sb->s_dirt = 1;
1993 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001994 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001995
Chris Mason54aa1f42007-06-22 14:16:25 -04001996 if (err)
1997 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001998
Chris Masond3c2fdc2007-09-17 10:58:06 -04001999 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002000 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002001fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002002 mutex_unlock(&root->fs_info->fs_mutex);
2003
2004 if (drop_inode) {
2005 inode_dec_link_count(inode);
2006 iput(inode);
2007 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002008 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002009 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002010 return err;
2011}
2012
Chris Mason39279cc2007-06-12 06:35:45 -04002013static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2014{
2015 struct inode *inode;
2016 struct btrfs_trans_handle *trans;
2017 struct btrfs_root *root = BTRFS_I(dir)->root;
2018 int err = 0;
2019 int drop_on_err = 0;
2020 u64 objectid;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002021 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002022
2023 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002024 err = btrfs_check_free_space(root, 1, 0);
2025 if (err)
2026 goto out_unlock;
2027
Chris Mason39279cc2007-06-12 06:35:45 -04002028 trans = btrfs_start_transaction(root, 1);
2029 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002030
Chris Mason39279cc2007-06-12 06:35:45 -04002031 if (IS_ERR(trans)) {
2032 err = PTR_ERR(trans);
2033 goto out_unlock;
2034 }
2035
2036 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2037 if (err) {
2038 err = -ENOSPC;
2039 goto out_unlock;
2040 }
2041
Chris Mason9c583092008-01-29 15:15:18 -05002042 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2043 dentry->d_name.len,
2044 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002045 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2046 if (IS_ERR(inode)) {
2047 err = PTR_ERR(inode);
2048 goto out_fail;
2049 }
Chris Mason5f39d392007-10-15 16:14:19 -04002050
Chris Mason39279cc2007-06-12 06:35:45 -04002051 drop_on_err = 1;
2052 inode->i_op = &btrfs_dir_inode_operations;
2053 inode->i_fop = &btrfs_dir_file_operations;
2054 btrfs_set_trans_block_group(trans, inode);
2055
Chris Mason39544012007-12-12 14:38:19 -05002056 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002057 err = btrfs_update_inode(trans, root, inode);
2058 if (err)
2059 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002060
Chris Mason9c583092008-01-29 15:15:18 -05002061 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002062 if (err)
2063 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002064
Chris Mason39279cc2007-06-12 06:35:45 -04002065 d_instantiate(dentry, inode);
2066 drop_on_err = 0;
2067 dir->i_sb->s_dirt = 1;
2068 btrfs_update_inode_block_group(trans, inode);
2069 btrfs_update_inode_block_group(trans, dir);
2070
2071out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002072 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002073 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002074
Chris Mason39279cc2007-06-12 06:35:45 -04002075out_unlock:
2076 mutex_unlock(&root->fs_info->fs_mutex);
2077 if (drop_on_err)
2078 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002079 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002080 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002081 return err;
2082}
2083
Chris Masona52d9a82007-08-27 16:49:44 -04002084struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002085 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002086 int create)
2087{
2088 int ret;
2089 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002090 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002091 u64 extent_start = 0;
2092 u64 extent_end = 0;
2093 u64 objectid = inode->i_ino;
2094 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002095 struct btrfs_path *path;
2096 struct btrfs_root *root = BTRFS_I(inode)->root;
2097 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002098 struct extent_buffer *leaf;
2099 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002100 struct extent_map *em = NULL;
2101 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002102 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002103 struct btrfs_trans_handle *trans = NULL;
2104
2105 path = btrfs_alloc_path();
2106 BUG_ON(!path);
2107 mutex_lock(&root->fs_info->fs_mutex);
2108
2109again:
Chris Masond1310b22008-01-24 16:13:08 -05002110 spin_lock(&em_tree->lock);
2111 em = lookup_extent_mapping(em_tree, start, len);
2112 spin_unlock(&em_tree->lock);
2113
Chris Masona52d9a82007-08-27 16:49:44 -04002114 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05002115 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05002116 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
2117 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05002118 WARN_ON(1);
2119 }
Chris Mason70dec802008-01-29 09:59:12 -05002120 if (em->block_start == EXTENT_MAP_INLINE && page)
2121 free_extent_map(em);
2122 else
2123 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002124 }
Chris Masond1310b22008-01-24 16:13:08 -05002125 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002126 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002127 err = -ENOMEM;
2128 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002129 }
Chris Masond1310b22008-01-24 16:13:08 -05002130
2131 em->start = EXTENT_MAP_HOLE;
2132 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04002133 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04002134 ret = btrfs_lookup_file_extent(trans, root, path,
2135 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002136 if (ret < 0) {
2137 err = ret;
2138 goto out;
2139 }
2140
2141 if (ret != 0) {
2142 if (path->slots[0] == 0)
2143 goto not_found;
2144 path->slots[0]--;
2145 }
2146
Chris Mason5f39d392007-10-15 16:14:19 -04002147 leaf = path->nodes[0];
2148 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002149 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002150 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002151 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2152 found_type = btrfs_key_type(&found_key);
2153 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002154 found_type != BTRFS_EXTENT_DATA_KEY) {
2155 goto not_found;
2156 }
2157
Chris Mason5f39d392007-10-15 16:14:19 -04002158 found_type = btrfs_file_extent_type(leaf, item);
2159 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002160 if (found_type == BTRFS_FILE_EXTENT_REG) {
2161 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002162 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002163 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002164 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002165 em->start = start;
2166 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002167 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002168 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002169 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002170 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002171 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002172 }
2173 goto not_found_em;
2174 }
Chris Masondb945352007-10-15 16:15:53 -04002175 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2176 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002177 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002178 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002179 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002180 goto insert;
2181 }
Chris Masondb945352007-10-15 16:15:53 -04002182 bytenr += btrfs_file_extent_offset(leaf, item);
2183 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002184 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002185 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002186 goto insert;
2187 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002188 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002189 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002190 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002191 size_t size;
2192 size_t extent_offset;
2193 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002194
Chris Mason5f39d392007-10-15 16:14:19 -04002195 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2196 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002197 extent_end = (extent_start + size + root->sectorsize - 1) &
2198 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002199 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002200 em->start = start;
2201 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002202 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002203 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002204 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002205 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002206 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002207 }
2208 goto not_found_em;
2209 }
2210 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002211
2212 if (!page) {
2213 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002214 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002215 goto out;
2216 }
2217
Chris Mason70dec802008-01-29 09:59:12 -05002218 page_start = page_offset(page) + pg_offset;
2219 extent_offset = page_start - extent_start;
2220 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002221 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002222 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002223 em->len = (copy_size + root->sectorsize - 1) &
2224 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002225 map = kmap(page);
2226 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002227 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002228 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002229 copy_size);
2230 flush_dcache_page(page);
2231 } else if (create && PageUptodate(page)) {
2232 if (!trans) {
2233 kunmap(page);
2234 free_extent_map(em);
2235 em = NULL;
2236 btrfs_release_path(root, path);
2237 trans = btrfs_start_transaction(root, 1);
2238 goto again;
2239 }
Chris Mason70dec802008-01-29 09:59:12 -05002240 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002241 copy_size);
2242 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002243 }
Chris Masona52d9a82007-08-27 16:49:44 -04002244 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002245 set_extent_uptodate(io_tree, em->start,
2246 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002247 goto insert;
2248 } else {
2249 printk("unkknown found_type %d\n", found_type);
2250 WARN_ON(1);
2251 }
2252not_found:
2253 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002254 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002255not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002256 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002257insert:
2258 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002259 if (em->start > start || extent_map_end(em) <= start) {
2260 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002261 err = -EIO;
2262 goto out;
2263 }
Chris Masond1310b22008-01-24 16:13:08 -05002264
2265 err = 0;
2266 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002267 ret = add_extent_mapping(em_tree, em);
2268 if (ret == -EEXIST) {
2269 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002270 em = lookup_extent_mapping(em_tree, start, len);
2271 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002272 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002273 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002274 }
Chris Masona52d9a82007-08-27 16:49:44 -04002275 }
Chris Masond1310b22008-01-24 16:13:08 -05002276 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002277out:
2278 btrfs_free_path(path);
2279 if (trans) {
2280 ret = btrfs_end_transaction(trans, root);
2281 if (!err)
2282 err = ret;
2283 }
2284 mutex_unlock(&root->fs_info->fs_mutex);
2285 if (err) {
2286 free_extent_map(em);
2287 WARN_ON(1);
2288 return ERR_PTR(err);
2289 }
2290 return em;
2291}
2292
Chris Mason16432982008-04-10 10:23:21 -04002293static int btrfs_get_block(struct inode *inode, sector_t iblock,
2294 struct buffer_head *bh_result, int create)
2295{
2296 struct extent_map *em;
2297 u64 start = (u64)iblock << inode->i_blkbits;
2298 struct btrfs_multi_bio *multi = NULL;
2299 struct btrfs_root *root = BTRFS_I(inode)->root;
2300 u64 len;
2301 u64 logical;
2302 u64 map_length;
2303 int ret = 0;
2304
2305 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2306
2307 if (!em || IS_ERR(em))
2308 goto out;
2309
2310 if (em->start > start || em->start + em->len <= start)
2311 goto out;
2312
2313 if (em->block_start == EXTENT_MAP_INLINE) {
2314 ret = -EINVAL;
2315 goto out;
2316 }
2317
2318 if (em->block_start == EXTENT_MAP_HOLE ||
2319 em->block_start == EXTENT_MAP_DELALLOC) {
2320 goto out;
2321 }
2322
2323 len = em->start + em->len - start;
2324 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2325
2326 logical = start - em->start;
2327 logical = em->block_start + logical;
2328
2329 map_length = len;
2330 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2331 logical, &map_length, &multi, 0);
2332 BUG_ON(ret);
2333 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2334 bh_result->b_size = min(map_length, len);
2335 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2336 set_buffer_mapped(bh_result);
2337 kfree(multi);
2338out:
2339 free_extent_map(em);
2340 return ret;
2341}
2342
2343static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2344 const struct iovec *iov, loff_t offset,
2345 unsigned long nr_segs)
2346{
2347 struct file *file = iocb->ki_filp;
2348 struct inode *inode = file->f_mapping->host;
2349
2350 if (rw == WRITE)
2351 return -EINVAL;
2352
2353 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2354 offset, nr_segs, btrfs_get_block, NULL);
2355}
2356
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002357static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002358{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002359 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002360}
2361
Chris Mason9ebefb182007-06-15 13:50:00 -04002362int btrfs_readpage(struct file *file, struct page *page)
2363{
Chris Masond1310b22008-01-24 16:13:08 -05002364 struct extent_io_tree *tree;
2365 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002366 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002367}
Chris Mason1832a6d2007-12-21 16:27:21 -05002368
Chris Mason39279cc2007-06-12 06:35:45 -04002369static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2370{
Chris Masond1310b22008-01-24 16:13:08 -05002371 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002372
2373
2374 if (current->flags & PF_MEMALLOC) {
2375 redirty_page_for_writepage(wbc, page);
2376 unlock_page(page);
2377 return 0;
2378 }
Chris Masond1310b22008-01-24 16:13:08 -05002379 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002380 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2381}
Chris Mason39279cc2007-06-12 06:35:45 -04002382
Chris Masonb293f022007-11-01 19:45:34 -04002383static int btrfs_writepages(struct address_space *mapping,
2384 struct writeback_control *wbc)
2385{
Chris Masond1310b22008-01-24 16:13:08 -05002386 struct extent_io_tree *tree;
2387 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002388 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2389}
2390
Chris Mason3ab2fb52007-11-08 10:59:22 -05002391static int
2392btrfs_readpages(struct file *file, struct address_space *mapping,
2393 struct list_head *pages, unsigned nr_pages)
2394{
Chris Masond1310b22008-01-24 16:13:08 -05002395 struct extent_io_tree *tree;
2396 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002397 return extent_readpages(tree, mapping, pages, nr_pages,
2398 btrfs_get_extent);
2399}
2400
Chris Mason70dec802008-01-29 09:59:12 -05002401static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002402{
Chris Masond1310b22008-01-24 16:13:08 -05002403 struct extent_io_tree *tree;
2404 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002405 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002406
Chris Masond1310b22008-01-24 16:13:08 -05002407 tree = &BTRFS_I(page->mapping->host)->io_tree;
2408 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002409 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002410 if (ret == 1) {
2411 ClearPagePrivate(page);
2412 set_page_private(page, 0);
2413 page_cache_release(page);
2414 }
2415 return ret;
2416}
Chris Mason39279cc2007-06-12 06:35:45 -04002417
Chris Masona52d9a82007-08-27 16:49:44 -04002418static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2419{
Chris Masond1310b22008-01-24 16:13:08 -05002420 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002421
Chris Masond1310b22008-01-24 16:13:08 -05002422 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002423 extent_invalidatepage(tree, page, offset);
2424 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002425}
2426
Chris Mason9ebefb182007-06-15 13:50:00 -04002427/*
2428 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2429 * called from a page fault handler when a page is first dirtied. Hence we must
2430 * be careful to check for EOF conditions here. We set the page up correctly
2431 * for a written page which means we get ENOSPC checking when writing into
2432 * holes and correct delalloc and unwritten extent mapping on filesystems that
2433 * support these features.
2434 *
2435 * We are not allowed to take the i_mutex here so we have to play games to
2436 * protect against truncate races as the page could now be beyond EOF. Because
2437 * vmtruncate() writes the inode size before removing pages, once we have the
2438 * page lock we can determine safely if the page is beyond EOF. If it is not
2439 * beyond EOF, then the page is guaranteed safe against truncation until we
2440 * unlock the page.
2441 */
2442int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2443{
Chris Mason6da6aba2007-12-18 16:15:09 -05002444 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002445 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002446 unsigned long end;
2447 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002448 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002449 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002450
Chris Mason1832a6d2007-12-21 16:27:21 -05002451 mutex_lock(&root->fs_info->fs_mutex);
2452 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002453 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002454 if (ret)
2455 goto out;
2456
2457 ret = -EINVAL;
2458
Chris Mason9ebefb182007-06-15 13:50:00 -04002459 lock_page(page);
2460 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002461 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002462 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002463
Chris Mason9ebefb182007-06-15 13:50:00 -04002464 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002465 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002466 /* page got truncated out from underneath us */
2467 goto out_unlock;
2468 }
2469
2470 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002471 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002472 end = size & ~PAGE_CACHE_MASK;
2473 else
2474 end = PAGE_CACHE_SIZE;
2475
Chris Masonb888db22007-08-27 16:49:44 -04002476 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002477
2478out_unlock:
2479 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002480out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002481 return ret;
2482}
2483
Chris Mason39279cc2007-06-12 06:35:45 -04002484static void btrfs_truncate(struct inode *inode)
2485{
2486 struct btrfs_root *root = BTRFS_I(inode)->root;
2487 int ret;
2488 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002489 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002490
2491 if (!S_ISREG(inode->i_mode))
2492 return;
2493 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2494 return;
2495
2496 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2497
2498 mutex_lock(&root->fs_info->fs_mutex);
2499 trans = btrfs_start_transaction(root, 1);
2500 btrfs_set_trans_block_group(trans, inode);
2501
2502 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002503 ret = btrfs_truncate_in_trans(trans, root, inode,
2504 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002505 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002506 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002507
Chris Mason39279cc2007-06-12 06:35:45 -04002508 ret = btrfs_end_transaction(trans, root);
2509 BUG_ON(ret);
2510 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002511 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002512 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002513}
2514
Chris Mason4313b392008-01-03 09:08:48 -05002515static int noinline create_subvol(struct btrfs_root *root, char *name,
2516 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002517{
2518 struct btrfs_trans_handle *trans;
2519 struct btrfs_key key;
2520 struct btrfs_root_item root_item;
2521 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002522 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002523 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002524 struct inode *inode;
2525 struct inode *dir;
2526 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002527 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002528 u64 objectid;
2529 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002530 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002531
2532 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002533 ret = btrfs_check_free_space(root, 1, 0);
2534 if (ret)
2535 goto fail_commit;
2536
Chris Mason39279cc2007-06-12 06:35:45 -04002537 trans = btrfs_start_transaction(root, 1);
2538 BUG_ON(!trans);
2539
Chris Mason7bb86312007-12-11 09:25:06 -05002540 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2541 0, &objectid);
2542 if (ret)
2543 goto fail;
2544
2545 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2546 objectid, trans->transid, 0, 0,
2547 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002548 if (IS_ERR(leaf))
2549 return PTR_ERR(leaf);
2550
2551 btrfs_set_header_nritems(leaf, 0);
2552 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002553 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002554 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002555 btrfs_set_header_owner(leaf, objectid);
2556
Chris Mason5f39d392007-10-15 16:14:19 -04002557 write_extent_buffer(leaf, root->fs_info->fsid,
2558 (unsigned long)btrfs_header_fsid(leaf),
2559 BTRFS_FSID_SIZE);
2560 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002561
2562 inode_item = &root_item.inode;
2563 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002564 inode_item->generation = cpu_to_le64(1);
2565 inode_item->size = cpu_to_le64(3);
2566 inode_item->nlink = cpu_to_le32(1);
2567 inode_item->nblocks = cpu_to_le64(1);
2568 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002569
Chris Masondb945352007-10-15 16:15:53 -04002570 btrfs_set_root_bytenr(&root_item, leaf->start);
2571 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002572 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002573 btrfs_set_root_used(&root_item, 0);
2574
Chris Mason5eda7b52007-06-22 14:16:25 -04002575 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2576 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002577
2578 free_extent_buffer(leaf);
2579 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002580
Chris Mason39279cc2007-06-12 06:35:45 -04002581 btrfs_set_root_dirid(&root_item, new_dirid);
2582
2583 key.objectid = objectid;
2584 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002585 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2586 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2587 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002588 if (ret)
2589 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002590
2591 /*
2592 * insert the directory item
2593 */
2594 key.offset = (u64)-1;
2595 dir = root->fs_info->sb->s_root->d_inode;
2596 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2597 name, namelen, dir->i_ino, &key,
2598 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002599 if (ret)
2600 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002601
Chris Mason39544012007-12-12 14:38:19 -05002602 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2603 name, namelen, objectid,
2604 root->fs_info->sb->s_root->d_inode->i_ino);
2605 if (ret)
2606 goto fail;
2607
Chris Mason39279cc2007-06-12 06:35:45 -04002608 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002609 if (ret)
2610 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002611
Josef Bacik58176a92007-08-29 15:47:34 -04002612 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002613 BUG_ON(!new_root);
2614
2615 trans = btrfs_start_transaction(new_root, 1);
2616 BUG_ON(!trans);
2617
Chris Mason9c583092008-01-29 15:15:18 -05002618 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2619 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002620 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002621 if (IS_ERR(inode))
2622 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002623 inode->i_op = &btrfs_dir_inode_operations;
2624 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002625 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002626
Chris Mason39544012007-12-12 14:38:19 -05002627 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2628 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002629 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002630 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002631 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002632 if (ret)
2633 goto fail;
2634fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002635 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002636 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002637 if (err && !ret)
2638 ret = err;
2639fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002640 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002641 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002642 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002643 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002644}
2645
2646static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2647{
Chris Mason3063d292008-01-08 15:46:30 -05002648 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002649 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002650 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002651 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002652 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002653
2654 if (!root->ref_cows)
2655 return -EINVAL;
2656
2657 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002658 ret = btrfs_check_free_space(root, 1, 0);
2659 if (ret)
2660 goto fail_unlock;
2661
Chris Mason3063d292008-01-08 15:46:30 -05002662 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2663 if (!pending_snapshot) {
2664 ret = -ENOMEM;
2665 goto fail_unlock;
2666 }
Yanfb4bc1e2008-01-17 11:59:51 -05002667 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002668 if (!pending_snapshot->name) {
2669 ret = -ENOMEM;
2670 kfree(pending_snapshot);
2671 goto fail_unlock;
2672 }
Yanfb4bc1e2008-01-17 11:59:51 -05002673 memcpy(pending_snapshot->name, name, namelen);
2674 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002675 trans = btrfs_start_transaction(root, 1);
2676 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002677 pending_snapshot->root = root;
2678 list_add(&pending_snapshot->list,
2679 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002680 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002681 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002682
Chris Mason1832a6d2007-12-21 16:27:21 -05002683fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002684 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002685 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002686 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002687 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002688}
2689
Chris Masonedbd8d42007-12-21 16:27:24 -05002690unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002691 struct file_ra_state *ra, struct file *file,
2692 pgoff_t offset, pgoff_t last_index)
2693{
2694 pgoff_t req_size;
2695
2696#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2697 req_size = last_index - offset + 1;
2698 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2699 return offset;
2700#else
2701 req_size = min(last_index - offset + 1, (pgoff_t)128);
2702 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2703 return offset + req_size;
2704#endif
2705}
2706
2707int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002708 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002709 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002710 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002711 struct page *page;
2712 unsigned long last_index;
2713 unsigned long ra_index = 0;
2714 u64 page_start;
2715 u64 page_end;
2716 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002717 int ret;
2718
2719 mutex_lock(&root->fs_info->fs_mutex);
2720 ret = btrfs_check_free_space(root, inode->i_size, 0);
2721 mutex_unlock(&root->fs_info->fs_mutex);
2722 if (ret)
2723 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002724
2725 mutex_lock(&inode->i_mutex);
2726 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2727 for (i = 0; i <= last_index; i++) {
2728 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002729 ra_index = btrfs_force_ra(inode->i_mapping,
2730 &file->f_ra,
2731 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002732 }
2733 page = grab_cache_page(inode->i_mapping, i);
2734 if (!page)
2735 goto out_unlock;
2736 if (!PageUptodate(page)) {
2737 btrfs_readpage(NULL, page);
2738 lock_page(page);
2739 if (!PageUptodate(page)) {
2740 unlock_page(page);
2741 page_cache_release(page);
2742 goto out_unlock;
2743 }
2744 }
Chris Mason35ebb932007-10-30 16:56:53 -04002745 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002746 page_end = page_start + PAGE_CACHE_SIZE - 1;
2747
Chris Masond1310b22008-01-24 16:13:08 -05002748 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002749 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002750 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002751
Chris Masond1310b22008-01-24 16:13:08 -05002752 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002753 set_page_dirty(page);
2754 unlock_page(page);
2755 page_cache_release(page);
2756 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2757 }
2758
2759out_unlock:
2760 mutex_unlock(&inode->i_mutex);
2761 return 0;
2762}
2763
Chris Masonedbd8d42007-12-21 16:27:24 -05002764static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2765{
2766 u64 new_size;
2767 u64 old_size;
2768 struct btrfs_ioctl_vol_args *vol_args;
2769 struct btrfs_trans_handle *trans;
2770 char *sizestr;
2771 int ret = 0;
2772 int namelen;
2773 int mod = 0;
2774
2775 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2776
2777 if (!vol_args)
2778 return -ENOMEM;
2779
2780 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2781 ret = -EFAULT;
2782 goto out;
2783 }
2784 namelen = strlen(vol_args->name);
2785 if (namelen > BTRFS_VOL_NAME_MAX) {
2786 ret = -EINVAL;
2787 goto out;
2788 }
2789
2790 sizestr = vol_args->name;
2791 if (!strcmp(sizestr, "max"))
2792 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2793 else {
2794 if (sizestr[0] == '-') {
2795 mod = -1;
2796 sizestr++;
2797 } else if (sizestr[0] == '+') {
2798 mod = 1;
2799 sizestr++;
2800 }
2801 new_size = btrfs_parse_size(sizestr);
2802 if (new_size == 0) {
2803 ret = -EINVAL;
2804 goto out;
2805 }
2806 }
2807
2808 mutex_lock(&root->fs_info->fs_mutex);
2809 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2810
2811 if (mod < 0) {
2812 if (new_size > old_size) {
2813 ret = -EINVAL;
2814 goto out_unlock;
2815 }
2816 new_size = old_size - new_size;
2817 } else if (mod > 0) {
2818 new_size = old_size + new_size;
2819 }
2820
2821 if (new_size < 256 * 1024 * 1024) {
2822 ret = -EINVAL;
2823 goto out_unlock;
2824 }
2825 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2826 ret = -EFBIG;
2827 goto out_unlock;
2828 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002829
2830 do_div(new_size, root->sectorsize);
2831 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002832
2833printk("new size is %Lu\n", new_size);
2834 if (new_size > old_size) {
2835 trans = btrfs_start_transaction(root, 1);
2836 ret = btrfs_grow_extent_tree(trans, root, new_size);
2837 btrfs_commit_transaction(trans, root);
2838 } else {
2839 ret = btrfs_shrink_extent_tree(root, new_size);
2840 }
2841
2842out_unlock:
2843 mutex_unlock(&root->fs_info->fs_mutex);
2844out:
2845 kfree(vol_args);
2846 return ret;
2847}
2848
Chris Mason4313b392008-01-03 09:08:48 -05002849static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2850 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002851{
Chris Mason4aec2b52007-12-18 16:25:45 -05002852 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002853 struct btrfs_dir_item *di;
2854 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002855 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002856 int namelen;
2857 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002858
Chris Mason4aec2b52007-12-18 16:25:45 -05002859 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002860
Chris Mason4aec2b52007-12-18 16:25:45 -05002861 if (!vol_args)
2862 return -ENOMEM;
2863
2864 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2865 ret = -EFAULT;
2866 goto out;
2867 }
2868
2869 namelen = strlen(vol_args->name);
2870 if (namelen > BTRFS_VOL_NAME_MAX) {
2871 ret = -EINVAL;
2872 goto out;
2873 }
2874 if (strchr(vol_args->name, '/')) {
2875 ret = -EINVAL;
2876 goto out;
2877 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002878
2879 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002880 if (!path) {
2881 ret = -ENOMEM;
2882 goto out;
2883 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002884
2885 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2886 mutex_lock(&root->fs_info->fs_mutex);
2887 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2888 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002889 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002890 mutex_unlock(&root->fs_info->fs_mutex);
2891 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002892
2893 if (di && !IS_ERR(di)) {
2894 ret = -EEXIST;
2895 goto out;
2896 }
2897
2898 if (IS_ERR(di)) {
2899 ret = PTR_ERR(di);
2900 goto out;
2901 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002902
2903 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002904 ret = create_subvol(root, vol_args->name, namelen);
2905 else
2906 ret = create_snapshot(root, vol_args->name, namelen);
2907out:
2908 kfree(vol_args);
2909 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002910}
2911
2912static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002913{
Chris Mason6da6aba2007-12-18 16:15:09 -05002914 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002915 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002916
2917 switch (inode->i_mode & S_IFMT) {
2918 case S_IFDIR:
2919 mutex_lock(&root->fs_info->fs_mutex);
2920 btrfs_defrag_root(root, 0);
2921 btrfs_defrag_root(root->fs_info->extent_root, 0);
2922 mutex_unlock(&root->fs_info->fs_mutex);
2923 break;
2924 case S_IFREG:
2925 btrfs_defrag_file(file);
2926 break;
2927 }
2928
2929 return 0;
2930}
2931
2932long btrfs_ioctl(struct file *file, unsigned int
2933 cmd, unsigned long arg)
2934{
Chris Mason6da6aba2007-12-18 16:15:09 -05002935 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002936
2937 switch (cmd) {
2938 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002939 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002940 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002941 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002942 case BTRFS_IOC_RESIZE:
2943 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002944 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002945
2946 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002947}
2948
Chris Mason39279cc2007-06-12 06:35:45 -04002949/*
2950 * Called inside transaction, so use GFP_NOFS
2951 */
2952struct inode *btrfs_alloc_inode(struct super_block *sb)
2953{
2954 struct btrfs_inode *ei;
2955
2956 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2957 if (!ei)
2958 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002959 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002960 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002961 return &ei->vfs_inode;
2962}
2963
2964void btrfs_destroy_inode(struct inode *inode)
2965{
2966 WARN_ON(!list_empty(&inode->i_dentry));
2967 WARN_ON(inode->i_data.nrpages);
2968
Chris Mason8c416c92008-01-14 15:10:26 -05002969 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002970 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2971}
2972
Chris Mason44ec0b72007-10-29 10:55:05 -04002973#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2974static void init_once(struct kmem_cache * cachep, void *foo)
2975#else
Chris Mason39279cc2007-06-12 06:35:45 -04002976static void init_once(void * foo, struct kmem_cache * cachep,
2977 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002978#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002979{
2980 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2981
2982 inode_init_once(&ei->vfs_inode);
2983}
2984
2985void btrfs_destroy_cachep(void)
2986{
2987 if (btrfs_inode_cachep)
2988 kmem_cache_destroy(btrfs_inode_cachep);
2989 if (btrfs_trans_handle_cachep)
2990 kmem_cache_destroy(btrfs_trans_handle_cachep);
2991 if (btrfs_transaction_cachep)
2992 kmem_cache_destroy(btrfs_transaction_cachep);
2993 if (btrfs_bit_radix_cachep)
2994 kmem_cache_destroy(btrfs_bit_radix_cachep);
2995 if (btrfs_path_cachep)
2996 kmem_cache_destroy(btrfs_path_cachep);
2997}
2998
Chris Mason86479a02007-09-10 19:58:16 -04002999struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003000 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04003001#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3002 void (*ctor)(struct kmem_cache *, void *)
3003#else
Chris Mason92fee662007-07-25 12:31:35 -04003004 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003005 unsigned long)
3006#endif
3007 )
Chris Mason92fee662007-07-25 12:31:35 -04003008{
3009 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3010 SLAB_MEM_SPREAD | extra_flags), ctor
3011#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3012 ,NULL
3013#endif
3014 );
3015}
3016
Chris Mason39279cc2007-06-12 06:35:45 -04003017int btrfs_init_cachep(void)
3018{
Chris Mason86479a02007-09-10 19:58:16 -04003019 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003020 sizeof(struct btrfs_inode),
3021 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003022 if (!btrfs_inode_cachep)
3023 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003024 btrfs_trans_handle_cachep =
3025 btrfs_cache_create("btrfs_trans_handle_cache",
3026 sizeof(struct btrfs_trans_handle),
3027 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003028 if (!btrfs_trans_handle_cachep)
3029 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003030 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003031 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003032 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003033 if (!btrfs_transaction_cachep)
3034 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003035 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003036 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003037 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003038 if (!btrfs_path_cachep)
3039 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003040 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003041 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003042 if (!btrfs_bit_radix_cachep)
3043 goto fail;
3044 return 0;
3045fail:
3046 btrfs_destroy_cachep();
3047 return -ENOMEM;
3048}
3049
3050static int btrfs_getattr(struct vfsmount *mnt,
3051 struct dentry *dentry, struct kstat *stat)
3052{
3053 struct inode *inode = dentry->d_inode;
3054 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003055 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003056 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003057 return 0;
3058}
3059
3060static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3061 struct inode * new_dir,struct dentry *new_dentry)
3062{
3063 struct btrfs_trans_handle *trans;
3064 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3065 struct inode *new_inode = new_dentry->d_inode;
3066 struct inode *old_inode = old_dentry->d_inode;
3067 struct timespec ctime = CURRENT_TIME;
3068 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003069 int ret;
3070
3071 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3072 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3073 return -ENOTEMPTY;
3074 }
Chris Mason5f39d392007-10-15 16:14:19 -04003075
Chris Mason39279cc2007-06-12 06:35:45 -04003076 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003077 ret = btrfs_check_free_space(root, 1, 0);
3078 if (ret)
3079 goto out_unlock;
3080
Chris Mason39279cc2007-06-12 06:35:45 -04003081 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003082
Chris Mason39279cc2007-06-12 06:35:45 -04003083 btrfs_set_trans_block_group(trans, new_dir);
3084 path = btrfs_alloc_path();
3085 if (!path) {
3086 ret = -ENOMEM;
3087 goto out_fail;
3088 }
3089
3090 old_dentry->d_inode->i_nlink++;
3091 old_dir->i_ctime = old_dir->i_mtime = ctime;
3092 new_dir->i_ctime = new_dir->i_mtime = ctime;
3093 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003094
Chris Mason39279cc2007-06-12 06:35:45 -04003095 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3096 if (ret)
3097 goto out_fail;
3098
3099 if (new_inode) {
3100 new_inode->i_ctime = CURRENT_TIME;
3101 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3102 if (ret)
3103 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003104 }
Chris Mason9c583092008-01-29 15:15:18 -05003105 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003106 if (ret)
3107 goto out_fail;
3108
3109out_fail:
3110 btrfs_free_path(path);
3111 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003112out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003113 mutex_unlock(&root->fs_info->fs_mutex);
3114 return ret;
3115}
3116
3117static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3118 const char *symname)
3119{
3120 struct btrfs_trans_handle *trans;
3121 struct btrfs_root *root = BTRFS_I(dir)->root;
3122 struct btrfs_path *path;
3123 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003124 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003125 int err;
3126 int drop_inode = 0;
3127 u64 objectid;
3128 int name_len;
3129 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003130 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003131 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003132 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003133 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003134
3135 name_len = strlen(symname) + 1;
3136 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3137 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003138
Chris Mason39279cc2007-06-12 06:35:45 -04003139 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003140 err = btrfs_check_free_space(root, 1, 0);
3141 if (err)
3142 goto out_fail;
3143
Chris Mason39279cc2007-06-12 06:35:45 -04003144 trans = btrfs_start_transaction(root, 1);
3145 btrfs_set_trans_block_group(trans, dir);
3146
3147 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3148 if (err) {
3149 err = -ENOSPC;
3150 goto out_unlock;
3151 }
3152
Chris Mason9c583092008-01-29 15:15:18 -05003153 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3154 dentry->d_name.len,
3155 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003156 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3157 err = PTR_ERR(inode);
3158 if (IS_ERR(inode))
3159 goto out_unlock;
3160
3161 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003162 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003163 if (err)
3164 drop_inode = 1;
3165 else {
3166 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003167 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003168 inode->i_fop = &btrfs_file_operations;
3169 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003170 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3171 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003172 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003173 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3174 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05003175 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003176 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003177 }
3178 dir->i_sb->s_dirt = 1;
3179 btrfs_update_inode_block_group(trans, inode);
3180 btrfs_update_inode_block_group(trans, dir);
3181 if (drop_inode)
3182 goto out_unlock;
3183
3184 path = btrfs_alloc_path();
3185 BUG_ON(!path);
3186 key.objectid = inode->i_ino;
3187 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003188 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3189 datasize = btrfs_file_extent_calc_inline_size(name_len);
3190 err = btrfs_insert_empty_item(trans, root, path, &key,
3191 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003192 if (err) {
3193 drop_inode = 1;
3194 goto out_unlock;
3195 }
Chris Mason5f39d392007-10-15 16:14:19 -04003196 leaf = path->nodes[0];
3197 ei = btrfs_item_ptr(leaf, path->slots[0],
3198 struct btrfs_file_extent_item);
3199 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3200 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003201 BTRFS_FILE_EXTENT_INLINE);
3202 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003203 write_extent_buffer(leaf, symname, ptr, name_len);
3204 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003205 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003206
Chris Mason39279cc2007-06-12 06:35:45 -04003207 inode->i_op = &btrfs_symlink_inode_operations;
3208 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003209 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003210 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04003211 err = btrfs_update_inode(trans, root, inode);
3212 if (err)
3213 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003214
3215out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003216 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04003217 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003218out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003219 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04003220 if (drop_inode) {
3221 inode_dec_link_count(inode);
3222 iput(inode);
3223 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003224 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05003225 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003226 return err;
3227}
Chris Mason16432982008-04-10 10:23:21 -04003228
Yanfdebe2b2008-01-14 13:26:08 -05003229static int btrfs_permission(struct inode *inode, int mask,
3230 struct nameidata *nd)
3231{
3232 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3233 return -EACCES;
3234 return generic_permission(inode, mask, NULL);
3235}
Chris Mason39279cc2007-06-12 06:35:45 -04003236
3237static struct inode_operations btrfs_dir_inode_operations = {
3238 .lookup = btrfs_lookup,
3239 .create = btrfs_create,
3240 .unlink = btrfs_unlink,
3241 .link = btrfs_link,
3242 .mkdir = btrfs_mkdir,
3243 .rmdir = btrfs_rmdir,
3244 .rename = btrfs_rename,
3245 .symlink = btrfs_symlink,
3246 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003247 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003248 .setxattr = generic_setxattr,
3249 .getxattr = generic_getxattr,
3250 .listxattr = btrfs_listxattr,
3251 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003252 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003253};
Chris Mason39279cc2007-06-12 06:35:45 -04003254static struct inode_operations btrfs_dir_ro_inode_operations = {
3255 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003256 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003257};
Chris Mason39279cc2007-06-12 06:35:45 -04003258static struct file_operations btrfs_dir_file_operations = {
3259 .llseek = generic_file_llseek,
3260 .read = generic_read_dir,
3261 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003262 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003263#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003264 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003265#endif
3266};
3267
Chris Masond1310b22008-01-24 16:13:08 -05003268static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003269 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003270 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003271 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003272 .readpage_io_hook = btrfs_readpage_io_hook,
3273 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Mason7e383262008-04-09 16:28:12 -04003274 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003275 .set_bit_hook = btrfs_set_bit_hook,
3276 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003277};
3278
Chris Mason39279cc2007-06-12 06:35:45 -04003279static struct address_space_operations btrfs_aops = {
3280 .readpage = btrfs_readpage,
3281 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003282 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003283 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003284 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003285 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003286 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003287 .invalidatepage = btrfs_invalidatepage,
3288 .releasepage = btrfs_releasepage,
3289 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003290};
3291
3292static struct address_space_operations btrfs_symlink_aops = {
3293 .readpage = btrfs_readpage,
3294 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003295 .invalidatepage = btrfs_invalidatepage,
3296 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003297};
3298
3299static struct inode_operations btrfs_file_inode_operations = {
3300 .truncate = btrfs_truncate,
3301 .getattr = btrfs_getattr,
3302 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003303 .setxattr = generic_setxattr,
3304 .getxattr = generic_getxattr,
3305 .listxattr = btrfs_listxattr,
3306 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003307 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003308};
Josef Bacik618e21d2007-07-11 10:18:17 -04003309static struct inode_operations btrfs_special_inode_operations = {
3310 .getattr = btrfs_getattr,
3311 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003312 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003313};
Chris Mason39279cc2007-06-12 06:35:45 -04003314static struct inode_operations btrfs_symlink_inode_operations = {
3315 .readlink = generic_readlink,
3316 .follow_link = page_follow_link_light,
3317 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003318 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003319};