blob: e875c7c8a647c220ba2a5a34975dd0ca5995692e [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason065631f2008-02-20 12:07:25 -050019#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040020#include <linux/buffer_head.h>
21#include <linux/fs.h>
22#include <linux/pagemap.h>
23#include <linux/highmem.h>
24#include <linux/time.h>
25#include <linux/init.h>
26#include <linux/string.h>
27#include <linux/smp_lock.h>
28#include <linux/backing-dev.h>
29#include <linux/mpage.h>
30#include <linux/swap.h>
31#include <linux/writeback.h>
32#include <linux/statfs.h>
33#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040035#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040037#include "ctree.h"
38#include "disk-io.h"
39#include "transaction.h"
40#include "btrfs_inode.h"
41#include "ioctl.h"
42#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040043#include "volumes.h"
Chris Mason39279cc2007-06-12 06:35:45 -040044
45struct btrfs_iget_args {
46 u64 ino;
47 struct btrfs_root *root;
48};
49
50static struct inode_operations btrfs_dir_inode_operations;
51static struct inode_operations btrfs_symlink_inode_operations;
52static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040053static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040054static struct inode_operations btrfs_file_inode_operations;
55static struct address_space_operations btrfs_aops;
56static struct address_space_operations btrfs_symlink_aops;
57static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050058static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040059
60static struct kmem_cache *btrfs_inode_cachep;
61struct kmem_cache *btrfs_trans_handle_cachep;
62struct kmem_cache *btrfs_transaction_cachep;
63struct kmem_cache *btrfs_bit_radix_cachep;
64struct kmem_cache *btrfs_path_cachep;
65
66#define S_SHIFT 12
67static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
68 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
69 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
70 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
71 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
72 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
73 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
74 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
75};
76
Chris Mason1832a6d2007-12-21 16:27:21 -050077int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
78 int for_del)
79{
80 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
81 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
82 u64 thresh;
83 int ret = 0;
84
85 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050086 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050087 else
Chris Masonf9ef6602008-01-03 09:22:38 -050088 thresh = total * 85;
89
90 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050091
92 spin_lock(&root->fs_info->delalloc_lock);
93 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
94 ret = -ENOSPC;
95 spin_unlock(&root->fs_info->delalloc_lock);
96 return ret;
97}
98
Chris Masonbe20aa92007-12-17 20:14:01 -050099static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400100{
101 struct btrfs_root *root = BTRFS_I(inode)->root;
102 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400103 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400104 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500105 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400106 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500107 u64 orig_start = start;
108 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500109 struct btrfs_key ins;
110 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400111
Chris Masonb888db22007-08-27 16:49:44 -0400112 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400113 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500114 btrfs_set_trans_block_group(trans, inode);
115
Chris Masondb945352007-10-15 16:15:53 -0400116 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500117 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400118 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400119 start, start + num_bytes, start, &alloc_hint);
Chris Masond1310b22008-01-24 16:13:08 -0500120 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400121
Chris Mason179e29e2007-11-01 11:28:41 -0400122 if (alloc_hint == EXTENT_MAP_INLINE)
123 goto out;
124
Chris Mason3b951512008-04-17 11:29:12 -0400125 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
126
Chris Masonc59f8952007-12-17 20:14:04 -0500127 while(num_bytes > 0) {
128 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
129 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
Chris Mason98d20f62008-04-14 09:46:10 -0400130 root->sectorsize,
Chris Masonc59f8952007-12-17 20:14:04 -0500131 root->root_key.objectid,
132 trans->transid,
133 inode->i_ino, start, 0,
134 alloc_hint, (u64)-1, &ins, 1);
135 if (ret) {
136 WARN_ON(1);
137 goto out;
138 }
Chris Mason98d20f62008-04-14 09:46:10 -0400139 cur_alloc_size = ins.offset;
Chris Masonc59f8952007-12-17 20:14:04 -0500140 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
141 start, ins.objectid, ins.offset,
142 ins.offset);
Chris Mason90692182008-02-08 13:49:28 -0500143 inode->i_blocks += ins.offset >> 9;
Chris Mason5f564062008-01-22 16:47:59 -0500144 btrfs_check_file(root, inode);
Chris Mason3b951512008-04-17 11:29:12 -0400145 if (num_bytes < cur_alloc_size) {
146 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
147 cur_alloc_size);
148 break;
149 }
Chris Masonc59f8952007-12-17 20:14:04 -0500150 num_bytes -= cur_alloc_size;
151 alloc_hint = ins.objectid + ins.offset;
152 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400153 }
Chris Masond1310b22008-01-24 16:13:08 -0500154 btrfs_drop_extent_cache(inode, orig_start,
155 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500156 btrfs_add_ordered_inode(inode);
Chris Mason90692182008-02-08 13:49:28 -0500157 btrfs_update_inode(trans, root, inode);
Chris Masonb888db22007-08-27 16:49:44 -0400158out:
159 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500160 return ret;
161}
162
163static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
164{
165 u64 extent_start;
166 u64 extent_end;
167 u64 bytenr;
168 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500169 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500170 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500171 struct btrfs_root *root = BTRFS_I(inode)->root;
172 struct extent_buffer *leaf;
173 int found_type;
174 struct btrfs_path *path;
175 struct btrfs_file_extent_item *item;
176 int ret;
177 int err;
178 struct btrfs_key found_key;
179
Chris Masonc31f8832008-01-08 15:46:31 -0500180 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500181 path = btrfs_alloc_path();
182 BUG_ON(!path);
183again:
184 ret = btrfs_lookup_file_extent(NULL, root, path,
185 inode->i_ino, start, 0);
186 if (ret < 0) {
187 btrfs_free_path(path);
188 return ret;
189 }
190
191 cow_end = end;
192 if (ret != 0) {
193 if (path->slots[0] == 0)
194 goto not_found;
195 path->slots[0]--;
196 }
197
198 leaf = path->nodes[0];
199 item = btrfs_item_ptr(leaf, path->slots[0],
200 struct btrfs_file_extent_item);
201
202 /* are we inside the extent that was found? */
203 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
204 found_type = btrfs_key_type(&found_key);
205 if (found_key.objectid != inode->i_ino ||
206 found_type != BTRFS_EXTENT_DATA_KEY) {
207 goto not_found;
208 }
209
210 found_type = btrfs_file_extent_type(leaf, item);
211 extent_start = found_key.offset;
212 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500213 u64 extent_num_bytes;
214
215 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
216 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500217 err = 0;
218
Chris Mason1832a6d2007-12-21 16:27:21 -0500219 if (loops && start != extent_start)
220 goto not_found;
221
Chris Masonbe20aa92007-12-17 20:14:01 -0500222 if (start < extent_start || start >= extent_end)
223 goto not_found;
224
225 cow_end = min(end, extent_end - 1);
226 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
227 if (bytenr == 0)
228 goto not_found;
229
Chris Masonc31f8832008-01-08 15:46:31 -0500230 /*
231 * we may be called by the resizer, make sure we're inside
232 * the limits of the FS
233 */
234 if (bytenr + extent_num_bytes > total_fs_bytes)
235 goto not_found;
236
Chris Masonbe20aa92007-12-17 20:14:01 -0500237 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
238 goto not_found;
239 }
240
241 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500242 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500243 goto not_found;
244 }
245loop:
246 if (start > end) {
247 btrfs_free_path(path);
248 return 0;
249 }
250 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500251 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500252 goto again;
253
254not_found:
255 cow_file_range(inode, start, cow_end);
256 start = cow_end + 1;
257 goto loop;
258}
259
260static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
261{
262 struct btrfs_root *root = BTRFS_I(inode)->root;
263 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500264 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500265 if (btrfs_test_opt(root, NODATACOW) ||
266 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500267 ret = run_delalloc_nocow(inode, start, end);
268 else
269 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500270
Chris Masonb888db22007-08-27 16:49:44 -0400271 mutex_unlock(&root->fs_info->fs_mutex);
272 return ret;
273}
274
Chris Mason291d6732008-01-29 15:55:23 -0500275int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500276 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500277{
Chris Masonb0c68f82008-01-31 11:05:37 -0500278 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500279 struct btrfs_root *root = BTRFS_I(inode)->root;
280 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -0500281 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500282 root->fs_info->delalloc_bytes += end - start + 1;
283 spin_unlock(&root->fs_info->delalloc_lock);
284 }
285 return 0;
286}
287
288int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500289 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500290{
Chris Masonb0c68f82008-01-31 11:05:37 -0500291 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500292 struct btrfs_root *root = BTRFS_I(inode)->root;
293 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -0500294 if (end - start + 1 > root->fs_info->delalloc_bytes) {
295 printk("warning: delalloc account %Lu %Lu\n",
296 end - start + 1, root->fs_info->delalloc_bytes);
297 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500298 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500299 } else {
300 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500301 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500302 }
Chris Mason291d6732008-01-29 15:55:23 -0500303 spin_unlock(&root->fs_info->delalloc_lock);
304 }
305 return 0;
306}
307
Chris Mason239b14b2008-03-24 15:02:07 -0400308int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
309 size_t size, struct bio *bio)
310{
311 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
312 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400313 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400314 u64 length = 0;
315 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400316 int ret;
317
Chris Masonf2d8d742008-04-21 10:03:05 -0400318 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400319 map_tree = &root->fs_info->mapping_tree;
320 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400321 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400322 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400323
Chris Mason239b14b2008-03-24 15:02:07 -0400324 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400325 return 1;
326 }
327 return 0;
328}
329
Chris Mason44b8bd72008-04-16 11:14:51 -0400330int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400331 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500332{
Chris Mason065631f2008-02-20 12:07:25 -0500333 struct btrfs_root *root = BTRFS_I(inode)->root;
334 struct btrfs_trans_handle *trans;
335 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400336 char *sums = NULL;
337
338 ret = btrfs_csum_one_bio(root, bio, &sums);
339 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500340
Chris Mason44b8bd72008-04-16 11:14:51 -0400341 mutex_lock(&root->fs_info->fs_mutex);
342 trans = btrfs_start_transaction(root, 1);
Chris Masone0156402008-04-16 11:15:20 -0400343
Chris Mason44b8bd72008-04-16 11:14:51 -0400344 btrfs_set_trans_block_group(trans, inode);
Chris Masone0156402008-04-16 11:15:20 -0400345 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
346
Chris Mason44b8bd72008-04-16 11:14:51 -0400347 ret = btrfs_end_transaction(trans, root);
348 BUG_ON(ret);
349 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0156402008-04-16 11:15:20 -0400350
351 kfree(sums);
352
Chris Mason44b8bd72008-04-16 11:14:51 -0400353 return btrfs_map_bio(root, rw, bio, mirror_num);
354}
355
356int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
357 int mirror_num)
358{
359 struct btrfs_root *root = BTRFS_I(inode)->root;
360 int ret = 0;
361
Chris Mason22c59942008-04-09 16:28:12 -0400362 if (!(rw & (1 << BIO_RW))) {
363 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
364 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400365 goto mapit;
366 }
Chris Mason065631f2008-02-20 12:07:25 -0500367
368 if (btrfs_test_opt(root, NODATASUM) ||
Chris Mason0b86a832008-03-24 15:01:56 -0400369 btrfs_test_flag(inode, NODATASUM)) {
370 goto mapit;
371 }
Chris Mason065631f2008-02-20 12:07:25 -0500372
Chris Mason44b8bd72008-04-16 11:14:51 -0400373 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
374 inode, rw, bio, mirror_num,
375 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400376mapit:
Chris Masonf1885912008-04-09 16:28:12 -0400377 return btrfs_map_bio(root, rw, bio, mirror_num);
Chris Mason065631f2008-02-20 12:07:25 -0500378}
Chris Mason6885f302008-02-20 16:11:05 -0500379
Chris Mason07157aa2007-08-30 08:50:51 -0400380int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
381{
382 int ret = 0;
383 struct inode *inode = page->mapping->host;
384 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500385 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400386 struct btrfs_csum_item *item;
387 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400388 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400389
Yanb98b6762008-01-08 15:54:37 -0500390 if (btrfs_test_opt(root, NODATASUM) ||
391 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500392 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400393
Chris Mason07157aa2007-08-30 08:50:51 -0400394 mutex_lock(&root->fs_info->fs_mutex);
395 path = btrfs_alloc_path();
396 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
397 if (IS_ERR(item)) {
398 ret = PTR_ERR(item);
399 /* a csum that isn't present is a preallocated region. */
400 if (ret == -ENOENT || ret == -EFBIG)
401 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400402 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500403 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400404 goto out;
405 }
Chris Masonff79f812007-10-15 16:22:25 -0400406 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
407 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500408 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400409out:
410 if (path)
411 btrfs_free_path(path);
412 mutex_unlock(&root->fs_info->fs_mutex);
413 return ret;
414}
415
Chris Mason7e383262008-04-09 16:28:12 -0400416struct io_failure_record {
417 struct page *page;
418 u64 start;
419 u64 len;
420 u64 logical;
421 int last_mirror;
422};
423
424int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
425 struct page *page, u64 start, u64 end,
426 struct extent_state *state)
427{
428 struct io_failure_record *failrec = NULL;
429 u64 private;
430 struct extent_map *em;
431 struct inode *inode = page->mapping->host;
432 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400433 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400434 struct bio *bio;
435 int num_copies;
436 int ret;
437 u64 logical;
438
439 ret = get_state_private(failure_tree, start, &private);
440 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400441 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
442 if (!failrec)
443 return -ENOMEM;
444 failrec->start = start;
445 failrec->len = end - start + 1;
446 failrec->last_mirror = 0;
447
Chris Mason3b951512008-04-17 11:29:12 -0400448 spin_lock(&em_tree->lock);
449 em = lookup_extent_mapping(em_tree, start, failrec->len);
450 if (em->start > start || em->start + em->len < start) {
451 free_extent_map(em);
452 em = NULL;
453 }
454 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400455
456 if (!em || IS_ERR(em)) {
457 kfree(failrec);
458 return -EIO;
459 }
460 logical = start - em->start;
461 logical = em->block_start + logical;
462 failrec->logical = logical;
463 free_extent_map(em);
464 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
465 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400466 set_state_private(failure_tree, start,
467 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400468 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400469 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400470 }
471 num_copies = btrfs_num_copies(
472 &BTRFS_I(inode)->root->fs_info->mapping_tree,
473 failrec->logical, failrec->len);
474 failrec->last_mirror++;
475 if (!state) {
476 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
477 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
478 failrec->start,
479 EXTENT_LOCKED);
480 if (state && state->start != failrec->start)
481 state = NULL;
482 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
483 }
484 if (!state || failrec->last_mirror > num_copies) {
485 set_state_private(failure_tree, failrec->start, 0);
486 clear_extent_bits(failure_tree, failrec->start,
487 failrec->start + failrec->len - 1,
488 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
489 kfree(failrec);
490 return -EIO;
491 }
492 bio = bio_alloc(GFP_NOFS, 1);
493 bio->bi_private = state;
494 bio->bi_end_io = failed_bio->bi_end_io;
495 bio->bi_sector = failrec->logical >> 9;
496 bio->bi_bdev = failed_bio->bi_bdev;
497 bio_add_page(bio, page, failrec->len, start - page_offset(page));
498 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
499 return 0;
500}
501
Chris Mason70dec802008-01-29 09:59:12 -0500502int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
503 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400504{
Chris Mason35ebb932007-10-30 16:56:53 -0400505 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400506 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500507 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400508 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500509 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400510 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400511 struct btrfs_root *root = BTRFS_I(inode)->root;
512 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400513 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500514
Yanb98b6762008-01-08 15:54:37 -0500515 if (btrfs_test_opt(root, NODATASUM) ||
516 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500517 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500518 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500519 private = state->private;
520 ret = 0;
521 } else {
522 ret = get_state_private(io_tree, start, &private);
523 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400524 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400525 kaddr = kmap_atomic(page, KM_IRQ0);
526 if (ret) {
527 goto zeroit;
528 }
Chris Masonff79f812007-10-15 16:22:25 -0400529 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
530 btrfs_csum_final(csum, (char *)&csum);
531 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400532 goto zeroit;
533 }
534 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400535 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400536
537 /* if the io failure tree for this inode is non-empty,
538 * check to see if we've recovered from a failed IO
539 */
540 private = 0;
541 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
542 (u64)-1, 1, EXTENT_DIRTY)) {
543 u64 private_failure;
544 struct io_failure_record *failure;
545 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
546 start, &private_failure);
547 if (ret == 0) {
Chris Mason587f7702008-04-11 12:16:46 -0400548 failure = (struct io_failure_record *)(unsigned long)
549 private_failure;
Chris Mason7e383262008-04-09 16:28:12 -0400550 set_state_private(&BTRFS_I(inode)->io_failure_tree,
551 failure->start, 0);
552 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
553 failure->start,
554 failure->start + failure->len - 1,
555 EXTENT_DIRTY | EXTENT_LOCKED,
556 GFP_NOFS);
557 kfree(failure);
558 }
559 }
Chris Mason07157aa2007-08-30 08:50:51 -0400560 return 0;
561
562zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500563 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
564 page->mapping->host->i_ino, (unsigned long long)start, csum,
565 private);
Chris Masondb945352007-10-15 16:15:53 -0400566 memset(kaddr + offset, 1, end - start + 1);
567 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400568 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400569 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400570 if (private == 0)
571 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400572 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400573}
Chris Masonb888db22007-08-27 16:49:44 -0400574
Chris Mason39279cc2007-06-12 06:35:45 -0400575void btrfs_read_locked_inode(struct inode *inode)
576{
577 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400578 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400579 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400580 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400581 struct btrfs_root *root = BTRFS_I(inode)->root;
582 struct btrfs_key location;
583 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400584 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400585 int ret;
586
587 path = btrfs_alloc_path();
588 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400589 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400590 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500591
Chris Mason39279cc2007-06-12 06:35:45 -0400592 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400593 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400594 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400595
Chris Mason5f39d392007-10-15 16:14:19 -0400596 leaf = path->nodes[0];
597 inode_item = btrfs_item_ptr(leaf, path->slots[0],
598 struct btrfs_inode_item);
599
600 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
601 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
602 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
603 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
604 inode->i_size = btrfs_inode_size(leaf, inode_item);
605
606 tspec = btrfs_inode_atime(inode_item);
607 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
608 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
609
610 tspec = btrfs_inode_mtime(inode_item);
611 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
612 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
613
614 tspec = btrfs_inode_ctime(inode_item);
615 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
616 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
617
618 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
619 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400620 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400621 rdev = btrfs_inode_rdev(leaf, inode_item);
622
623 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400624 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
625 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500626 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500627 if (!BTRFS_I(inode)->block_group) {
628 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400629 NULL, 0,
630 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500631 }
Chris Mason39279cc2007-06-12 06:35:45 -0400632 btrfs_free_path(path);
633 inode_item = NULL;
634
635 mutex_unlock(&root->fs_info->fs_mutex);
636
637 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400638 case S_IFREG:
639 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400640 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500641 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400642 inode->i_fop = &btrfs_file_operations;
643 inode->i_op = &btrfs_file_inode_operations;
644 break;
645 case S_IFDIR:
646 inode->i_fop = &btrfs_dir_file_operations;
647 if (root == root->fs_info->tree_root)
648 inode->i_op = &btrfs_dir_ro_inode_operations;
649 else
650 inode->i_op = &btrfs_dir_inode_operations;
651 break;
652 case S_IFLNK:
653 inode->i_op = &btrfs_symlink_inode_operations;
654 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400655 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400656 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400657 default:
658 init_special_inode(inode, inode->i_mode, rdev);
659 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400660 }
661 return;
662
663make_bad:
664 btrfs_release_path(root, path);
665 btrfs_free_path(path);
666 mutex_unlock(&root->fs_info->fs_mutex);
667 make_bad_inode(inode);
668}
669
Chris Mason5f39d392007-10-15 16:14:19 -0400670static void fill_inode_item(struct extent_buffer *leaf,
671 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400672 struct inode *inode)
673{
Chris Mason5f39d392007-10-15 16:14:19 -0400674 btrfs_set_inode_uid(leaf, item, inode->i_uid);
675 btrfs_set_inode_gid(leaf, item, inode->i_gid);
676 btrfs_set_inode_size(leaf, item, inode->i_size);
677 btrfs_set_inode_mode(leaf, item, inode->i_mode);
678 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
679
680 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
681 inode->i_atime.tv_sec);
682 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
683 inode->i_atime.tv_nsec);
684
685 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
686 inode->i_mtime.tv_sec);
687 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
688 inode->i_mtime.tv_nsec);
689
690 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
691 inode->i_ctime.tv_sec);
692 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
693 inode->i_ctime.tv_nsec);
694
695 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
696 btrfs_set_inode_generation(leaf, item, inode->i_generation);
697 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500698 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400699 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400700 BTRFS_I(inode)->block_group->key.objectid);
701}
702
Chris Masona52d9a82007-08-27 16:49:44 -0400703int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400704 struct btrfs_root *root,
705 struct inode *inode)
706{
707 struct btrfs_inode_item *inode_item;
708 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400709 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400710 int ret;
711
712 path = btrfs_alloc_path();
713 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400714 ret = btrfs_lookup_inode(trans, root, path,
715 &BTRFS_I(inode)->location, 1);
716 if (ret) {
717 if (ret > 0)
718 ret = -ENOENT;
719 goto failed;
720 }
721
Chris Mason5f39d392007-10-15 16:14:19 -0400722 leaf = path->nodes[0];
723 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400724 struct btrfs_inode_item);
725
Chris Mason5f39d392007-10-15 16:14:19 -0400726 fill_inode_item(leaf, inode_item, inode);
727 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400728 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400729 ret = 0;
730failed:
731 btrfs_release_path(root, path);
732 btrfs_free_path(path);
733 return ret;
734}
735
736
737static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
738 struct btrfs_root *root,
739 struct inode *dir,
740 struct dentry *dentry)
741{
742 struct btrfs_path *path;
743 const char *name = dentry->d_name.name;
744 int name_len = dentry->d_name.len;
745 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400746 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400747 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400748 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400749
750 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400751 if (!path) {
752 ret = -ENOMEM;
753 goto err;
754 }
755
Chris Mason39279cc2007-06-12 06:35:45 -0400756 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
757 name, name_len, -1);
758 if (IS_ERR(di)) {
759 ret = PTR_ERR(di);
760 goto err;
761 }
762 if (!di) {
763 ret = -ENOENT;
764 goto err;
765 }
Chris Mason5f39d392007-10-15 16:14:19 -0400766 leaf = path->nodes[0];
767 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400768 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400769 if (ret)
770 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400771 btrfs_release_path(root, path);
772
773 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400774 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400775 if (IS_ERR(di)) {
776 ret = PTR_ERR(di);
777 goto err;
778 }
779 if (!di) {
780 ret = -ENOENT;
781 goto err;
782 }
783 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400784
785 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500786 ret = btrfs_del_inode_ref(trans, root, name, name_len,
787 dentry->d_inode->i_ino,
788 dentry->d_parent->d_inode->i_ino);
789 if (ret) {
790 printk("failed to delete reference to %.*s, "
791 "inode %lu parent %lu\n", name_len, name,
792 dentry->d_inode->i_ino,
793 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500794 }
Chris Mason39279cc2007-06-12 06:35:45 -0400795err:
796 btrfs_free_path(path);
797 if (!ret) {
798 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400799 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400800 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500801#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
802 dentry->d_inode->i_nlink--;
803#else
Chris Mason39279cc2007-06-12 06:35:45 -0400804 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500805#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400806 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400807 dir->i_sb->s_dirt = 1;
808 }
809 return ret;
810}
811
812static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
813{
814 struct btrfs_root *root;
815 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500816 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400817 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500818 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400819
820 root = BTRFS_I(dir)->root;
821 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500822
823 ret = btrfs_check_free_space(root, 1, 1);
824 if (ret)
825 goto fail;
826
Chris Mason39279cc2007-06-12 06:35:45 -0400827 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400828
Chris Mason39279cc2007-06-12 06:35:45 -0400829 btrfs_set_trans_block_group(trans, dir);
830 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400831 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400832
Chris Mason2da98f02008-01-16 11:44:43 -0500833 if (inode->i_nlink == 0) {
834 int found;
835 /* if the inode isn't linked anywhere,
836 * we don't need to worry about
837 * data=ordered
838 */
839 found = btrfs_del_ordered_inode(inode);
840 if (found == 1) {
841 atomic_dec(&inode->i_count);
842 }
843 }
844
Chris Mason39279cc2007-06-12 06:35:45 -0400845 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500846fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400847 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400848 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500849 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400850 return ret;
851}
852
853static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
854{
855 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500856 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400857 int ret;
858 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400859 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500860 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400861
Yan134d4512007-10-25 15:49:25 -0400862 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
863 return -ENOTEMPTY;
864
Chris Mason39279cc2007-06-12 06:35:45 -0400865 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500866 ret = btrfs_check_free_space(root, 1, 1);
867 if (ret)
868 goto fail;
869
Chris Mason39279cc2007-06-12 06:35:45 -0400870 trans = btrfs_start_transaction(root, 1);
871 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400872
873 /* now the directory is empty */
874 err = btrfs_unlink_trans(trans, root, dir, dentry);
875 if (!err) {
876 inode->i_size = 0;
877 }
Chris Mason39544012007-12-12 14:38:19 -0500878
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400879 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400880 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500881fail:
Yan134d4512007-10-25 15:49:25 -0400882 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400883 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500884 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500885
Chris Mason39279cc2007-06-12 06:35:45 -0400886 if (ret && !err)
887 err = ret;
888 return err;
889}
890
Chris Mason39279cc2007-06-12 06:35:45 -0400891/*
Chris Mason39279cc2007-06-12 06:35:45 -0400892 * this can truncate away extent items, csum items and directory items.
893 * It starts at a high offset and removes keys until it can't find
894 * any higher than i_size.
895 *
896 * csum items that cross the new i_size are truncated to the new size
897 * as well.
898 */
899static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
900 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500901 struct inode *inode,
902 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400903{
904 int ret;
905 struct btrfs_path *path;
906 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400907 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400908 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400909 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400910 struct btrfs_file_extent_item *fi;
911 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400912 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400913 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500914 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500915 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400916 int found_extent;
917 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500918 int pending_del_nr = 0;
919 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400920 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -0400921 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400922
Chris Mason3b951512008-04-17 11:29:12 -0400923 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400924 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400925 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400926 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400927
Chris Mason39279cc2007-06-12 06:35:45 -0400928 /* FIXME, add redo link to tree so we don't leak on crash */
929 key.objectid = inode->i_ino;
930 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400931 key.type = (u8)-1;
932
Chris Mason85e21ba2008-01-29 15:11:36 -0500933 btrfs_init_path(path);
934search_again:
935 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
936 if (ret < 0) {
937 goto error;
938 }
939 if (ret > 0) {
940 BUG_ON(path->slots[0] == 0);
941 path->slots[0]--;
942 }
943
Chris Mason39279cc2007-06-12 06:35:45 -0400944 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400945 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400946 leaf = path->nodes[0];
947 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
948 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400949
Chris Mason5f39d392007-10-15 16:14:19 -0400950 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400951 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400952
Chris Mason85e21ba2008-01-29 15:11:36 -0500953 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400954 break;
955
Chris Mason5f39d392007-10-15 16:14:19 -0400956 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400957 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400958 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400959 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400960 extent_type = btrfs_file_extent_type(leaf, fi);
961 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400962 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400963 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400964 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
965 struct btrfs_item *item = btrfs_item_nr(leaf,
966 path->slots[0]);
967 item_end += btrfs_file_extent_inline_len(leaf,
968 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400969 }
Yan008630c2007-11-07 13:31:09 -0500970 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400971 }
972 if (found_type == BTRFS_CSUM_ITEM_KEY) {
973 ret = btrfs_csum_truncate(trans, root, path,
974 inode->i_size);
975 BUG_ON(ret);
976 }
Yan008630c2007-11-07 13:31:09 -0500977 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400978 if (found_type == BTRFS_DIR_ITEM_KEY) {
979 found_type = BTRFS_INODE_ITEM_KEY;
980 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
981 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500982 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
983 found_type = BTRFS_XATTR_ITEM_KEY;
984 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
985 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400986 } else if (found_type) {
987 found_type--;
988 } else {
989 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400990 }
Yana61721d2007-09-17 11:08:38 -0400991 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500992 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400993 }
Chris Mason5f39d392007-10-15 16:14:19 -0400994 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400995 del_item = 1;
996 else
997 del_item = 0;
998 found_extent = 0;
999
1000 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001001 if (found_type != BTRFS_EXTENT_DATA_KEY)
1002 goto delete;
1003
1004 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001005 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001006 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001007 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001008 u64 orig_num_bytes =
1009 btrfs_file_extent_num_bytes(leaf, fi);
1010 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001011 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001012 extent_num_bytes = extent_num_bytes &
1013 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001014 btrfs_set_file_extent_num_bytes(leaf, fi,
1015 extent_num_bytes);
1016 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001017 extent_num_bytes);
1018 if (extent_start != 0)
1019 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001020 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001021 } else {
Chris Masondb945352007-10-15 16:15:53 -04001022 extent_num_bytes =
1023 btrfs_file_extent_disk_num_bytes(leaf,
1024 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001025 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001026 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001027 if (extent_start != 0) {
1028 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001029 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001030 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001031 root_gen = btrfs_header_generation(leaf);
1032 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001033 }
Chris Mason90692182008-02-08 13:49:28 -05001034 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1035 if (!del_item) {
1036 u32 newsize = inode->i_size - found_key.offset;
1037 dec_i_blocks(inode, item_end + 1 -
1038 found_key.offset - newsize);
1039 newsize =
1040 btrfs_file_extent_calc_inline_size(newsize);
1041 ret = btrfs_truncate_item(trans, root, path,
1042 newsize, 1);
1043 BUG_ON(ret);
1044 } else {
1045 dec_i_blocks(inode, item_end + 1 -
1046 found_key.offset);
1047 }
Chris Mason39279cc2007-06-12 06:35:45 -04001048 }
Chris Mason179e29e2007-11-01 11:28:41 -04001049delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001050 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001051 if (!pending_del_nr) {
1052 /* no pending yet, add ourselves */
1053 pending_del_slot = path->slots[0];
1054 pending_del_nr = 1;
1055 } else if (pending_del_nr &&
1056 path->slots[0] + 1 == pending_del_slot) {
1057 /* hop on the pending chunk */
1058 pending_del_nr++;
1059 pending_del_slot = path->slots[0];
1060 } else {
1061 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1062 }
Chris Mason39279cc2007-06-12 06:35:45 -04001063 } else {
1064 break;
1065 }
Chris Mason39279cc2007-06-12 06:35:45 -04001066 if (found_extent) {
1067 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001068 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001069 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001070 root_gen, inode->i_ino,
1071 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001072 BUG_ON(ret);
1073 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001074next:
1075 if (path->slots[0] == 0) {
1076 if (pending_del_nr)
1077 goto del_pending;
1078 btrfs_release_path(root, path);
1079 goto search_again;
1080 }
1081
1082 path->slots[0]--;
1083 if (pending_del_nr &&
1084 path->slots[0] + 1 != pending_del_slot) {
1085 struct btrfs_key debug;
1086del_pending:
1087 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1088 pending_del_slot);
1089 ret = btrfs_del_items(trans, root, path,
1090 pending_del_slot,
1091 pending_del_nr);
1092 BUG_ON(ret);
1093 pending_del_nr = 0;
1094 btrfs_release_path(root, path);
1095 goto search_again;
1096 }
Chris Mason39279cc2007-06-12 06:35:45 -04001097 }
1098 ret = 0;
1099error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001100 if (pending_del_nr) {
1101 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1102 pending_del_nr);
1103 }
Chris Mason39279cc2007-06-12 06:35:45 -04001104 btrfs_release_path(root, path);
1105 btrfs_free_path(path);
1106 inode->i_sb->s_dirt = 1;
1107 return ret;
1108}
1109
Chris Masonb888db22007-08-27 16:49:44 -04001110static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -04001111 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001112{
Chris Mason39279cc2007-06-12 06:35:45 -04001113 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -05001114 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001115 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001116 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -05001117 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001118
Chris Mason190662b2007-12-18 16:25:45 -05001119 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001120 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001121
Chris Masond1310b22008-01-24 16:13:08 -05001122 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001123 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -04001124 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -05001125
Chris Masona52d9a82007-08-27 16:49:44 -04001126 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -04001127 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001128 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1129 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -04001130 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001131 }
Chris Masonb888db22007-08-27 16:49:44 -04001132 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -05001133 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001134
Chris Masona52d9a82007-08-27 16:49:44 -04001135 return ret;
1136}
1137
1138/*
1139 * taken from block_truncate_page, but does cow as it zeros out
1140 * any bytes left in the last page in the file.
1141 */
1142static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1143{
1144 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001145 struct btrfs_root *root = BTRFS_I(inode)->root;
1146 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001147 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1148 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1149 struct page *page;
1150 int ret = 0;
1151 u64 page_start;
1152
1153 if ((offset & (blocksize - 1)) == 0)
1154 goto out;
1155
1156 ret = -ENOMEM;
1157 page = grab_cache_page(mapping, index);
1158 if (!page)
1159 goto out;
1160 if (!PageUptodate(page)) {
1161 ret = btrfs_readpage(NULL, page);
1162 lock_page(page);
1163 if (!PageUptodate(page)) {
1164 ret = -EIO;
1165 goto out;
1166 }
1167 }
Chris Mason35ebb932007-10-30 16:56:53 -04001168 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001169
Chris Masonb888db22007-08-27 16:49:44 -04001170 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001171
Chris Mason39279cc2007-06-12 06:35:45 -04001172 unlock_page(page);
1173 page_cache_release(page);
1174out:
1175 return ret;
1176}
1177
1178static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1179{
1180 struct inode *inode = dentry->d_inode;
1181 int err;
1182
1183 err = inode_change_ok(inode, attr);
1184 if (err)
1185 return err;
1186
1187 if (S_ISREG(inode->i_mode) &&
1188 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1189 struct btrfs_trans_handle *trans;
1190 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001191 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001192
Chris Mason5f39d392007-10-15 16:14:19 -04001193 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001194 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001195 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001196 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001197 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001198
Chris Mason1b0f7c22008-01-30 14:33:02 -05001199 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001200 goto out;
1201
Chris Mason1832a6d2007-12-21 16:27:21 -05001202 mutex_lock(&root->fs_info->fs_mutex);
1203 err = btrfs_check_free_space(root, 1, 0);
1204 mutex_unlock(&root->fs_info->fs_mutex);
1205 if (err)
1206 goto fail;
1207
Chris Mason39279cc2007-06-12 06:35:45 -04001208 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1209
Chris Mason1b0f7c22008-01-30 14:33:02 -05001210 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001211 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001212
1213 mutex_lock(&root->fs_info->fs_mutex);
1214 trans = btrfs_start_transaction(root, 1);
1215 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001216 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001217 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001218 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001219
Chris Mason179e29e2007-11-01 11:28:41 -04001220 if (alloc_hint != EXTENT_MAP_INLINE) {
1221 err = btrfs_insert_file_extent(trans, root,
1222 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001223 hole_start, 0, 0,
1224 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001225 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001226 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001227 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001228 }
Chris Mason39279cc2007-06-12 06:35:45 -04001229 btrfs_end_transaction(trans, root);
1230 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001231 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001232 if (err)
1233 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001234 }
1235out:
1236 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001237fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001238 return err;
1239}
Chris Mason61295eb2008-01-14 16:24:38 -05001240
Chris Mason2da98f02008-01-16 11:44:43 -05001241void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001242{
Chris Mason2da98f02008-01-16 11:44:43 -05001243 int ret;
1244
1245 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001246 return;
1247 }
Chris Mason2da98f02008-01-16 11:44:43 -05001248
1249 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1250 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1251 return;
1252
1253 ret = btrfs_del_ordered_inode(inode);
1254 if (ret == 1) {
1255 atomic_dec(&inode->i_count);
1256 }
Chris Mason61295eb2008-01-14 16:24:38 -05001257}
1258
Chris Mason39279cc2007-06-12 06:35:45 -04001259void btrfs_delete_inode(struct inode *inode)
1260{
1261 struct btrfs_trans_handle *trans;
1262 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001263 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001264 int ret;
1265
1266 truncate_inode_pages(&inode->i_data, 0);
1267 if (is_bad_inode(inode)) {
1268 goto no_delete;
1269 }
Chris Mason5f39d392007-10-15 16:14:19 -04001270
Chris Mason39279cc2007-06-12 06:35:45 -04001271 inode->i_size = 0;
1272 mutex_lock(&root->fs_info->fs_mutex);
1273 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001274
Chris Mason39279cc2007-06-12 06:35:45 -04001275 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001276 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001277 if (ret)
1278 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001279
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001280 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001281 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001282
Chris Mason39279cc2007-06-12 06:35:45 -04001283 btrfs_end_transaction(trans, root);
1284 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001285 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001286 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001287 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001288
1289no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001290 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001291 btrfs_end_transaction(trans, root);
1292 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001293 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001294 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001295no_delete:
1296 clear_inode(inode);
1297}
1298
1299/*
1300 * this returns the key found in the dir entry in the location pointer.
1301 * If no dir entries were found, location->objectid is 0.
1302 */
1303static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1304 struct btrfs_key *location)
1305{
1306 const char *name = dentry->d_name.name;
1307 int namelen = dentry->d_name.len;
1308 struct btrfs_dir_item *di;
1309 struct btrfs_path *path;
1310 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001311 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001312
Chris Mason39544012007-12-12 14:38:19 -05001313 if (namelen == 1 && strcmp(name, ".") == 0) {
1314 location->objectid = dir->i_ino;
1315 location->type = BTRFS_INODE_ITEM_KEY;
1316 location->offset = 0;
1317 return 0;
1318 }
Chris Mason39279cc2007-06-12 06:35:45 -04001319 path = btrfs_alloc_path();
1320 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001321
Chris Mason7a720532007-12-13 09:06:59 -05001322 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001323 struct btrfs_key key;
1324 struct extent_buffer *leaf;
1325 u32 nritems;
1326 int slot;
1327
1328 key.objectid = dir->i_ino;
1329 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1330 key.offset = 0;
1331 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1332 BUG_ON(ret == 0);
1333 ret = 0;
1334
1335 leaf = path->nodes[0];
1336 slot = path->slots[0];
1337 nritems = btrfs_header_nritems(leaf);
1338 if (slot >= nritems)
1339 goto out_err;
1340
1341 btrfs_item_key_to_cpu(leaf, &key, slot);
1342 if (key.objectid != dir->i_ino ||
1343 key.type != BTRFS_INODE_REF_KEY) {
1344 goto out_err;
1345 }
1346 location->objectid = key.offset;
1347 location->type = BTRFS_INODE_ITEM_KEY;
1348 location->offset = 0;
1349 goto out;
1350 }
1351
Chris Mason39279cc2007-06-12 06:35:45 -04001352 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1353 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001354 if (IS_ERR(di))
1355 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001356 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001357 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001358 }
Chris Mason5f39d392007-10-15 16:14:19 -04001359 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001360out:
Chris Mason39279cc2007-06-12 06:35:45 -04001361 btrfs_free_path(path);
1362 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001363out_err:
1364 location->objectid = 0;
1365 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001366}
1367
1368/*
1369 * when we hit a tree root in a directory, the btrfs part of the inode
1370 * needs to be changed to reflect the root directory of the tree root. This
1371 * is kind of like crossing a mount point.
1372 */
1373static int fixup_tree_root_location(struct btrfs_root *root,
1374 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001375 struct btrfs_root **sub_root,
1376 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001377{
1378 struct btrfs_path *path;
1379 struct btrfs_root_item *ri;
1380
1381 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1382 return 0;
1383 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1384 return 0;
1385
1386 path = btrfs_alloc_path();
1387 BUG_ON(!path);
1388 mutex_lock(&root->fs_info->fs_mutex);
1389
Josef Bacik58176a92007-08-29 15:47:34 -04001390 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1391 dentry->d_name.name,
1392 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001393 if (IS_ERR(*sub_root))
1394 return PTR_ERR(*sub_root);
1395
1396 ri = &(*sub_root)->root_item;
1397 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001398 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1399 location->offset = 0;
1400
1401 btrfs_free_path(path);
1402 mutex_unlock(&root->fs_info->fs_mutex);
1403 return 0;
1404}
1405
1406static int btrfs_init_locked_inode(struct inode *inode, void *p)
1407{
1408 struct btrfs_iget_args *args = p;
1409 inode->i_ino = args->ino;
1410 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001411 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001412 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1413 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001414 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001415 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1416 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001417 return 0;
1418}
1419
1420static int btrfs_find_actor(struct inode *inode, void *opaque)
1421{
1422 struct btrfs_iget_args *args = opaque;
1423 return (args->ino == inode->i_ino &&
1424 args->root == BTRFS_I(inode)->root);
1425}
1426
Chris Masondc17ff82008-01-08 15:46:30 -05001427struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1428 u64 root_objectid)
1429{
1430 struct btrfs_iget_args args;
1431 args.ino = objectid;
1432 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1433
1434 if (!args.root)
1435 return NULL;
1436
1437 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1438}
1439
Chris Mason39279cc2007-06-12 06:35:45 -04001440struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1441 struct btrfs_root *root)
1442{
1443 struct inode *inode;
1444 struct btrfs_iget_args args;
1445 args.ino = objectid;
1446 args.root = root;
1447
1448 inode = iget5_locked(s, objectid, btrfs_find_actor,
1449 btrfs_init_locked_inode,
1450 (void *)&args);
1451 return inode;
1452}
1453
1454static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1455 struct nameidata *nd)
1456{
1457 struct inode * inode;
1458 struct btrfs_inode *bi = BTRFS_I(dir);
1459 struct btrfs_root *root = bi->root;
1460 struct btrfs_root *sub_root = root;
1461 struct btrfs_key location;
1462 int ret;
1463
1464 if (dentry->d_name.len > BTRFS_NAME_LEN)
1465 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001466
Chris Mason39279cc2007-06-12 06:35:45 -04001467 mutex_lock(&root->fs_info->fs_mutex);
1468 ret = btrfs_inode_by_name(dir, dentry, &location);
1469 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001470
Chris Mason39279cc2007-06-12 06:35:45 -04001471 if (ret < 0)
1472 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001473
Chris Mason39279cc2007-06-12 06:35:45 -04001474 inode = NULL;
1475 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001476 ret = fixup_tree_root_location(root, &location, &sub_root,
1477 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001478 if (ret < 0)
1479 return ERR_PTR(ret);
1480 if (ret > 0)
1481 return ERR_PTR(-ENOENT);
1482 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1483 sub_root);
1484 if (!inode)
1485 return ERR_PTR(-EACCES);
1486 if (inode->i_state & I_NEW) {
1487 /* the inode and parent dir are two different roots */
1488 if (sub_root != root) {
1489 igrab(inode);
1490 sub_root->inode = inode;
1491 }
1492 BTRFS_I(inode)->root = sub_root;
1493 memcpy(&BTRFS_I(inode)->location, &location,
1494 sizeof(location));
1495 btrfs_read_locked_inode(inode);
1496 unlock_new_inode(inode);
1497 }
1498 }
1499 return d_splice_alias(inode, dentry);
1500}
1501
Chris Mason39279cc2007-06-12 06:35:45 -04001502static unsigned char btrfs_filetype_table[] = {
1503 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1504};
1505
1506static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1507{
Chris Mason6da6aba2007-12-18 16:15:09 -05001508 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001509 struct btrfs_root *root = BTRFS_I(inode)->root;
1510 struct btrfs_item *item;
1511 struct btrfs_dir_item *di;
1512 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001513 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001514 struct btrfs_path *path;
1515 int ret;
1516 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001517 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001518 int slot;
1519 int advance;
1520 unsigned char d_type;
1521 int over = 0;
1522 u32 di_cur;
1523 u32 di_total;
1524 u32 di_len;
1525 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001526 char tmp_name[32];
1527 char *name_ptr;
1528 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001529
1530 /* FIXME, use a real flag for deciding about the key type */
1531 if (root->fs_info->tree_root == root)
1532 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001533
Chris Mason39544012007-12-12 14:38:19 -05001534 /* special case for "." */
1535 if (filp->f_pos == 0) {
1536 over = filldir(dirent, ".", 1,
1537 1, inode->i_ino,
1538 DT_DIR);
1539 if (over)
1540 return 0;
1541 filp->f_pos = 1;
1542 }
1543
Chris Mason39279cc2007-06-12 06:35:45 -04001544 mutex_lock(&root->fs_info->fs_mutex);
1545 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001546 path = btrfs_alloc_path();
1547 path->reada = 2;
1548
1549 /* special case for .., just use the back ref */
1550 if (filp->f_pos == 1) {
1551 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1552 key.offset = 0;
1553 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1554 BUG_ON(ret == 0);
1555 leaf = path->nodes[0];
1556 slot = path->slots[0];
1557 nritems = btrfs_header_nritems(leaf);
1558 if (slot >= nritems) {
1559 btrfs_release_path(root, path);
1560 goto read_dir_items;
1561 }
1562 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1563 btrfs_release_path(root, path);
1564 if (found_key.objectid != key.objectid ||
1565 found_key.type != BTRFS_INODE_REF_KEY)
1566 goto read_dir_items;
1567 over = filldir(dirent, "..", 2,
1568 2, found_key.offset, DT_DIR);
1569 if (over)
1570 goto nopos;
1571 filp->f_pos = 2;
1572 }
1573
1574read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001575 btrfs_set_key_type(&key, key_type);
1576 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001577
Chris Mason39279cc2007-06-12 06:35:45 -04001578 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1579 if (ret < 0)
1580 goto err;
1581 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001582 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001583 leaf = path->nodes[0];
1584 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001585 slot = path->slots[0];
1586 if (advance || slot >= nritems) {
1587 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001588 ret = btrfs_next_leaf(root, path);
1589 if (ret)
1590 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001591 leaf = path->nodes[0];
1592 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001593 slot = path->slots[0];
1594 } else {
1595 slot++;
1596 path->slots[0]++;
1597 }
1598 }
1599 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001600 item = btrfs_item_nr(leaf, slot);
1601 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1602
1603 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001604 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001605 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001606 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001607 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001608 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001609
1610 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001611 advance = 1;
1612 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1613 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001614 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001615 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001616 struct btrfs_key location;
1617
1618 name_len = btrfs_dir_name_len(leaf, di);
1619 if (name_len < 32) {
1620 name_ptr = tmp_name;
1621 } else {
1622 name_ptr = kmalloc(name_len, GFP_NOFS);
1623 BUG_ON(!name_ptr);
1624 }
1625 read_extent_buffer(leaf, name_ptr,
1626 (unsigned long)(di + 1), name_len);
1627
1628 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1629 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001630 over = filldir(dirent, name_ptr, name_len,
1631 found_key.offset,
1632 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001633 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001634
1635 if (name_ptr != tmp_name)
1636 kfree(name_ptr);
1637
Chris Mason39279cc2007-06-12 06:35:45 -04001638 if (over)
1639 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001640 di_len = btrfs_dir_name_len(leaf, di) +
1641 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001642 di_cur += di_len;
1643 di = (struct btrfs_dir_item *)((char *)di + di_len);
1644 }
1645 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001646 if (key_type == BTRFS_DIR_INDEX_KEY)
1647 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1648 else
1649 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001650nopos:
1651 ret = 0;
1652err:
1653 btrfs_release_path(root, path);
1654 btrfs_free_path(path);
1655 mutex_unlock(&root->fs_info->fs_mutex);
1656 return ret;
1657}
1658
1659int btrfs_write_inode(struct inode *inode, int wait)
1660{
1661 struct btrfs_root *root = BTRFS_I(inode)->root;
1662 struct btrfs_trans_handle *trans;
1663 int ret = 0;
1664
1665 if (wait) {
1666 mutex_lock(&root->fs_info->fs_mutex);
1667 trans = btrfs_start_transaction(root, 1);
1668 btrfs_set_trans_block_group(trans, inode);
1669 ret = btrfs_commit_transaction(trans, root);
1670 mutex_unlock(&root->fs_info->fs_mutex);
1671 }
1672 return ret;
1673}
1674
1675/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001676 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001677 * inode changes. But, it is most likely to find the inode in cache.
1678 * FIXME, needs more benchmarking...there are no reasons other than performance
1679 * to keep or drop this code.
1680 */
1681void btrfs_dirty_inode(struct inode *inode)
1682{
1683 struct btrfs_root *root = BTRFS_I(inode)->root;
1684 struct btrfs_trans_handle *trans;
1685
1686 mutex_lock(&root->fs_info->fs_mutex);
1687 trans = btrfs_start_transaction(root, 1);
1688 btrfs_set_trans_block_group(trans, inode);
1689 btrfs_update_inode(trans, root, inode);
1690 btrfs_end_transaction(trans, root);
1691 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001692}
1693
1694static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1695 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001696 const char *name, int name_len,
1697 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001698 u64 objectid,
1699 struct btrfs_block_group_cache *group,
1700 int mode)
1701{
1702 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001703 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001704 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001705 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001706 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001707 struct btrfs_inode_ref *ref;
1708 struct btrfs_key key[2];
1709 u32 sizes[2];
1710 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001711 int ret;
1712 int owner;
1713
Chris Mason5f39d392007-10-15 16:14:19 -04001714 path = btrfs_alloc_path();
1715 BUG_ON(!path);
1716
Chris Mason39279cc2007-06-12 06:35:45 -04001717 inode = new_inode(root->fs_info->sb);
1718 if (!inode)
1719 return ERR_PTR(-ENOMEM);
1720
Chris Masond1310b22008-01-24 16:13:08 -05001721 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1722 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001723 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001724 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1725 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001726 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001727 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001728
Chris Mason39279cc2007-06-12 06:35:45 -04001729 if (mode & S_IFDIR)
1730 owner = 0;
1731 else
1732 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001733 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001734 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001735 if (!new_inode_group) {
1736 printk("find_block group failed\n");
1737 new_inode_group = group;
1738 }
1739 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001740 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001741
1742 key[0].objectid = objectid;
1743 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1744 key[0].offset = 0;
1745
1746 key[1].objectid = objectid;
1747 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1748 key[1].offset = ref_objectid;
1749
1750 sizes[0] = sizeof(struct btrfs_inode_item);
1751 sizes[1] = name_len + sizeof(*ref);
1752
1753 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1754 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001755 goto fail;
1756
Chris Mason9c583092008-01-29 15:15:18 -05001757 if (objectid > root->highest_inode)
1758 root->highest_inode = objectid;
1759
Chris Mason39279cc2007-06-12 06:35:45 -04001760 inode->i_uid = current->fsuid;
1761 inode->i_gid = current->fsgid;
1762 inode->i_mode = mode;
1763 inode->i_ino = objectid;
1764 inode->i_blocks = 0;
1765 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001766 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1767 struct btrfs_inode_item);
1768 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001769
1770 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1771 struct btrfs_inode_ref);
1772 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1773 ptr = (unsigned long)(ref + 1);
1774 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1775
Chris Mason5f39d392007-10-15 16:14:19 -04001776 btrfs_mark_buffer_dirty(path->nodes[0]);
1777 btrfs_free_path(path);
1778
Chris Mason39279cc2007-06-12 06:35:45 -04001779 location = &BTRFS_I(inode)->location;
1780 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001781 location->offset = 0;
1782 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1783
Chris Mason39279cc2007-06-12 06:35:45 -04001784 insert_inode_hash(inode);
1785 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001786fail:
1787 btrfs_free_path(path);
1788 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001789}
1790
1791static inline u8 btrfs_inode_type(struct inode *inode)
1792{
1793 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1794}
1795
1796static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001797 struct dentry *dentry, struct inode *inode,
1798 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001799{
1800 int ret;
1801 struct btrfs_key key;
1802 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001803 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001804
Chris Mason39279cc2007-06-12 06:35:45 -04001805 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001806 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1807 key.offset = 0;
1808
1809 ret = btrfs_insert_dir_item(trans, root,
1810 dentry->d_name.name, dentry->d_name.len,
1811 dentry->d_parent->d_inode->i_ino,
1812 &key, btrfs_inode_type(inode));
1813 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001814 if (add_backref) {
1815 ret = btrfs_insert_inode_ref(trans, root,
1816 dentry->d_name.name,
1817 dentry->d_name.len,
1818 inode->i_ino,
1819 dentry->d_parent->d_inode->i_ino);
1820 }
Chris Mason79c44582007-06-25 10:09:33 -04001821 parent_inode = dentry->d_parent->d_inode;
1822 parent_inode->i_size += dentry->d_name.len * 2;
1823 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001824 ret = btrfs_update_inode(trans, root,
1825 dentry->d_parent->d_inode);
1826 }
1827 return ret;
1828}
1829
1830static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001831 struct dentry *dentry, struct inode *inode,
1832 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001833{
Chris Mason9c583092008-01-29 15:15:18 -05001834 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001835 if (!err) {
1836 d_instantiate(dentry, inode);
1837 return 0;
1838 }
1839 if (err > 0)
1840 err = -EEXIST;
1841 return err;
1842}
1843
Josef Bacik618e21d2007-07-11 10:18:17 -04001844static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1845 int mode, dev_t rdev)
1846{
1847 struct btrfs_trans_handle *trans;
1848 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001849 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001850 int err;
1851 int drop_inode = 0;
1852 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001853 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001854
1855 if (!new_valid_dev(rdev))
1856 return -EINVAL;
1857
1858 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001859 err = btrfs_check_free_space(root, 1, 0);
1860 if (err)
1861 goto fail;
1862
Josef Bacik618e21d2007-07-11 10:18:17 -04001863 trans = btrfs_start_transaction(root, 1);
1864 btrfs_set_trans_block_group(trans, dir);
1865
1866 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1867 if (err) {
1868 err = -ENOSPC;
1869 goto out_unlock;
1870 }
1871
Chris Mason9c583092008-01-29 15:15:18 -05001872 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1873 dentry->d_name.len,
1874 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001875 BTRFS_I(dir)->block_group, mode);
1876 err = PTR_ERR(inode);
1877 if (IS_ERR(inode))
1878 goto out_unlock;
1879
1880 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001881 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001882 if (err)
1883 drop_inode = 1;
1884 else {
1885 inode->i_op = &btrfs_special_inode_operations;
1886 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001887 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001888 }
1889 dir->i_sb->s_dirt = 1;
1890 btrfs_update_inode_block_group(trans, inode);
1891 btrfs_update_inode_block_group(trans, dir);
1892out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001893 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001894 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001895fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001896 mutex_unlock(&root->fs_info->fs_mutex);
1897
1898 if (drop_inode) {
1899 inode_dec_link_count(inode);
1900 iput(inode);
1901 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001902 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001903 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001904 return err;
1905}
1906
Chris Mason39279cc2007-06-12 06:35:45 -04001907static int btrfs_create(struct inode *dir, struct dentry *dentry,
1908 int mode, struct nameidata *nd)
1909{
1910 struct btrfs_trans_handle *trans;
1911 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001912 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001913 int err;
1914 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001915 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001916 u64 objectid;
1917
1918 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001919 err = btrfs_check_free_space(root, 1, 0);
1920 if (err)
1921 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001922 trans = btrfs_start_transaction(root, 1);
1923 btrfs_set_trans_block_group(trans, dir);
1924
1925 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1926 if (err) {
1927 err = -ENOSPC;
1928 goto out_unlock;
1929 }
1930
Chris Mason9c583092008-01-29 15:15:18 -05001931 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1932 dentry->d_name.len,
1933 dentry->d_parent->d_inode->i_ino,
1934 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001935 err = PTR_ERR(inode);
1936 if (IS_ERR(inode))
1937 goto out_unlock;
1938
1939 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001940 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001941 if (err)
1942 drop_inode = 1;
1943 else {
1944 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001945 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001946 inode->i_fop = &btrfs_file_operations;
1947 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001948 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1949 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001950 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001951 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1952 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001953 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001954 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001955 }
1956 dir->i_sb->s_dirt = 1;
1957 btrfs_update_inode_block_group(trans, inode);
1958 btrfs_update_inode_block_group(trans, dir);
1959out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001960 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001961 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001962fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001963 mutex_unlock(&root->fs_info->fs_mutex);
1964
1965 if (drop_inode) {
1966 inode_dec_link_count(inode);
1967 iput(inode);
1968 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001969 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001970 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001971 return err;
1972}
1973
1974static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1975 struct dentry *dentry)
1976{
1977 struct btrfs_trans_handle *trans;
1978 struct btrfs_root *root = BTRFS_I(dir)->root;
1979 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001980 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001981 int err;
1982 int drop_inode = 0;
1983
1984 if (inode->i_nlink == 0)
1985 return -ENOENT;
1986
Chris Mason6da6aba2007-12-18 16:15:09 -05001987#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1988 inode->i_nlink++;
1989#else
Chris Mason39279cc2007-06-12 06:35:45 -04001990 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001991#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001992 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001993 err = btrfs_check_free_space(root, 1, 0);
1994 if (err)
1995 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001996 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001997
Chris Mason39279cc2007-06-12 06:35:45 -04001998 btrfs_set_trans_block_group(trans, dir);
1999 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05002000 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002001
Chris Mason39279cc2007-06-12 06:35:45 -04002002 if (err)
2003 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002004
Chris Mason39279cc2007-06-12 06:35:45 -04002005 dir->i_sb->s_dirt = 1;
2006 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002007 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002008
Chris Mason54aa1f42007-06-22 14:16:25 -04002009 if (err)
2010 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002011
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002012 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002013 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002014fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002015 mutex_unlock(&root->fs_info->fs_mutex);
2016
2017 if (drop_inode) {
2018 inode_dec_link_count(inode);
2019 iput(inode);
2020 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002021 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002022 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002023 return err;
2024}
2025
Chris Mason39279cc2007-06-12 06:35:45 -04002026static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2027{
2028 struct inode *inode;
2029 struct btrfs_trans_handle *trans;
2030 struct btrfs_root *root = BTRFS_I(dir)->root;
2031 int err = 0;
2032 int drop_on_err = 0;
2033 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002034 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002035
2036 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002037 err = btrfs_check_free_space(root, 1, 0);
2038 if (err)
2039 goto out_unlock;
2040
Chris Mason39279cc2007-06-12 06:35:45 -04002041 trans = btrfs_start_transaction(root, 1);
2042 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002043
Chris Mason39279cc2007-06-12 06:35:45 -04002044 if (IS_ERR(trans)) {
2045 err = PTR_ERR(trans);
2046 goto out_unlock;
2047 }
2048
2049 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2050 if (err) {
2051 err = -ENOSPC;
2052 goto out_unlock;
2053 }
2054
Chris Mason9c583092008-01-29 15:15:18 -05002055 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2056 dentry->d_name.len,
2057 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002058 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2059 if (IS_ERR(inode)) {
2060 err = PTR_ERR(inode);
2061 goto out_fail;
2062 }
Chris Mason5f39d392007-10-15 16:14:19 -04002063
Chris Mason39279cc2007-06-12 06:35:45 -04002064 drop_on_err = 1;
2065 inode->i_op = &btrfs_dir_inode_operations;
2066 inode->i_fop = &btrfs_dir_file_operations;
2067 btrfs_set_trans_block_group(trans, inode);
2068
Chris Mason39544012007-12-12 14:38:19 -05002069 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002070 err = btrfs_update_inode(trans, root, inode);
2071 if (err)
2072 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002073
Chris Mason9c583092008-01-29 15:15:18 -05002074 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002075 if (err)
2076 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002077
Chris Mason39279cc2007-06-12 06:35:45 -04002078 d_instantiate(dentry, inode);
2079 drop_on_err = 0;
2080 dir->i_sb->s_dirt = 1;
2081 btrfs_update_inode_block_group(trans, inode);
2082 btrfs_update_inode_block_group(trans, dir);
2083
2084out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002085 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002086 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002087
Chris Mason39279cc2007-06-12 06:35:45 -04002088out_unlock:
2089 mutex_unlock(&root->fs_info->fs_mutex);
2090 if (drop_on_err)
2091 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002092 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002093 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002094 return err;
2095}
2096
Chris Mason3b951512008-04-17 11:29:12 -04002097static int merge_extent_mapping(struct extent_map_tree *em_tree,
2098 struct extent_map *existing,
2099 struct extent_map *em)
2100{
2101 u64 start_diff;
2102 u64 new_end;
2103 int ret = 0;
2104 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2105
2106 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2107 goto invalid;
2108
2109 if (!real_blocks && em->block_start != existing->block_start)
2110 goto invalid;
2111
2112 new_end = max(existing->start + existing->len, em->start + em->len);
2113
2114 if (existing->start >= em->start) {
2115 if (em->start + em->len < existing->start)
2116 goto invalid;
2117
2118 start_diff = existing->start - em->start;
2119 if (real_blocks && em->block_start + start_diff !=
2120 existing->block_start)
2121 goto invalid;
2122
2123 em->len = new_end - em->start;
2124
2125 remove_extent_mapping(em_tree, existing);
2126 /* free for the tree */
2127 free_extent_map(existing);
2128 ret = add_extent_mapping(em_tree, em);
2129
2130 } else if (em->start > existing->start) {
2131
2132 if (existing->start + existing->len < em->start)
2133 goto invalid;
2134
2135 start_diff = em->start - existing->start;
2136 if (real_blocks && existing->block_start + start_diff !=
2137 em->block_start)
2138 goto invalid;
2139
2140 remove_extent_mapping(em_tree, existing);
2141 em->block_start = existing->block_start;
2142 em->start = existing->start;
2143 em->len = new_end - existing->start;
2144 free_extent_map(existing);
2145
2146 ret = add_extent_mapping(em_tree, em);
2147 } else {
2148 goto invalid;
2149 }
2150 return ret;
2151
2152invalid:
2153 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2154 existing->start, existing->len, existing->block_start,
2155 em->start, em->len, em->block_start);
2156 return -EIO;
2157}
2158
Chris Masona52d9a82007-08-27 16:49:44 -04002159struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002160 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002161 int create)
2162{
2163 int ret;
2164 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002165 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002166 u64 extent_start = 0;
2167 u64 extent_end = 0;
2168 u64 objectid = inode->i_ino;
2169 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002170 struct btrfs_path *path;
2171 struct btrfs_root *root = BTRFS_I(inode)->root;
2172 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002173 struct extent_buffer *leaf;
2174 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002175 struct extent_map *em = NULL;
2176 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002177 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002178 struct btrfs_trans_handle *trans = NULL;
2179
2180 path = btrfs_alloc_path();
2181 BUG_ON(!path);
2182 mutex_lock(&root->fs_info->fs_mutex);
2183
2184again:
Chris Masond1310b22008-01-24 16:13:08 -05002185 spin_lock(&em_tree->lock);
2186 em = lookup_extent_mapping(em_tree, start, len);
2187 spin_unlock(&em_tree->lock);
2188
Chris Masona52d9a82007-08-27 16:49:44 -04002189 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05002190 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05002191 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
2192 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05002193 WARN_ON(1);
2194 }
Chris Mason70dec802008-01-29 09:59:12 -05002195 if (em->block_start == EXTENT_MAP_INLINE && page)
2196 free_extent_map(em);
2197 else
2198 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002199 }
Chris Masond1310b22008-01-24 16:13:08 -05002200 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002201 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002202 err = -ENOMEM;
2203 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002204 }
Chris Masond1310b22008-01-24 16:13:08 -05002205
2206 em->start = EXTENT_MAP_HOLE;
2207 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04002208 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04002209 ret = btrfs_lookup_file_extent(trans, root, path,
2210 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002211 if (ret < 0) {
2212 err = ret;
2213 goto out;
2214 }
2215
2216 if (ret != 0) {
2217 if (path->slots[0] == 0)
2218 goto not_found;
2219 path->slots[0]--;
2220 }
2221
Chris Mason5f39d392007-10-15 16:14:19 -04002222 leaf = path->nodes[0];
2223 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002224 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002225 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002226 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2227 found_type = btrfs_key_type(&found_key);
2228 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002229 found_type != BTRFS_EXTENT_DATA_KEY) {
2230 goto not_found;
2231 }
2232
Chris Mason5f39d392007-10-15 16:14:19 -04002233 found_type = btrfs_file_extent_type(leaf, item);
2234 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002235 if (found_type == BTRFS_FILE_EXTENT_REG) {
2236 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002237 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002238 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002239 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002240 em->start = start;
2241 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002242 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002243 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002244 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002245 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002246 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002247 }
2248 goto not_found_em;
2249 }
Chris Masondb945352007-10-15 16:15:53 -04002250 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2251 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002252 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002253 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002254 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002255 goto insert;
2256 }
Chris Masondb945352007-10-15 16:15:53 -04002257 bytenr += btrfs_file_extent_offset(leaf, item);
2258 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002259 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002260 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002261 goto insert;
2262 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002263 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002264 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002265 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002266 size_t size;
2267 size_t extent_offset;
2268 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002269
Chris Mason5f39d392007-10-15 16:14:19 -04002270 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2271 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002272 extent_end = (extent_start + size + root->sectorsize - 1) &
2273 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-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 Masonb888db22007-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 }
2285 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002286
2287 if (!page) {
2288 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002289 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002290 goto out;
2291 }
2292
Chris Mason70dec802008-01-29 09:59:12 -05002293 page_start = page_offset(page) + pg_offset;
2294 extent_offset = page_start - extent_start;
2295 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002296 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002297 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002298 em->len = (copy_size + root->sectorsize - 1) &
2299 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002300 map = kmap(page);
2301 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002302 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002303 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002304 copy_size);
2305 flush_dcache_page(page);
2306 } else if (create && PageUptodate(page)) {
2307 if (!trans) {
2308 kunmap(page);
2309 free_extent_map(em);
2310 em = NULL;
2311 btrfs_release_path(root, path);
2312 trans = btrfs_start_transaction(root, 1);
2313 goto again;
2314 }
Chris Mason70dec802008-01-29 09:59:12 -05002315 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002316 copy_size);
2317 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002318 }
Chris Masona52d9a82007-08-27 16:49:44 -04002319 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002320 set_extent_uptodate(io_tree, em->start,
2321 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002322 goto insert;
2323 } else {
2324 printk("unkknown found_type %d\n", found_type);
2325 WARN_ON(1);
2326 }
2327not_found:
2328 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002329 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002330not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002331 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002332insert:
2333 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002334 if (em->start > start || extent_map_end(em) <= start) {
2335 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002336 err = -EIO;
2337 goto out;
2338 }
Chris Masond1310b22008-01-24 16:13:08 -05002339
2340 err = 0;
2341 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002342 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002343
2344 /* it is possible that someone inserted the extent into the tree
2345 * while we had the lock dropped. It is also possible that
2346 * an overlapping map exists in the tree
2347 */
Chris Masona52d9a82007-08-27 16:49:44 -04002348 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002349 struct extent_map *existing;
2350 existing = lookup_extent_mapping(em_tree, start, len);
2351 if (!existing) {
2352 existing = lookup_extent_mapping(em_tree, em->start,
2353 em->len);
2354 if (existing) {
2355 err = merge_extent_mapping(em_tree, existing,
2356 em);
2357 free_extent_map(existing);
2358 if (err) {
2359 free_extent_map(em);
2360 em = NULL;
2361 }
2362 } else {
2363 err = -EIO;
2364 printk("failing to insert %Lu %Lu\n",
2365 start, len);
2366 free_extent_map(em);
2367 em = NULL;
2368 }
2369 } else {
2370 free_extent_map(em);
2371 em = existing;
Chris Masona52d9a82007-08-27 16:49:44 -04002372 }
Chris Masona52d9a82007-08-27 16:49:44 -04002373 }
Chris Masond1310b22008-01-24 16:13:08 -05002374 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002375out:
2376 btrfs_free_path(path);
2377 if (trans) {
2378 ret = btrfs_end_transaction(trans, root);
2379 if (!err)
2380 err = ret;
2381 }
2382 mutex_unlock(&root->fs_info->fs_mutex);
2383 if (err) {
2384 free_extent_map(em);
2385 WARN_ON(1);
2386 return ERR_PTR(err);
2387 }
2388 return em;
2389}
2390
Chris Mason16432982008-04-10 10:23:21 -04002391static int btrfs_get_block(struct inode *inode, sector_t iblock,
2392 struct buffer_head *bh_result, int create)
2393{
2394 struct extent_map *em;
2395 u64 start = (u64)iblock << inode->i_blkbits;
2396 struct btrfs_multi_bio *multi = NULL;
2397 struct btrfs_root *root = BTRFS_I(inode)->root;
2398 u64 len;
2399 u64 logical;
2400 u64 map_length;
2401 int ret = 0;
2402
2403 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2404
2405 if (!em || IS_ERR(em))
2406 goto out;
2407
2408 if (em->start > start || em->start + em->len <= start)
2409 goto out;
2410
2411 if (em->block_start == EXTENT_MAP_INLINE) {
2412 ret = -EINVAL;
2413 goto out;
2414 }
2415
2416 if (em->block_start == EXTENT_MAP_HOLE ||
2417 em->block_start == EXTENT_MAP_DELALLOC) {
2418 goto out;
2419 }
2420
2421 len = em->start + em->len - start;
2422 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2423
2424 logical = start - em->start;
2425 logical = em->block_start + logical;
2426
2427 map_length = len;
2428 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2429 logical, &map_length, &multi, 0);
2430 BUG_ON(ret);
2431 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2432 bh_result->b_size = min(map_length, len);
2433 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2434 set_buffer_mapped(bh_result);
2435 kfree(multi);
2436out:
2437 free_extent_map(em);
2438 return ret;
2439}
2440
2441static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2442 const struct iovec *iov, loff_t offset,
2443 unsigned long nr_segs)
2444{
2445 struct file *file = iocb->ki_filp;
2446 struct inode *inode = file->f_mapping->host;
2447
2448 if (rw == WRITE)
2449 return -EINVAL;
2450
2451 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2452 offset, nr_segs, btrfs_get_block, NULL);
2453}
2454
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002455static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002456{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002457 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002458}
2459
Chris Mason9ebefb182007-06-15 13:50:00 -04002460int btrfs_readpage(struct file *file, struct page *page)
2461{
Chris Masond1310b22008-01-24 16:13:08 -05002462 struct extent_io_tree *tree;
2463 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002464 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002465}
Chris Mason1832a6d2007-12-21 16:27:21 -05002466
Chris Mason39279cc2007-06-12 06:35:45 -04002467static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2468{
Chris Masond1310b22008-01-24 16:13:08 -05002469 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002470
2471
2472 if (current->flags & PF_MEMALLOC) {
2473 redirty_page_for_writepage(wbc, page);
2474 unlock_page(page);
2475 return 0;
2476 }
Chris Masond1310b22008-01-24 16:13:08 -05002477 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002478 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2479}
Chris Mason39279cc2007-06-12 06:35:45 -04002480
Chris Masonb293f022007-11-01 19:45:34 -04002481static int btrfs_writepages(struct address_space *mapping,
2482 struct writeback_control *wbc)
2483{
Chris Masond1310b22008-01-24 16:13:08 -05002484 struct extent_io_tree *tree;
2485 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002486 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2487}
2488
Chris Mason3ab2fb52007-11-08 10:59:22 -05002489static int
2490btrfs_readpages(struct file *file, struct address_space *mapping,
2491 struct list_head *pages, unsigned nr_pages)
2492{
Chris Masond1310b22008-01-24 16:13:08 -05002493 struct extent_io_tree *tree;
2494 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002495 return extent_readpages(tree, mapping, pages, nr_pages,
2496 btrfs_get_extent);
2497}
2498
Chris Mason70dec802008-01-29 09:59:12 -05002499static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002500{
Chris Masond1310b22008-01-24 16:13:08 -05002501 struct extent_io_tree *tree;
2502 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002503 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002504
Chris Masond1310b22008-01-24 16:13:08 -05002505 tree = &BTRFS_I(page->mapping->host)->io_tree;
2506 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002507 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002508 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002509 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002510 ClearPagePrivate(page);
2511 set_page_private(page, 0);
2512 page_cache_release(page);
2513 }
2514 return ret;
2515}
Chris Mason39279cc2007-06-12 06:35:45 -04002516
Chris Masona52d9a82007-08-27 16:49:44 -04002517static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2518{
Chris Masond1310b22008-01-24 16:13:08 -05002519 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002520
Chris Masond1310b22008-01-24 16:13:08 -05002521 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002522 extent_invalidatepage(tree, page, offset);
2523 btrfs_releasepage(page, GFP_NOFS);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002524 if (PagePrivate(page)) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002525 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002526 ClearPagePrivate(page);
2527 set_page_private(page, 0);
2528 page_cache_release(page);
2529 }
Chris Mason39279cc2007-06-12 06:35:45 -04002530}
2531
Chris Mason9ebefb182007-06-15 13:50:00 -04002532/*
2533 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2534 * called from a page fault handler when a page is first dirtied. Hence we must
2535 * be careful to check for EOF conditions here. We set the page up correctly
2536 * for a written page which means we get ENOSPC checking when writing into
2537 * holes and correct delalloc and unwritten extent mapping on filesystems that
2538 * support these features.
2539 *
2540 * We are not allowed to take the i_mutex here so we have to play games to
2541 * protect against truncate races as the page could now be beyond EOF. Because
2542 * vmtruncate() writes the inode size before removing pages, once we have the
2543 * page lock we can determine safely if the page is beyond EOF. If it is not
2544 * beyond EOF, then the page is guaranteed safe against truncation until we
2545 * unlock the page.
2546 */
2547int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2548{
Chris Mason6da6aba2007-12-18 16:15:09 -05002549 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002550 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002551 unsigned long end;
2552 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002553 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002554 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002555
Chris Mason1832a6d2007-12-21 16:27:21 -05002556 mutex_lock(&root->fs_info->fs_mutex);
2557 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002558 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002559 if (ret)
2560 goto out;
2561
2562 ret = -EINVAL;
2563
Chris Mason9ebefb182007-06-15 13:50:00 -04002564 lock_page(page);
2565 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002566 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002567 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002568
Chris Mason9ebefb182007-06-15 13:50:00 -04002569 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002570 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002571 /* page got truncated out from underneath us */
2572 goto out_unlock;
2573 }
2574
2575 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002576 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002577 end = size & ~PAGE_CACHE_MASK;
2578 else
2579 end = PAGE_CACHE_SIZE;
2580
Chris Masonb888db22007-08-27 16:49:44 -04002581 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002582
2583out_unlock:
2584 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002585out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002586 return ret;
2587}
2588
Chris Mason39279cc2007-06-12 06:35:45 -04002589static void btrfs_truncate(struct inode *inode)
2590{
2591 struct btrfs_root *root = BTRFS_I(inode)->root;
2592 int ret;
2593 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002594 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002595
2596 if (!S_ISREG(inode->i_mode))
2597 return;
2598 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2599 return;
2600
2601 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2602
2603 mutex_lock(&root->fs_info->fs_mutex);
2604 trans = btrfs_start_transaction(root, 1);
2605 btrfs_set_trans_block_group(trans, inode);
2606
2607 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002608 ret = btrfs_truncate_in_trans(trans, root, inode,
2609 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002610 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002611 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002612
Chris Mason39279cc2007-06-12 06:35:45 -04002613 ret = btrfs_end_transaction(trans, root);
2614 BUG_ON(ret);
2615 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002616 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002617 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002618}
2619
Chris Mason4313b392008-01-03 09:08:48 -05002620static int noinline create_subvol(struct btrfs_root *root, char *name,
2621 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002622{
2623 struct btrfs_trans_handle *trans;
2624 struct btrfs_key key;
2625 struct btrfs_root_item root_item;
2626 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002627 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002628 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002629 struct inode *inode;
2630 struct inode *dir;
2631 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002632 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002633 u64 objectid;
2634 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002635 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002636
2637 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002638 ret = btrfs_check_free_space(root, 1, 0);
2639 if (ret)
2640 goto fail_commit;
2641
Chris Mason39279cc2007-06-12 06:35:45 -04002642 trans = btrfs_start_transaction(root, 1);
2643 BUG_ON(!trans);
2644
Chris Mason7bb86312007-12-11 09:25:06 -05002645 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2646 0, &objectid);
2647 if (ret)
2648 goto fail;
2649
2650 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2651 objectid, trans->transid, 0, 0,
2652 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002653 if (IS_ERR(leaf))
2654 return PTR_ERR(leaf);
2655
2656 btrfs_set_header_nritems(leaf, 0);
2657 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002658 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002659 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002660 btrfs_set_header_owner(leaf, objectid);
2661
Chris Mason5f39d392007-10-15 16:14:19 -04002662 write_extent_buffer(leaf, root->fs_info->fsid,
2663 (unsigned long)btrfs_header_fsid(leaf),
2664 BTRFS_FSID_SIZE);
2665 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002666
2667 inode_item = &root_item.inode;
2668 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002669 inode_item->generation = cpu_to_le64(1);
2670 inode_item->size = cpu_to_le64(3);
2671 inode_item->nlink = cpu_to_le32(1);
2672 inode_item->nblocks = cpu_to_le64(1);
2673 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002674
Chris Masondb945352007-10-15 16:15:53 -04002675 btrfs_set_root_bytenr(&root_item, leaf->start);
2676 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002677 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002678 btrfs_set_root_used(&root_item, 0);
2679
Chris Mason5eda7b52007-06-22 14:16:25 -04002680 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2681 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002682
2683 free_extent_buffer(leaf);
2684 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002685
Chris Mason39279cc2007-06-12 06:35:45 -04002686 btrfs_set_root_dirid(&root_item, new_dirid);
2687
2688 key.objectid = objectid;
2689 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002690 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2691 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2692 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002693 if (ret)
2694 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002695
2696 /*
2697 * insert the directory item
2698 */
2699 key.offset = (u64)-1;
2700 dir = root->fs_info->sb->s_root->d_inode;
2701 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2702 name, namelen, dir->i_ino, &key,
2703 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002704 if (ret)
2705 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002706
Chris Mason39544012007-12-12 14:38:19 -05002707 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2708 name, namelen, objectid,
2709 root->fs_info->sb->s_root->d_inode->i_ino);
2710 if (ret)
2711 goto fail;
2712
Chris Mason39279cc2007-06-12 06:35:45 -04002713 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002714 if (ret)
2715 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002716
Josef Bacik58176a92007-08-29 15:47:34 -04002717 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002718 BUG_ON(!new_root);
2719
2720 trans = btrfs_start_transaction(new_root, 1);
2721 BUG_ON(!trans);
2722
Chris Mason9c583092008-01-29 15:15:18 -05002723 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2724 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002725 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002726 if (IS_ERR(inode))
2727 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002728 inode->i_op = &btrfs_dir_inode_operations;
2729 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002730 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002731
Chris Mason39544012007-12-12 14:38:19 -05002732 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2733 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002734 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002735 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002736 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002737 if (ret)
2738 goto fail;
2739fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002740 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002741 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002742 if (err && !ret)
2743 ret = err;
2744fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002745 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002746 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002747 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002748 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002749}
2750
2751static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2752{
Chris Mason3063d292008-01-08 15:46:30 -05002753 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002754 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002755 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002756 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002757 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002758
2759 if (!root->ref_cows)
2760 return -EINVAL;
2761
2762 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002763 ret = btrfs_check_free_space(root, 1, 0);
2764 if (ret)
2765 goto fail_unlock;
2766
Chris Mason3063d292008-01-08 15:46:30 -05002767 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2768 if (!pending_snapshot) {
2769 ret = -ENOMEM;
2770 goto fail_unlock;
2771 }
Yanfb4bc1e2008-01-17 11:59:51 -05002772 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002773 if (!pending_snapshot->name) {
2774 ret = -ENOMEM;
2775 kfree(pending_snapshot);
2776 goto fail_unlock;
2777 }
Yanfb4bc1e2008-01-17 11:59:51 -05002778 memcpy(pending_snapshot->name, name, namelen);
2779 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002780 trans = btrfs_start_transaction(root, 1);
2781 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002782 pending_snapshot->root = root;
2783 list_add(&pending_snapshot->list,
2784 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002785 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002786 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002787
Chris Mason1832a6d2007-12-21 16:27:21 -05002788fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002789 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002790 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002791 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002792 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002793}
2794
Chris Masonedbd8d42007-12-21 16:27:24 -05002795unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002796 struct file_ra_state *ra, struct file *file,
2797 pgoff_t offset, pgoff_t last_index)
2798{
2799 pgoff_t req_size;
2800
2801#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2802 req_size = last_index - offset + 1;
2803 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2804 return offset;
2805#else
2806 req_size = min(last_index - offset + 1, (pgoff_t)128);
2807 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2808 return offset + req_size;
2809#endif
2810}
2811
2812int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002813 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002814 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002815 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002816 struct page *page;
2817 unsigned long last_index;
2818 unsigned long ra_index = 0;
2819 u64 page_start;
2820 u64 page_end;
2821 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002822 int ret;
2823
2824 mutex_lock(&root->fs_info->fs_mutex);
2825 ret = btrfs_check_free_space(root, inode->i_size, 0);
2826 mutex_unlock(&root->fs_info->fs_mutex);
2827 if (ret)
2828 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002829
2830 mutex_lock(&inode->i_mutex);
2831 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2832 for (i = 0; i <= last_index; i++) {
2833 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002834 ra_index = btrfs_force_ra(inode->i_mapping,
2835 &file->f_ra,
2836 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002837 }
2838 page = grab_cache_page(inode->i_mapping, i);
2839 if (!page)
2840 goto out_unlock;
2841 if (!PageUptodate(page)) {
2842 btrfs_readpage(NULL, page);
2843 lock_page(page);
2844 if (!PageUptodate(page)) {
2845 unlock_page(page);
2846 page_cache_release(page);
2847 goto out_unlock;
2848 }
2849 }
Chris Mason35ebb932007-10-30 16:56:53 -04002850 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002851 page_end = page_start + PAGE_CACHE_SIZE - 1;
2852
Chris Masond1310b22008-01-24 16:13:08 -05002853 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002854 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002855 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002856
Chris Masond1310b22008-01-24 16:13:08 -05002857 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002858 set_page_dirty(page);
2859 unlock_page(page);
2860 page_cache_release(page);
2861 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2862 }
2863
2864out_unlock:
2865 mutex_unlock(&inode->i_mutex);
2866 return 0;
2867}
2868
Chris Masonedbd8d42007-12-21 16:27:24 -05002869static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2870{
2871 u64 new_size;
2872 u64 old_size;
2873 struct btrfs_ioctl_vol_args *vol_args;
2874 struct btrfs_trans_handle *trans;
2875 char *sizestr;
2876 int ret = 0;
2877 int namelen;
2878 int mod = 0;
2879
2880 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2881
2882 if (!vol_args)
2883 return -ENOMEM;
2884
2885 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2886 ret = -EFAULT;
2887 goto out;
2888 }
2889 namelen = strlen(vol_args->name);
2890 if (namelen > BTRFS_VOL_NAME_MAX) {
2891 ret = -EINVAL;
2892 goto out;
2893 }
2894
2895 sizestr = vol_args->name;
2896 if (!strcmp(sizestr, "max"))
2897 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2898 else {
2899 if (sizestr[0] == '-') {
2900 mod = -1;
2901 sizestr++;
2902 } else if (sizestr[0] == '+') {
2903 mod = 1;
2904 sizestr++;
2905 }
2906 new_size = btrfs_parse_size(sizestr);
2907 if (new_size == 0) {
2908 ret = -EINVAL;
2909 goto out;
2910 }
2911 }
2912
2913 mutex_lock(&root->fs_info->fs_mutex);
2914 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2915
2916 if (mod < 0) {
2917 if (new_size > old_size) {
2918 ret = -EINVAL;
2919 goto out_unlock;
2920 }
2921 new_size = old_size - new_size;
2922 } else if (mod > 0) {
2923 new_size = old_size + new_size;
2924 }
2925
2926 if (new_size < 256 * 1024 * 1024) {
2927 ret = -EINVAL;
2928 goto out_unlock;
2929 }
2930 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2931 ret = -EFBIG;
2932 goto out_unlock;
2933 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002934
2935 do_div(new_size, root->sectorsize);
2936 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002937
2938printk("new size is %Lu\n", new_size);
2939 if (new_size > old_size) {
2940 trans = btrfs_start_transaction(root, 1);
2941 ret = btrfs_grow_extent_tree(trans, root, new_size);
2942 btrfs_commit_transaction(trans, root);
2943 } else {
2944 ret = btrfs_shrink_extent_tree(root, new_size);
2945 }
2946
2947out_unlock:
2948 mutex_unlock(&root->fs_info->fs_mutex);
2949out:
2950 kfree(vol_args);
2951 return ret;
2952}
2953
Chris Mason4313b392008-01-03 09:08:48 -05002954static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2955 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002956{
Chris Mason4aec2b52007-12-18 16:25:45 -05002957 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002958 struct btrfs_dir_item *di;
2959 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002960 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002961 int namelen;
2962 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002963
Chris Mason4aec2b52007-12-18 16:25:45 -05002964 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002965
Chris Mason4aec2b52007-12-18 16:25:45 -05002966 if (!vol_args)
2967 return -ENOMEM;
2968
2969 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2970 ret = -EFAULT;
2971 goto out;
2972 }
2973
2974 namelen = strlen(vol_args->name);
2975 if (namelen > BTRFS_VOL_NAME_MAX) {
2976 ret = -EINVAL;
2977 goto out;
2978 }
2979 if (strchr(vol_args->name, '/')) {
2980 ret = -EINVAL;
2981 goto out;
2982 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002983
2984 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002985 if (!path) {
2986 ret = -ENOMEM;
2987 goto out;
2988 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002989
2990 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2991 mutex_lock(&root->fs_info->fs_mutex);
2992 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2993 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002994 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002995 mutex_unlock(&root->fs_info->fs_mutex);
2996 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002997
2998 if (di && !IS_ERR(di)) {
2999 ret = -EEXIST;
3000 goto out;
3001 }
3002
3003 if (IS_ERR(di)) {
3004 ret = PTR_ERR(di);
3005 goto out;
3006 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003007
3008 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05003009 ret = create_subvol(root, vol_args->name, namelen);
3010 else
3011 ret = create_snapshot(root, vol_args->name, namelen);
3012out:
3013 kfree(vol_args);
3014 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003015}
3016
3017static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04003018{
Chris Mason6da6aba2007-12-18 16:15:09 -05003019 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003020 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003021
3022 switch (inode->i_mode & S_IFMT) {
3023 case S_IFDIR:
3024 mutex_lock(&root->fs_info->fs_mutex);
3025 btrfs_defrag_root(root, 0);
3026 btrfs_defrag_root(root->fs_info->extent_root, 0);
3027 mutex_unlock(&root->fs_info->fs_mutex);
3028 break;
3029 case S_IFREG:
3030 btrfs_defrag_file(file);
3031 break;
3032 }
3033
3034 return 0;
3035}
3036
3037long btrfs_ioctl(struct file *file, unsigned int
3038 cmd, unsigned long arg)
3039{
Chris Mason6da6aba2007-12-18 16:15:09 -05003040 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003041
3042 switch (cmd) {
3043 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003044 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04003045 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003046 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05003047 case BTRFS_IOC_RESIZE:
3048 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04003049 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003050
3051 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04003052}
3053
Chris Mason39279cc2007-06-12 06:35:45 -04003054/*
3055 * Called inside transaction, so use GFP_NOFS
3056 */
3057struct inode *btrfs_alloc_inode(struct super_block *sb)
3058{
3059 struct btrfs_inode *ei;
3060
3061 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3062 if (!ei)
3063 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003064 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05003065 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003066 return &ei->vfs_inode;
3067}
3068
3069void btrfs_destroy_inode(struct inode *inode)
3070{
3071 WARN_ON(!list_empty(&inode->i_dentry));
3072 WARN_ON(inode->i_data.nrpages);
3073
Chris Mason8c416c92008-01-14 15:10:26 -05003074 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003075 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3076}
3077
Chris Mason44ec0b72007-10-29 10:55:05 -04003078#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3079static void init_once(struct kmem_cache * cachep, void *foo)
3080#else
Chris Mason39279cc2007-06-12 06:35:45 -04003081static void init_once(void * foo, struct kmem_cache * cachep,
3082 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003083#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003084{
3085 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3086
3087 inode_init_once(&ei->vfs_inode);
3088}
3089
3090void btrfs_destroy_cachep(void)
3091{
3092 if (btrfs_inode_cachep)
3093 kmem_cache_destroy(btrfs_inode_cachep);
3094 if (btrfs_trans_handle_cachep)
3095 kmem_cache_destroy(btrfs_trans_handle_cachep);
3096 if (btrfs_transaction_cachep)
3097 kmem_cache_destroy(btrfs_transaction_cachep);
3098 if (btrfs_bit_radix_cachep)
3099 kmem_cache_destroy(btrfs_bit_radix_cachep);
3100 if (btrfs_path_cachep)
3101 kmem_cache_destroy(btrfs_path_cachep);
3102}
3103
Chris Mason86479a02007-09-10 19:58:16 -04003104struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003105 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04003106#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3107 void (*ctor)(struct kmem_cache *, void *)
3108#else
Chris Mason92fee662007-07-25 12:31:35 -04003109 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003110 unsigned long)
3111#endif
3112 )
Chris Mason92fee662007-07-25 12:31:35 -04003113{
3114 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3115 SLAB_MEM_SPREAD | extra_flags), ctor
3116#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3117 ,NULL
3118#endif
3119 );
3120}
3121
Chris Mason39279cc2007-06-12 06:35:45 -04003122int btrfs_init_cachep(void)
3123{
Chris Mason86479a02007-09-10 19:58:16 -04003124 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003125 sizeof(struct btrfs_inode),
3126 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003127 if (!btrfs_inode_cachep)
3128 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003129 btrfs_trans_handle_cachep =
3130 btrfs_cache_create("btrfs_trans_handle_cache",
3131 sizeof(struct btrfs_trans_handle),
3132 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003133 if (!btrfs_trans_handle_cachep)
3134 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003135 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003136 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003137 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003138 if (!btrfs_transaction_cachep)
3139 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003140 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003141 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003142 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003143 if (!btrfs_path_cachep)
3144 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003145 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003146 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003147 if (!btrfs_bit_radix_cachep)
3148 goto fail;
3149 return 0;
3150fail:
3151 btrfs_destroy_cachep();
3152 return -ENOMEM;
3153}
3154
3155static int btrfs_getattr(struct vfsmount *mnt,
3156 struct dentry *dentry, struct kstat *stat)
3157{
3158 struct inode *inode = dentry->d_inode;
3159 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003160 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003161 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003162 return 0;
3163}
3164
3165static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3166 struct inode * new_dir,struct dentry *new_dentry)
3167{
3168 struct btrfs_trans_handle *trans;
3169 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3170 struct inode *new_inode = new_dentry->d_inode;
3171 struct inode *old_inode = old_dentry->d_inode;
3172 struct timespec ctime = CURRENT_TIME;
3173 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003174 int ret;
3175
3176 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3177 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3178 return -ENOTEMPTY;
3179 }
Chris Mason5f39d392007-10-15 16:14:19 -04003180
Chris Mason39279cc2007-06-12 06:35:45 -04003181 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003182 ret = btrfs_check_free_space(root, 1, 0);
3183 if (ret)
3184 goto out_unlock;
3185
Chris Mason39279cc2007-06-12 06:35:45 -04003186 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003187
Chris Mason39279cc2007-06-12 06:35:45 -04003188 btrfs_set_trans_block_group(trans, new_dir);
3189 path = btrfs_alloc_path();
3190 if (!path) {
3191 ret = -ENOMEM;
3192 goto out_fail;
3193 }
3194
3195 old_dentry->d_inode->i_nlink++;
3196 old_dir->i_ctime = old_dir->i_mtime = ctime;
3197 new_dir->i_ctime = new_dir->i_mtime = ctime;
3198 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003199
Chris Mason39279cc2007-06-12 06:35:45 -04003200 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3201 if (ret)
3202 goto out_fail;
3203
3204 if (new_inode) {
3205 new_inode->i_ctime = CURRENT_TIME;
3206 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3207 if (ret)
3208 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003209 }
Chris Mason9c583092008-01-29 15:15:18 -05003210 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003211 if (ret)
3212 goto out_fail;
3213
3214out_fail:
3215 btrfs_free_path(path);
3216 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003217out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003218 mutex_unlock(&root->fs_info->fs_mutex);
3219 return ret;
3220}
3221
3222static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3223 const char *symname)
3224{
3225 struct btrfs_trans_handle *trans;
3226 struct btrfs_root *root = BTRFS_I(dir)->root;
3227 struct btrfs_path *path;
3228 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003229 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003230 int err;
3231 int drop_inode = 0;
3232 u64 objectid;
3233 int name_len;
3234 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003235 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003236 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003237 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003238 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003239
3240 name_len = strlen(symname) + 1;
3241 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3242 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003243
Chris Mason39279cc2007-06-12 06:35:45 -04003244 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003245 err = btrfs_check_free_space(root, 1, 0);
3246 if (err)
3247 goto out_fail;
3248
Chris Mason39279cc2007-06-12 06:35:45 -04003249 trans = btrfs_start_transaction(root, 1);
3250 btrfs_set_trans_block_group(trans, dir);
3251
3252 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3253 if (err) {
3254 err = -ENOSPC;
3255 goto out_unlock;
3256 }
3257
Chris Mason9c583092008-01-29 15:15:18 -05003258 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3259 dentry->d_name.len,
3260 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003261 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3262 err = PTR_ERR(inode);
3263 if (IS_ERR(inode))
3264 goto out_unlock;
3265
3266 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003267 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003268 if (err)
3269 drop_inode = 1;
3270 else {
3271 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003272 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003273 inode->i_fop = &btrfs_file_operations;
3274 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003275 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3276 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003277 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003278 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3279 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05003280 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003281 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003282 }
3283 dir->i_sb->s_dirt = 1;
3284 btrfs_update_inode_block_group(trans, inode);
3285 btrfs_update_inode_block_group(trans, dir);
3286 if (drop_inode)
3287 goto out_unlock;
3288
3289 path = btrfs_alloc_path();
3290 BUG_ON(!path);
3291 key.objectid = inode->i_ino;
3292 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003293 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3294 datasize = btrfs_file_extent_calc_inline_size(name_len);
3295 err = btrfs_insert_empty_item(trans, root, path, &key,
3296 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003297 if (err) {
3298 drop_inode = 1;
3299 goto out_unlock;
3300 }
Chris Mason5f39d392007-10-15 16:14:19 -04003301 leaf = path->nodes[0];
3302 ei = btrfs_item_ptr(leaf, path->slots[0],
3303 struct btrfs_file_extent_item);
3304 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3305 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003306 BTRFS_FILE_EXTENT_INLINE);
3307 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003308 write_extent_buffer(leaf, symname, ptr, name_len);
3309 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003310 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003311
Chris Mason39279cc2007-06-12 06:35:45 -04003312 inode->i_op = &btrfs_symlink_inode_operations;
3313 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003314 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003315 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04003316 err = btrfs_update_inode(trans, root, inode);
3317 if (err)
3318 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003319
3320out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003321 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04003322 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003323out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003324 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04003325 if (drop_inode) {
3326 inode_dec_link_count(inode);
3327 iput(inode);
3328 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003329 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05003330 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003331 return err;
3332}
Chris Mason16432982008-04-10 10:23:21 -04003333
Yanfdebe2b2008-01-14 13:26:08 -05003334static int btrfs_permission(struct inode *inode, int mask,
3335 struct nameidata *nd)
3336{
3337 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3338 return -EACCES;
3339 return generic_permission(inode, mask, NULL);
3340}
Chris Mason39279cc2007-06-12 06:35:45 -04003341
3342static struct inode_operations btrfs_dir_inode_operations = {
3343 .lookup = btrfs_lookup,
3344 .create = btrfs_create,
3345 .unlink = btrfs_unlink,
3346 .link = btrfs_link,
3347 .mkdir = btrfs_mkdir,
3348 .rmdir = btrfs_rmdir,
3349 .rename = btrfs_rename,
3350 .symlink = btrfs_symlink,
3351 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003352 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003353 .setxattr = generic_setxattr,
3354 .getxattr = generic_getxattr,
3355 .listxattr = btrfs_listxattr,
3356 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003357 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003358};
Chris Mason39279cc2007-06-12 06:35:45 -04003359static struct inode_operations btrfs_dir_ro_inode_operations = {
3360 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003361 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003362};
Chris Mason39279cc2007-06-12 06:35:45 -04003363static struct file_operations btrfs_dir_file_operations = {
3364 .llseek = generic_file_llseek,
3365 .read = generic_read_dir,
3366 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003367 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003368#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003369 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003370#endif
3371};
3372
Chris Masond1310b22008-01-24 16:13:08 -05003373static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003374 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003375 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003376 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003377 .readpage_io_hook = btrfs_readpage_io_hook,
3378 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Mason7e383262008-04-09 16:28:12 -04003379 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003380 .set_bit_hook = btrfs_set_bit_hook,
3381 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003382};
3383
Chris Mason39279cc2007-06-12 06:35:45 -04003384static struct address_space_operations btrfs_aops = {
3385 .readpage = btrfs_readpage,
3386 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003387 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003388 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003389 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003390 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003391 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003392 .invalidatepage = btrfs_invalidatepage,
3393 .releasepage = btrfs_releasepage,
3394 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003395};
3396
3397static struct address_space_operations btrfs_symlink_aops = {
3398 .readpage = btrfs_readpage,
3399 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003400 .invalidatepage = btrfs_invalidatepage,
3401 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003402};
3403
3404static struct inode_operations btrfs_file_inode_operations = {
3405 .truncate = btrfs_truncate,
3406 .getattr = btrfs_getattr,
3407 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003408 .setxattr = generic_setxattr,
3409 .getxattr = generic_getxattr,
3410 .listxattr = btrfs_listxattr,
3411 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003412 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003413};
Josef Bacik618e21d2007-07-11 10:18:17 -04003414static struct inode_operations btrfs_special_inode_operations = {
3415 .getattr = btrfs_getattr,
3416 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003417 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003418};
Chris Mason39279cc2007-06-12 06:35:45 -04003419static struct inode_operations btrfs_symlink_inode_operations = {
3420 .readlink = generic_readlink,
3421 .follow_link = page_follow_link_light,
3422 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003423 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003424};