blob: 40b4a8ec17fef7a56a1fa7b33f5373332125e8c4 [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 Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040037#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050038#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040039#include "ctree.h"
40#include "disk-io.h"
41#include "transaction.h"
42#include "btrfs_inode.h"
43#include "ioctl.h"
44#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040045#include "volumes.h"
Chris Mason39279cc2007-06-12 06:35:45 -040046
47struct btrfs_iget_args {
48 u64 ino;
49 struct btrfs_root *root;
50};
51
52static struct inode_operations btrfs_dir_inode_operations;
53static struct inode_operations btrfs_symlink_inode_operations;
54static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040055static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040056static struct inode_operations btrfs_file_inode_operations;
57static struct address_space_operations btrfs_aops;
58static struct address_space_operations btrfs_symlink_aops;
59static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050060static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040061
62static struct kmem_cache *btrfs_inode_cachep;
63struct kmem_cache *btrfs_trans_handle_cachep;
64struct kmem_cache *btrfs_transaction_cachep;
65struct kmem_cache *btrfs_bit_radix_cachep;
66struct kmem_cache *btrfs_path_cachep;
67
68#define S_SHIFT 12
69static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
70 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
71 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
72 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
73 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
74 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
75 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
76 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
77};
78
Chris Mason1832a6d2007-12-21 16:27:21 -050079int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
80 int for_del)
81{
82 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
83 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
84 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040085 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050086 int ret = 0;
87
88 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050089 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050090 else
Chris Masonf9ef6602008-01-03 09:22:38 -050091 thresh = total * 85;
92
93 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050094
Chris Masonbcbfce82008-04-22 13:26:47 -040095 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -050096 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
97 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -040098 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -050099 return ret;
100}
101
Chris Masonbe20aa92007-12-17 20:14:01 -0500102static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db2b2007-08-27 16:49:44 -0400103{
104 struct btrfs_root *root = BTRFS_I(inode)->root;
105 struct btrfs_trans_handle *trans;
Chris Masonb888db2b2007-08-27 16:49:44 -0400106 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400107 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500108 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400109 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500110 u64 orig_start = start;
111 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500112 struct btrfs_key ins;
113 int ret;
Chris Masonb888db2b2007-08-27 16:49:44 -0400114
Chris Masonb888db2b2007-08-27 16:49:44 -0400115 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db2b2007-08-27 16:49:44 -0400116 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500117 btrfs_set_trans_block_group(trans, inode);
118
Chris Masondb945352007-10-15 16:15:53 -0400119 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500120 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db2b2007-08-27 16:49:44 -0400121 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400122 start, start + num_bytes, start, &alloc_hint);
Chris Masond1310b22008-01-24 16:13:08 -0500123 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400124
Chris Mason179e29e2007-11-01 11:28:41 -0400125 if (alloc_hint == EXTENT_MAP_INLINE)
126 goto out;
127
Chris Mason3b951512008-04-17 11:29:12 -0400128 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
129
Chris Masonc59f8952007-12-17 20:14:04 -0500130 while(num_bytes > 0) {
131 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
132 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
Chris Mason98d20f62008-04-14 09:46:10 -0400133 root->sectorsize,
Chris Masonc59f8952007-12-17 20:14:04 -0500134 root->root_key.objectid,
135 trans->transid,
136 inode->i_ino, start, 0,
137 alloc_hint, (u64)-1, &ins, 1);
138 if (ret) {
139 WARN_ON(1);
140 goto out;
141 }
Chris Mason98d20f62008-04-14 09:46:10 -0400142 cur_alloc_size = ins.offset;
Chris Masonc59f8952007-12-17 20:14:04 -0500143 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
144 start, ins.objectid, ins.offset,
Sage Weilf2eb0a22008-05-02 14:43:14 -0400145 ins.offset, 0);
Chris Mason90692182008-02-08 13:49:28 -0500146 inode->i_blocks += ins.offset >> 9;
Chris Mason5f564062008-01-22 16:47:59 -0500147 btrfs_check_file(root, inode);
Chris Mason3b951512008-04-17 11:29:12 -0400148 if (num_bytes < cur_alloc_size) {
149 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
150 cur_alloc_size);
151 break;
152 }
Chris Masonc59f8952007-12-17 20:14:04 -0500153 num_bytes -= cur_alloc_size;
154 alloc_hint = ins.objectid + ins.offset;
155 start += cur_alloc_size;
Chris Masonb888db2b2007-08-27 16:49:44 -0400156 }
Chris Masond1310b22008-01-24 16:13:08 -0500157 btrfs_drop_extent_cache(inode, orig_start,
158 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500159 btrfs_add_ordered_inode(inode);
Chris Mason90692182008-02-08 13:49:28 -0500160 btrfs_update_inode(trans, root, inode);
Chris Masonb888db2b2007-08-27 16:49:44 -0400161out:
162 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500163 return ret;
164}
165
166static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
167{
168 u64 extent_start;
169 u64 extent_end;
170 u64 bytenr;
171 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500172 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500173 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500174 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400175 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500176 struct extent_buffer *leaf;
177 int found_type;
178 struct btrfs_path *path;
179 struct btrfs_file_extent_item *item;
180 int ret;
181 int err;
182 struct btrfs_key found_key;
183
Chris Masonc31f8832008-01-08 15:46:31 -0500184 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500185 path = btrfs_alloc_path();
186 BUG_ON(!path);
187again:
188 ret = btrfs_lookup_file_extent(NULL, root, path,
189 inode->i_ino, start, 0);
190 if (ret < 0) {
191 btrfs_free_path(path);
192 return ret;
193 }
194
195 cow_end = end;
196 if (ret != 0) {
197 if (path->slots[0] == 0)
198 goto not_found;
199 path->slots[0]--;
200 }
201
202 leaf = path->nodes[0];
203 item = btrfs_item_ptr(leaf, path->slots[0],
204 struct btrfs_file_extent_item);
205
206 /* are we inside the extent that was found? */
207 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
208 found_type = btrfs_key_type(&found_key);
209 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400210 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500211 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500212
213 found_type = btrfs_file_extent_type(leaf, item);
214 extent_start = found_key.offset;
215 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500216 u64 extent_num_bytes;
217
218 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
219 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500220 err = 0;
221
Chris Mason1832a6d2007-12-21 16:27:21 -0500222 if (loops && start != extent_start)
223 goto not_found;
224
Chris Masonbe20aa92007-12-17 20:14:01 -0500225 if (start < extent_start || start >= extent_end)
226 goto not_found;
227
228 cow_end = min(end, extent_end - 1);
229 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
230 if (bytenr == 0)
231 goto not_found;
232
Chris Masona68d5932008-05-08 14:11:56 -0400233 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
234 bytenr) != 1) {
235 goto not_found;
236 }
237
Chris Masonc31f8832008-01-08 15:46:31 -0500238 /*
239 * we may be called by the resizer, make sure we're inside
240 * the limits of the FS
241 */
Chris Masona68d5932008-05-08 14:11:56 -0400242 block_group = btrfs_lookup_block_group(root->fs_info,
243 bytenr);
244 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500245 goto not_found;
246
Chris Masonbe20aa92007-12-17 20:14:01 -0500247 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500248 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500249 goto not_found;
250 }
251loop:
252 if (start > end) {
253 btrfs_free_path(path);
254 return 0;
255 }
256 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500257 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500258 goto again;
259
260not_found:
Chris Masonbbaf5492008-05-08 16:31:21 -0400261 cow_file_range(inode, start, end);
262 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500263 goto loop;
264}
265
266static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
267{
268 struct btrfs_root *root = BTRFS_I(inode)->root;
269 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500270 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500271 if (btrfs_test_opt(root, NODATACOW) ||
272 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500273 ret = run_delalloc_nocow(inode, start, end);
274 else
275 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500276
Chris Masonb888db2b2007-08-27 16:49:44 -0400277 mutex_unlock(&root->fs_info->fs_mutex);
278 return ret;
279}
280
Chris Mason291d6732008-01-29 15:55:23 -0500281int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500282 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500283{
Chris Masonbcbfce82008-04-22 13:26:47 -0400284 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500285 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500286 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400287 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500288 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500289 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400290 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500291 }
292 return 0;
293}
294
295int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500296 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500297{
Chris Masonb0c68f82008-01-31 11:05:37 -0500298 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500299 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400300 unsigned long flags;
301
302 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500303 if (end - start + 1 > root->fs_info->delalloc_bytes) {
304 printk("warning: delalloc account %Lu %Lu\n",
305 end - start + 1, root->fs_info->delalloc_bytes);
306 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500307 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500308 } else {
309 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500310 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500311 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400312 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500313 }
314 return 0;
315}
316
Chris Mason239b14b2008-03-24 15:02:07 -0400317int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
318 size_t size, struct bio *bio)
319{
320 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
321 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400322 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400323 u64 length = 0;
324 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400325 int ret;
326
Chris Masonf2d8d742008-04-21 10:03:05 -0400327 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400328 map_tree = &root->fs_info->mapping_tree;
329 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400330 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400331 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400332
Chris Mason239b14b2008-03-24 15:02:07 -0400333 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400334 return 1;
335 }
336 return 0;
337}
338
Chris Mason44b8bd72008-04-16 11:14:51 -0400339int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400340 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500341{
Chris Mason065631f2008-02-20 12:07:25 -0500342 struct btrfs_root *root = BTRFS_I(inode)->root;
343 struct btrfs_trans_handle *trans;
344 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400345 char *sums = NULL;
346
347 ret = btrfs_csum_one_bio(root, bio, &sums);
348 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500349
Chris Mason44b8bd72008-04-16 11:14:51 -0400350 mutex_lock(&root->fs_info->fs_mutex);
351 trans = btrfs_start_transaction(root, 1);
Chris Masone0156402008-04-16 11:15:20 -0400352
Chris Mason44b8bd72008-04-16 11:14:51 -0400353 btrfs_set_trans_block_group(trans, inode);
Chris Masone0156402008-04-16 11:15:20 -0400354 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
355
Chris Mason44b8bd72008-04-16 11:14:51 -0400356 ret = btrfs_end_transaction(trans, root);
357 BUG_ON(ret);
358 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0156402008-04-16 11:15:20 -0400359
360 kfree(sums);
361
Chris Mason44b8bd72008-04-16 11:14:51 -0400362 return btrfs_map_bio(root, rw, bio, mirror_num);
363}
364
365int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
366 int mirror_num)
367{
368 struct btrfs_root *root = BTRFS_I(inode)->root;
369 int ret = 0;
370
Chris Mason22c59942008-04-09 16:28:12 -0400371 if (!(rw & (1 << BIO_RW))) {
372 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
373 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400374 goto mapit;
375 }
Chris Mason065631f2008-02-20 12:07:25 -0500376
377 if (btrfs_test_opt(root, NODATASUM) ||
Chris Mason0b86a832008-03-24 15:01:56 -0400378 btrfs_test_flag(inode, NODATASUM)) {
379 goto mapit;
380 }
Chris Mason065631f2008-02-20 12:07:25 -0500381
Chris Mason44b8bd72008-04-16 11:14:51 -0400382 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
383 inode, rw, bio, mirror_num,
384 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400385mapit:
Chris Masonf1885912008-04-09 16:28:12 -0400386 return btrfs_map_bio(root, rw, bio, mirror_num);
Chris Mason065631f2008-02-20 12:07:25 -0500387}
Chris Mason6885f302008-02-20 16:11:05 -0500388
Chris Mason07157aa2007-08-30 08:50:51 -0400389int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
390{
391 int ret = 0;
392 struct inode *inode = page->mapping->host;
393 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500394 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400395 struct btrfs_csum_item *item;
396 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400397 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400398
Yanb98b6762008-01-08 15:54:37 -0500399 if (btrfs_test_opt(root, NODATASUM) ||
400 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500401 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400402
Chris Mason07157aa2007-08-30 08:50:51 -0400403 mutex_lock(&root->fs_info->fs_mutex);
404 path = btrfs_alloc_path();
405 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
406 if (IS_ERR(item)) {
407 ret = PTR_ERR(item);
408 /* a csum that isn't present is a preallocated region. */
409 if (ret == -ENOENT || ret == -EFBIG)
410 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400411 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500412 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400413 goto out;
414 }
Chris Masonff79f812007-10-15 16:22:25 -0400415 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
416 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500417 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400418out:
419 if (path)
420 btrfs_free_path(path);
421 mutex_unlock(&root->fs_info->fs_mutex);
422 return ret;
423}
424
Chris Mason7e383262008-04-09 16:28:12 -0400425struct io_failure_record {
426 struct page *page;
427 u64 start;
428 u64 len;
429 u64 logical;
430 int last_mirror;
431};
432
Chris Mason1259ab72008-05-12 13:39:03 -0400433int btrfs_io_failed_hook(struct bio *failed_bio,
434 struct page *page, u64 start, u64 end,
435 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400436{
437 struct io_failure_record *failrec = NULL;
438 u64 private;
439 struct extent_map *em;
440 struct inode *inode = page->mapping->host;
441 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400442 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400443 struct bio *bio;
444 int num_copies;
445 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400446 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400447 u64 logical;
448
449 ret = get_state_private(failure_tree, start, &private);
450 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400451 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
452 if (!failrec)
453 return -ENOMEM;
454 failrec->start = start;
455 failrec->len = end - start + 1;
456 failrec->last_mirror = 0;
457
Chris Mason3b951512008-04-17 11:29:12 -0400458 spin_lock(&em_tree->lock);
459 em = lookup_extent_mapping(em_tree, start, failrec->len);
460 if (em->start > start || em->start + em->len < start) {
461 free_extent_map(em);
462 em = NULL;
463 }
464 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400465
466 if (!em || IS_ERR(em)) {
467 kfree(failrec);
468 return -EIO;
469 }
470 logical = start - em->start;
471 logical = em->block_start + logical;
472 failrec->logical = logical;
473 free_extent_map(em);
474 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
475 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400476 set_state_private(failure_tree, start,
477 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400478 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400479 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400480 }
481 num_copies = btrfs_num_copies(
482 &BTRFS_I(inode)->root->fs_info->mapping_tree,
483 failrec->logical, failrec->len);
484 failrec->last_mirror++;
485 if (!state) {
486 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
487 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
488 failrec->start,
489 EXTENT_LOCKED);
490 if (state && state->start != failrec->start)
491 state = NULL;
492 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
493 }
494 if (!state || failrec->last_mirror > num_copies) {
495 set_state_private(failure_tree, failrec->start, 0);
496 clear_extent_bits(failure_tree, failrec->start,
497 failrec->start + failrec->len - 1,
498 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
499 kfree(failrec);
500 return -EIO;
501 }
502 bio = bio_alloc(GFP_NOFS, 1);
503 bio->bi_private = state;
504 bio->bi_end_io = failed_bio->bi_end_io;
505 bio->bi_sector = failrec->logical >> 9;
506 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400507 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400508 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400509 if (failed_bio->bi_rw & (1 << BIO_RW))
510 rw = WRITE;
511 else
512 rw = READ;
513
514 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
515 failrec->last_mirror);
516 return 0;
517}
518
519int btrfs_clean_io_failures(struct inode *inode, u64 start)
520{
521 u64 private;
522 u64 private_failure;
523 struct io_failure_record *failure;
524 int ret;
525
526 private = 0;
527 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
528 (u64)-1, 1, EXTENT_DIRTY)) {
529 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
530 start, &private_failure);
531 if (ret == 0) {
532 failure = (struct io_failure_record *)(unsigned long)
533 private_failure;
534 set_state_private(&BTRFS_I(inode)->io_failure_tree,
535 failure->start, 0);
536 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
537 failure->start,
538 failure->start + failure->len - 1,
539 EXTENT_DIRTY | EXTENT_LOCKED,
540 GFP_NOFS);
541 kfree(failure);
542 }
543 }
Chris Mason7e383262008-04-09 16:28:12 -0400544 return 0;
545}
546
Chris Mason70dec802008-01-29 09:59:12 -0500547int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
548 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400549{
Chris Mason35ebb932007-10-30 16:56:53 -0400550 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400551 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500552 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400553 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500554 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400555 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400556 struct btrfs_root *root = BTRFS_I(inode)->root;
557 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400558 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500559
Yanb98b6762008-01-08 15:54:37 -0500560 if (btrfs_test_opt(root, NODATASUM) ||
561 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500562 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500563 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500564 private = state->private;
565 ret = 0;
566 } else {
567 ret = get_state_private(io_tree, start, &private);
568 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400569 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400570 kaddr = kmap_atomic(page, KM_IRQ0);
571 if (ret) {
572 goto zeroit;
573 }
Chris Masonff79f812007-10-15 16:22:25 -0400574 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
575 btrfs_csum_final(csum, (char *)&csum);
576 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400577 goto zeroit;
578 }
579 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400580 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400581
582 /* if the io failure tree for this inode is non-empty,
583 * check to see if we've recovered from a failed IO
584 */
Chris Mason1259ab72008-05-12 13:39:03 -0400585 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400586 return 0;
587
588zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500589 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
590 page->mapping->host->i_ino, (unsigned long long)start, csum,
591 private);
Chris Masondb945352007-10-15 16:15:53 -0400592 memset(kaddr + offset, 1, end - start + 1);
593 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400594 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400595 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400596 if (private == 0)
597 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400598 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400599}
Chris Masonb888db2b2007-08-27 16:49:44 -0400600
Chris Mason39279cc2007-06-12 06:35:45 -0400601void btrfs_read_locked_inode(struct inode *inode)
602{
603 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400604 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400605 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400606 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400607 struct btrfs_root *root = BTRFS_I(inode)->root;
608 struct btrfs_key location;
609 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400610 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400611 int ret;
612
613 path = btrfs_alloc_path();
614 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400615 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400616 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500617
Chris Mason39279cc2007-06-12 06:35:45 -0400618 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400619 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400620 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400621
Chris Mason5f39d392007-10-15 16:14:19 -0400622 leaf = path->nodes[0];
623 inode_item = btrfs_item_ptr(leaf, path->slots[0],
624 struct btrfs_inode_item);
625
626 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
627 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
628 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
629 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
630 inode->i_size = btrfs_inode_size(leaf, inode_item);
631
632 tspec = btrfs_inode_atime(inode_item);
633 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
634 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
635
636 tspec = btrfs_inode_mtime(inode_item);
637 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
638 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
639
640 tspec = btrfs_inode_ctime(inode_item);
641 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
642 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
643
644 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
645 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400646 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400647 rdev = btrfs_inode_rdev(leaf, inode_item);
648
649 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400650 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
651 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500652 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500653 if (!BTRFS_I(inode)->block_group) {
654 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400655 NULL, 0,
656 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500657 }
Chris Mason39279cc2007-06-12 06:35:45 -0400658 btrfs_free_path(path);
659 inode_item = NULL;
660
661 mutex_unlock(&root->fs_info->fs_mutex);
662
663 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400664 case S_IFREG:
665 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400666 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500667 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400668 inode->i_fop = &btrfs_file_operations;
669 inode->i_op = &btrfs_file_inode_operations;
670 break;
671 case S_IFDIR:
672 inode->i_fop = &btrfs_dir_file_operations;
673 if (root == root->fs_info->tree_root)
674 inode->i_op = &btrfs_dir_ro_inode_operations;
675 else
676 inode->i_op = &btrfs_dir_inode_operations;
677 break;
678 case S_IFLNK:
679 inode->i_op = &btrfs_symlink_inode_operations;
680 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400681 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400682 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400683 default:
684 init_special_inode(inode, inode->i_mode, rdev);
685 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400686 }
687 return;
688
689make_bad:
690 btrfs_release_path(root, path);
691 btrfs_free_path(path);
692 mutex_unlock(&root->fs_info->fs_mutex);
693 make_bad_inode(inode);
694}
695
Chris Mason5f39d392007-10-15 16:14:19 -0400696static void fill_inode_item(struct extent_buffer *leaf,
697 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400698 struct inode *inode)
699{
Chris Mason5f39d392007-10-15 16:14:19 -0400700 btrfs_set_inode_uid(leaf, item, inode->i_uid);
701 btrfs_set_inode_gid(leaf, item, inode->i_gid);
702 btrfs_set_inode_size(leaf, item, inode->i_size);
703 btrfs_set_inode_mode(leaf, item, inode->i_mode);
704 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
705
706 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
707 inode->i_atime.tv_sec);
708 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
709 inode->i_atime.tv_nsec);
710
711 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
712 inode->i_mtime.tv_sec);
713 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
714 inode->i_mtime.tv_nsec);
715
716 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
717 inode->i_ctime.tv_sec);
718 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
719 inode->i_ctime.tv_nsec);
720
721 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
722 btrfs_set_inode_generation(leaf, item, inode->i_generation);
723 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500724 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400725 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400726 BTRFS_I(inode)->block_group->key.objectid);
727}
728
Chris Masona52d9a82007-08-27 16:49:44 -0400729int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400730 struct btrfs_root *root,
731 struct inode *inode)
732{
733 struct btrfs_inode_item *inode_item;
734 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400735 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400736 int ret;
737
738 path = btrfs_alloc_path();
739 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400740 ret = btrfs_lookup_inode(trans, root, path,
741 &BTRFS_I(inode)->location, 1);
742 if (ret) {
743 if (ret > 0)
744 ret = -ENOENT;
745 goto failed;
746 }
747
Chris Mason5f39d392007-10-15 16:14:19 -0400748 leaf = path->nodes[0];
749 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400750 struct btrfs_inode_item);
751
Chris Mason5f39d392007-10-15 16:14:19 -0400752 fill_inode_item(leaf, inode_item, inode);
753 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400754 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400755 ret = 0;
756failed:
757 btrfs_release_path(root, path);
758 btrfs_free_path(path);
759 return ret;
760}
761
762
763static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
764 struct btrfs_root *root,
765 struct inode *dir,
766 struct dentry *dentry)
767{
768 struct btrfs_path *path;
769 const char *name = dentry->d_name.name;
770 int name_len = dentry->d_name.len;
771 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400772 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400773 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400774 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400775
776 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400777 if (!path) {
778 ret = -ENOMEM;
779 goto err;
780 }
781
Chris Mason39279cc2007-06-12 06:35:45 -0400782 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
783 name, name_len, -1);
784 if (IS_ERR(di)) {
785 ret = PTR_ERR(di);
786 goto err;
787 }
788 if (!di) {
789 ret = -ENOENT;
790 goto err;
791 }
Chris Mason5f39d392007-10-15 16:14:19 -0400792 leaf = path->nodes[0];
793 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400794 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400795 if (ret)
796 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400797 btrfs_release_path(root, path);
798
799 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400800 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400801 if (IS_ERR(di)) {
802 ret = PTR_ERR(di);
803 goto err;
804 }
805 if (!di) {
806 ret = -ENOENT;
807 goto err;
808 }
809 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400810
811 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500812 ret = btrfs_del_inode_ref(trans, root, name, name_len,
813 dentry->d_inode->i_ino,
814 dentry->d_parent->d_inode->i_ino);
815 if (ret) {
816 printk("failed to delete reference to %.*s, "
817 "inode %lu parent %lu\n", name_len, name,
818 dentry->d_inode->i_ino,
819 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500820 }
Chris Mason39279cc2007-06-12 06:35:45 -0400821err:
822 btrfs_free_path(path);
823 if (!ret) {
824 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400825 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400826 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500827#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
828 dentry->d_inode->i_nlink--;
829#else
Chris Mason39279cc2007-06-12 06:35:45 -0400830 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500831#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400832 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400833 dir->i_sb->s_dirt = 1;
834 }
835 return ret;
836}
837
838static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
839{
840 struct btrfs_root *root;
841 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500842 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400843 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500844 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400845
846 root = BTRFS_I(dir)->root;
847 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500848
849 ret = btrfs_check_free_space(root, 1, 1);
850 if (ret)
851 goto fail;
852
Chris Mason39279cc2007-06-12 06:35:45 -0400853 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400854
Chris Mason39279cc2007-06-12 06:35:45 -0400855 btrfs_set_trans_block_group(trans, dir);
856 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400857 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400858
Chris Mason2da98f02008-01-16 11:44:43 -0500859 if (inode->i_nlink == 0) {
860 int found;
861 /* if the inode isn't linked anywhere,
862 * we don't need to worry about
863 * data=ordered
864 */
865 found = btrfs_del_ordered_inode(inode);
866 if (found == 1) {
867 atomic_dec(&inode->i_count);
868 }
869 }
870
Chris Mason39279cc2007-06-12 06:35:45 -0400871 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500872fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400873 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400874 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500875 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400876 return ret;
877}
878
879static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
880{
881 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500882 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400883 int ret;
884 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400885 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500886 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400887
Yan134d4512007-10-25 15:49:25 -0400888 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
889 return -ENOTEMPTY;
890
Chris Mason39279cc2007-06-12 06:35:45 -0400891 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500892 ret = btrfs_check_free_space(root, 1, 1);
893 if (ret)
894 goto fail;
895
Chris Mason39279cc2007-06-12 06:35:45 -0400896 trans = btrfs_start_transaction(root, 1);
897 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400898
899 /* now the directory is empty */
900 err = btrfs_unlink_trans(trans, root, dir, dentry);
901 if (!err) {
902 inode->i_size = 0;
903 }
Chris Mason39544012007-12-12 14:38:19 -0500904
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400905 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400906 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500907fail:
Yan134d4512007-10-25 15:49:25 -0400908 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400909 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500910 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500911
Chris Mason39279cc2007-06-12 06:35:45 -0400912 if (ret && !err)
913 err = ret;
914 return err;
915}
916
Chris Mason39279cc2007-06-12 06:35:45 -0400917/*
Chris Mason39279cc2007-06-12 06:35:45 -0400918 * this can truncate away extent items, csum items and directory items.
919 * It starts at a high offset and removes keys until it can't find
920 * any higher than i_size.
921 *
922 * csum items that cross the new i_size are truncated to the new size
923 * as well.
924 */
925static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
926 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500927 struct inode *inode,
928 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400929{
930 int ret;
931 struct btrfs_path *path;
932 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400933 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400934 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400935 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400936 struct btrfs_file_extent_item *fi;
937 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400938 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400939 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500940 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500941 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400942 int found_extent;
943 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500944 int pending_del_nr = 0;
945 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400946 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -0400947 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400948
Chris Mason3b951512008-04-17 11:29:12 -0400949 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400950 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400951 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400952 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400953
Chris Mason39279cc2007-06-12 06:35:45 -0400954 /* FIXME, add redo link to tree so we don't leak on crash */
955 key.objectid = inode->i_ino;
956 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400957 key.type = (u8)-1;
958
Chris Mason85e21ba2008-01-29 15:11:36 -0500959 btrfs_init_path(path);
960search_again:
961 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
962 if (ret < 0) {
963 goto error;
964 }
965 if (ret > 0) {
966 BUG_ON(path->slots[0] == 0);
967 path->slots[0]--;
968 }
969
Chris Mason39279cc2007-06-12 06:35:45 -0400970 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400971 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400972 leaf = path->nodes[0];
973 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
974 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400975
Chris Mason5f39d392007-10-15 16:14:19 -0400976 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400977 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400978
Chris Mason85e21ba2008-01-29 15:11:36 -0500979 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400980 break;
981
Chris Mason5f39d392007-10-15 16:14:19 -0400982 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400983 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400984 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400985 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400986 extent_type = btrfs_file_extent_type(leaf, fi);
987 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400988 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400989 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400990 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
991 struct btrfs_item *item = btrfs_item_nr(leaf,
992 path->slots[0]);
993 item_end += btrfs_file_extent_inline_len(leaf,
994 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400995 }
Yan008630c2007-11-07 13:31:09 -0500996 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400997 }
998 if (found_type == BTRFS_CSUM_ITEM_KEY) {
999 ret = btrfs_csum_truncate(trans, root, path,
1000 inode->i_size);
1001 BUG_ON(ret);
1002 }
Yan008630c2007-11-07 13:31:09 -05001003 if (item_end < inode->i_size) {
Chris Masonb888db2b2007-08-27 16:49:44 -04001004 if (found_type == BTRFS_DIR_ITEM_KEY) {
1005 found_type = BTRFS_INODE_ITEM_KEY;
1006 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1007 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001008 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1009 found_type = BTRFS_XATTR_ITEM_KEY;
1010 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1011 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db2b2007-08-27 16:49:44 -04001012 } else if (found_type) {
1013 found_type--;
1014 } else {
1015 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001016 }
Yana61721d2007-09-17 11:08:38 -04001017 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001018 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001019 }
Chris Mason5f39d392007-10-15 16:14:19 -04001020 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001021 del_item = 1;
1022 else
1023 del_item = 0;
1024 found_extent = 0;
1025
1026 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001027 if (found_type != BTRFS_EXTENT_DATA_KEY)
1028 goto delete;
1029
1030 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001031 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001032 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001033 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001034 u64 orig_num_bytes =
1035 btrfs_file_extent_num_bytes(leaf, fi);
1036 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001037 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001038 extent_num_bytes = extent_num_bytes &
1039 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001040 btrfs_set_file_extent_num_bytes(leaf, fi,
1041 extent_num_bytes);
1042 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001043 extent_num_bytes);
1044 if (extent_start != 0)
1045 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001046 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001047 } else {
Chris Masondb945352007-10-15 16:15:53 -04001048 extent_num_bytes =
1049 btrfs_file_extent_disk_num_bytes(leaf,
1050 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001051 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001052 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001053 if (extent_start != 0) {
1054 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001055 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001056 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001057 root_gen = btrfs_header_generation(leaf);
1058 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001059 }
Chris Mason90692182008-02-08 13:49:28 -05001060 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1061 if (!del_item) {
1062 u32 newsize = inode->i_size - found_key.offset;
1063 dec_i_blocks(inode, item_end + 1 -
1064 found_key.offset - newsize);
1065 newsize =
1066 btrfs_file_extent_calc_inline_size(newsize);
1067 ret = btrfs_truncate_item(trans, root, path,
1068 newsize, 1);
1069 BUG_ON(ret);
1070 } else {
1071 dec_i_blocks(inode, item_end + 1 -
1072 found_key.offset);
1073 }
Chris Mason39279cc2007-06-12 06:35:45 -04001074 }
Chris Mason179e29e2007-11-01 11:28:41 -04001075delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001076 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001077 if (!pending_del_nr) {
1078 /* no pending yet, add ourselves */
1079 pending_del_slot = path->slots[0];
1080 pending_del_nr = 1;
1081 } else if (pending_del_nr &&
1082 path->slots[0] + 1 == pending_del_slot) {
1083 /* hop on the pending chunk */
1084 pending_del_nr++;
1085 pending_del_slot = path->slots[0];
1086 } else {
1087 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1088 }
Chris Mason39279cc2007-06-12 06:35:45 -04001089 } else {
1090 break;
1091 }
Chris Mason39279cc2007-06-12 06:35:45 -04001092 if (found_extent) {
1093 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001094 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001095 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001096 root_gen, inode->i_ino,
1097 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001098 BUG_ON(ret);
1099 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001100next:
1101 if (path->slots[0] == 0) {
1102 if (pending_del_nr)
1103 goto del_pending;
1104 btrfs_release_path(root, path);
1105 goto search_again;
1106 }
1107
1108 path->slots[0]--;
1109 if (pending_del_nr &&
1110 path->slots[0] + 1 != pending_del_slot) {
1111 struct btrfs_key debug;
1112del_pending:
1113 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1114 pending_del_slot);
1115 ret = btrfs_del_items(trans, root, path,
1116 pending_del_slot,
1117 pending_del_nr);
1118 BUG_ON(ret);
1119 pending_del_nr = 0;
1120 btrfs_release_path(root, path);
1121 goto search_again;
1122 }
Chris Mason39279cc2007-06-12 06:35:45 -04001123 }
1124 ret = 0;
1125error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001126 if (pending_del_nr) {
1127 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1128 pending_del_nr);
1129 }
Chris Mason39279cc2007-06-12 06:35:45 -04001130 btrfs_release_path(root, path);
1131 btrfs_free_path(path);
1132 inode->i_sb->s_dirt = 1;
1133 return ret;
1134}
1135
Chris Masonb888db2b2007-08-27 16:49:44 -04001136static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -04001137 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001138{
Chris Mason39279cc2007-06-12 06:35:45 -04001139 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -05001140 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001141 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db2b2007-08-27 16:49:44 -04001142 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -05001143 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001144
Chris Mason190662b2007-12-18 16:25:45 -05001145 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001146 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001147
Chris Masond1310b22008-01-24 16:13:08 -05001148 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001149 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db2b2007-08-27 16:49:44 -04001150 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -05001151
Chris Masona52d9a82007-08-27 16:49:44 -04001152 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db2b2007-08-27 16:49:44 -04001153 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001154 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1155 flush_dcache_page(page);
Chris Masonb888db2b2007-08-27 16:49:44 -04001156 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001157 }
Chris Masonb888db2b2007-08-27 16:49:44 -04001158 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -05001159 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001160
Chris Masona52d9a82007-08-27 16:49:44 -04001161 return ret;
1162}
1163
1164/*
1165 * taken from block_truncate_page, but does cow as it zeros out
1166 * any bytes left in the last page in the file.
1167 */
1168static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1169{
1170 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001171 struct btrfs_root *root = BTRFS_I(inode)->root;
1172 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001173 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1174 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1175 struct page *page;
1176 int ret = 0;
1177 u64 page_start;
1178
1179 if ((offset & (blocksize - 1)) == 0)
1180 goto out;
1181
1182 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001183again:
Chris Masona52d9a82007-08-27 16:49:44 -04001184 page = grab_cache_page(mapping, index);
1185 if (!page)
1186 goto out;
1187 if (!PageUptodate(page)) {
1188 ret = btrfs_readpage(NULL, page);
1189 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001190 if (page->mapping != mapping) {
1191 unlock_page(page);
1192 page_cache_release(page);
1193 goto again;
1194 }
Chris Masona52d9a82007-08-27 16:49:44 -04001195 if (!PageUptodate(page)) {
1196 ret = -EIO;
1197 goto out;
1198 }
1199 }
Chris Masona52d9a82007-08-27 16:49:44 -04001200
Chris Mason211c17f2008-05-15 09:13:45 -04001201 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1202 wait_on_page_writeback(page);
Chris Masonb888db2b2007-08-27 16:49:44 -04001203 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001204
Chris Mason39279cc2007-06-12 06:35:45 -04001205 unlock_page(page);
1206 page_cache_release(page);
1207out:
1208 return ret;
1209}
1210
1211static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1212{
1213 struct inode *inode = dentry->d_inode;
1214 int err;
1215
1216 err = inode_change_ok(inode, attr);
1217 if (err)
1218 return err;
1219
1220 if (S_ISREG(inode->i_mode) &&
1221 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1222 struct btrfs_trans_handle *trans;
1223 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001224 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001225
Chris Mason5f39d392007-10-15 16:14:19 -04001226 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001227 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001228 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001229 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001230 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001231
Chris Mason1b0f7c22008-01-30 14:33:02 -05001232 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001233 goto out;
1234
Chris Mason1832a6d2007-12-21 16:27:21 -05001235 mutex_lock(&root->fs_info->fs_mutex);
1236 err = btrfs_check_free_space(root, 1, 0);
1237 mutex_unlock(&root->fs_info->fs_mutex);
1238 if (err)
1239 goto fail;
1240
Chris Mason39279cc2007-06-12 06:35:45 -04001241 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1242
Chris Mason1b0f7c22008-01-30 14:33:02 -05001243 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001244 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001245
1246 mutex_lock(&root->fs_info->fs_mutex);
1247 trans = btrfs_start_transaction(root, 1);
1248 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001249 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001250 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001251 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001252
Chris Mason179e29e2007-11-01 11:28:41 -04001253 if (alloc_hint != EXTENT_MAP_INLINE) {
1254 err = btrfs_insert_file_extent(trans, root,
1255 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001256 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001257 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001258 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001259 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001260 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001261 }
Chris Mason39279cc2007-06-12 06:35:45 -04001262 btrfs_end_transaction(trans, root);
1263 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001264 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001265 if (err)
1266 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001267 }
1268out:
1269 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001270fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001271 return err;
1272}
Chris Mason61295eb2008-01-14 16:24:38 -05001273
Chris Mason2da98f02008-01-16 11:44:43 -05001274void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001275{
Chris Mason2da98f02008-01-16 11:44:43 -05001276 int ret;
1277
1278 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001279 return;
1280 }
Chris Mason2da98f02008-01-16 11:44:43 -05001281
1282 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1283 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1284 return;
1285
1286 ret = btrfs_del_ordered_inode(inode);
1287 if (ret == 1) {
1288 atomic_dec(&inode->i_count);
1289 }
Chris Mason61295eb2008-01-14 16:24:38 -05001290}
1291
Chris Mason39279cc2007-06-12 06:35:45 -04001292void btrfs_delete_inode(struct inode *inode)
1293{
1294 struct btrfs_trans_handle *trans;
1295 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001296 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001297 int ret;
1298
1299 truncate_inode_pages(&inode->i_data, 0);
1300 if (is_bad_inode(inode)) {
1301 goto no_delete;
1302 }
Chris Mason5f39d392007-10-15 16:14:19 -04001303
Chris Mason39279cc2007-06-12 06:35:45 -04001304 inode->i_size = 0;
1305 mutex_lock(&root->fs_info->fs_mutex);
1306 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001307
Chris Mason39279cc2007-06-12 06:35:45 -04001308 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001309 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001310 if (ret)
1311 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001312
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001313 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001314 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001315
Chris Mason39279cc2007-06-12 06:35:45 -04001316 btrfs_end_transaction(trans, root);
1317 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001318 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001319 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001320 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001321
1322no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001323 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001324 btrfs_end_transaction(trans, root);
1325 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001326 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001327 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001328no_delete:
1329 clear_inode(inode);
1330}
1331
1332/*
1333 * this returns the key found in the dir entry in the location pointer.
1334 * If no dir entries were found, location->objectid is 0.
1335 */
1336static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1337 struct btrfs_key *location)
1338{
1339 const char *name = dentry->d_name.name;
1340 int namelen = dentry->d_name.len;
1341 struct btrfs_dir_item *di;
1342 struct btrfs_path *path;
1343 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001344 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001345
Chris Mason39544012007-12-12 14:38:19 -05001346 if (namelen == 1 && strcmp(name, ".") == 0) {
1347 location->objectid = dir->i_ino;
1348 location->type = BTRFS_INODE_ITEM_KEY;
1349 location->offset = 0;
1350 return 0;
1351 }
Chris Mason39279cc2007-06-12 06:35:45 -04001352 path = btrfs_alloc_path();
1353 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001354
Chris Mason7a720532007-12-13 09:06:59 -05001355 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001356 struct btrfs_key key;
1357 struct extent_buffer *leaf;
1358 u32 nritems;
1359 int slot;
1360
1361 key.objectid = dir->i_ino;
1362 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1363 key.offset = 0;
1364 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1365 BUG_ON(ret == 0);
1366 ret = 0;
1367
1368 leaf = path->nodes[0];
1369 slot = path->slots[0];
1370 nritems = btrfs_header_nritems(leaf);
1371 if (slot >= nritems)
1372 goto out_err;
1373
1374 btrfs_item_key_to_cpu(leaf, &key, slot);
1375 if (key.objectid != dir->i_ino ||
1376 key.type != BTRFS_INODE_REF_KEY) {
1377 goto out_err;
1378 }
1379 location->objectid = key.offset;
1380 location->type = BTRFS_INODE_ITEM_KEY;
1381 location->offset = 0;
1382 goto out;
1383 }
1384
Chris Mason39279cc2007-06-12 06:35:45 -04001385 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1386 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001387 if (IS_ERR(di))
1388 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001389 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001390 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001391 }
Chris Mason5f39d392007-10-15 16:14:19 -04001392 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001393out:
Chris Mason39279cc2007-06-12 06:35:45 -04001394 btrfs_free_path(path);
1395 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001396out_err:
1397 location->objectid = 0;
1398 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001399}
1400
1401/*
1402 * when we hit a tree root in a directory, the btrfs part of the inode
1403 * needs to be changed to reflect the root directory of the tree root. This
1404 * is kind of like crossing a mount point.
1405 */
1406static int fixup_tree_root_location(struct btrfs_root *root,
1407 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001408 struct btrfs_root **sub_root,
1409 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001410{
1411 struct btrfs_path *path;
1412 struct btrfs_root_item *ri;
1413
1414 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1415 return 0;
1416 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1417 return 0;
1418
1419 path = btrfs_alloc_path();
1420 BUG_ON(!path);
1421 mutex_lock(&root->fs_info->fs_mutex);
1422
Josef Bacik58176a92007-08-29 15:47:34 -04001423 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1424 dentry->d_name.name,
1425 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001426 if (IS_ERR(*sub_root))
1427 return PTR_ERR(*sub_root);
1428
1429 ri = &(*sub_root)->root_item;
1430 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001431 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1432 location->offset = 0;
1433
1434 btrfs_free_path(path);
1435 mutex_unlock(&root->fs_info->fs_mutex);
1436 return 0;
1437}
1438
1439static int btrfs_init_locked_inode(struct inode *inode, void *p)
1440{
1441 struct btrfs_iget_args *args = p;
1442 inode->i_ino = args->ino;
1443 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001444 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001445 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1446 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db2b2007-08-27 16:49:44 -04001447 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001448 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1449 inode->i_mapping, GFP_NOFS);
Chris Mason81d7ed22008-04-25 08:51:48 -04001450 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001451 return 0;
1452}
1453
1454static int btrfs_find_actor(struct inode *inode, void *opaque)
1455{
1456 struct btrfs_iget_args *args = opaque;
1457 return (args->ino == inode->i_ino &&
1458 args->root == BTRFS_I(inode)->root);
1459}
1460
Chris Masondc17ff82008-01-08 15:46:30 -05001461struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1462 u64 root_objectid)
1463{
1464 struct btrfs_iget_args args;
1465 args.ino = objectid;
1466 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1467
1468 if (!args.root)
1469 return NULL;
1470
1471 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1472}
1473
Chris Mason39279cc2007-06-12 06:35:45 -04001474struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1475 struct btrfs_root *root)
1476{
1477 struct inode *inode;
1478 struct btrfs_iget_args args;
1479 args.ino = objectid;
1480 args.root = root;
1481
1482 inode = iget5_locked(s, objectid, btrfs_find_actor,
1483 btrfs_init_locked_inode,
1484 (void *)&args);
1485 return inode;
1486}
1487
1488static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1489 struct nameidata *nd)
1490{
1491 struct inode * inode;
1492 struct btrfs_inode *bi = BTRFS_I(dir);
1493 struct btrfs_root *root = bi->root;
1494 struct btrfs_root *sub_root = root;
1495 struct btrfs_key location;
1496 int ret;
1497
1498 if (dentry->d_name.len > BTRFS_NAME_LEN)
1499 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001500
Chris Mason39279cc2007-06-12 06:35:45 -04001501 mutex_lock(&root->fs_info->fs_mutex);
1502 ret = btrfs_inode_by_name(dir, dentry, &location);
1503 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001504
Chris Mason39279cc2007-06-12 06:35:45 -04001505 if (ret < 0)
1506 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001507
Chris Mason39279cc2007-06-12 06:35:45 -04001508 inode = NULL;
1509 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001510 ret = fixup_tree_root_location(root, &location, &sub_root,
1511 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001512 if (ret < 0)
1513 return ERR_PTR(ret);
1514 if (ret > 0)
1515 return ERR_PTR(-ENOENT);
1516 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1517 sub_root);
1518 if (!inode)
1519 return ERR_PTR(-EACCES);
1520 if (inode->i_state & I_NEW) {
1521 /* the inode and parent dir are two different roots */
1522 if (sub_root != root) {
1523 igrab(inode);
1524 sub_root->inode = inode;
1525 }
1526 BTRFS_I(inode)->root = sub_root;
1527 memcpy(&BTRFS_I(inode)->location, &location,
1528 sizeof(location));
1529 btrfs_read_locked_inode(inode);
1530 unlock_new_inode(inode);
1531 }
1532 }
1533 return d_splice_alias(inode, dentry);
1534}
1535
Chris Mason39279cc2007-06-12 06:35:45 -04001536static unsigned char btrfs_filetype_table[] = {
1537 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1538};
1539
1540static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1541{
Chris Mason6da6aba2007-12-18 16:15:09 -05001542 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001543 struct btrfs_root *root = BTRFS_I(inode)->root;
1544 struct btrfs_item *item;
1545 struct btrfs_dir_item *di;
1546 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001547 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001548 struct btrfs_path *path;
1549 int ret;
1550 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001551 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001552 int slot;
1553 int advance;
1554 unsigned char d_type;
1555 int over = 0;
1556 u32 di_cur;
1557 u32 di_total;
1558 u32 di_len;
1559 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001560 char tmp_name[32];
1561 char *name_ptr;
1562 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001563
1564 /* FIXME, use a real flag for deciding about the key type */
1565 if (root->fs_info->tree_root == root)
1566 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001567
Chris Mason39544012007-12-12 14:38:19 -05001568 /* special case for "." */
1569 if (filp->f_pos == 0) {
1570 over = filldir(dirent, ".", 1,
1571 1, inode->i_ino,
1572 DT_DIR);
1573 if (over)
1574 return 0;
1575 filp->f_pos = 1;
1576 }
1577
Chris Mason39279cc2007-06-12 06:35:45 -04001578 mutex_lock(&root->fs_info->fs_mutex);
1579 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001580 path = btrfs_alloc_path();
1581 path->reada = 2;
1582
1583 /* special case for .., just use the back ref */
1584 if (filp->f_pos == 1) {
1585 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1586 key.offset = 0;
1587 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1588 BUG_ON(ret == 0);
1589 leaf = path->nodes[0];
1590 slot = path->slots[0];
1591 nritems = btrfs_header_nritems(leaf);
1592 if (slot >= nritems) {
1593 btrfs_release_path(root, path);
1594 goto read_dir_items;
1595 }
1596 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1597 btrfs_release_path(root, path);
1598 if (found_key.objectid != key.objectid ||
1599 found_key.type != BTRFS_INODE_REF_KEY)
1600 goto read_dir_items;
1601 over = filldir(dirent, "..", 2,
1602 2, found_key.offset, DT_DIR);
1603 if (over)
1604 goto nopos;
1605 filp->f_pos = 2;
1606 }
1607
1608read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001609 btrfs_set_key_type(&key, key_type);
1610 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001611
Chris Mason39279cc2007-06-12 06:35:45 -04001612 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1613 if (ret < 0)
1614 goto err;
1615 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001616 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001617 leaf = path->nodes[0];
1618 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001619 slot = path->slots[0];
1620 if (advance || slot >= nritems) {
1621 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001622 ret = btrfs_next_leaf(root, path);
1623 if (ret)
1624 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001625 leaf = path->nodes[0];
1626 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001627 slot = path->slots[0];
1628 } else {
1629 slot++;
1630 path->slots[0]++;
1631 }
1632 }
1633 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001634 item = btrfs_item_nr(leaf, slot);
1635 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1636
1637 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001638 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001639 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001640 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001641 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001642 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001643
1644 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001645 advance = 1;
1646 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1647 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001648 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001649 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001650 struct btrfs_key location;
1651
1652 name_len = btrfs_dir_name_len(leaf, di);
1653 if (name_len < 32) {
1654 name_ptr = tmp_name;
1655 } else {
1656 name_ptr = kmalloc(name_len, GFP_NOFS);
1657 BUG_ON(!name_ptr);
1658 }
1659 read_extent_buffer(leaf, name_ptr,
1660 (unsigned long)(di + 1), name_len);
1661
1662 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1663 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001664 over = filldir(dirent, name_ptr, name_len,
1665 found_key.offset,
1666 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001667 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001668
1669 if (name_ptr != tmp_name)
1670 kfree(name_ptr);
1671
Chris Mason39279cc2007-06-12 06:35:45 -04001672 if (over)
1673 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001674 di_len = btrfs_dir_name_len(leaf, di) +
1675 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001676 di_cur += di_len;
1677 di = (struct btrfs_dir_item *)((char *)di + di_len);
1678 }
1679 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001680 if (key_type == BTRFS_DIR_INDEX_KEY)
1681 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1682 else
1683 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001684nopos:
1685 ret = 0;
1686err:
1687 btrfs_release_path(root, path);
1688 btrfs_free_path(path);
1689 mutex_unlock(&root->fs_info->fs_mutex);
1690 return ret;
1691}
1692
1693int btrfs_write_inode(struct inode *inode, int wait)
1694{
1695 struct btrfs_root *root = BTRFS_I(inode)->root;
1696 struct btrfs_trans_handle *trans;
1697 int ret = 0;
1698
1699 if (wait) {
1700 mutex_lock(&root->fs_info->fs_mutex);
1701 trans = btrfs_start_transaction(root, 1);
1702 btrfs_set_trans_block_group(trans, inode);
1703 ret = btrfs_commit_transaction(trans, root);
1704 mutex_unlock(&root->fs_info->fs_mutex);
1705 }
1706 return ret;
1707}
1708
1709/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001710 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001711 * inode changes. But, it is most likely to find the inode in cache.
1712 * FIXME, needs more benchmarking...there are no reasons other than performance
1713 * to keep or drop this code.
1714 */
1715void btrfs_dirty_inode(struct inode *inode)
1716{
1717 struct btrfs_root *root = BTRFS_I(inode)->root;
1718 struct btrfs_trans_handle *trans;
1719
1720 mutex_lock(&root->fs_info->fs_mutex);
1721 trans = btrfs_start_transaction(root, 1);
1722 btrfs_set_trans_block_group(trans, inode);
1723 btrfs_update_inode(trans, root, inode);
1724 btrfs_end_transaction(trans, root);
1725 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001726}
1727
1728static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1729 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001730 const char *name, int name_len,
1731 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001732 u64 objectid,
1733 struct btrfs_block_group_cache *group,
1734 int mode)
1735{
1736 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001737 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001738 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001739 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001740 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001741 struct btrfs_inode_ref *ref;
1742 struct btrfs_key key[2];
1743 u32 sizes[2];
1744 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001745 int ret;
1746 int owner;
1747
Chris Mason5f39d392007-10-15 16:14:19 -04001748 path = btrfs_alloc_path();
1749 BUG_ON(!path);
1750
Chris Mason39279cc2007-06-12 06:35:45 -04001751 inode = new_inode(root->fs_info->sb);
1752 if (!inode)
1753 return ERR_PTR(-ENOMEM);
1754
Chris Masond1310b22008-01-24 16:13:08 -05001755 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1756 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db2b2007-08-27 16:49:44 -04001757 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001758 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1759 inode->i_mapping, GFP_NOFS);
Chris Mason81d7ed22008-04-25 08:51:48 -04001760 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Mason90692182008-02-08 13:49:28 -05001761 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001762 BTRFS_I(inode)->root = root;
Chris Masonb888db2b2007-08-27 16:49:44 -04001763
Chris Mason39279cc2007-06-12 06:35:45 -04001764 if (mode & S_IFDIR)
1765 owner = 0;
1766 else
1767 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001768 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001769 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001770 if (!new_inode_group) {
1771 printk("find_block group failed\n");
1772 new_inode_group = group;
1773 }
1774 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001775 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001776
1777 key[0].objectid = objectid;
1778 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1779 key[0].offset = 0;
1780
1781 key[1].objectid = objectid;
1782 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1783 key[1].offset = ref_objectid;
1784
1785 sizes[0] = sizeof(struct btrfs_inode_item);
1786 sizes[1] = name_len + sizeof(*ref);
1787
1788 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1789 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001790 goto fail;
1791
Chris Mason9c583092008-01-29 15:15:18 -05001792 if (objectid > root->highest_inode)
1793 root->highest_inode = objectid;
1794
Chris Mason39279cc2007-06-12 06:35:45 -04001795 inode->i_uid = current->fsuid;
1796 inode->i_gid = current->fsgid;
1797 inode->i_mode = mode;
1798 inode->i_ino = objectid;
1799 inode->i_blocks = 0;
1800 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001801 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1802 struct btrfs_inode_item);
1803 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001804
1805 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1806 struct btrfs_inode_ref);
1807 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1808 ptr = (unsigned long)(ref + 1);
1809 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1810
Chris Mason5f39d392007-10-15 16:14:19 -04001811 btrfs_mark_buffer_dirty(path->nodes[0]);
1812 btrfs_free_path(path);
1813
Chris Mason39279cc2007-06-12 06:35:45 -04001814 location = &BTRFS_I(inode)->location;
1815 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001816 location->offset = 0;
1817 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1818
Chris Mason39279cc2007-06-12 06:35:45 -04001819 insert_inode_hash(inode);
1820 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001821fail:
1822 btrfs_free_path(path);
1823 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001824}
1825
1826static inline u8 btrfs_inode_type(struct inode *inode)
1827{
1828 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1829}
1830
1831static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001832 struct dentry *dentry, struct inode *inode,
1833 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001834{
1835 int ret;
1836 struct btrfs_key key;
1837 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001838 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001839
Chris Mason39279cc2007-06-12 06:35:45 -04001840 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001841 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1842 key.offset = 0;
1843
1844 ret = btrfs_insert_dir_item(trans, root,
1845 dentry->d_name.name, dentry->d_name.len,
1846 dentry->d_parent->d_inode->i_ino,
1847 &key, btrfs_inode_type(inode));
1848 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001849 if (add_backref) {
1850 ret = btrfs_insert_inode_ref(trans, root,
1851 dentry->d_name.name,
1852 dentry->d_name.len,
1853 inode->i_ino,
1854 dentry->d_parent->d_inode->i_ino);
1855 }
Chris Mason79c44582007-06-25 10:09:33 -04001856 parent_inode = dentry->d_parent->d_inode;
1857 parent_inode->i_size += dentry->d_name.len * 2;
1858 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001859 ret = btrfs_update_inode(trans, root,
1860 dentry->d_parent->d_inode);
1861 }
1862 return ret;
1863}
1864
1865static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001866 struct dentry *dentry, struct inode *inode,
1867 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001868{
Chris Mason9c583092008-01-29 15:15:18 -05001869 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001870 if (!err) {
1871 d_instantiate(dentry, inode);
1872 return 0;
1873 }
1874 if (err > 0)
1875 err = -EEXIST;
1876 return err;
1877}
1878
Josef Bacik618e21d2007-07-11 10:18:17 -04001879static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1880 int mode, dev_t rdev)
1881{
1882 struct btrfs_trans_handle *trans;
1883 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001884 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001885 int err;
1886 int drop_inode = 0;
1887 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001888 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001889
1890 if (!new_valid_dev(rdev))
1891 return -EINVAL;
1892
1893 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001894 err = btrfs_check_free_space(root, 1, 0);
1895 if (err)
1896 goto fail;
1897
Josef Bacik618e21d2007-07-11 10:18:17 -04001898 trans = btrfs_start_transaction(root, 1);
1899 btrfs_set_trans_block_group(trans, dir);
1900
1901 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1902 if (err) {
1903 err = -ENOSPC;
1904 goto out_unlock;
1905 }
1906
Chris Mason9c583092008-01-29 15:15:18 -05001907 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1908 dentry->d_name.len,
1909 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001910 BTRFS_I(dir)->block_group, mode);
1911 err = PTR_ERR(inode);
1912 if (IS_ERR(inode))
1913 goto out_unlock;
1914
1915 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001916 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001917 if (err)
1918 drop_inode = 1;
1919 else {
1920 inode->i_op = &btrfs_special_inode_operations;
1921 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001922 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001923 }
1924 dir->i_sb->s_dirt = 1;
1925 btrfs_update_inode_block_group(trans, inode);
1926 btrfs_update_inode_block_group(trans, dir);
1927out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001928 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001929 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001930fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001931 mutex_unlock(&root->fs_info->fs_mutex);
1932
1933 if (drop_inode) {
1934 inode_dec_link_count(inode);
1935 iput(inode);
1936 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001937 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001938 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001939 return err;
1940}
1941
Chris Mason39279cc2007-06-12 06:35:45 -04001942static int btrfs_create(struct inode *dir, struct dentry *dentry,
1943 int mode, struct nameidata *nd)
1944{
1945 struct btrfs_trans_handle *trans;
1946 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001947 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001948 int err;
1949 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001950 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001951 u64 objectid;
1952
1953 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001954 err = btrfs_check_free_space(root, 1, 0);
1955 if (err)
1956 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001957 trans = btrfs_start_transaction(root, 1);
1958 btrfs_set_trans_block_group(trans, dir);
1959
1960 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1961 if (err) {
1962 err = -ENOSPC;
1963 goto out_unlock;
1964 }
1965
Chris Mason9c583092008-01-29 15:15:18 -05001966 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1967 dentry->d_name.len,
1968 dentry->d_parent->d_inode->i_ino,
1969 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001970 err = PTR_ERR(inode);
1971 if (IS_ERR(inode))
1972 goto out_unlock;
1973
1974 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001975 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001976 if (err)
1977 drop_inode = 1;
1978 else {
1979 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001980 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001981 inode->i_fop = &btrfs_file_operations;
1982 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001983 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1984 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001985 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001986 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1987 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001988 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason81d7ed22008-04-25 08:51:48 -04001989 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001990 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001991 }
1992 dir->i_sb->s_dirt = 1;
1993 btrfs_update_inode_block_group(trans, inode);
1994 btrfs_update_inode_block_group(trans, dir);
1995out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001996 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001997 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001998fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001999 mutex_unlock(&root->fs_info->fs_mutex);
2000
2001 if (drop_inode) {
2002 inode_dec_link_count(inode);
2003 iput(inode);
2004 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002005 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002006 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002007 return err;
2008}
2009
2010static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2011 struct dentry *dentry)
2012{
2013 struct btrfs_trans_handle *trans;
2014 struct btrfs_root *root = BTRFS_I(dir)->root;
2015 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002016 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002017 int err;
2018 int drop_inode = 0;
2019
2020 if (inode->i_nlink == 0)
2021 return -ENOENT;
2022
Chris Mason6da6aba2007-12-18 16:15:09 -05002023#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2024 inode->i_nlink++;
2025#else
Chris Mason39279cc2007-06-12 06:35:45 -04002026 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002027#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002028 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002029 err = btrfs_check_free_space(root, 1, 0);
2030 if (err)
2031 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002032 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002033
Chris Mason39279cc2007-06-12 06:35:45 -04002034 btrfs_set_trans_block_group(trans, dir);
2035 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05002036 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002037
Chris Mason39279cc2007-06-12 06:35:45 -04002038 if (err)
2039 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002040
Chris Mason39279cc2007-06-12 06:35:45 -04002041 dir->i_sb->s_dirt = 1;
2042 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002043 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002044
Chris Mason54aa1f42007-06-22 14:16:25 -04002045 if (err)
2046 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002047
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002048 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002049 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002050fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002051 mutex_unlock(&root->fs_info->fs_mutex);
2052
2053 if (drop_inode) {
2054 inode_dec_link_count(inode);
2055 iput(inode);
2056 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002057 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002058 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002059 return err;
2060}
2061
Chris Mason39279cc2007-06-12 06:35:45 -04002062static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2063{
Chris Masonb9d86662008-05-02 16:13:49 -04002064 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002065 struct btrfs_trans_handle *trans;
2066 struct btrfs_root *root = BTRFS_I(dir)->root;
2067 int err = 0;
2068 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002069 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002070 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002071
2072 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002073 err = btrfs_check_free_space(root, 1, 0);
2074 if (err)
2075 goto out_unlock;
2076
Chris Mason39279cc2007-06-12 06:35:45 -04002077 trans = btrfs_start_transaction(root, 1);
2078 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002079
Chris Mason39279cc2007-06-12 06:35:45 -04002080 if (IS_ERR(trans)) {
2081 err = PTR_ERR(trans);
2082 goto out_unlock;
2083 }
2084
2085 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2086 if (err) {
2087 err = -ENOSPC;
2088 goto out_unlock;
2089 }
2090
Chris Mason9c583092008-01-29 15:15:18 -05002091 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2092 dentry->d_name.len,
2093 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002094 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2095 if (IS_ERR(inode)) {
2096 err = PTR_ERR(inode);
2097 goto out_fail;
2098 }
Chris Mason5f39d392007-10-15 16:14:19 -04002099
Chris Mason39279cc2007-06-12 06:35:45 -04002100 drop_on_err = 1;
2101 inode->i_op = &btrfs_dir_inode_operations;
2102 inode->i_fop = &btrfs_dir_file_operations;
2103 btrfs_set_trans_block_group(trans, inode);
2104
Chris Mason39544012007-12-12 14:38:19 -05002105 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002106 err = btrfs_update_inode(trans, root, inode);
2107 if (err)
2108 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002109
Chris Mason9c583092008-01-29 15:15:18 -05002110 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002111 if (err)
2112 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002113
Chris Mason39279cc2007-06-12 06:35:45 -04002114 d_instantiate(dentry, inode);
2115 drop_on_err = 0;
2116 dir->i_sb->s_dirt = 1;
2117 btrfs_update_inode_block_group(trans, inode);
2118 btrfs_update_inode_block_group(trans, dir);
2119
2120out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002121 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002122 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002123
Chris Mason39279cc2007-06-12 06:35:45 -04002124out_unlock:
2125 mutex_unlock(&root->fs_info->fs_mutex);
2126 if (drop_on_err)
2127 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002128 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002129 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002130 return err;
2131}
2132
Chris Mason3b951512008-04-17 11:29:12 -04002133static int merge_extent_mapping(struct extent_map_tree *em_tree,
2134 struct extent_map *existing,
2135 struct extent_map *em)
2136{
2137 u64 start_diff;
2138 u64 new_end;
2139 int ret = 0;
2140 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2141
2142 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2143 goto invalid;
2144
2145 if (!real_blocks && em->block_start != existing->block_start)
2146 goto invalid;
2147
2148 new_end = max(existing->start + existing->len, em->start + em->len);
2149
2150 if (existing->start >= em->start) {
2151 if (em->start + em->len < existing->start)
2152 goto invalid;
2153
2154 start_diff = existing->start - em->start;
2155 if (real_blocks && em->block_start + start_diff !=
2156 existing->block_start)
2157 goto invalid;
2158
2159 em->len = new_end - em->start;
2160
2161 remove_extent_mapping(em_tree, existing);
2162 /* free for the tree */
2163 free_extent_map(existing);
2164 ret = add_extent_mapping(em_tree, em);
2165
2166 } else if (em->start > existing->start) {
2167
2168 if (existing->start + existing->len < em->start)
2169 goto invalid;
2170
2171 start_diff = em->start - existing->start;
2172 if (real_blocks && existing->block_start + start_diff !=
2173 em->block_start)
2174 goto invalid;
2175
2176 remove_extent_mapping(em_tree, existing);
2177 em->block_start = existing->block_start;
2178 em->start = existing->start;
2179 em->len = new_end - existing->start;
2180 free_extent_map(existing);
2181
2182 ret = add_extent_mapping(em_tree, em);
2183 } else {
2184 goto invalid;
2185 }
2186 return ret;
2187
2188invalid:
2189 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2190 existing->start, existing->len, existing->block_start,
2191 em->start, em->len, em->block_start);
2192 return -EIO;
2193}
2194
Chris Masona52d9a82007-08-27 16:49:44 -04002195struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002196 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002197 int create)
2198{
2199 int ret;
2200 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002201 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002202 u64 extent_start = 0;
2203 u64 extent_end = 0;
2204 u64 objectid = inode->i_ino;
2205 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002206 struct btrfs_path *path;
2207 struct btrfs_root *root = BTRFS_I(inode)->root;
2208 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002209 struct extent_buffer *leaf;
2210 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002211 struct extent_map *em = NULL;
2212 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002213 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002214 struct btrfs_trans_handle *trans = NULL;
2215
2216 path = btrfs_alloc_path();
2217 BUG_ON(!path);
2218 mutex_lock(&root->fs_info->fs_mutex);
2219
2220again:
Chris Masond1310b22008-01-24 16:13:08 -05002221 spin_lock(&em_tree->lock);
2222 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002223 if (em)
2224 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002225 spin_unlock(&em_tree->lock);
2226
Chris Masona52d9a82007-08-27 16:49:44 -04002227 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002228 if (em->start > start || em->start + em->len <= start)
2229 free_extent_map(em);
2230 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002231 free_extent_map(em);
2232 else
2233 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002234 }
Chris Masond1310b22008-01-24 16:13:08 -05002235 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002236 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002237 err = -ENOMEM;
2238 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002239 }
Chris Masond1310b22008-01-24 16:13:08 -05002240
2241 em->start = EXTENT_MAP_HOLE;
2242 em->len = (u64)-1;
Chris Masona061fc82008-05-07 11:43:44 -04002243 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04002244 ret = btrfs_lookup_file_extent(trans, root, path,
2245 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002246 if (ret < 0) {
2247 err = ret;
2248 goto out;
2249 }
2250
2251 if (ret != 0) {
2252 if (path->slots[0] == 0)
2253 goto not_found;
2254 path->slots[0]--;
2255 }
2256
Chris Mason5f39d392007-10-15 16:14:19 -04002257 leaf = path->nodes[0];
2258 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002259 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002260 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002261 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2262 found_type = btrfs_key_type(&found_key);
2263 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002264 found_type != BTRFS_EXTENT_DATA_KEY) {
2265 goto not_found;
2266 }
2267
Chris Mason5f39d392007-10-15 16:14:19 -04002268 found_type = btrfs_file_extent_type(leaf, item);
2269 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002270 if (found_type == BTRFS_FILE_EXTENT_REG) {
2271 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002272 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002273 err = 0;
Chris Masonb888db2b2007-08-27 16:49:44 -04002274 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002275 em->start = start;
2276 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002277 if (start + len <= extent_start)
Chris Masonb888db2b2007-08-27 16:49:44 -04002278 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002279 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002280 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002281 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002282 }
2283 goto not_found_em;
2284 }
Chris Masondb945352007-10-15 16:15:53 -04002285 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2286 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002287 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002288 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002289 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002290 goto insert;
2291 }
Chris Masondb945352007-10-15 16:15:53 -04002292 bytenr += btrfs_file_extent_offset(leaf, item);
2293 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002294 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002295 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002296 goto insert;
2297 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002298 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002299 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002300 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002301 size_t size;
2302 size_t extent_offset;
2303 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002304
Chris Mason5f39d392007-10-15 16:14:19 -04002305 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2306 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002307 extent_end = (extent_start + size + root->sectorsize - 1) &
2308 ~((u64)root->sectorsize - 1);
Chris Masonb888db2b2007-08-27 16:49:44 -04002309 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002310 em->start = start;
2311 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002312 if (start + len <= extent_start)
Chris Masonb888db2b2007-08-27 16:49:44 -04002313 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002314 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002315 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002316 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002317 }
2318 goto not_found_em;
2319 }
2320 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002321
2322 if (!page) {
2323 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002324 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002325 goto out;
2326 }
2327
Chris Mason70dec802008-01-29 09:59:12 -05002328 page_start = page_offset(page) + pg_offset;
2329 extent_offset = page_start - extent_start;
2330 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002331 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002332 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002333 em->len = (copy_size + root->sectorsize - 1) &
2334 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002335 map = kmap(page);
2336 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002337 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002338 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002339 copy_size);
2340 flush_dcache_page(page);
2341 } else if (create && PageUptodate(page)) {
2342 if (!trans) {
2343 kunmap(page);
2344 free_extent_map(em);
2345 em = NULL;
2346 btrfs_release_path(root, path);
2347 trans = btrfs_start_transaction(root, 1);
2348 goto again;
2349 }
Chris Mason70dec802008-01-29 09:59:12 -05002350 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002351 copy_size);
2352 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002353 }
Chris Masona52d9a82007-08-27 16:49:44 -04002354 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002355 set_extent_uptodate(io_tree, em->start,
2356 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002357 goto insert;
2358 } else {
2359 printk("unkknown found_type %d\n", found_type);
2360 WARN_ON(1);
2361 }
2362not_found:
2363 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002364 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002365not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002366 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002367insert:
2368 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002369 if (em->start > start || extent_map_end(em) <= start) {
2370 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002371 err = -EIO;
2372 goto out;
2373 }
Chris Masond1310b22008-01-24 16:13:08 -05002374
2375 err = 0;
2376 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002377 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002378 /* it is possible that someone inserted the extent into the tree
2379 * while we had the lock dropped. It is also possible that
2380 * an overlapping map exists in the tree
2381 */
Chris Masona52d9a82007-08-27 16:49:44 -04002382 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002383 struct extent_map *existing;
2384 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002385 if (existing && (existing->start > start ||
2386 existing->start + existing->len <= start)) {
2387 free_extent_map(existing);
2388 existing = NULL;
2389 }
Chris Mason3b951512008-04-17 11:29:12 -04002390 if (!existing) {
2391 existing = lookup_extent_mapping(em_tree, em->start,
2392 em->len);
2393 if (existing) {
2394 err = merge_extent_mapping(em_tree, existing,
2395 em);
2396 free_extent_map(existing);
2397 if (err) {
2398 free_extent_map(em);
2399 em = NULL;
2400 }
2401 } else {
2402 err = -EIO;
2403 printk("failing to insert %Lu %Lu\n",
2404 start, len);
2405 free_extent_map(em);
2406 em = NULL;
2407 }
2408 } else {
2409 free_extent_map(em);
2410 em = existing;
Chris Masona52d9a82007-08-27 16:49:44 -04002411 }
Chris Masona52d9a82007-08-27 16:49:44 -04002412 }
Chris Masond1310b22008-01-24 16:13:08 -05002413 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002414out:
2415 btrfs_free_path(path);
2416 if (trans) {
2417 ret = btrfs_end_transaction(trans, root);
2418 if (!err)
2419 err = ret;
2420 }
2421 mutex_unlock(&root->fs_info->fs_mutex);
2422 if (err) {
2423 free_extent_map(em);
2424 WARN_ON(1);
2425 return ERR_PTR(err);
2426 }
2427 return em;
2428}
2429
Chris Masone1c4b742008-04-22 13:26:46 -04002430#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002431static int btrfs_get_block(struct inode *inode, sector_t iblock,
2432 struct buffer_head *bh_result, int create)
2433{
2434 struct extent_map *em;
2435 u64 start = (u64)iblock << inode->i_blkbits;
2436 struct btrfs_multi_bio *multi = NULL;
2437 struct btrfs_root *root = BTRFS_I(inode)->root;
2438 u64 len;
2439 u64 logical;
2440 u64 map_length;
2441 int ret = 0;
2442
2443 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2444
2445 if (!em || IS_ERR(em))
2446 goto out;
2447
Chris Masone1c4b742008-04-22 13:26:46 -04002448 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002449 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002450 }
Chris Mason16432982008-04-10 10:23:21 -04002451
2452 if (em->block_start == EXTENT_MAP_INLINE) {
2453 ret = -EINVAL;
2454 goto out;
2455 }
2456
Chris Mason16432982008-04-10 10:23:21 -04002457 len = em->start + em->len - start;
2458 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2459
Chris Masone1c4b742008-04-22 13:26:46 -04002460 if (em->block_start == EXTENT_MAP_HOLE ||
2461 em->block_start == EXTENT_MAP_DELALLOC) {
2462 bh_result->b_size = len;
2463 goto out;
2464 }
2465
Chris Mason16432982008-04-10 10:23:21 -04002466 logical = start - em->start;
2467 logical = em->block_start + logical;
2468
2469 map_length = len;
2470 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2471 logical, &map_length, &multi, 0);
2472 BUG_ON(ret);
2473 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2474 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002475
Chris Mason16432982008-04-10 10:23:21 -04002476 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2477 set_buffer_mapped(bh_result);
2478 kfree(multi);
2479out:
2480 free_extent_map(em);
2481 return ret;
2482}
Chris Masone1c4b742008-04-22 13:26:46 -04002483#endif
Chris Mason16432982008-04-10 10:23:21 -04002484
2485static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2486 const struct iovec *iov, loff_t offset,
2487 unsigned long nr_segs)
2488{
Chris Masone1c4b742008-04-22 13:26:46 -04002489 return -EINVAL;
2490#if 0
Chris Mason16432982008-04-10 10:23:21 -04002491 struct file *file = iocb->ki_filp;
2492 struct inode *inode = file->f_mapping->host;
2493
2494 if (rw == WRITE)
2495 return -EINVAL;
2496
2497 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2498 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002499#endif
Chris Mason16432982008-04-10 10:23:21 -04002500}
2501
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002502static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002503{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002504 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002505}
2506
Chris Mason9ebefb182007-06-15 13:50:00 -04002507int btrfs_readpage(struct file *file, struct page *page)
2508{
Chris Masond1310b22008-01-24 16:13:08 -05002509 struct extent_io_tree *tree;
2510 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002511 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002512}
Chris Mason1832a6d2007-12-21 16:27:21 -05002513
Chris Mason39279cc2007-06-12 06:35:45 -04002514static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2515{
Chris Masond1310b22008-01-24 16:13:08 -05002516 struct extent_io_tree *tree;
Chris Masonb888db2b2007-08-27 16:49:44 -04002517
2518
2519 if (current->flags & PF_MEMALLOC) {
2520 redirty_page_for_writepage(wbc, page);
2521 unlock_page(page);
2522 return 0;
2523 }
Chris Masond1310b22008-01-24 16:13:08 -05002524 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002525 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2526}
Chris Mason39279cc2007-06-12 06:35:45 -04002527
Chris Masonb293f02e2007-11-01 19:45:34 -04002528static int btrfs_writepages(struct address_space *mapping,
2529 struct writeback_control *wbc)
2530{
Chris Masond1310b22008-01-24 16:13:08 -05002531 struct extent_io_tree *tree;
2532 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04002533 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2534}
2535
Chris Mason3ab2fb52007-11-08 10:59:22 -05002536static int
2537btrfs_readpages(struct file *file, struct address_space *mapping,
2538 struct list_head *pages, unsigned nr_pages)
2539{
Chris Masond1310b22008-01-24 16:13:08 -05002540 struct extent_io_tree *tree;
2541 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002542 return extent_readpages(tree, mapping, pages, nr_pages,
2543 btrfs_get_extent);
2544}
2545
Chris Mason70dec802008-01-29 09:59:12 -05002546static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002547{
Chris Masond1310b22008-01-24 16:13:08 -05002548 struct extent_io_tree *tree;
2549 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002550 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002551
Chris Masond1310b22008-01-24 16:13:08 -05002552 tree = &BTRFS_I(page->mapping->host)->io_tree;
2553 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002554 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002555 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002556 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002557 ClearPagePrivate(page);
2558 set_page_private(page, 0);
2559 page_cache_release(page);
2560 }
2561 return ret;
2562}
Chris Mason39279cc2007-06-12 06:35:45 -04002563
Chris Masona52d9a82007-08-27 16:49:44 -04002564static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2565{
Chris Masond1310b22008-01-24 16:13:08 -05002566 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002567
Chris Masond1310b22008-01-24 16:13:08 -05002568 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002569 extent_invalidatepage(tree, page, offset);
2570 btrfs_releasepage(page, GFP_NOFS);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002571 if (PagePrivate(page)) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002572 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002573 ClearPagePrivate(page);
2574 set_page_private(page, 0);
2575 page_cache_release(page);
2576 }
Chris Mason39279cc2007-06-12 06:35:45 -04002577}
2578
Chris Mason9ebefb182007-06-15 13:50:00 -04002579/*
2580 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2581 * called from a page fault handler when a page is first dirtied. Hence we must
2582 * be careful to check for EOF conditions here. We set the page up correctly
2583 * for a written page which means we get ENOSPC checking when writing into
2584 * holes and correct delalloc and unwritten extent mapping on filesystems that
2585 * support these features.
2586 *
2587 * We are not allowed to take the i_mutex here so we have to play games to
2588 * protect against truncate races as the page could now be beyond EOF. Because
2589 * vmtruncate() writes the inode size before removing pages, once we have the
2590 * page lock we can determine safely if the page is beyond EOF. If it is not
2591 * beyond EOF, then the page is guaranteed safe against truncation until we
2592 * unlock the page.
2593 */
2594int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2595{
Chris Mason6da6aba2007-12-18 16:15:09 -05002596 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002597 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002598 unsigned long end;
2599 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002600 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002601 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002602
Chris Mason1832a6d2007-12-21 16:27:21 -05002603 mutex_lock(&root->fs_info->fs_mutex);
2604 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002605 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002606 if (ret)
2607 goto out;
2608
2609 ret = -EINVAL;
2610
Chris Mason9ebefb182007-06-15 13:50:00 -04002611 lock_page(page);
2612 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002613 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002614 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002615
Chris Mason9ebefb182007-06-15 13:50:00 -04002616 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002617 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002618 /* page got truncated out from underneath us */
2619 goto out_unlock;
2620 }
2621
2622 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002623 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002624 end = size & ~PAGE_CACHE_MASK;
2625 else
2626 end = PAGE_CACHE_SIZE;
2627
Chris Masonb888db2b2007-08-27 16:49:44 -04002628 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002629
2630out_unlock:
2631 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002632out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002633 return ret;
2634}
2635
Chris Mason39279cc2007-06-12 06:35:45 -04002636static void btrfs_truncate(struct inode *inode)
2637{
2638 struct btrfs_root *root = BTRFS_I(inode)->root;
2639 int ret;
2640 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002641 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002642
2643 if (!S_ISREG(inode->i_mode))
2644 return;
2645 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2646 return;
2647
2648 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2649
2650 mutex_lock(&root->fs_info->fs_mutex);
2651 trans = btrfs_start_transaction(root, 1);
2652 btrfs_set_trans_block_group(trans, inode);
2653
2654 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002655 ret = btrfs_truncate_in_trans(trans, root, inode,
2656 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002657 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002658 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002659
Chris Mason39279cc2007-06-12 06:35:45 -04002660 ret = btrfs_end_transaction(trans, root);
2661 BUG_ON(ret);
2662 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002663 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002664 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002665}
2666
Chris Mason4313b392008-01-03 09:08:48 -05002667static int noinline create_subvol(struct btrfs_root *root, char *name,
2668 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002669{
2670 struct btrfs_trans_handle *trans;
2671 struct btrfs_key key;
2672 struct btrfs_root_item root_item;
2673 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002674 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002675 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002676 struct inode *inode;
2677 struct inode *dir;
2678 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002679 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002680 u64 objectid;
2681 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002682 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002683
2684 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002685 ret = btrfs_check_free_space(root, 1, 0);
2686 if (ret)
2687 goto fail_commit;
2688
Chris Mason39279cc2007-06-12 06:35:45 -04002689 trans = btrfs_start_transaction(root, 1);
2690 BUG_ON(!trans);
2691
Chris Mason7bb86312007-12-11 09:25:06 -05002692 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2693 0, &objectid);
2694 if (ret)
2695 goto fail;
2696
2697 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2698 objectid, trans->transid, 0, 0,
2699 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002700 if (IS_ERR(leaf))
2701 return PTR_ERR(leaf);
2702
2703 btrfs_set_header_nritems(leaf, 0);
2704 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002705 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002706 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002707 btrfs_set_header_owner(leaf, objectid);
2708
Chris Mason5f39d392007-10-15 16:14:19 -04002709 write_extent_buffer(leaf, root->fs_info->fsid,
2710 (unsigned long)btrfs_header_fsid(leaf),
2711 BTRFS_FSID_SIZE);
2712 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002713
2714 inode_item = &root_item.inode;
2715 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002716 inode_item->generation = cpu_to_le64(1);
2717 inode_item->size = cpu_to_le64(3);
2718 inode_item->nlink = cpu_to_le32(1);
2719 inode_item->nblocks = cpu_to_le64(1);
2720 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002721
Chris Masondb945352007-10-15 16:15:53 -04002722 btrfs_set_root_bytenr(&root_item, leaf->start);
2723 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002724 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002725 btrfs_set_root_used(&root_item, 0);
2726
Chris Mason5eda7b52007-06-22 14:16:25 -04002727 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2728 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002729
2730 free_extent_buffer(leaf);
2731 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002732
Chris Mason39279cc2007-06-12 06:35:45 -04002733 btrfs_set_root_dirid(&root_item, new_dirid);
2734
2735 key.objectid = objectid;
2736 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002737 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2738 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2739 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002740 if (ret)
2741 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002742
2743 /*
2744 * insert the directory item
2745 */
2746 key.offset = (u64)-1;
2747 dir = root->fs_info->sb->s_root->d_inode;
2748 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2749 name, namelen, dir->i_ino, &key,
2750 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002751 if (ret)
2752 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002753
Chris Mason39544012007-12-12 14:38:19 -05002754 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2755 name, namelen, objectid,
2756 root->fs_info->sb->s_root->d_inode->i_ino);
2757 if (ret)
2758 goto fail;
2759
Chris Mason39279cc2007-06-12 06:35:45 -04002760 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002761 if (ret)
2762 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002763
Josef Bacik58176a92007-08-29 15:47:34 -04002764 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002765 BUG_ON(!new_root);
2766
2767 trans = btrfs_start_transaction(new_root, 1);
2768 BUG_ON(!trans);
2769
Chris Mason9c583092008-01-29 15:15:18 -05002770 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2771 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002772 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002773 if (IS_ERR(inode))
2774 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002775 inode->i_op = &btrfs_dir_inode_operations;
2776 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002777 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002778
Chris Mason39544012007-12-12 14:38:19 -05002779 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2780 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002781 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002782 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002783 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002784 if (ret)
2785 goto fail;
2786fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002787 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002788 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002789 if (err && !ret)
2790 ret = err;
2791fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002792 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002793 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002794 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002795 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002796}
2797
2798static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2799{
Chris Mason3063d292008-01-08 15:46:30 -05002800 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002801 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002802 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002803 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002804 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002805
2806 if (!root->ref_cows)
2807 return -EINVAL;
2808
2809 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002810 ret = btrfs_check_free_space(root, 1, 0);
2811 if (ret)
2812 goto fail_unlock;
2813
Chris Mason3063d292008-01-08 15:46:30 -05002814 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2815 if (!pending_snapshot) {
2816 ret = -ENOMEM;
2817 goto fail_unlock;
2818 }
Yanfb4bc1e2008-01-17 11:59:51 -05002819 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002820 if (!pending_snapshot->name) {
2821 ret = -ENOMEM;
2822 kfree(pending_snapshot);
2823 goto fail_unlock;
2824 }
Yanfb4bc1e2008-01-17 11:59:51 -05002825 memcpy(pending_snapshot->name, name, namelen);
2826 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002827 trans = btrfs_start_transaction(root, 1);
2828 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002829 pending_snapshot->root = root;
2830 list_add(&pending_snapshot->list,
2831 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002832 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002833 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002834
Chris Mason1832a6d2007-12-21 16:27:21 -05002835fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002836 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002837 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002838 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002839 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002840}
2841
Chris Masonedbd8d42007-12-21 16:27:24 -05002842unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002843 struct file_ra_state *ra, struct file *file,
2844 pgoff_t offset, pgoff_t last_index)
2845{
Chris Mason8e7bf942008-04-28 09:02:36 -04002846 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04002847
2848#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04002849 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2850 return offset;
2851#else
Chris Mason86479a02007-09-10 19:58:16 -04002852 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2853 return offset + req_size;
2854#endif
2855}
2856
2857int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002858 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002859 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002860 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002861 struct page *page;
2862 unsigned long last_index;
Chris Mason8e7bf942008-04-28 09:02:36 -04002863 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
2864 unsigned long total_read = 0;
Chris Mason86479a02007-09-10 19:58:16 -04002865 u64 page_start;
2866 u64 page_end;
2867 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002868 int ret;
2869
2870 mutex_lock(&root->fs_info->fs_mutex);
2871 ret = btrfs_check_free_space(root, inode->i_size, 0);
2872 mutex_unlock(&root->fs_info->fs_mutex);
2873 if (ret)
2874 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002875
2876 mutex_lock(&inode->i_mutex);
2877 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2878 for (i = 0; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04002879 if (total_read % ra_pages == 0) {
2880 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
2881 min(last_index, i + ra_pages - 1));
Chris Mason86479a02007-09-10 19:58:16 -04002882 }
Chris Mason8e7bf942008-04-28 09:02:36 -04002883 total_read++;
Chris Mason86479a02007-09-10 19:58:16 -04002884 page = grab_cache_page(inode->i_mapping, i);
2885 if (!page)
2886 goto out_unlock;
2887 if (!PageUptodate(page)) {
2888 btrfs_readpage(NULL, page);
2889 lock_page(page);
2890 if (!PageUptodate(page)) {
2891 unlock_page(page);
2892 page_cache_release(page);
2893 goto out_unlock;
2894 }
2895 }
Chris Masonec44a352008-04-28 15:29:52 -04002896
2897#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2898 ClearPageDirty(page);
2899#else
2900 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2901#endif
2902 wait_on_page_writeback(page);
2903 set_page_extent_mapped(page);
2904
Chris Mason35ebb932007-10-30 16:56:53 -04002905 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002906 page_end = page_start + PAGE_CACHE_SIZE - 1;
2907
Chris Masond1310b22008-01-24 16:13:08 -05002908 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002909 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002910 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002911
Chris Masond1310b22008-01-24 16:13:08 -05002912 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002913 set_page_dirty(page);
2914 unlock_page(page);
2915 page_cache_release(page);
2916 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2917 }
2918
2919out_unlock:
2920 mutex_unlock(&inode->i_mutex);
2921 return 0;
2922}
2923
Chris Masonedbd8d42007-12-21 16:27:24 -05002924static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2925{
2926 u64 new_size;
2927 u64 old_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002928 u64 devid = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05002929 struct btrfs_ioctl_vol_args *vol_args;
2930 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002931 struct btrfs_device *device = NULL;
Chris Masonedbd8d42007-12-21 16:27:24 -05002932 char *sizestr;
Chris Mason8f18cf12008-04-25 16:53:30 -04002933 char *devstr = NULL;
Chris Masonedbd8d42007-12-21 16:27:24 -05002934 int ret = 0;
2935 int namelen;
2936 int mod = 0;
2937
2938 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2939
2940 if (!vol_args)
2941 return -ENOMEM;
2942
2943 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2944 ret = -EFAULT;
2945 goto out;
2946 }
2947 namelen = strlen(vol_args->name);
2948 if (namelen > BTRFS_VOL_NAME_MAX) {
2949 ret = -EINVAL;
2950 goto out;
2951 }
2952
Chris Mason8f18cf12008-04-25 16:53:30 -04002953 mutex_lock(&root->fs_info->fs_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05002954 sizestr = vol_args->name;
Chris Mason8f18cf12008-04-25 16:53:30 -04002955 devstr = strchr(sizestr, ':');
2956 if (devstr) {
2957 char *end;
2958 sizestr = devstr + 1;
2959 *devstr = '\0';
2960 devstr = vol_args->name;
2961 devid = simple_strtoull(devstr, &end, 10);
2962printk("resizing devid %Lu\n", devid);
2963 }
2964 device = btrfs_find_device(root, devid, NULL);
2965 if (!device) {
2966 printk("resizer unable to find device %Lu\n", devid);
2967 ret = -EINVAL;
2968 goto out_unlock;
2969 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002970 if (!strcmp(sizestr, "max"))
Chris Mason8f18cf12008-04-25 16:53:30 -04002971 new_size = device->bdev->bd_inode->i_size;
Chris Masonedbd8d42007-12-21 16:27:24 -05002972 else {
2973 if (sizestr[0] == '-') {
2974 mod = -1;
2975 sizestr++;
2976 } else if (sizestr[0] == '+') {
2977 mod = 1;
2978 sizestr++;
2979 }
2980 new_size = btrfs_parse_size(sizestr);
2981 if (new_size == 0) {
2982 ret = -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002983 goto out_unlock;
Chris Masonedbd8d42007-12-21 16:27:24 -05002984 }
2985 }
2986
Chris Mason8f18cf12008-04-25 16:53:30 -04002987 old_size = device->total_bytes;
Chris Masonedbd8d42007-12-21 16:27:24 -05002988
2989 if (mod < 0) {
2990 if (new_size > old_size) {
2991 ret = -EINVAL;
2992 goto out_unlock;
2993 }
2994 new_size = old_size - new_size;
2995 } else if (mod > 0) {
2996 new_size = old_size + new_size;
2997 }
2998
2999 if (new_size < 256 * 1024 * 1024) {
3000 ret = -EINVAL;
3001 goto out_unlock;
3002 }
Chris Mason8f18cf12008-04-25 16:53:30 -04003003 if (new_size > device->bdev->bd_inode->i_size) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003004 ret = -EFBIG;
3005 goto out_unlock;
3006 }
Chris Masonf9ef6602008-01-03 09:22:38 -05003007
3008 do_div(new_size, root->sectorsize);
3009 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05003010
Chris Mason8f18cf12008-04-25 16:53:30 -04003011printk("new size for %s is %llu\n", device->name, (unsigned long long)new_size);
3012
Chris Masonedbd8d42007-12-21 16:27:24 -05003013 if (new_size > old_size) {
3014 trans = btrfs_start_transaction(root, 1);
Chris Mason8f18cf12008-04-25 16:53:30 -04003015 ret = btrfs_grow_device(trans, device, new_size);
Chris Masonedbd8d42007-12-21 16:27:24 -05003016 btrfs_commit_transaction(trans, root);
3017 } else {
Chris Mason8f18cf12008-04-25 16:53:30 -04003018 ret = btrfs_shrink_device(device, new_size);
Chris Masonedbd8d42007-12-21 16:27:24 -05003019 }
3020
3021out_unlock:
3022 mutex_unlock(&root->fs_info->fs_mutex);
3023out:
3024 kfree(vol_args);
3025 return ret;
3026}
3027
Chris Mason4313b392008-01-03 09:08:48 -05003028static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
3029 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003030{
Chris Mason4aec2b52007-12-18 16:25:45 -05003031 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003032 struct btrfs_dir_item *di;
3033 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003034 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05003035 int namelen;
3036 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003037
Chris Mason4aec2b52007-12-18 16:25:45 -05003038 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04003039
Chris Mason4aec2b52007-12-18 16:25:45 -05003040 if (!vol_args)
3041 return -ENOMEM;
3042
3043 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3044 ret = -EFAULT;
3045 goto out;
3046 }
3047
3048 namelen = strlen(vol_args->name);
3049 if (namelen > BTRFS_VOL_NAME_MAX) {
3050 ret = -EINVAL;
3051 goto out;
3052 }
3053 if (strchr(vol_args->name, '/')) {
3054 ret = -EINVAL;
3055 goto out;
3056 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003057
3058 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05003059 if (!path) {
3060 ret = -ENOMEM;
3061 goto out;
3062 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003063
3064 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
3065 mutex_lock(&root->fs_info->fs_mutex);
3066 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
3067 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05003068 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003069 mutex_unlock(&root->fs_info->fs_mutex);
3070 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05003071
3072 if (di && !IS_ERR(di)) {
3073 ret = -EEXIST;
3074 goto out;
3075 }
3076
3077 if (IS_ERR(di)) {
3078 ret = PTR_ERR(di);
3079 goto out;
3080 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003081
3082 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05003083 ret = create_subvol(root, vol_args->name, namelen);
3084 else
3085 ret = create_snapshot(root, vol_args->name, namelen);
3086out:
3087 kfree(vol_args);
3088 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003089}
3090
3091static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04003092{
Chris Mason6da6aba2007-12-18 16:15:09 -05003093 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003094 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003095
3096 switch (inode->i_mode & S_IFMT) {
3097 case S_IFDIR:
3098 mutex_lock(&root->fs_info->fs_mutex);
3099 btrfs_defrag_root(root, 0);
3100 btrfs_defrag_root(root->fs_info->extent_root, 0);
3101 mutex_unlock(&root->fs_info->fs_mutex);
3102 break;
3103 case S_IFREG:
3104 btrfs_defrag_file(file);
3105 break;
3106 }
3107
3108 return 0;
3109}
3110
Chris Mason788f20e2008-04-28 15:29:42 -04003111long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
3112{
3113 struct btrfs_ioctl_vol_args *vol_args;
3114 int ret;
3115
3116 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3117
3118 if (!vol_args)
3119 return -ENOMEM;
3120
3121 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3122 ret = -EFAULT;
3123 goto out;
3124 }
3125 ret = btrfs_init_new_device(root, vol_args->name);
3126
3127out:
3128 kfree(vol_args);
3129 return ret;
3130}
3131
Chris Masona061fc82008-05-07 11:43:44 -04003132long btrfs_ioctl_rm_dev(struct btrfs_root *root, void __user *arg)
3133{
3134 struct btrfs_ioctl_vol_args *vol_args;
3135 int ret;
3136
3137 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3138
3139 if (!vol_args)
3140 return -ENOMEM;
3141
3142 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3143 ret = -EFAULT;
3144 goto out;
3145 }
3146 ret = btrfs_rm_device(root, vol_args->name);
3147
3148out:
3149 kfree(vol_args);
3150 return ret;
3151}
3152
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003153int dup_item_to_inode(struct btrfs_trans_handle *trans,
Sage Weilf2eb0a22008-05-02 14:43:14 -04003154 struct btrfs_root *root,
3155 struct btrfs_path *path,
3156 struct extent_buffer *leaf,
3157 int slot,
3158 struct btrfs_key *key,
3159 u64 destino)
3160{
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003161 char *dup;
Sage Weilf2eb0a22008-05-02 14:43:14 -04003162 int len = btrfs_item_size_nr(leaf, slot);
Sage Weilf2eb0a22008-05-02 14:43:14 -04003163 struct btrfs_key ckey = *key;
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003164 int ret = 0;
3165
3166 dup = kmalloc(len, GFP_NOFS);
3167 if (!dup)
3168 return -ENOMEM;
3169
3170 read_extent_buffer(leaf, dup, btrfs_item_ptr_offset(leaf, slot), len);
3171 btrfs_release_path(root, path);
Sage Weilf2eb0a22008-05-02 14:43:14 -04003172
3173 ckey.objectid = destino;
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003174 ret = btrfs_insert_item(trans, root, &ckey, dup, len);
3175 kfree(dup);
3176 return ret;
Sage Weilf2eb0a22008-05-02 14:43:14 -04003177}
3178
3179long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
3180{
3181 struct inode *inode = fdentry(file)->d_inode;
3182 struct btrfs_root *root = BTRFS_I(inode)->root;
3183 struct file *src_file;
3184 struct inode *src;
3185 struct btrfs_trans_handle *trans;
3186 int ret;
3187 u64 pos;
3188 struct btrfs_path *path;
3189 struct btrfs_key key;
3190 struct extent_buffer *leaf;
3191 u32 nritems;
Sage Weilf2eb0a22008-05-02 14:43:14 -04003192 int slot;
3193
3194 src_file = fget(src_fd);
3195 if (!src_file)
3196 return -EBADF;
3197 src = src_file->f_dentry->d_inode;
3198
3199 ret = -EXDEV;
3200 if (src->i_sb != inode->i_sb)
3201 goto out_fput;
3202
3203 if (inode < src) {
3204 mutex_lock(&inode->i_mutex);
3205 mutex_lock(&src->i_mutex);
3206 } else {
3207 mutex_lock(&src->i_mutex);
3208 mutex_lock(&inode->i_mutex);
3209 }
3210
3211 ret = -ENOTEMPTY;
3212 if (inode->i_size)
3213 goto out_unlock;
3214
3215 /* do any pending delalloc/csum calc on src, one way or
3216 another, and lock file content */
3217 while (1) {
3218 filemap_write_and_wait(src->i_mapping);
3219 lock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3220 if (BTRFS_I(src)->delalloc_bytes == 0)
3221 break;
3222 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3223 }
3224
3225 mutex_lock(&root->fs_info->fs_mutex);
3226 trans = btrfs_start_transaction(root, 0);
3227 path = btrfs_alloc_path();
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003228 if (!path) {
3229 ret = -ENOMEM;
3230 goto out;
3231 }
3232 key.offset = 0;
3233 key.type = BTRFS_EXTENT_DATA_KEY;
3234 key.objectid = src->i_ino;
Sage Weilf2eb0a22008-05-02 14:43:14 -04003235 pos = 0;
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003236 path->reada = 2;
3237
Sage Weilf2eb0a22008-05-02 14:43:14 -04003238 while (1) {
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003239 /*
3240 * note the key will change type as we walk through the
3241 * tree.
3242 */
3243 ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
Sage Weilf2eb0a22008-05-02 14:43:14 -04003244 if (ret < 0)
3245 goto out;
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003246
3247 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
3248 ret = btrfs_next_leaf(root, path);
3249 if (ret < 0)
Sage Weilf2eb0a22008-05-02 14:43:14 -04003250 goto out;
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003251 if (ret > 0)
3252 break;
Sage Weilf2eb0a22008-05-02 14:43:14 -04003253 }
Sage Weilf2eb0a22008-05-02 14:43:14 -04003254 leaf = path->nodes[0];
3255 slot = path->slots[0];
3256 btrfs_item_key_to_cpu(leaf, &key, slot);
3257 nritems = btrfs_header_nritems(leaf);
3258
3259 if (btrfs_key_type(&key) > BTRFS_CSUM_ITEM_KEY ||
3260 key.objectid != src->i_ino)
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003261 break;
3262
Sage Weilf2eb0a22008-05-02 14:43:14 -04003263 if (btrfs_key_type(&key) == BTRFS_EXTENT_DATA_KEY) {
3264 struct btrfs_file_extent_item *extent;
3265 int found_type;
3266 pos = key.offset;
3267 extent = btrfs_item_ptr(leaf, slot,
3268 struct btrfs_file_extent_item);
3269 found_type = btrfs_file_extent_type(leaf, extent);
3270 if (found_type == BTRFS_FILE_EXTENT_REG) {
3271 u64 len = btrfs_file_extent_num_bytes(leaf,
3272 extent);
3273 u64 ds = btrfs_file_extent_disk_bytenr(leaf,
3274 extent);
3275 u64 dl = btrfs_file_extent_disk_num_bytes(leaf,
3276 extent);
3277 u64 off = btrfs_file_extent_offset(leaf,
3278 extent);
3279 btrfs_insert_file_extent(trans, root,
3280 inode->i_ino, pos,
3281 ds, dl, len, off);
3282 /* ds == 0 means there's a hole */
3283 if (ds != 0) {
3284 btrfs_inc_extent_ref(trans, root,
3285 ds, dl,
3286 root->root_key.objectid,
3287 trans->transid,
3288 inode->i_ino, pos);
3289 }
3290 pos = key.offset + len;
3291 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003292 ret = dup_item_to_inode(trans, root, path,
3293 leaf, slot, &key,
3294 inode->i_ino);
3295 if (ret)
3296 goto out;
Sage Weilf2eb0a22008-05-02 14:43:14 -04003297 pos = key.offset + btrfs_item_size_nr(leaf,
3298 slot);
3299 }
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003300 } else if (btrfs_key_type(&key) == BTRFS_CSUM_ITEM_KEY) {
3301 ret = dup_item_to_inode(trans, root, path, leaf,
3302 slot, &key, inode->i_ino);
Sage Weilf2eb0a22008-05-02 14:43:14 -04003303
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003304 if (ret)
Sage Weilf2eb0a22008-05-02 14:43:14 -04003305 goto out;
Sage Weilf2eb0a22008-05-02 14:43:14 -04003306 }
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003307 key.offset++;
3308 btrfs_release_path(root, path);
Sage Weilf2eb0a22008-05-02 14:43:14 -04003309 }
3310
Chris Mason5d9cd9e2008-05-05 06:26:21 -04003311 ret = 0;
Sage Weilf2eb0a22008-05-02 14:43:14 -04003312out:
3313 btrfs_free_path(path);
Sage Weilf2eb0a22008-05-02 14:43:14 -04003314
3315 inode->i_blocks = src->i_blocks;
3316 i_size_write(inode, src->i_size);
3317 btrfs_update_inode(trans, root, inode);
3318
3319 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
3320
3321 btrfs_end_transaction(trans, root);
3322 mutex_unlock(&root->fs_info->fs_mutex);
3323
3324out_unlock:
3325 mutex_unlock(&src->i_mutex);
3326 mutex_unlock(&inode->i_mutex);
3327out_fput:
3328 fput(src_file);
3329 return ret;
3330}
3331
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003332long btrfs_ioctl(struct file *file, unsigned int
3333 cmd, unsigned long arg)
3334{
Chris Mason6da6aba2007-12-18 16:15:09 -05003335 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003336
3337 switch (cmd) {
3338 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003339 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04003340 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003341 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05003342 case BTRFS_IOC_RESIZE:
3343 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason788f20e2008-04-28 15:29:42 -04003344 case BTRFS_IOC_ADD_DEV:
3345 return btrfs_ioctl_add_dev(root, (void __user *)arg);
Chris Masona061fc82008-05-07 11:43:44 -04003346 case BTRFS_IOC_RM_DEV:
3347 return btrfs_ioctl_rm_dev(root, (void __user *)arg);
Chris Masonec44a352008-04-28 15:29:52 -04003348 case BTRFS_IOC_BALANCE:
3349 return btrfs_balance(root->fs_info->dev_root);
Sage Weilf2eb0a22008-05-02 14:43:14 -04003350 case BTRFS_IOC_CLONE:
3351 return btrfs_ioctl_clone(file, arg);
Chris Mason39279cc2007-06-12 06:35:45 -04003352 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003353
3354 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04003355}
3356
Chris Mason39279cc2007-06-12 06:35:45 -04003357/*
3358 * Called inside transaction, so use GFP_NOFS
3359 */
3360struct inode *btrfs_alloc_inode(struct super_block *sb)
3361{
3362 struct btrfs_inode *ei;
3363
3364 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3365 if (!ei)
3366 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003367 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05003368 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003369 return &ei->vfs_inode;
3370}
3371
3372void btrfs_destroy_inode(struct inode *inode)
3373{
3374 WARN_ON(!list_empty(&inode->i_dentry));
3375 WARN_ON(inode->i_data.nrpages);
3376
Chris Mason8c416c92008-01-14 15:10:26 -05003377 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003378 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3379}
3380
Chris Mason44ec0b72007-10-29 10:55:05 -04003381#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3382static void init_once(struct kmem_cache * cachep, void *foo)
3383#else
Chris Mason39279cc2007-06-12 06:35:45 -04003384static void init_once(void * foo, struct kmem_cache * cachep,
3385 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003386#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003387{
3388 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3389
3390 inode_init_once(&ei->vfs_inode);
3391}
3392
3393void btrfs_destroy_cachep(void)
3394{
3395 if (btrfs_inode_cachep)
3396 kmem_cache_destroy(btrfs_inode_cachep);
3397 if (btrfs_trans_handle_cachep)
3398 kmem_cache_destroy(btrfs_trans_handle_cachep);
3399 if (btrfs_transaction_cachep)
3400 kmem_cache_destroy(btrfs_transaction_cachep);
3401 if (btrfs_bit_radix_cachep)
3402 kmem_cache_destroy(btrfs_bit_radix_cachep);
3403 if (btrfs_path_cachep)
3404 kmem_cache_destroy(btrfs_path_cachep);
3405}
3406
Chris Mason86479a02007-09-10 19:58:16 -04003407struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003408 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04003409#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3410 void (*ctor)(struct kmem_cache *, void *)
3411#else
Chris Mason92fee662007-07-25 12:31:35 -04003412 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003413 unsigned long)
3414#endif
3415 )
Chris Mason92fee662007-07-25 12:31:35 -04003416{
3417 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3418 SLAB_MEM_SPREAD | extra_flags), ctor
3419#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3420 ,NULL
3421#endif
3422 );
3423}
3424
Chris Mason39279cc2007-06-12 06:35:45 -04003425int btrfs_init_cachep(void)
3426{
Chris Mason86479a02007-09-10 19:58:16 -04003427 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003428 sizeof(struct btrfs_inode),
3429 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003430 if (!btrfs_inode_cachep)
3431 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003432 btrfs_trans_handle_cachep =
3433 btrfs_cache_create("btrfs_trans_handle_cache",
3434 sizeof(struct btrfs_trans_handle),
3435 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003436 if (!btrfs_trans_handle_cachep)
3437 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003438 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003439 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003440 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003441 if (!btrfs_transaction_cachep)
3442 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003443 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003444 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003445 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003446 if (!btrfs_path_cachep)
3447 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003448 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003449 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003450 if (!btrfs_bit_radix_cachep)
3451 goto fail;
3452 return 0;
3453fail:
3454 btrfs_destroy_cachep();
3455 return -ENOMEM;
3456}
3457
3458static int btrfs_getattr(struct vfsmount *mnt,
3459 struct dentry *dentry, struct kstat *stat)
3460{
3461 struct inode *inode = dentry->d_inode;
3462 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003463 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003464 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003465 return 0;
3466}
3467
3468static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3469 struct inode * new_dir,struct dentry *new_dentry)
3470{
3471 struct btrfs_trans_handle *trans;
3472 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3473 struct inode *new_inode = new_dentry->d_inode;
3474 struct inode *old_inode = old_dentry->d_inode;
3475 struct timespec ctime = CURRENT_TIME;
3476 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003477 int ret;
3478
3479 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3480 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3481 return -ENOTEMPTY;
3482 }
Chris Mason5f39d392007-10-15 16:14:19 -04003483
Chris Mason39279cc2007-06-12 06:35:45 -04003484 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003485 ret = btrfs_check_free_space(root, 1, 0);
3486 if (ret)
3487 goto out_unlock;
3488
Chris Mason39279cc2007-06-12 06:35:45 -04003489 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003490
Chris Mason39279cc2007-06-12 06:35:45 -04003491 btrfs_set_trans_block_group(trans, new_dir);
3492 path = btrfs_alloc_path();
3493 if (!path) {
3494 ret = -ENOMEM;
3495 goto out_fail;
3496 }
3497
3498 old_dentry->d_inode->i_nlink++;
3499 old_dir->i_ctime = old_dir->i_mtime = ctime;
3500 new_dir->i_ctime = new_dir->i_mtime = ctime;
3501 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003502
Chris Mason39279cc2007-06-12 06:35:45 -04003503 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3504 if (ret)
3505 goto out_fail;
3506
3507 if (new_inode) {
3508 new_inode->i_ctime = CURRENT_TIME;
3509 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3510 if (ret)
3511 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003512 }
Chris Mason9c583092008-01-29 15:15:18 -05003513 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003514 if (ret)
3515 goto out_fail;
3516
3517out_fail:
3518 btrfs_free_path(path);
3519 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003520out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003521 mutex_unlock(&root->fs_info->fs_mutex);
3522 return ret;
3523}
3524
3525static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3526 const char *symname)
3527{
3528 struct btrfs_trans_handle *trans;
3529 struct btrfs_root *root = BTRFS_I(dir)->root;
3530 struct btrfs_path *path;
3531 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003532 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003533 int err;
3534 int drop_inode = 0;
3535 u64 objectid;
3536 int name_len;
3537 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003538 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003539 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003540 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003541 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003542
3543 name_len = strlen(symname) + 1;
3544 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3545 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003546
Chris Mason39279cc2007-06-12 06:35:45 -04003547 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003548 err = btrfs_check_free_space(root, 1, 0);
3549 if (err)
3550 goto out_fail;
3551
Chris Mason39279cc2007-06-12 06:35:45 -04003552 trans = btrfs_start_transaction(root, 1);
3553 btrfs_set_trans_block_group(trans, dir);
3554
3555 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3556 if (err) {
3557 err = -ENOSPC;
3558 goto out_unlock;
3559 }
3560
Chris Mason9c583092008-01-29 15:15:18 -05003561 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3562 dentry->d_name.len,
3563 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003564 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3565 err = PTR_ERR(inode);
3566 if (IS_ERR(inode))
3567 goto out_unlock;
3568
3569 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003570 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003571 if (err)
3572 drop_inode = 1;
3573 else {
3574 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003575 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003576 inode->i_fop = &btrfs_file_operations;
3577 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003578 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3579 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003580 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003581 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3582 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05003583 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason81d7ed22008-04-25 08:51:48 -04003584 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003585 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003586 }
3587 dir->i_sb->s_dirt = 1;
3588 btrfs_update_inode_block_group(trans, inode);
3589 btrfs_update_inode_block_group(trans, dir);
3590 if (drop_inode)
3591 goto out_unlock;
3592
3593 path = btrfs_alloc_path();
3594 BUG_ON(!path);
3595 key.objectid = inode->i_ino;
3596 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003597 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3598 datasize = btrfs_file_extent_calc_inline_size(name_len);
3599 err = btrfs_insert_empty_item(trans, root, path, &key,
3600 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003601 if (err) {
3602 drop_inode = 1;
3603 goto out_unlock;
3604 }
Chris Mason5f39d392007-10-15 16:14:19 -04003605 leaf = path->nodes[0];
3606 ei = btrfs_item_ptr(leaf, path->slots[0],
3607 struct btrfs_file_extent_item);
3608 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3609 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003610 BTRFS_FILE_EXTENT_INLINE);
3611 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003612 write_extent_buffer(leaf, symname, ptr, name_len);
3613 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003614 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003615
Chris Mason39279cc2007-06-12 06:35:45 -04003616 inode->i_op = &btrfs_symlink_inode_operations;
3617 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003618 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003619 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04003620 err = btrfs_update_inode(trans, root, inode);
3621 if (err)
3622 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003623
3624out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003625 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04003626 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003627out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003628 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04003629 if (drop_inode) {
3630 inode_dec_link_count(inode);
3631 iput(inode);
3632 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003633 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05003634 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003635 return err;
3636}
Chris Mason16432982008-04-10 10:23:21 -04003637
Yanfdebe2b2008-01-14 13:26:08 -05003638static int btrfs_permission(struct inode *inode, int mask,
3639 struct nameidata *nd)
3640{
3641 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3642 return -EACCES;
3643 return generic_permission(inode, mask, NULL);
3644}
Chris Mason39279cc2007-06-12 06:35:45 -04003645
3646static struct inode_operations btrfs_dir_inode_operations = {
3647 .lookup = btrfs_lookup,
3648 .create = btrfs_create,
3649 .unlink = btrfs_unlink,
3650 .link = btrfs_link,
3651 .mkdir = btrfs_mkdir,
3652 .rmdir = btrfs_rmdir,
3653 .rename = btrfs_rename,
3654 .symlink = btrfs_symlink,
3655 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003656 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003657 .setxattr = generic_setxattr,
3658 .getxattr = generic_getxattr,
3659 .listxattr = btrfs_listxattr,
3660 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003661 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003662};
Chris Mason39279cc2007-06-12 06:35:45 -04003663static struct inode_operations btrfs_dir_ro_inode_operations = {
3664 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003665 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003666};
Chris Mason39279cc2007-06-12 06:35:45 -04003667static struct file_operations btrfs_dir_file_operations = {
3668 .llseek = generic_file_llseek,
3669 .read = generic_read_dir,
3670 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003671 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003672#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003673 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003674#endif
3675};
3676
Chris Masond1310b22008-01-24 16:13:08 -05003677static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003678 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003679 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003680 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003681 .readpage_io_hook = btrfs_readpage_io_hook,
3682 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003683 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003684 .set_bit_hook = btrfs_set_bit_hook,
3685 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003686};
3687
Chris Mason39279cc2007-06-12 06:35:45 -04003688static struct address_space_operations btrfs_aops = {
3689 .readpage = btrfs_readpage,
3690 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04003691 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003692 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003693 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003694 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003695 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003696 .invalidatepage = btrfs_invalidatepage,
3697 .releasepage = btrfs_releasepage,
3698 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003699};
3700
3701static struct address_space_operations btrfs_symlink_aops = {
3702 .readpage = btrfs_readpage,
3703 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003704 .invalidatepage = btrfs_invalidatepage,
3705 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003706};
3707
3708static struct inode_operations btrfs_file_inode_operations = {
3709 .truncate = btrfs_truncate,
3710 .getattr = btrfs_getattr,
3711 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003712 .setxattr = generic_setxattr,
3713 .getxattr = generic_getxattr,
3714 .listxattr = btrfs_listxattr,
3715 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003716 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003717};
Josef Bacik618e21d2007-07-11 10:18:17 -04003718static struct inode_operations btrfs_special_inode_operations = {
3719 .getattr = btrfs_getattr,
3720 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003721 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003722};
Chris Mason39279cc2007-06-12 06:35:45 -04003723static struct inode_operations btrfs_symlink_inode_operations = {
3724 .readlink = generic_readlink,
3725 .follow_link = page_follow_link_light,
3726 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003727 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003728};