blob: 32e778fb014ef3c68ee32e8e754532bf591d8641 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason065631f2008-02-20 12:07:25 -050019#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040020#include <linux/buffer_head.h>
21#include <linux/fs.h>
22#include <linux/pagemap.h>
23#include <linux/highmem.h>
24#include <linux/time.h>
25#include <linux/init.h>
26#include <linux/string.h>
27#include <linux/smp_lock.h>
28#include <linux/backing-dev.h>
29#include <linux/mpage.h>
30#include <linux/swap.h>
31#include <linux/writeback.h>
32#include <linux/statfs.h>
33#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040035#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040037#include "ctree.h"
38#include "disk-io.h"
39#include "transaction.h"
40#include "btrfs_inode.h"
41#include "ioctl.h"
42#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040043#include "volumes.h"
Chris Mason39279cc2007-06-12 06:35:45 -040044
45struct btrfs_iget_args {
46 u64 ino;
47 struct btrfs_root *root;
48};
49
50static struct inode_operations btrfs_dir_inode_operations;
51static struct inode_operations btrfs_symlink_inode_operations;
52static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040053static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040054static struct inode_operations btrfs_file_inode_operations;
55static struct address_space_operations btrfs_aops;
56static struct address_space_operations btrfs_symlink_aops;
57static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050058static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040059
60static struct kmem_cache *btrfs_inode_cachep;
61struct kmem_cache *btrfs_trans_handle_cachep;
62struct kmem_cache *btrfs_transaction_cachep;
63struct kmem_cache *btrfs_bit_radix_cachep;
64struct kmem_cache *btrfs_path_cachep;
65
66#define S_SHIFT 12
67static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
68 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
69 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
70 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
71 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
72 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
73 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
74 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
75};
76
Chris Mason1832a6d2007-12-21 16:27:21 -050077int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
78 int for_del)
79{
80 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
81 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
82 u64 thresh;
83 int ret = 0;
84
85 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050086 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050087 else
Chris Masonf9ef6602008-01-03 09:22:38 -050088 thresh = total * 85;
89
90 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050091
92 spin_lock(&root->fs_info->delalloc_lock);
93 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
94 ret = -ENOSPC;
95 spin_unlock(&root->fs_info->delalloc_lock);
96 return ret;
97}
98
Chris Masonbe20aa92007-12-17 20:14:01 -050099static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400100{
101 struct btrfs_root *root = BTRFS_I(inode)->root;
102 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400103 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400104 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500105 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400106 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500107 u64 orig_start = start;
108 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500109 struct btrfs_key ins;
110 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400111
Chris Masonb888db22007-08-27 16:49:44 -0400112 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400113 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500114 btrfs_set_trans_block_group(trans, inode);
115
Chris Masondb945352007-10-15 16:15:53 -0400116 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500117 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400118 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400119 start, start + num_bytes, start, &alloc_hint);
Chris Masond1310b22008-01-24 16:13:08 -0500120 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400121
Chris Mason179e29e2007-11-01 11:28:41 -0400122 if (alloc_hint == EXTENT_MAP_INLINE)
123 goto out;
124
Chris Masonc59f8952007-12-17 20:14:04 -0500125 while(num_bytes > 0) {
126 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
127 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
128 root->root_key.objectid,
129 trans->transid,
130 inode->i_ino, start, 0,
131 alloc_hint, (u64)-1, &ins, 1);
132 if (ret) {
133 WARN_ON(1);
134 goto out;
135 }
136 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
137 start, ins.objectid, ins.offset,
138 ins.offset);
Chris Mason90692182008-02-08 13:49:28 -0500139 inode->i_blocks += ins.offset >> 9;
Chris Mason5f564062008-01-22 16:47:59 -0500140 btrfs_check_file(root, inode);
Chris Masonc59f8952007-12-17 20:14:04 -0500141 num_bytes -= cur_alloc_size;
142 alloc_hint = ins.objectid + ins.offset;
143 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400144 }
Chris Masond1310b22008-01-24 16:13:08 -0500145 btrfs_drop_extent_cache(inode, orig_start,
146 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500147 btrfs_add_ordered_inode(inode);
Chris Mason90692182008-02-08 13:49:28 -0500148 btrfs_update_inode(trans, root, inode);
Chris Masonb888db22007-08-27 16:49:44 -0400149out:
150 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500151 return ret;
152}
153
154static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
155{
156 u64 extent_start;
157 u64 extent_end;
158 u64 bytenr;
159 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500160 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500161 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500162 struct btrfs_root *root = BTRFS_I(inode)->root;
163 struct extent_buffer *leaf;
164 int found_type;
165 struct btrfs_path *path;
166 struct btrfs_file_extent_item *item;
167 int ret;
168 int err;
169 struct btrfs_key found_key;
170
Chris Masonc31f8832008-01-08 15:46:31 -0500171 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500172 path = btrfs_alloc_path();
173 BUG_ON(!path);
174again:
175 ret = btrfs_lookup_file_extent(NULL, root, path,
176 inode->i_ino, start, 0);
177 if (ret < 0) {
178 btrfs_free_path(path);
179 return ret;
180 }
181
182 cow_end = end;
183 if (ret != 0) {
184 if (path->slots[0] == 0)
185 goto not_found;
186 path->slots[0]--;
187 }
188
189 leaf = path->nodes[0];
190 item = btrfs_item_ptr(leaf, path->slots[0],
191 struct btrfs_file_extent_item);
192
193 /* are we inside the extent that was found? */
194 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
195 found_type = btrfs_key_type(&found_key);
196 if (found_key.objectid != inode->i_ino ||
197 found_type != BTRFS_EXTENT_DATA_KEY) {
198 goto not_found;
199 }
200
201 found_type = btrfs_file_extent_type(leaf, item);
202 extent_start = found_key.offset;
203 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500204 u64 extent_num_bytes;
205
206 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
207 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500208 err = 0;
209
Chris Mason1832a6d2007-12-21 16:27:21 -0500210 if (loops && start != extent_start)
211 goto not_found;
212
Chris Masonbe20aa92007-12-17 20:14:01 -0500213 if (start < extent_start || start >= extent_end)
214 goto not_found;
215
216 cow_end = min(end, extent_end - 1);
217 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
218 if (bytenr == 0)
219 goto not_found;
220
Chris Masonc31f8832008-01-08 15:46:31 -0500221 /*
222 * we may be called by the resizer, make sure we're inside
223 * the limits of the FS
224 */
225 if (bytenr + extent_num_bytes > total_fs_bytes)
226 goto not_found;
227
Chris Masonbe20aa92007-12-17 20:14:01 -0500228 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
229 goto not_found;
230 }
231
232 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500233 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500234 goto not_found;
235 }
236loop:
237 if (start > end) {
238 btrfs_free_path(path);
239 return 0;
240 }
241 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500242 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500243 goto again;
244
245not_found:
246 cow_file_range(inode, start, cow_end);
247 start = cow_end + 1;
248 goto loop;
249}
250
251static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
252{
253 struct btrfs_root *root = BTRFS_I(inode)->root;
254 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500255 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500256 if (btrfs_test_opt(root, NODATACOW) ||
257 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500258 ret = run_delalloc_nocow(inode, start, end);
259 else
260 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500261
Chris Masonb888db22007-08-27 16:49:44 -0400262 mutex_unlock(&root->fs_info->fs_mutex);
263 return ret;
264}
265
Chris Mason291d6732008-01-29 15:55:23 -0500266int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500267 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500268{
Chris Masonb0c68f82008-01-31 11:05:37 -0500269 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500270 struct btrfs_root *root = BTRFS_I(inode)->root;
271 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -0500272 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500273 root->fs_info->delalloc_bytes += end - start + 1;
274 spin_unlock(&root->fs_info->delalloc_lock);
275 }
276 return 0;
277}
278
279int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500280 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500281{
Chris Masonb0c68f82008-01-31 11:05:37 -0500282 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500283 struct btrfs_root *root = BTRFS_I(inode)->root;
284 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -0500285 if (end - start + 1 > root->fs_info->delalloc_bytes) {
286 printk("warning: delalloc account %Lu %Lu\n",
287 end - start + 1, root->fs_info->delalloc_bytes);
288 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500289 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500290 } else {
291 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500292 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500293 }
Chris Mason291d6732008-01-29 15:55:23 -0500294 spin_unlock(&root->fs_info->delalloc_lock);
295 }
296 return 0;
297}
298
Chris Mason239b14b2008-03-24 15:02:07 -0400299int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
300 size_t size, struct bio *bio)
301{
302 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
303 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400304 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400305 u64 length = 0;
306 u64 map_length;
307 struct bio_vec *bvec;
308 int i;
309 int ret;
310
311 bio_for_each_segment(bvec, bio, i) {
312 length += bvec->bv_len;
313 }
314 map_tree = &root->fs_info->mapping_tree;
315 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400316 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400317 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400318
Chris Mason239b14b2008-03-24 15:02:07 -0400319 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400320 return 1;
321 }
322 return 0;
323}
324
Chris Masonf1885912008-04-09 16:28:12 -0400325int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
326 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500327{
Chris Mason065631f2008-02-20 12:07:25 -0500328 struct btrfs_root *root = BTRFS_I(inode)->root;
329 struct btrfs_trans_handle *trans;
330 int ret = 0;
331
Chris Mason22c59942008-04-09 16:28:12 -0400332 if (!(rw & (1 << BIO_RW))) {
333 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
334 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400335 goto mapit;
336 }
Chris Mason065631f2008-02-20 12:07:25 -0500337
338 if (btrfs_test_opt(root, NODATASUM) ||
Chris Mason0b86a832008-03-24 15:01:56 -0400339 btrfs_test_flag(inode, NODATASUM)) {
340 goto mapit;
341 }
Chris Mason065631f2008-02-20 12:07:25 -0500342
343 mutex_lock(&root->fs_info->fs_mutex);
344 trans = btrfs_start_transaction(root, 1);
345 btrfs_set_trans_block_group(trans, inode);
346 btrfs_csum_file_blocks(trans, root, inode, bio);
347 ret = btrfs_end_transaction(trans, root);
348 BUG_ON(ret);
349 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -0400350mapit:
Chris Masonf1885912008-04-09 16:28:12 -0400351 return btrfs_map_bio(root, rw, bio, mirror_num);
Chris Mason065631f2008-02-20 12:07:25 -0500352}
Chris Mason6885f302008-02-20 16:11:05 -0500353
Chris Mason07157aa2007-08-30 08:50:51 -0400354int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
355{
356 int ret = 0;
357 struct inode *inode = page->mapping->host;
358 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500359 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400360 struct btrfs_csum_item *item;
361 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400362 u32 csum;
Yanb98b6762008-01-08 15:54:37 -0500363 if (btrfs_test_opt(root, NODATASUM) ||
364 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500365 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400366 mutex_lock(&root->fs_info->fs_mutex);
367 path = btrfs_alloc_path();
368 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
369 if (IS_ERR(item)) {
370 ret = PTR_ERR(item);
371 /* a csum that isn't present is a preallocated region. */
372 if (ret == -ENOENT || ret == -EFBIG)
373 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400374 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500375 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400376 goto out;
377 }
Chris Masonff79f812007-10-15 16:22:25 -0400378 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
379 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500380 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400381out:
382 if (path)
383 btrfs_free_path(path);
384 mutex_unlock(&root->fs_info->fs_mutex);
385 return ret;
386}
387
Chris Mason7e383262008-04-09 16:28:12 -0400388struct io_failure_record {
389 struct page *page;
390 u64 start;
391 u64 len;
392 u64 logical;
393 int last_mirror;
394};
395
396int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
397 struct page *page, u64 start, u64 end,
398 struct extent_state *state)
399{
400 struct io_failure_record *failrec = NULL;
401 u64 private;
402 struct extent_map *em;
403 struct inode *inode = page->mapping->host;
404 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
405 struct bio *bio;
406 int num_copies;
407 int ret;
408 u64 logical;
409
410 ret = get_state_private(failure_tree, start, &private);
411 if (ret) {
412 size_t pg_offset = start - page_offset(page);
413 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
414 if (!failrec)
415 return -ENOMEM;
416 failrec->start = start;
417 failrec->len = end - start + 1;
418 failrec->last_mirror = 0;
419
420 em = btrfs_get_extent(inode, NULL, pg_offset, start,
421 failrec->len, 0);
422
423 if (!em || IS_ERR(em)) {
424 kfree(failrec);
425 return -EIO;
426 }
427 logical = start - em->start;
428 logical = em->block_start + logical;
429 failrec->logical = logical;
430 free_extent_map(em);
431 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
432 EXTENT_DIRTY, GFP_NOFS);
433 set_state_private(failure_tree, start, (u64)failrec);
434 } else {
435 failrec = (struct io_failure_record *)private;
436 }
437 num_copies = btrfs_num_copies(
438 &BTRFS_I(inode)->root->fs_info->mapping_tree,
439 failrec->logical, failrec->len);
440 failrec->last_mirror++;
441 if (!state) {
442 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
443 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
444 failrec->start,
445 EXTENT_LOCKED);
446 if (state && state->start != failrec->start)
447 state = NULL;
448 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
449 }
450 if (!state || failrec->last_mirror > num_copies) {
451 set_state_private(failure_tree, failrec->start, 0);
452 clear_extent_bits(failure_tree, failrec->start,
453 failrec->start + failrec->len - 1,
454 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
455 kfree(failrec);
456 return -EIO;
457 }
458 bio = bio_alloc(GFP_NOFS, 1);
459 bio->bi_private = state;
460 bio->bi_end_io = failed_bio->bi_end_io;
461 bio->bi_sector = failrec->logical >> 9;
462 bio->bi_bdev = failed_bio->bi_bdev;
463 bio_add_page(bio, page, failrec->len, start - page_offset(page));
464 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
465 return 0;
466}
467
Chris Mason70dec802008-01-29 09:59:12 -0500468int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
469 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400470{
Chris Mason35ebb932007-10-30 16:56:53 -0400471 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400472 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500473 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400474 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500475 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400476 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400477 struct btrfs_root *root = BTRFS_I(inode)->root;
478 u32 csum = ~(u32)0;
Jens Axboebbf0d0062007-10-19 09:23:07 -0400479 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500480
Yanb98b6762008-01-08 15:54:37 -0500481 if (btrfs_test_opt(root, NODATASUM) ||
482 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500483 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500484 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500485 private = state->private;
486 ret = 0;
487 } else {
488 ret = get_state_private(io_tree, start, &private);
489 }
Jens Axboebbf0d0062007-10-19 09:23:07 -0400490 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400491 kaddr = kmap_atomic(page, KM_IRQ0);
492 if (ret) {
493 goto zeroit;
494 }
Chris Masonff79f812007-10-15 16:22:25 -0400495 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
496 btrfs_csum_final(csum, (char *)&csum);
497 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400498 goto zeroit;
499 }
500 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -0400501 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400502
503 /* if the io failure tree for this inode is non-empty,
504 * check to see if we've recovered from a failed IO
505 */
506 private = 0;
507 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
508 (u64)-1, 1, EXTENT_DIRTY)) {
509 u64 private_failure;
510 struct io_failure_record *failure;
511 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
512 start, &private_failure);
513 if (ret == 0) {
514 failure = (struct io_failure_record *)private_failure;
515 set_state_private(&BTRFS_I(inode)->io_failure_tree,
516 failure->start, 0);
517 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
518 failure->start,
519 failure->start + failure->len - 1,
520 EXTENT_DIRTY | EXTENT_LOCKED,
521 GFP_NOFS);
522 kfree(failure);
523 }
524 }
Chris Mason07157aa2007-08-30 08:50:51 -0400525 return 0;
526
527zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500528 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
529 page->mapping->host->i_ino, (unsigned long long)start, csum,
530 private);
Chris Masondb945352007-10-15 16:15:53 -0400531 memset(kaddr + offset, 1, end - start + 1);
532 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400533 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -0400534 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400535 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400536}
Chris Masonb888db22007-08-27 16:49:44 -0400537
Chris Mason39279cc2007-06-12 06:35:45 -0400538void btrfs_read_locked_inode(struct inode *inode)
539{
540 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400541 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400542 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400543 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400544 struct btrfs_root *root = BTRFS_I(inode)->root;
545 struct btrfs_key location;
546 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400547 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400548 int ret;
549
550 path = btrfs_alloc_path();
551 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400552 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400553 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500554
Chris Mason39279cc2007-06-12 06:35:45 -0400555 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400556 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400557 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400558
Chris Mason5f39d392007-10-15 16:14:19 -0400559 leaf = path->nodes[0];
560 inode_item = btrfs_item_ptr(leaf, path->slots[0],
561 struct btrfs_inode_item);
562
563 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
564 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
565 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
566 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
567 inode->i_size = btrfs_inode_size(leaf, inode_item);
568
569 tspec = btrfs_inode_atime(inode_item);
570 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
571 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
572
573 tspec = btrfs_inode_mtime(inode_item);
574 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
575 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
576
577 tspec = btrfs_inode_ctime(inode_item);
578 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
579 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
580
581 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
582 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400583 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400584 rdev = btrfs_inode_rdev(leaf, inode_item);
585
586 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400587 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
588 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500589 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500590 if (!BTRFS_I(inode)->block_group) {
591 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400592 NULL, 0,
593 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500594 }
Chris Mason39279cc2007-06-12 06:35:45 -0400595 btrfs_free_path(path);
596 inode_item = NULL;
597
598 mutex_unlock(&root->fs_info->fs_mutex);
599
600 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400601 case S_IFREG:
602 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400603 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500604 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400605 inode->i_fop = &btrfs_file_operations;
606 inode->i_op = &btrfs_file_inode_operations;
607 break;
608 case S_IFDIR:
609 inode->i_fop = &btrfs_dir_file_operations;
610 if (root == root->fs_info->tree_root)
611 inode->i_op = &btrfs_dir_ro_inode_operations;
612 else
613 inode->i_op = &btrfs_dir_inode_operations;
614 break;
615 case S_IFLNK:
616 inode->i_op = &btrfs_symlink_inode_operations;
617 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400618 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400619 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400620 default:
621 init_special_inode(inode, inode->i_mode, rdev);
622 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400623 }
624 return;
625
626make_bad:
627 btrfs_release_path(root, path);
628 btrfs_free_path(path);
629 mutex_unlock(&root->fs_info->fs_mutex);
630 make_bad_inode(inode);
631}
632
Chris Mason5f39d392007-10-15 16:14:19 -0400633static void fill_inode_item(struct extent_buffer *leaf,
634 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400635 struct inode *inode)
636{
Chris Mason5f39d392007-10-15 16:14:19 -0400637 btrfs_set_inode_uid(leaf, item, inode->i_uid);
638 btrfs_set_inode_gid(leaf, item, inode->i_gid);
639 btrfs_set_inode_size(leaf, item, inode->i_size);
640 btrfs_set_inode_mode(leaf, item, inode->i_mode);
641 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
642
643 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
644 inode->i_atime.tv_sec);
645 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
646 inode->i_atime.tv_nsec);
647
648 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
649 inode->i_mtime.tv_sec);
650 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
651 inode->i_mtime.tv_nsec);
652
653 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
654 inode->i_ctime.tv_sec);
655 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
656 inode->i_ctime.tv_nsec);
657
658 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
659 btrfs_set_inode_generation(leaf, item, inode->i_generation);
660 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500661 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400662 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400663 BTRFS_I(inode)->block_group->key.objectid);
664}
665
Chris Masona52d9a82007-08-27 16:49:44 -0400666int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400667 struct btrfs_root *root,
668 struct inode *inode)
669{
670 struct btrfs_inode_item *inode_item;
671 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400672 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400673 int ret;
674
675 path = btrfs_alloc_path();
676 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400677 ret = btrfs_lookup_inode(trans, root, path,
678 &BTRFS_I(inode)->location, 1);
679 if (ret) {
680 if (ret > 0)
681 ret = -ENOENT;
682 goto failed;
683 }
684
Chris Mason5f39d392007-10-15 16:14:19 -0400685 leaf = path->nodes[0];
686 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400687 struct btrfs_inode_item);
688
Chris Mason5f39d392007-10-15 16:14:19 -0400689 fill_inode_item(leaf, inode_item, inode);
690 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400691 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400692 ret = 0;
693failed:
694 btrfs_release_path(root, path);
695 btrfs_free_path(path);
696 return ret;
697}
698
699
700static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
701 struct btrfs_root *root,
702 struct inode *dir,
703 struct dentry *dentry)
704{
705 struct btrfs_path *path;
706 const char *name = dentry->d_name.name;
707 int name_len = dentry->d_name.len;
708 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400709 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400710 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400711 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400712
713 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400714 if (!path) {
715 ret = -ENOMEM;
716 goto err;
717 }
718
Chris Mason39279cc2007-06-12 06:35:45 -0400719 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
720 name, name_len, -1);
721 if (IS_ERR(di)) {
722 ret = PTR_ERR(di);
723 goto err;
724 }
725 if (!di) {
726 ret = -ENOENT;
727 goto err;
728 }
Chris Mason5f39d392007-10-15 16:14:19 -0400729 leaf = path->nodes[0];
730 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400731 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400732 if (ret)
733 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400734 btrfs_release_path(root, path);
735
736 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400737 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400738 if (IS_ERR(di)) {
739 ret = PTR_ERR(di);
740 goto err;
741 }
742 if (!di) {
743 ret = -ENOENT;
744 goto err;
745 }
746 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400747
748 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500749 ret = btrfs_del_inode_ref(trans, root, name, name_len,
750 dentry->d_inode->i_ino,
751 dentry->d_parent->d_inode->i_ino);
752 if (ret) {
753 printk("failed to delete reference to %.*s, "
754 "inode %lu parent %lu\n", name_len, name,
755 dentry->d_inode->i_ino,
756 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500757 }
Chris Mason39279cc2007-06-12 06:35:45 -0400758err:
759 btrfs_free_path(path);
760 if (!ret) {
761 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400762 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400763 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500764#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
765 dentry->d_inode->i_nlink--;
766#else
Chris Mason39279cc2007-06-12 06:35:45 -0400767 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500768#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400769 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400770 dir->i_sb->s_dirt = 1;
771 }
772 return ret;
773}
774
775static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
776{
777 struct btrfs_root *root;
778 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500779 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400780 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500781 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400782
783 root = BTRFS_I(dir)->root;
784 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500785
786 ret = btrfs_check_free_space(root, 1, 1);
787 if (ret)
788 goto fail;
789
Chris Mason39279cc2007-06-12 06:35:45 -0400790 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400791
Chris Mason39279cc2007-06-12 06:35:45 -0400792 btrfs_set_trans_block_group(trans, dir);
793 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400794 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400795
Chris Mason2da98f02008-01-16 11:44:43 -0500796 if (inode->i_nlink == 0) {
797 int found;
798 /* if the inode isn't linked anywhere,
799 * we don't need to worry about
800 * data=ordered
801 */
802 found = btrfs_del_ordered_inode(inode);
803 if (found == 1) {
804 atomic_dec(&inode->i_count);
805 }
806 }
807
Chris Mason39279cc2007-06-12 06:35:45 -0400808 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500809fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400810 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400811 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500812 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400813 return ret;
814}
815
816static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
817{
818 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500819 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400820 int ret;
821 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400822 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500823 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400824
Yan134d4512007-10-25 15:49:25 -0400825 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
826 return -ENOTEMPTY;
827
Chris Mason39279cc2007-06-12 06:35:45 -0400828 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500829 ret = btrfs_check_free_space(root, 1, 1);
830 if (ret)
831 goto fail;
832
Chris Mason39279cc2007-06-12 06:35:45 -0400833 trans = btrfs_start_transaction(root, 1);
834 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400835
836 /* now the directory is empty */
837 err = btrfs_unlink_trans(trans, root, dir, dentry);
838 if (!err) {
839 inode->i_size = 0;
840 }
Chris Mason39544012007-12-12 14:38:19 -0500841
Chris Masond3c2fdc2007-09-17 10:58:06 -0400842 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400843 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500844fail:
Yan134d4512007-10-25 15:49:25 -0400845 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400846 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500847 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500848
Chris Mason39279cc2007-06-12 06:35:45 -0400849 if (ret && !err)
850 err = ret;
851 return err;
852}
853
Chris Mason39279cc2007-06-12 06:35:45 -0400854/*
Chris Mason39279cc2007-06-12 06:35:45 -0400855 * this can truncate away extent items, csum items and directory items.
856 * It starts at a high offset and removes keys until it can't find
857 * any higher than i_size.
858 *
859 * csum items that cross the new i_size are truncated to the new size
860 * as well.
861 */
862static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
863 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500864 struct inode *inode,
865 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400866{
867 int ret;
868 struct btrfs_path *path;
869 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400870 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400871 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400872 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400873 struct btrfs_file_extent_item *fi;
874 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400875 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400876 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500877 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500878 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400879 int found_extent;
880 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500881 int pending_del_nr = 0;
882 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400883 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400884
Chris Masona52d9a82007-08-27 16:49:44 -0400885 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400886 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400887 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400888 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400889
Chris Mason39279cc2007-06-12 06:35:45 -0400890 /* FIXME, add redo link to tree so we don't leak on crash */
891 key.objectid = inode->i_ino;
892 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400893 key.type = (u8)-1;
894
Chris Mason85e21ba2008-01-29 15:11:36 -0500895 btrfs_init_path(path);
896search_again:
897 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
898 if (ret < 0) {
899 goto error;
900 }
901 if (ret > 0) {
902 BUG_ON(path->slots[0] == 0);
903 path->slots[0]--;
904 }
905
Chris Mason39279cc2007-06-12 06:35:45 -0400906 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400907 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400908 leaf = path->nodes[0];
909 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
910 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400911
Chris Mason5f39d392007-10-15 16:14:19 -0400912 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400913 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400914
Chris Mason85e21ba2008-01-29 15:11:36 -0500915 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400916 break;
917
Chris Mason5f39d392007-10-15 16:14:19 -0400918 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400919 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400920 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400921 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400922 extent_type = btrfs_file_extent_type(leaf, fi);
923 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400924 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400925 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400926 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
927 struct btrfs_item *item = btrfs_item_nr(leaf,
928 path->slots[0]);
929 item_end += btrfs_file_extent_inline_len(leaf,
930 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400931 }
Yan008630c2007-11-07 13:31:09 -0500932 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400933 }
934 if (found_type == BTRFS_CSUM_ITEM_KEY) {
935 ret = btrfs_csum_truncate(trans, root, path,
936 inode->i_size);
937 BUG_ON(ret);
938 }
Yan008630c2007-11-07 13:31:09 -0500939 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400940 if (found_type == BTRFS_DIR_ITEM_KEY) {
941 found_type = BTRFS_INODE_ITEM_KEY;
942 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
943 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500944 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
945 found_type = BTRFS_XATTR_ITEM_KEY;
946 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
947 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400948 } else if (found_type) {
949 found_type--;
950 } else {
951 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400952 }
Yana61721d2007-09-17 11:08:38 -0400953 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500954 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400955 }
Chris Mason5f39d392007-10-15 16:14:19 -0400956 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400957 del_item = 1;
958 else
959 del_item = 0;
960 found_extent = 0;
961
962 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400963 if (found_type != BTRFS_EXTENT_DATA_KEY)
964 goto delete;
965
966 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400967 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400968 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400969 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400970 u64 orig_num_bytes =
971 btrfs_file_extent_num_bytes(leaf, fi);
972 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400973 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -0500974 extent_num_bytes = extent_num_bytes &
975 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400976 btrfs_set_file_extent_num_bytes(leaf, fi,
977 extent_num_bytes);
978 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -0500979 extent_num_bytes);
980 if (extent_start != 0)
981 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -0400982 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400983 } else {
Chris Masondb945352007-10-15 16:15:53 -0400984 extent_num_bytes =
985 btrfs_file_extent_disk_num_bytes(leaf,
986 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400987 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -0500988 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400989 if (extent_start != 0) {
990 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -0500991 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -0400992 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500993 root_gen = btrfs_header_generation(leaf);
994 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400995 }
Chris Mason90692182008-02-08 13:49:28 -0500996 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
997 if (!del_item) {
998 u32 newsize = inode->i_size - found_key.offset;
999 dec_i_blocks(inode, item_end + 1 -
1000 found_key.offset - newsize);
1001 newsize =
1002 btrfs_file_extent_calc_inline_size(newsize);
1003 ret = btrfs_truncate_item(trans, root, path,
1004 newsize, 1);
1005 BUG_ON(ret);
1006 } else {
1007 dec_i_blocks(inode, item_end + 1 -
1008 found_key.offset);
1009 }
Chris Mason39279cc2007-06-12 06:35:45 -04001010 }
Chris Mason179e29e2007-11-01 11:28:41 -04001011delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001012 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001013 if (!pending_del_nr) {
1014 /* no pending yet, add ourselves */
1015 pending_del_slot = path->slots[0];
1016 pending_del_nr = 1;
1017 } else if (pending_del_nr &&
1018 path->slots[0] + 1 == pending_del_slot) {
1019 /* hop on the pending chunk */
1020 pending_del_nr++;
1021 pending_del_slot = path->slots[0];
1022 } else {
1023 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1024 }
Chris Mason39279cc2007-06-12 06:35:45 -04001025 } else {
1026 break;
1027 }
Chris Mason39279cc2007-06-12 06:35:45 -04001028 if (found_extent) {
1029 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001030 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001031 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001032 root_gen, inode->i_ino,
1033 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001034 BUG_ON(ret);
1035 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001036next:
1037 if (path->slots[0] == 0) {
1038 if (pending_del_nr)
1039 goto del_pending;
1040 btrfs_release_path(root, path);
1041 goto search_again;
1042 }
1043
1044 path->slots[0]--;
1045 if (pending_del_nr &&
1046 path->slots[0] + 1 != pending_del_slot) {
1047 struct btrfs_key debug;
1048del_pending:
1049 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1050 pending_del_slot);
1051 ret = btrfs_del_items(trans, root, path,
1052 pending_del_slot,
1053 pending_del_nr);
1054 BUG_ON(ret);
1055 pending_del_nr = 0;
1056 btrfs_release_path(root, path);
1057 goto search_again;
1058 }
Chris Mason39279cc2007-06-12 06:35:45 -04001059 }
1060 ret = 0;
1061error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001062 if (pending_del_nr) {
1063 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1064 pending_del_nr);
1065 }
Chris Mason39279cc2007-06-12 06:35:45 -04001066 btrfs_release_path(root, path);
1067 btrfs_free_path(path);
1068 inode->i_sb->s_dirt = 1;
1069 return ret;
1070}
1071
Chris Masonb888db22007-08-27 16:49:44 -04001072static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -04001073 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001074{
Chris Mason39279cc2007-06-12 06:35:45 -04001075 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -05001076 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001077 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001078 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -05001079 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001080
Chris Mason190662b2007-12-18 16:25:45 -05001081 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001082 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001083
Chris Masond1310b22008-01-24 16:13:08 -05001084 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001085 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -04001086 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -05001087
Chris Masona52d9a82007-08-27 16:49:44 -04001088 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -04001089 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001090 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1091 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -04001092 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001093 }
Chris Masonb888db22007-08-27 16:49:44 -04001094 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -05001095 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001096
Chris Masona52d9a82007-08-27 16:49:44 -04001097 return ret;
1098}
1099
1100/*
1101 * taken from block_truncate_page, but does cow as it zeros out
1102 * any bytes left in the last page in the file.
1103 */
1104static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1105{
1106 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001107 struct btrfs_root *root = BTRFS_I(inode)->root;
1108 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001109 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1110 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1111 struct page *page;
1112 int ret = 0;
1113 u64 page_start;
1114
1115 if ((offset & (blocksize - 1)) == 0)
1116 goto out;
1117
1118 ret = -ENOMEM;
1119 page = grab_cache_page(mapping, index);
1120 if (!page)
1121 goto out;
1122 if (!PageUptodate(page)) {
1123 ret = btrfs_readpage(NULL, page);
1124 lock_page(page);
1125 if (!PageUptodate(page)) {
1126 ret = -EIO;
1127 goto out;
1128 }
1129 }
Chris Mason35ebb932007-10-30 16:56:53 -04001130 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001131
Chris Masonb888db22007-08-27 16:49:44 -04001132 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001133
Chris Mason39279cc2007-06-12 06:35:45 -04001134 unlock_page(page);
1135 page_cache_release(page);
1136out:
1137 return ret;
1138}
1139
1140static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1141{
1142 struct inode *inode = dentry->d_inode;
1143 int err;
1144
1145 err = inode_change_ok(inode, attr);
1146 if (err)
1147 return err;
1148
1149 if (S_ISREG(inode->i_mode) &&
1150 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1151 struct btrfs_trans_handle *trans;
1152 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001153 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001154
Chris Mason5f39d392007-10-15 16:14:19 -04001155 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001156 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001157 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001158 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001159 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001160
Chris Mason1b0f7c22008-01-30 14:33:02 -05001161 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001162 goto out;
1163
Chris Mason1832a6d2007-12-21 16:27:21 -05001164 mutex_lock(&root->fs_info->fs_mutex);
1165 err = btrfs_check_free_space(root, 1, 0);
1166 mutex_unlock(&root->fs_info->fs_mutex);
1167 if (err)
1168 goto fail;
1169
Chris Mason39279cc2007-06-12 06:35:45 -04001170 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1171
Chris Mason1b0f7c22008-01-30 14:33:02 -05001172 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001173 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001174
1175 mutex_lock(&root->fs_info->fs_mutex);
1176 trans = btrfs_start_transaction(root, 1);
1177 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001178 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001179 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001180 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001181
Chris Mason179e29e2007-11-01 11:28:41 -04001182 if (alloc_hint != EXTENT_MAP_INLINE) {
1183 err = btrfs_insert_file_extent(trans, root,
1184 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001185 hole_start, 0, 0,
1186 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001187 btrfs_drop_extent_cache(inode, hole_start,
1188 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001189 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001190 }
Chris Mason39279cc2007-06-12 06:35:45 -04001191 btrfs_end_transaction(trans, root);
1192 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001193 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001194 if (err)
1195 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001196 }
1197out:
1198 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001199fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001200 return err;
1201}
Chris Mason61295eb2008-01-14 16:24:38 -05001202
Chris Mason2da98f02008-01-16 11:44:43 -05001203void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001204{
Chris Mason2da98f02008-01-16 11:44:43 -05001205 int ret;
1206
1207 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001208 return;
1209 }
Chris Mason2da98f02008-01-16 11:44:43 -05001210
1211 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1212 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1213 return;
1214
1215 ret = btrfs_del_ordered_inode(inode);
1216 if (ret == 1) {
1217 atomic_dec(&inode->i_count);
1218 }
Chris Mason61295eb2008-01-14 16:24:38 -05001219}
1220
Chris Mason39279cc2007-06-12 06:35:45 -04001221void btrfs_delete_inode(struct inode *inode)
1222{
1223 struct btrfs_trans_handle *trans;
1224 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001225 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001226 int ret;
1227
1228 truncate_inode_pages(&inode->i_data, 0);
1229 if (is_bad_inode(inode)) {
1230 goto no_delete;
1231 }
Chris Mason5f39d392007-10-15 16:14:19 -04001232
Chris Mason39279cc2007-06-12 06:35:45 -04001233 inode->i_size = 0;
1234 mutex_lock(&root->fs_info->fs_mutex);
1235 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001236
Chris Mason39279cc2007-06-12 06:35:45 -04001237 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001238 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001239 if (ret)
1240 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001241
Chris Masond3c2fdc2007-09-17 10:58:06 -04001242 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001243 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001244
Chris Mason39279cc2007-06-12 06:35:45 -04001245 btrfs_end_transaction(trans, root);
1246 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001247 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001248 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001249 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001250
1251no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001252 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001253 btrfs_end_transaction(trans, root);
1254 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001255 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001256 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001257no_delete:
1258 clear_inode(inode);
1259}
1260
1261/*
1262 * this returns the key found in the dir entry in the location pointer.
1263 * If no dir entries were found, location->objectid is 0.
1264 */
1265static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1266 struct btrfs_key *location)
1267{
1268 const char *name = dentry->d_name.name;
1269 int namelen = dentry->d_name.len;
1270 struct btrfs_dir_item *di;
1271 struct btrfs_path *path;
1272 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001273 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001274
Chris Mason39544012007-12-12 14:38:19 -05001275 if (namelen == 1 && strcmp(name, ".") == 0) {
1276 location->objectid = dir->i_ino;
1277 location->type = BTRFS_INODE_ITEM_KEY;
1278 location->offset = 0;
1279 return 0;
1280 }
Chris Mason39279cc2007-06-12 06:35:45 -04001281 path = btrfs_alloc_path();
1282 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001283
Chris Mason7a720532007-12-13 09:06:59 -05001284 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001285 struct btrfs_key key;
1286 struct extent_buffer *leaf;
1287 u32 nritems;
1288 int slot;
1289
1290 key.objectid = dir->i_ino;
1291 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1292 key.offset = 0;
1293 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1294 BUG_ON(ret == 0);
1295 ret = 0;
1296
1297 leaf = path->nodes[0];
1298 slot = path->slots[0];
1299 nritems = btrfs_header_nritems(leaf);
1300 if (slot >= nritems)
1301 goto out_err;
1302
1303 btrfs_item_key_to_cpu(leaf, &key, slot);
1304 if (key.objectid != dir->i_ino ||
1305 key.type != BTRFS_INODE_REF_KEY) {
1306 goto out_err;
1307 }
1308 location->objectid = key.offset;
1309 location->type = BTRFS_INODE_ITEM_KEY;
1310 location->offset = 0;
1311 goto out;
1312 }
1313
Chris Mason39279cc2007-06-12 06:35:45 -04001314 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1315 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001316 if (IS_ERR(di))
1317 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001318 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001319 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001320 }
Chris Mason5f39d392007-10-15 16:14:19 -04001321 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001322out:
Chris Mason39279cc2007-06-12 06:35:45 -04001323 btrfs_free_path(path);
1324 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001325out_err:
1326 location->objectid = 0;
1327 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001328}
1329
1330/*
1331 * when we hit a tree root in a directory, the btrfs part of the inode
1332 * needs to be changed to reflect the root directory of the tree root. This
1333 * is kind of like crossing a mount point.
1334 */
1335static int fixup_tree_root_location(struct btrfs_root *root,
1336 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001337 struct btrfs_root **sub_root,
1338 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001339{
1340 struct btrfs_path *path;
1341 struct btrfs_root_item *ri;
1342
1343 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1344 return 0;
1345 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1346 return 0;
1347
1348 path = btrfs_alloc_path();
1349 BUG_ON(!path);
1350 mutex_lock(&root->fs_info->fs_mutex);
1351
Josef Bacik58176a92007-08-29 15:47:34 -04001352 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1353 dentry->d_name.name,
1354 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001355 if (IS_ERR(*sub_root))
1356 return PTR_ERR(*sub_root);
1357
1358 ri = &(*sub_root)->root_item;
1359 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001360 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1361 location->offset = 0;
1362
1363 btrfs_free_path(path);
1364 mutex_unlock(&root->fs_info->fs_mutex);
1365 return 0;
1366}
1367
1368static int btrfs_init_locked_inode(struct inode *inode, void *p)
1369{
1370 struct btrfs_iget_args *args = p;
1371 inode->i_ino = args->ino;
1372 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001373 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001374 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1375 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001376 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001377 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1378 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001379 return 0;
1380}
1381
1382static int btrfs_find_actor(struct inode *inode, void *opaque)
1383{
1384 struct btrfs_iget_args *args = opaque;
1385 return (args->ino == inode->i_ino &&
1386 args->root == BTRFS_I(inode)->root);
1387}
1388
Chris Masondc17ff82008-01-08 15:46:30 -05001389struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1390 u64 root_objectid)
1391{
1392 struct btrfs_iget_args args;
1393 args.ino = objectid;
1394 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1395
1396 if (!args.root)
1397 return NULL;
1398
1399 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1400}
1401
Chris Mason39279cc2007-06-12 06:35:45 -04001402struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1403 struct btrfs_root *root)
1404{
1405 struct inode *inode;
1406 struct btrfs_iget_args args;
1407 args.ino = objectid;
1408 args.root = root;
1409
1410 inode = iget5_locked(s, objectid, btrfs_find_actor,
1411 btrfs_init_locked_inode,
1412 (void *)&args);
1413 return inode;
1414}
1415
1416static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1417 struct nameidata *nd)
1418{
1419 struct inode * inode;
1420 struct btrfs_inode *bi = BTRFS_I(dir);
1421 struct btrfs_root *root = bi->root;
1422 struct btrfs_root *sub_root = root;
1423 struct btrfs_key location;
1424 int ret;
1425
1426 if (dentry->d_name.len > BTRFS_NAME_LEN)
1427 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001428
Chris Mason39279cc2007-06-12 06:35:45 -04001429 mutex_lock(&root->fs_info->fs_mutex);
1430 ret = btrfs_inode_by_name(dir, dentry, &location);
1431 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001432
Chris Mason39279cc2007-06-12 06:35:45 -04001433 if (ret < 0)
1434 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001435
Chris Mason39279cc2007-06-12 06:35:45 -04001436 inode = NULL;
1437 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001438 ret = fixup_tree_root_location(root, &location, &sub_root,
1439 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001440 if (ret < 0)
1441 return ERR_PTR(ret);
1442 if (ret > 0)
1443 return ERR_PTR(-ENOENT);
1444 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1445 sub_root);
1446 if (!inode)
1447 return ERR_PTR(-EACCES);
1448 if (inode->i_state & I_NEW) {
1449 /* the inode and parent dir are two different roots */
1450 if (sub_root != root) {
1451 igrab(inode);
1452 sub_root->inode = inode;
1453 }
1454 BTRFS_I(inode)->root = sub_root;
1455 memcpy(&BTRFS_I(inode)->location, &location,
1456 sizeof(location));
1457 btrfs_read_locked_inode(inode);
1458 unlock_new_inode(inode);
1459 }
1460 }
1461 return d_splice_alias(inode, dentry);
1462}
1463
Chris Mason39279cc2007-06-12 06:35:45 -04001464static unsigned char btrfs_filetype_table[] = {
1465 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1466};
1467
1468static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1469{
Chris Mason6da6aba2007-12-18 16:15:09 -05001470 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001471 struct btrfs_root *root = BTRFS_I(inode)->root;
1472 struct btrfs_item *item;
1473 struct btrfs_dir_item *di;
1474 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001475 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001476 struct btrfs_path *path;
1477 int ret;
1478 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001479 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001480 int slot;
1481 int advance;
1482 unsigned char d_type;
1483 int over = 0;
1484 u32 di_cur;
1485 u32 di_total;
1486 u32 di_len;
1487 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001488 char tmp_name[32];
1489 char *name_ptr;
1490 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001491
1492 /* FIXME, use a real flag for deciding about the key type */
1493 if (root->fs_info->tree_root == root)
1494 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001495
Chris Mason39544012007-12-12 14:38:19 -05001496 /* special case for "." */
1497 if (filp->f_pos == 0) {
1498 over = filldir(dirent, ".", 1,
1499 1, inode->i_ino,
1500 DT_DIR);
1501 if (over)
1502 return 0;
1503 filp->f_pos = 1;
1504 }
1505
Chris Mason39279cc2007-06-12 06:35:45 -04001506 mutex_lock(&root->fs_info->fs_mutex);
1507 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001508 path = btrfs_alloc_path();
1509 path->reada = 2;
1510
1511 /* special case for .., just use the back ref */
1512 if (filp->f_pos == 1) {
1513 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1514 key.offset = 0;
1515 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1516 BUG_ON(ret == 0);
1517 leaf = path->nodes[0];
1518 slot = path->slots[0];
1519 nritems = btrfs_header_nritems(leaf);
1520 if (slot >= nritems) {
1521 btrfs_release_path(root, path);
1522 goto read_dir_items;
1523 }
1524 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1525 btrfs_release_path(root, path);
1526 if (found_key.objectid != key.objectid ||
1527 found_key.type != BTRFS_INODE_REF_KEY)
1528 goto read_dir_items;
1529 over = filldir(dirent, "..", 2,
1530 2, found_key.offset, DT_DIR);
1531 if (over)
1532 goto nopos;
1533 filp->f_pos = 2;
1534 }
1535
1536read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001537 btrfs_set_key_type(&key, key_type);
1538 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001539
Chris Mason39279cc2007-06-12 06:35:45 -04001540 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1541 if (ret < 0)
1542 goto err;
1543 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001544 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001545 leaf = path->nodes[0];
1546 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001547 slot = path->slots[0];
1548 if (advance || slot >= nritems) {
1549 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001550 ret = btrfs_next_leaf(root, path);
1551 if (ret)
1552 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001553 leaf = path->nodes[0];
1554 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001555 slot = path->slots[0];
1556 } else {
1557 slot++;
1558 path->slots[0]++;
1559 }
1560 }
1561 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001562 item = btrfs_item_nr(leaf, slot);
1563 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1564
1565 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001566 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001567 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001568 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001569 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001570 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001571
1572 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001573 advance = 1;
1574 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1575 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001576 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001577 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001578 struct btrfs_key location;
1579
1580 name_len = btrfs_dir_name_len(leaf, di);
1581 if (name_len < 32) {
1582 name_ptr = tmp_name;
1583 } else {
1584 name_ptr = kmalloc(name_len, GFP_NOFS);
1585 BUG_ON(!name_ptr);
1586 }
1587 read_extent_buffer(leaf, name_ptr,
1588 (unsigned long)(di + 1), name_len);
1589
1590 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1591 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001592 over = filldir(dirent, name_ptr, name_len,
1593 found_key.offset,
1594 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001595 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001596
1597 if (name_ptr != tmp_name)
1598 kfree(name_ptr);
1599
Chris Mason39279cc2007-06-12 06:35:45 -04001600 if (over)
1601 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001602 di_len = btrfs_dir_name_len(leaf, di) +
1603 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001604 di_cur += di_len;
1605 di = (struct btrfs_dir_item *)((char *)di + di_len);
1606 }
1607 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001608 if (key_type == BTRFS_DIR_INDEX_KEY)
1609 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1610 else
1611 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001612nopos:
1613 ret = 0;
1614err:
1615 btrfs_release_path(root, path);
1616 btrfs_free_path(path);
1617 mutex_unlock(&root->fs_info->fs_mutex);
1618 return ret;
1619}
1620
1621int btrfs_write_inode(struct inode *inode, int wait)
1622{
1623 struct btrfs_root *root = BTRFS_I(inode)->root;
1624 struct btrfs_trans_handle *trans;
1625 int ret = 0;
1626
1627 if (wait) {
1628 mutex_lock(&root->fs_info->fs_mutex);
1629 trans = btrfs_start_transaction(root, 1);
1630 btrfs_set_trans_block_group(trans, inode);
1631 ret = btrfs_commit_transaction(trans, root);
1632 mutex_unlock(&root->fs_info->fs_mutex);
1633 }
1634 return ret;
1635}
1636
1637/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001638 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001639 * inode changes. But, it is most likely to find the inode in cache.
1640 * FIXME, needs more benchmarking...there are no reasons other than performance
1641 * to keep or drop this code.
1642 */
1643void btrfs_dirty_inode(struct inode *inode)
1644{
1645 struct btrfs_root *root = BTRFS_I(inode)->root;
1646 struct btrfs_trans_handle *trans;
1647
1648 mutex_lock(&root->fs_info->fs_mutex);
1649 trans = btrfs_start_transaction(root, 1);
1650 btrfs_set_trans_block_group(trans, inode);
1651 btrfs_update_inode(trans, root, inode);
1652 btrfs_end_transaction(trans, root);
1653 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001654}
1655
1656static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1657 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001658 const char *name, int name_len,
1659 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001660 u64 objectid,
1661 struct btrfs_block_group_cache *group,
1662 int mode)
1663{
1664 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001665 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001666 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001667 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001668 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001669 struct btrfs_inode_ref *ref;
1670 struct btrfs_key key[2];
1671 u32 sizes[2];
1672 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001673 int ret;
1674 int owner;
1675
Chris Mason5f39d392007-10-15 16:14:19 -04001676 path = btrfs_alloc_path();
1677 BUG_ON(!path);
1678
Chris Mason39279cc2007-06-12 06:35:45 -04001679 inode = new_inode(root->fs_info->sb);
1680 if (!inode)
1681 return ERR_PTR(-ENOMEM);
1682
Chris Masond1310b22008-01-24 16:13:08 -05001683 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1684 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001685 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001686 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1687 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001688 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001689 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001690
Chris Mason39279cc2007-06-12 06:35:45 -04001691 if (mode & S_IFDIR)
1692 owner = 0;
1693 else
1694 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001695 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001696 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001697 if (!new_inode_group) {
1698 printk("find_block group failed\n");
1699 new_inode_group = group;
1700 }
1701 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001702 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001703
1704 key[0].objectid = objectid;
1705 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1706 key[0].offset = 0;
1707
1708 key[1].objectid = objectid;
1709 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1710 key[1].offset = ref_objectid;
1711
1712 sizes[0] = sizeof(struct btrfs_inode_item);
1713 sizes[1] = name_len + sizeof(*ref);
1714
1715 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1716 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001717 goto fail;
1718
Chris Mason9c583092008-01-29 15:15:18 -05001719 if (objectid > root->highest_inode)
1720 root->highest_inode = objectid;
1721
Chris Mason39279cc2007-06-12 06:35:45 -04001722 inode->i_uid = current->fsuid;
1723 inode->i_gid = current->fsgid;
1724 inode->i_mode = mode;
1725 inode->i_ino = objectid;
1726 inode->i_blocks = 0;
1727 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001728 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1729 struct btrfs_inode_item);
1730 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001731
1732 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1733 struct btrfs_inode_ref);
1734 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1735 ptr = (unsigned long)(ref + 1);
1736 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1737
Chris Mason5f39d392007-10-15 16:14:19 -04001738 btrfs_mark_buffer_dirty(path->nodes[0]);
1739 btrfs_free_path(path);
1740
Chris Mason39279cc2007-06-12 06:35:45 -04001741 location = &BTRFS_I(inode)->location;
1742 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001743 location->offset = 0;
1744 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1745
Chris Mason39279cc2007-06-12 06:35:45 -04001746 insert_inode_hash(inode);
1747 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001748fail:
1749 btrfs_free_path(path);
1750 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001751}
1752
1753static inline u8 btrfs_inode_type(struct inode *inode)
1754{
1755 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1756}
1757
1758static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001759 struct dentry *dentry, struct inode *inode,
1760 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001761{
1762 int ret;
1763 struct btrfs_key key;
1764 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001765 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001766
Chris Mason39279cc2007-06-12 06:35:45 -04001767 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001768 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1769 key.offset = 0;
1770
1771 ret = btrfs_insert_dir_item(trans, root,
1772 dentry->d_name.name, dentry->d_name.len,
1773 dentry->d_parent->d_inode->i_ino,
1774 &key, btrfs_inode_type(inode));
1775 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001776 if (add_backref) {
1777 ret = btrfs_insert_inode_ref(trans, root,
1778 dentry->d_name.name,
1779 dentry->d_name.len,
1780 inode->i_ino,
1781 dentry->d_parent->d_inode->i_ino);
1782 }
Chris Mason79c44582007-06-25 10:09:33 -04001783 parent_inode = dentry->d_parent->d_inode;
1784 parent_inode->i_size += dentry->d_name.len * 2;
1785 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001786 ret = btrfs_update_inode(trans, root,
1787 dentry->d_parent->d_inode);
1788 }
1789 return ret;
1790}
1791
1792static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001793 struct dentry *dentry, struct inode *inode,
1794 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001795{
Chris Mason9c583092008-01-29 15:15:18 -05001796 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001797 if (!err) {
1798 d_instantiate(dentry, inode);
1799 return 0;
1800 }
1801 if (err > 0)
1802 err = -EEXIST;
1803 return err;
1804}
1805
Josef Bacik618e21d2007-07-11 10:18:17 -04001806static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1807 int mode, dev_t rdev)
1808{
1809 struct btrfs_trans_handle *trans;
1810 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001811 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001812 int err;
1813 int drop_inode = 0;
1814 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001815 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001816
1817 if (!new_valid_dev(rdev))
1818 return -EINVAL;
1819
1820 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001821 err = btrfs_check_free_space(root, 1, 0);
1822 if (err)
1823 goto fail;
1824
Josef Bacik618e21d2007-07-11 10:18:17 -04001825 trans = btrfs_start_transaction(root, 1);
1826 btrfs_set_trans_block_group(trans, dir);
1827
1828 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1829 if (err) {
1830 err = -ENOSPC;
1831 goto out_unlock;
1832 }
1833
Chris Mason9c583092008-01-29 15:15:18 -05001834 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1835 dentry->d_name.len,
1836 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001837 BTRFS_I(dir)->block_group, mode);
1838 err = PTR_ERR(inode);
1839 if (IS_ERR(inode))
1840 goto out_unlock;
1841
1842 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001843 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001844 if (err)
1845 drop_inode = 1;
1846 else {
1847 inode->i_op = &btrfs_special_inode_operations;
1848 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001849 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001850 }
1851 dir->i_sb->s_dirt = 1;
1852 btrfs_update_inode_block_group(trans, inode);
1853 btrfs_update_inode_block_group(trans, dir);
1854out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001855 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001856 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001857fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001858 mutex_unlock(&root->fs_info->fs_mutex);
1859
1860 if (drop_inode) {
1861 inode_dec_link_count(inode);
1862 iput(inode);
1863 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001864 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001865 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001866 return err;
1867}
1868
Chris Mason39279cc2007-06-12 06:35:45 -04001869static int btrfs_create(struct inode *dir, struct dentry *dentry,
1870 int mode, struct nameidata *nd)
1871{
1872 struct btrfs_trans_handle *trans;
1873 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001874 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001875 int err;
1876 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001877 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001878 u64 objectid;
1879
1880 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001881 err = btrfs_check_free_space(root, 1, 0);
1882 if (err)
1883 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001884 trans = btrfs_start_transaction(root, 1);
1885 btrfs_set_trans_block_group(trans, dir);
1886
1887 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1888 if (err) {
1889 err = -ENOSPC;
1890 goto out_unlock;
1891 }
1892
Chris Mason9c583092008-01-29 15:15:18 -05001893 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1894 dentry->d_name.len,
1895 dentry->d_parent->d_inode->i_ino,
1896 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001897 err = PTR_ERR(inode);
1898 if (IS_ERR(inode))
1899 goto out_unlock;
1900
1901 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001902 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001903 if (err)
1904 drop_inode = 1;
1905 else {
1906 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001907 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001908 inode->i_fop = &btrfs_file_operations;
1909 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001910 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1911 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001912 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001913 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1914 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001915 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001916 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001917 }
1918 dir->i_sb->s_dirt = 1;
1919 btrfs_update_inode_block_group(trans, inode);
1920 btrfs_update_inode_block_group(trans, dir);
1921out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001922 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001923 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001924fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001925 mutex_unlock(&root->fs_info->fs_mutex);
1926
1927 if (drop_inode) {
1928 inode_dec_link_count(inode);
1929 iput(inode);
1930 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001931 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001932 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001933 return err;
1934}
1935
1936static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1937 struct dentry *dentry)
1938{
1939 struct btrfs_trans_handle *trans;
1940 struct btrfs_root *root = BTRFS_I(dir)->root;
1941 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001942 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001943 int err;
1944 int drop_inode = 0;
1945
1946 if (inode->i_nlink == 0)
1947 return -ENOENT;
1948
Chris Mason6da6aba2007-12-18 16:15:09 -05001949#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1950 inode->i_nlink++;
1951#else
Chris Mason39279cc2007-06-12 06:35:45 -04001952 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001953#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001954 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001955 err = btrfs_check_free_space(root, 1, 0);
1956 if (err)
1957 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001958 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001959
Chris Mason39279cc2007-06-12 06:35:45 -04001960 btrfs_set_trans_block_group(trans, dir);
1961 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05001962 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001963
Chris Mason39279cc2007-06-12 06:35:45 -04001964 if (err)
1965 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001966
Chris Mason39279cc2007-06-12 06:35:45 -04001967 dir->i_sb->s_dirt = 1;
1968 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001969 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001970
Chris Mason54aa1f42007-06-22 14:16:25 -04001971 if (err)
1972 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001973
Chris Masond3c2fdc2007-09-17 10:58:06 -04001974 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001975 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001976fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001977 mutex_unlock(&root->fs_info->fs_mutex);
1978
1979 if (drop_inode) {
1980 inode_dec_link_count(inode);
1981 iput(inode);
1982 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001983 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001984 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001985 return err;
1986}
1987
Chris Mason39279cc2007-06-12 06:35:45 -04001988static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1989{
1990 struct inode *inode;
1991 struct btrfs_trans_handle *trans;
1992 struct btrfs_root *root = BTRFS_I(dir)->root;
1993 int err = 0;
1994 int drop_on_err = 0;
1995 u64 objectid;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001996 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001997
1998 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001999 err = btrfs_check_free_space(root, 1, 0);
2000 if (err)
2001 goto out_unlock;
2002
Chris Mason39279cc2007-06-12 06:35:45 -04002003 trans = btrfs_start_transaction(root, 1);
2004 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002005
Chris Mason39279cc2007-06-12 06:35:45 -04002006 if (IS_ERR(trans)) {
2007 err = PTR_ERR(trans);
2008 goto out_unlock;
2009 }
2010
2011 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2012 if (err) {
2013 err = -ENOSPC;
2014 goto out_unlock;
2015 }
2016
Chris Mason9c583092008-01-29 15:15:18 -05002017 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2018 dentry->d_name.len,
2019 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002020 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2021 if (IS_ERR(inode)) {
2022 err = PTR_ERR(inode);
2023 goto out_fail;
2024 }
Chris Mason5f39d392007-10-15 16:14:19 -04002025
Chris Mason39279cc2007-06-12 06:35:45 -04002026 drop_on_err = 1;
2027 inode->i_op = &btrfs_dir_inode_operations;
2028 inode->i_fop = &btrfs_dir_file_operations;
2029 btrfs_set_trans_block_group(trans, inode);
2030
Chris Mason39544012007-12-12 14:38:19 -05002031 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002032 err = btrfs_update_inode(trans, root, inode);
2033 if (err)
2034 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002035
Chris Mason9c583092008-01-29 15:15:18 -05002036 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002037 if (err)
2038 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002039
Chris Mason39279cc2007-06-12 06:35:45 -04002040 d_instantiate(dentry, inode);
2041 drop_on_err = 0;
2042 dir->i_sb->s_dirt = 1;
2043 btrfs_update_inode_block_group(trans, inode);
2044 btrfs_update_inode_block_group(trans, dir);
2045
2046out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002047 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002048 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002049
Chris Mason39279cc2007-06-12 06:35:45 -04002050out_unlock:
2051 mutex_unlock(&root->fs_info->fs_mutex);
2052 if (drop_on_err)
2053 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002054 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002055 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002056 return err;
2057}
2058
Chris Masona52d9a82007-08-27 16:49:44 -04002059struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002060 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002061 int create)
2062{
2063 int ret;
2064 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002065 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002066 u64 extent_start = 0;
2067 u64 extent_end = 0;
2068 u64 objectid = inode->i_ino;
2069 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002070 struct btrfs_path *path;
2071 struct btrfs_root *root = BTRFS_I(inode)->root;
2072 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002073 struct extent_buffer *leaf;
2074 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002075 struct extent_map *em = NULL;
2076 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002077 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002078 struct btrfs_trans_handle *trans = NULL;
2079
2080 path = btrfs_alloc_path();
2081 BUG_ON(!path);
2082 mutex_lock(&root->fs_info->fs_mutex);
2083
2084again:
Chris Masond1310b22008-01-24 16:13:08 -05002085 spin_lock(&em_tree->lock);
2086 em = lookup_extent_mapping(em_tree, start, len);
2087 spin_unlock(&em_tree->lock);
2088
Chris Masona52d9a82007-08-27 16:49:44 -04002089 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05002090 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05002091 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
2092 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05002093 WARN_ON(1);
2094 }
Chris Mason70dec802008-01-29 09:59:12 -05002095 if (em->block_start == EXTENT_MAP_INLINE && page)
2096 free_extent_map(em);
2097 else
2098 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002099 }
Chris Masond1310b22008-01-24 16:13:08 -05002100 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002101 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002102 err = -ENOMEM;
2103 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002104 }
Chris Masond1310b22008-01-24 16:13:08 -05002105
2106 em->start = EXTENT_MAP_HOLE;
2107 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04002108 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04002109 ret = btrfs_lookup_file_extent(trans, root, path,
2110 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002111 if (ret < 0) {
2112 err = ret;
2113 goto out;
2114 }
2115
2116 if (ret != 0) {
2117 if (path->slots[0] == 0)
2118 goto not_found;
2119 path->slots[0]--;
2120 }
2121
Chris Mason5f39d392007-10-15 16:14:19 -04002122 leaf = path->nodes[0];
2123 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002124 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002125 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002126 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2127 found_type = btrfs_key_type(&found_key);
2128 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002129 found_type != BTRFS_EXTENT_DATA_KEY) {
2130 goto not_found;
2131 }
2132
Chris Mason5f39d392007-10-15 16:14:19 -04002133 found_type = btrfs_file_extent_type(leaf, item);
2134 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002135 if (found_type == BTRFS_FILE_EXTENT_REG) {
2136 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002137 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002138 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002139 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002140 em->start = start;
2141 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002142 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002143 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002144 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002145 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002146 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002147 }
2148 goto not_found_em;
2149 }
Chris Masondb945352007-10-15 16:15:53 -04002150 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2151 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002152 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002153 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002154 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002155 goto insert;
2156 }
Chris Masondb945352007-10-15 16:15:53 -04002157 bytenr += btrfs_file_extent_offset(leaf, item);
2158 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002159 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002160 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002161 goto insert;
2162 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002163 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002164 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002165 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002166 size_t size;
2167 size_t extent_offset;
2168 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002169
Chris Mason5f39d392007-10-15 16:14:19 -04002170 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2171 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002172 extent_end = (extent_start + size + root->sectorsize - 1) &
2173 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002174 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002175 em->start = start;
2176 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002177 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002178 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002179 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002180 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002181 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002182 }
2183 goto not_found_em;
2184 }
2185 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002186
2187 if (!page) {
2188 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002189 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002190 goto out;
2191 }
2192
Chris Mason70dec802008-01-29 09:59:12 -05002193 page_start = page_offset(page) + pg_offset;
2194 extent_offset = page_start - extent_start;
2195 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002196 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002197 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002198 em->len = (copy_size + root->sectorsize - 1) &
2199 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002200 map = kmap(page);
2201 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002202 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002203 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002204 copy_size);
2205 flush_dcache_page(page);
2206 } else if (create && PageUptodate(page)) {
2207 if (!trans) {
2208 kunmap(page);
2209 free_extent_map(em);
2210 em = NULL;
2211 btrfs_release_path(root, path);
2212 trans = btrfs_start_transaction(root, 1);
2213 goto again;
2214 }
Chris Mason70dec802008-01-29 09:59:12 -05002215 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002216 copy_size);
2217 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002218 }
Chris Masona52d9a82007-08-27 16:49:44 -04002219 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002220 set_extent_uptodate(io_tree, em->start,
2221 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002222 goto insert;
2223 } else {
2224 printk("unkknown found_type %d\n", found_type);
2225 WARN_ON(1);
2226 }
2227not_found:
2228 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002229 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002230not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002231 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002232insert:
2233 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002234 if (em->start > start || extent_map_end(em) <= start) {
2235 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002236 err = -EIO;
2237 goto out;
2238 }
Chris Masond1310b22008-01-24 16:13:08 -05002239
2240 err = 0;
2241 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002242 ret = add_extent_mapping(em_tree, em);
2243 if (ret == -EEXIST) {
2244 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002245 em = lookup_extent_mapping(em_tree, start, len);
2246 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002247 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002248 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002249 }
Chris Masona52d9a82007-08-27 16:49:44 -04002250 }
Chris Masond1310b22008-01-24 16:13:08 -05002251 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002252out:
2253 btrfs_free_path(path);
2254 if (trans) {
2255 ret = btrfs_end_transaction(trans, root);
2256 if (!err)
2257 err = ret;
2258 }
2259 mutex_unlock(&root->fs_info->fs_mutex);
2260 if (err) {
2261 free_extent_map(em);
2262 WARN_ON(1);
2263 return ERR_PTR(err);
2264 }
2265 return em;
2266}
2267
Chris Mason16432982008-04-10 10:23:21 -04002268static int btrfs_get_block(struct inode *inode, sector_t iblock,
2269 struct buffer_head *bh_result, int create)
2270{
2271 struct extent_map *em;
2272 u64 start = (u64)iblock << inode->i_blkbits;
2273 struct btrfs_multi_bio *multi = NULL;
2274 struct btrfs_root *root = BTRFS_I(inode)->root;
2275 u64 len;
2276 u64 logical;
2277 u64 map_length;
2278 int ret = 0;
2279
2280 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2281
2282 if (!em || IS_ERR(em))
2283 goto out;
2284
2285 if (em->start > start || em->start + em->len <= start)
2286 goto out;
2287
2288 if (em->block_start == EXTENT_MAP_INLINE) {
2289 ret = -EINVAL;
2290 goto out;
2291 }
2292
2293 if (em->block_start == EXTENT_MAP_HOLE ||
2294 em->block_start == EXTENT_MAP_DELALLOC) {
2295 goto out;
2296 }
2297
2298 len = em->start + em->len - start;
2299 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2300
2301 logical = start - em->start;
2302 logical = em->block_start + logical;
2303
2304 map_length = len;
2305 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2306 logical, &map_length, &multi, 0);
2307 BUG_ON(ret);
2308 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2309 bh_result->b_size = min(map_length, len);
2310 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2311 set_buffer_mapped(bh_result);
2312 kfree(multi);
2313out:
2314 free_extent_map(em);
2315 return ret;
2316}
2317
2318static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2319 const struct iovec *iov, loff_t offset,
2320 unsigned long nr_segs)
2321{
2322 struct file *file = iocb->ki_filp;
2323 struct inode *inode = file->f_mapping->host;
2324
2325 if (rw == WRITE)
2326 return -EINVAL;
2327
2328 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2329 offset, nr_segs, btrfs_get_block, NULL);
2330}
2331
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002332static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002333{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002334 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002335}
2336
Chris Mason9ebefb182007-06-15 13:50:00 -04002337int btrfs_readpage(struct file *file, struct page *page)
2338{
Chris Masond1310b22008-01-24 16:13:08 -05002339 struct extent_io_tree *tree;
2340 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002341 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002342}
Chris Mason1832a6d2007-12-21 16:27:21 -05002343
Chris Mason39279cc2007-06-12 06:35:45 -04002344static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2345{
Chris Masond1310b22008-01-24 16:13:08 -05002346 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002347
2348
2349 if (current->flags & PF_MEMALLOC) {
2350 redirty_page_for_writepage(wbc, page);
2351 unlock_page(page);
2352 return 0;
2353 }
Chris Masond1310b22008-01-24 16:13:08 -05002354 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002355 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2356}
Chris Mason39279cc2007-06-12 06:35:45 -04002357
Chris Masonb293f022007-11-01 19:45:34 -04002358static int btrfs_writepages(struct address_space *mapping,
2359 struct writeback_control *wbc)
2360{
Chris Masond1310b22008-01-24 16:13:08 -05002361 struct extent_io_tree *tree;
2362 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002363 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2364}
2365
Chris Mason3ab2fb52007-11-08 10:59:22 -05002366static int
2367btrfs_readpages(struct file *file, struct address_space *mapping,
2368 struct list_head *pages, unsigned nr_pages)
2369{
Chris Masond1310b22008-01-24 16:13:08 -05002370 struct extent_io_tree *tree;
2371 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002372 return extent_readpages(tree, mapping, pages, nr_pages,
2373 btrfs_get_extent);
2374}
2375
Chris Mason70dec802008-01-29 09:59:12 -05002376static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002377{
Chris Masond1310b22008-01-24 16:13:08 -05002378 struct extent_io_tree *tree;
2379 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002380 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002381
Chris Masond1310b22008-01-24 16:13:08 -05002382 tree = &BTRFS_I(page->mapping->host)->io_tree;
2383 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002384 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002385 if (ret == 1) {
2386 ClearPagePrivate(page);
2387 set_page_private(page, 0);
2388 page_cache_release(page);
2389 }
2390 return ret;
2391}
Chris Mason39279cc2007-06-12 06:35:45 -04002392
Chris Masona52d9a82007-08-27 16:49:44 -04002393static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2394{
Chris Masond1310b22008-01-24 16:13:08 -05002395 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002396
Chris Masond1310b22008-01-24 16:13:08 -05002397 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002398 extent_invalidatepage(tree, page, offset);
2399 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002400}
2401
Chris Mason9ebefb182007-06-15 13:50:00 -04002402/*
2403 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2404 * called from a page fault handler when a page is first dirtied. Hence we must
2405 * be careful to check for EOF conditions here. We set the page up correctly
2406 * for a written page which means we get ENOSPC checking when writing into
2407 * holes and correct delalloc and unwritten extent mapping on filesystems that
2408 * support these features.
2409 *
2410 * We are not allowed to take the i_mutex here so we have to play games to
2411 * protect against truncate races as the page could now be beyond EOF. Because
2412 * vmtruncate() writes the inode size before removing pages, once we have the
2413 * page lock we can determine safely if the page is beyond EOF. If it is not
2414 * beyond EOF, then the page is guaranteed safe against truncation until we
2415 * unlock the page.
2416 */
2417int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2418{
Chris Mason6da6aba2007-12-18 16:15:09 -05002419 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002420 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002421 unsigned long end;
2422 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002423 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002424 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002425
Chris Mason1832a6d2007-12-21 16:27:21 -05002426 mutex_lock(&root->fs_info->fs_mutex);
2427 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002428 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002429 if (ret)
2430 goto out;
2431
2432 ret = -EINVAL;
2433
Chris Mason9ebefb182007-06-15 13:50:00 -04002434 lock_page(page);
2435 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002436 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002437 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002438
Chris Mason9ebefb182007-06-15 13:50:00 -04002439 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002440 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002441 /* page got truncated out from underneath us */
2442 goto out_unlock;
2443 }
2444
2445 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002446 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002447 end = size & ~PAGE_CACHE_MASK;
2448 else
2449 end = PAGE_CACHE_SIZE;
2450
Chris Masonb888db22007-08-27 16:49:44 -04002451 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002452
2453out_unlock:
2454 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002455out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002456 return ret;
2457}
2458
Chris Mason39279cc2007-06-12 06:35:45 -04002459static void btrfs_truncate(struct inode *inode)
2460{
2461 struct btrfs_root *root = BTRFS_I(inode)->root;
2462 int ret;
2463 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002464 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002465
2466 if (!S_ISREG(inode->i_mode))
2467 return;
2468 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2469 return;
2470
2471 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2472
2473 mutex_lock(&root->fs_info->fs_mutex);
2474 trans = btrfs_start_transaction(root, 1);
2475 btrfs_set_trans_block_group(trans, inode);
2476
2477 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002478 ret = btrfs_truncate_in_trans(trans, root, inode,
2479 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002480 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002481 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002482
Chris Mason39279cc2007-06-12 06:35:45 -04002483 ret = btrfs_end_transaction(trans, root);
2484 BUG_ON(ret);
2485 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002486 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002487 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002488}
2489
Chris Mason4313b392008-01-03 09:08:48 -05002490static int noinline create_subvol(struct btrfs_root *root, char *name,
2491 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002492{
2493 struct btrfs_trans_handle *trans;
2494 struct btrfs_key key;
2495 struct btrfs_root_item root_item;
2496 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002497 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002498 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002499 struct inode *inode;
2500 struct inode *dir;
2501 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002502 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002503 u64 objectid;
2504 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002505 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002506
2507 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002508 ret = btrfs_check_free_space(root, 1, 0);
2509 if (ret)
2510 goto fail_commit;
2511
Chris Mason39279cc2007-06-12 06:35:45 -04002512 trans = btrfs_start_transaction(root, 1);
2513 BUG_ON(!trans);
2514
Chris Mason7bb86312007-12-11 09:25:06 -05002515 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2516 0, &objectid);
2517 if (ret)
2518 goto fail;
2519
2520 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2521 objectid, trans->transid, 0, 0,
2522 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002523 if (IS_ERR(leaf))
2524 return PTR_ERR(leaf);
2525
2526 btrfs_set_header_nritems(leaf, 0);
2527 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002528 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002529 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002530 btrfs_set_header_owner(leaf, objectid);
2531
Chris Mason5f39d392007-10-15 16:14:19 -04002532 write_extent_buffer(leaf, root->fs_info->fsid,
2533 (unsigned long)btrfs_header_fsid(leaf),
2534 BTRFS_FSID_SIZE);
2535 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002536
2537 inode_item = &root_item.inode;
2538 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002539 inode_item->generation = cpu_to_le64(1);
2540 inode_item->size = cpu_to_le64(3);
2541 inode_item->nlink = cpu_to_le32(1);
2542 inode_item->nblocks = cpu_to_le64(1);
2543 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002544
Chris Masondb945352007-10-15 16:15:53 -04002545 btrfs_set_root_bytenr(&root_item, leaf->start);
2546 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002547 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002548 btrfs_set_root_used(&root_item, 0);
2549
Chris Mason5eda7b52007-06-22 14:16:25 -04002550 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2551 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002552
2553 free_extent_buffer(leaf);
2554 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002555
Chris Mason39279cc2007-06-12 06:35:45 -04002556 btrfs_set_root_dirid(&root_item, new_dirid);
2557
2558 key.objectid = objectid;
2559 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002560 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2561 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2562 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002563 if (ret)
2564 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002565
2566 /*
2567 * insert the directory item
2568 */
2569 key.offset = (u64)-1;
2570 dir = root->fs_info->sb->s_root->d_inode;
2571 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2572 name, namelen, dir->i_ino, &key,
2573 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002574 if (ret)
2575 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002576
Chris Mason39544012007-12-12 14:38:19 -05002577 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2578 name, namelen, objectid,
2579 root->fs_info->sb->s_root->d_inode->i_ino);
2580 if (ret)
2581 goto fail;
2582
Chris Mason39279cc2007-06-12 06:35:45 -04002583 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002584 if (ret)
2585 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002586
Josef Bacik58176a92007-08-29 15:47:34 -04002587 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002588 BUG_ON(!new_root);
2589
2590 trans = btrfs_start_transaction(new_root, 1);
2591 BUG_ON(!trans);
2592
Chris Mason9c583092008-01-29 15:15:18 -05002593 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2594 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002595 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002596 if (IS_ERR(inode))
2597 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002598 inode->i_op = &btrfs_dir_inode_operations;
2599 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002600 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002601
Chris Mason39544012007-12-12 14:38:19 -05002602 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2603 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002604 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002605 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002606 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002607 if (ret)
2608 goto fail;
2609fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002610 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002611 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002612 if (err && !ret)
2613 ret = err;
2614fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002615 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002616 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002617 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002618 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002619}
2620
2621static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2622{
Chris Mason3063d292008-01-08 15:46:30 -05002623 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002624 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002625 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002626 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002627 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002628
2629 if (!root->ref_cows)
2630 return -EINVAL;
2631
2632 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002633 ret = btrfs_check_free_space(root, 1, 0);
2634 if (ret)
2635 goto fail_unlock;
2636
Chris Mason3063d292008-01-08 15:46:30 -05002637 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2638 if (!pending_snapshot) {
2639 ret = -ENOMEM;
2640 goto fail_unlock;
2641 }
Yanfb4bc1e2008-01-17 11:59:51 -05002642 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002643 if (!pending_snapshot->name) {
2644 ret = -ENOMEM;
2645 kfree(pending_snapshot);
2646 goto fail_unlock;
2647 }
Yanfb4bc1e2008-01-17 11:59:51 -05002648 memcpy(pending_snapshot->name, name, namelen);
2649 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002650 trans = btrfs_start_transaction(root, 1);
2651 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002652 pending_snapshot->root = root;
2653 list_add(&pending_snapshot->list,
2654 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002655 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002656 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002657
Chris Mason1832a6d2007-12-21 16:27:21 -05002658fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002659 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002660 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002661 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002662 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002663}
2664
Chris Masonedbd8d42007-12-21 16:27:24 -05002665unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002666 struct file_ra_state *ra, struct file *file,
2667 pgoff_t offset, pgoff_t last_index)
2668{
2669 pgoff_t req_size;
2670
2671#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2672 req_size = last_index - offset + 1;
2673 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2674 return offset;
2675#else
2676 req_size = min(last_index - offset + 1, (pgoff_t)128);
2677 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2678 return offset + req_size;
2679#endif
2680}
2681
2682int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002683 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002684 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002685 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002686 struct page *page;
2687 unsigned long last_index;
2688 unsigned long ra_index = 0;
2689 u64 page_start;
2690 u64 page_end;
2691 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002692 int ret;
2693
2694 mutex_lock(&root->fs_info->fs_mutex);
2695 ret = btrfs_check_free_space(root, inode->i_size, 0);
2696 mutex_unlock(&root->fs_info->fs_mutex);
2697 if (ret)
2698 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002699
2700 mutex_lock(&inode->i_mutex);
2701 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2702 for (i = 0; i <= last_index; i++) {
2703 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002704 ra_index = btrfs_force_ra(inode->i_mapping,
2705 &file->f_ra,
2706 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002707 }
2708 page = grab_cache_page(inode->i_mapping, i);
2709 if (!page)
2710 goto out_unlock;
2711 if (!PageUptodate(page)) {
2712 btrfs_readpage(NULL, page);
2713 lock_page(page);
2714 if (!PageUptodate(page)) {
2715 unlock_page(page);
2716 page_cache_release(page);
2717 goto out_unlock;
2718 }
2719 }
Chris Mason35ebb932007-10-30 16:56:53 -04002720 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002721 page_end = page_start + PAGE_CACHE_SIZE - 1;
2722
Chris Masond1310b22008-01-24 16:13:08 -05002723 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002724 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002725 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002726
Chris Masond1310b22008-01-24 16:13:08 -05002727 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002728 set_page_dirty(page);
2729 unlock_page(page);
2730 page_cache_release(page);
2731 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2732 }
2733
2734out_unlock:
2735 mutex_unlock(&inode->i_mutex);
2736 return 0;
2737}
2738
Chris Masonedbd8d42007-12-21 16:27:24 -05002739static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2740{
2741 u64 new_size;
2742 u64 old_size;
2743 struct btrfs_ioctl_vol_args *vol_args;
2744 struct btrfs_trans_handle *trans;
2745 char *sizestr;
2746 int ret = 0;
2747 int namelen;
2748 int mod = 0;
2749
2750 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2751
2752 if (!vol_args)
2753 return -ENOMEM;
2754
2755 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2756 ret = -EFAULT;
2757 goto out;
2758 }
2759 namelen = strlen(vol_args->name);
2760 if (namelen > BTRFS_VOL_NAME_MAX) {
2761 ret = -EINVAL;
2762 goto out;
2763 }
2764
2765 sizestr = vol_args->name;
2766 if (!strcmp(sizestr, "max"))
2767 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2768 else {
2769 if (sizestr[0] == '-') {
2770 mod = -1;
2771 sizestr++;
2772 } else if (sizestr[0] == '+') {
2773 mod = 1;
2774 sizestr++;
2775 }
2776 new_size = btrfs_parse_size(sizestr);
2777 if (new_size == 0) {
2778 ret = -EINVAL;
2779 goto out;
2780 }
2781 }
2782
2783 mutex_lock(&root->fs_info->fs_mutex);
2784 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2785
2786 if (mod < 0) {
2787 if (new_size > old_size) {
2788 ret = -EINVAL;
2789 goto out_unlock;
2790 }
2791 new_size = old_size - new_size;
2792 } else if (mod > 0) {
2793 new_size = old_size + new_size;
2794 }
2795
2796 if (new_size < 256 * 1024 * 1024) {
2797 ret = -EINVAL;
2798 goto out_unlock;
2799 }
2800 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2801 ret = -EFBIG;
2802 goto out_unlock;
2803 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002804
2805 do_div(new_size, root->sectorsize);
2806 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002807
2808printk("new size is %Lu\n", new_size);
2809 if (new_size > old_size) {
2810 trans = btrfs_start_transaction(root, 1);
2811 ret = btrfs_grow_extent_tree(trans, root, new_size);
2812 btrfs_commit_transaction(trans, root);
2813 } else {
2814 ret = btrfs_shrink_extent_tree(root, new_size);
2815 }
2816
2817out_unlock:
2818 mutex_unlock(&root->fs_info->fs_mutex);
2819out:
2820 kfree(vol_args);
2821 return ret;
2822}
2823
Chris Mason4313b392008-01-03 09:08:48 -05002824static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2825 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002826{
Chris Mason4aec2b52007-12-18 16:25:45 -05002827 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002828 struct btrfs_dir_item *di;
2829 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002830 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002831 int namelen;
2832 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002833
Chris Mason4aec2b52007-12-18 16:25:45 -05002834 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002835
Chris Mason4aec2b52007-12-18 16:25:45 -05002836 if (!vol_args)
2837 return -ENOMEM;
2838
2839 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2840 ret = -EFAULT;
2841 goto out;
2842 }
2843
2844 namelen = strlen(vol_args->name);
2845 if (namelen > BTRFS_VOL_NAME_MAX) {
2846 ret = -EINVAL;
2847 goto out;
2848 }
2849 if (strchr(vol_args->name, '/')) {
2850 ret = -EINVAL;
2851 goto out;
2852 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002853
2854 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002855 if (!path) {
2856 ret = -ENOMEM;
2857 goto out;
2858 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002859
2860 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2861 mutex_lock(&root->fs_info->fs_mutex);
2862 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2863 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002864 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002865 mutex_unlock(&root->fs_info->fs_mutex);
2866 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002867
2868 if (di && !IS_ERR(di)) {
2869 ret = -EEXIST;
2870 goto out;
2871 }
2872
2873 if (IS_ERR(di)) {
2874 ret = PTR_ERR(di);
2875 goto out;
2876 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002877
2878 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002879 ret = create_subvol(root, vol_args->name, namelen);
2880 else
2881 ret = create_snapshot(root, vol_args->name, namelen);
2882out:
2883 kfree(vol_args);
2884 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002885}
2886
2887static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002888{
Chris Mason6da6aba2007-12-18 16:15:09 -05002889 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002890 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002891
2892 switch (inode->i_mode & S_IFMT) {
2893 case S_IFDIR:
2894 mutex_lock(&root->fs_info->fs_mutex);
2895 btrfs_defrag_root(root, 0);
2896 btrfs_defrag_root(root->fs_info->extent_root, 0);
2897 mutex_unlock(&root->fs_info->fs_mutex);
2898 break;
2899 case S_IFREG:
2900 btrfs_defrag_file(file);
2901 break;
2902 }
2903
2904 return 0;
2905}
2906
2907long btrfs_ioctl(struct file *file, unsigned int
2908 cmd, unsigned long arg)
2909{
Chris Mason6da6aba2007-12-18 16:15:09 -05002910 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002911
2912 switch (cmd) {
2913 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002914 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002915 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002916 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002917 case BTRFS_IOC_RESIZE:
2918 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002919 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002920
2921 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002922}
2923
Chris Mason39279cc2007-06-12 06:35:45 -04002924/*
2925 * Called inside transaction, so use GFP_NOFS
2926 */
2927struct inode *btrfs_alloc_inode(struct super_block *sb)
2928{
2929 struct btrfs_inode *ei;
2930
2931 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2932 if (!ei)
2933 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002934 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002935 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002936 return &ei->vfs_inode;
2937}
2938
2939void btrfs_destroy_inode(struct inode *inode)
2940{
2941 WARN_ON(!list_empty(&inode->i_dentry));
2942 WARN_ON(inode->i_data.nrpages);
2943
Chris Mason8c416c92008-01-14 15:10:26 -05002944 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002945 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2946}
2947
Chris Mason44ec0b72007-10-29 10:55:05 -04002948#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2949static void init_once(struct kmem_cache * cachep, void *foo)
2950#else
Chris Mason39279cc2007-06-12 06:35:45 -04002951static void init_once(void * foo, struct kmem_cache * cachep,
2952 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002953#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002954{
2955 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2956
2957 inode_init_once(&ei->vfs_inode);
2958}
2959
2960void btrfs_destroy_cachep(void)
2961{
2962 if (btrfs_inode_cachep)
2963 kmem_cache_destroy(btrfs_inode_cachep);
2964 if (btrfs_trans_handle_cachep)
2965 kmem_cache_destroy(btrfs_trans_handle_cachep);
2966 if (btrfs_transaction_cachep)
2967 kmem_cache_destroy(btrfs_transaction_cachep);
2968 if (btrfs_bit_radix_cachep)
2969 kmem_cache_destroy(btrfs_bit_radix_cachep);
2970 if (btrfs_path_cachep)
2971 kmem_cache_destroy(btrfs_path_cachep);
2972}
2973
Chris Mason86479a02007-09-10 19:58:16 -04002974struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002975 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002976#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2977 void (*ctor)(struct kmem_cache *, void *)
2978#else
Chris Mason92fee662007-07-25 12:31:35 -04002979 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002980 unsigned long)
2981#endif
2982 )
Chris Mason92fee662007-07-25 12:31:35 -04002983{
2984 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2985 SLAB_MEM_SPREAD | extra_flags), ctor
2986#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2987 ,NULL
2988#endif
2989 );
2990}
2991
Chris Mason39279cc2007-06-12 06:35:45 -04002992int btrfs_init_cachep(void)
2993{
Chris Mason86479a02007-09-10 19:58:16 -04002994 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002995 sizeof(struct btrfs_inode),
2996 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002997 if (!btrfs_inode_cachep)
2998 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002999 btrfs_trans_handle_cachep =
3000 btrfs_cache_create("btrfs_trans_handle_cache",
3001 sizeof(struct btrfs_trans_handle),
3002 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003003 if (!btrfs_trans_handle_cachep)
3004 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003005 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003006 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003007 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003008 if (!btrfs_transaction_cachep)
3009 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003010 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003011 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003012 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003013 if (!btrfs_path_cachep)
3014 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003015 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003016 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003017 if (!btrfs_bit_radix_cachep)
3018 goto fail;
3019 return 0;
3020fail:
3021 btrfs_destroy_cachep();
3022 return -ENOMEM;
3023}
3024
3025static int btrfs_getattr(struct vfsmount *mnt,
3026 struct dentry *dentry, struct kstat *stat)
3027{
3028 struct inode *inode = dentry->d_inode;
3029 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003030 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003031 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003032 return 0;
3033}
3034
3035static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3036 struct inode * new_dir,struct dentry *new_dentry)
3037{
3038 struct btrfs_trans_handle *trans;
3039 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3040 struct inode *new_inode = new_dentry->d_inode;
3041 struct inode *old_inode = old_dentry->d_inode;
3042 struct timespec ctime = CURRENT_TIME;
3043 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003044 int ret;
3045
3046 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3047 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3048 return -ENOTEMPTY;
3049 }
Chris Mason5f39d392007-10-15 16:14:19 -04003050
Chris Mason39279cc2007-06-12 06:35:45 -04003051 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003052 ret = btrfs_check_free_space(root, 1, 0);
3053 if (ret)
3054 goto out_unlock;
3055
Chris Mason39279cc2007-06-12 06:35:45 -04003056 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003057
Chris Mason39279cc2007-06-12 06:35:45 -04003058 btrfs_set_trans_block_group(trans, new_dir);
3059 path = btrfs_alloc_path();
3060 if (!path) {
3061 ret = -ENOMEM;
3062 goto out_fail;
3063 }
3064
3065 old_dentry->d_inode->i_nlink++;
3066 old_dir->i_ctime = old_dir->i_mtime = ctime;
3067 new_dir->i_ctime = new_dir->i_mtime = ctime;
3068 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003069
Chris Mason39279cc2007-06-12 06:35:45 -04003070 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3071 if (ret)
3072 goto out_fail;
3073
3074 if (new_inode) {
3075 new_inode->i_ctime = CURRENT_TIME;
3076 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3077 if (ret)
3078 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003079 }
Chris Mason9c583092008-01-29 15:15:18 -05003080 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003081 if (ret)
3082 goto out_fail;
3083
3084out_fail:
3085 btrfs_free_path(path);
3086 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003087out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003088 mutex_unlock(&root->fs_info->fs_mutex);
3089 return ret;
3090}
3091
3092static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3093 const char *symname)
3094{
3095 struct btrfs_trans_handle *trans;
3096 struct btrfs_root *root = BTRFS_I(dir)->root;
3097 struct btrfs_path *path;
3098 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003099 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003100 int err;
3101 int drop_inode = 0;
3102 u64 objectid;
3103 int name_len;
3104 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003105 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003106 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003107 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003108 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003109
3110 name_len = strlen(symname) + 1;
3111 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3112 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003113
Chris Mason39279cc2007-06-12 06:35:45 -04003114 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003115 err = btrfs_check_free_space(root, 1, 0);
3116 if (err)
3117 goto out_fail;
3118
Chris Mason39279cc2007-06-12 06:35:45 -04003119 trans = btrfs_start_transaction(root, 1);
3120 btrfs_set_trans_block_group(trans, dir);
3121
3122 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3123 if (err) {
3124 err = -ENOSPC;
3125 goto out_unlock;
3126 }
3127
Chris Mason9c583092008-01-29 15:15:18 -05003128 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3129 dentry->d_name.len,
3130 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003131 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3132 err = PTR_ERR(inode);
3133 if (IS_ERR(inode))
3134 goto out_unlock;
3135
3136 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003137 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003138 if (err)
3139 drop_inode = 1;
3140 else {
3141 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003142 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003143 inode->i_fop = &btrfs_file_operations;
3144 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003145 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3146 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003147 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003148 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3149 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05003150 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003151 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003152 }
3153 dir->i_sb->s_dirt = 1;
3154 btrfs_update_inode_block_group(trans, inode);
3155 btrfs_update_inode_block_group(trans, dir);
3156 if (drop_inode)
3157 goto out_unlock;
3158
3159 path = btrfs_alloc_path();
3160 BUG_ON(!path);
3161 key.objectid = inode->i_ino;
3162 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003163 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3164 datasize = btrfs_file_extent_calc_inline_size(name_len);
3165 err = btrfs_insert_empty_item(trans, root, path, &key,
3166 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003167 if (err) {
3168 drop_inode = 1;
3169 goto out_unlock;
3170 }
Chris Mason5f39d392007-10-15 16:14:19 -04003171 leaf = path->nodes[0];
3172 ei = btrfs_item_ptr(leaf, path->slots[0],
3173 struct btrfs_file_extent_item);
3174 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3175 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003176 BTRFS_FILE_EXTENT_INLINE);
3177 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003178 write_extent_buffer(leaf, symname, ptr, name_len);
3179 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003180 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003181
Chris Mason39279cc2007-06-12 06:35:45 -04003182 inode->i_op = &btrfs_symlink_inode_operations;
3183 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003184 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003185 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04003186 err = btrfs_update_inode(trans, root, inode);
3187 if (err)
3188 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003189
3190out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003191 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04003192 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003193out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003194 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04003195 if (drop_inode) {
3196 inode_dec_link_count(inode);
3197 iput(inode);
3198 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003199 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05003200 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003201 return err;
3202}
Chris Mason16432982008-04-10 10:23:21 -04003203
Yanfdebe2b2008-01-14 13:26:08 -05003204static int btrfs_permission(struct inode *inode, int mask,
3205 struct nameidata *nd)
3206{
3207 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3208 return -EACCES;
3209 return generic_permission(inode, mask, NULL);
3210}
Chris Mason39279cc2007-06-12 06:35:45 -04003211
3212static struct inode_operations btrfs_dir_inode_operations = {
3213 .lookup = btrfs_lookup,
3214 .create = btrfs_create,
3215 .unlink = btrfs_unlink,
3216 .link = btrfs_link,
3217 .mkdir = btrfs_mkdir,
3218 .rmdir = btrfs_rmdir,
3219 .rename = btrfs_rename,
3220 .symlink = btrfs_symlink,
3221 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003222 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003223 .setxattr = generic_setxattr,
3224 .getxattr = generic_getxattr,
3225 .listxattr = btrfs_listxattr,
3226 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003227 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003228};
Chris Mason39279cc2007-06-12 06:35:45 -04003229static struct inode_operations btrfs_dir_ro_inode_operations = {
3230 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003231 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003232};
Chris Mason39279cc2007-06-12 06:35:45 -04003233static struct file_operations btrfs_dir_file_operations = {
3234 .llseek = generic_file_llseek,
3235 .read = generic_read_dir,
3236 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003237 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003238#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003239 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003240#endif
3241};
3242
Chris Masond1310b22008-01-24 16:13:08 -05003243static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003244 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003245 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003246 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003247 .readpage_io_hook = btrfs_readpage_io_hook,
3248 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Mason7e383262008-04-09 16:28:12 -04003249 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003250 .set_bit_hook = btrfs_set_bit_hook,
3251 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003252};
3253
Chris Mason39279cc2007-06-12 06:35:45 -04003254static struct address_space_operations btrfs_aops = {
3255 .readpage = btrfs_readpage,
3256 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003257 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003258 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003259 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003260 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003261 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003262 .invalidatepage = btrfs_invalidatepage,
3263 .releasepage = btrfs_releasepage,
3264 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003265};
3266
3267static struct address_space_operations btrfs_symlink_aops = {
3268 .readpage = btrfs_readpage,
3269 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003270 .invalidatepage = btrfs_invalidatepage,
3271 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003272};
3273
3274static struct inode_operations btrfs_file_inode_operations = {
3275 .truncate = btrfs_truncate,
3276 .getattr = btrfs_getattr,
3277 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003278 .setxattr = generic_setxattr,
3279 .getxattr = generic_getxattr,
3280 .listxattr = btrfs_listxattr,
3281 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003282 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003283};
Josef Bacik618e21d2007-07-11 10:18:17 -04003284static struct inode_operations btrfs_special_inode_operations = {
3285 .getattr = btrfs_getattr,
3286 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003287 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003288};
Chris Mason39279cc2007-06-12 06:35:45 -04003289static struct inode_operations btrfs_symlink_inode_operations = {
3290 .readlink = generic_readlink,
3291 .follow_link = page_follow_link_light,
3292 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003293 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003294};