blob: d58b4d5a1fefae6341c6f8a9dc36ae1e89d46045 [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;
Chris Masonbcbfce82008-04-22 13:26:47 -040083 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050084 int ret = 0;
85
86 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050087 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050088 else
Chris Masonf9ef6602008-01-03 09:22:38 -050089 thresh = total * 85;
90
91 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050092
Chris Masonbcbfce82008-04-22 13:26:47 -040093 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -050094 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
95 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -040096 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -050097 return ret;
98}
99
Chris Masonbe20aa92007-12-17 20:14:01 -0500100static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400101{
102 struct btrfs_root *root = BTRFS_I(inode)->root;
103 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400104 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400105 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500106 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400107 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500108 u64 orig_start = start;
109 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500110 struct btrfs_key ins;
111 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400112
Chris Masonb888db22007-08-27 16:49:44 -0400113 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400114 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500115 btrfs_set_trans_block_group(trans, inode);
116
Chris Masondb945352007-10-15 16:15:53 -0400117 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500118 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400119 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400120 start, start + num_bytes, start, &alloc_hint);
Chris Masond1310b22008-01-24 16:13:08 -0500121 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400122
Chris Mason179e29e2007-11-01 11:28:41 -0400123 if (alloc_hint == EXTENT_MAP_INLINE)
124 goto out;
125
Chris Mason3b951512008-04-17 11:29:12 -0400126 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
127
Chris Masonc59f8952007-12-17 20:14:04 -0500128 while(num_bytes > 0) {
129 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
130 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
Chris Mason98d20f62008-04-14 09:46:10 -0400131 root->sectorsize,
Chris Masonc59f8952007-12-17 20:14:04 -0500132 root->root_key.objectid,
133 trans->transid,
134 inode->i_ino, start, 0,
135 alloc_hint, (u64)-1, &ins, 1);
136 if (ret) {
137 WARN_ON(1);
138 goto out;
139 }
Chris Mason98d20f62008-04-14 09:46:10 -0400140 cur_alloc_size = ins.offset;
Chris Masonc59f8952007-12-17 20:14:04 -0500141 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
142 start, ins.objectid, ins.offset,
143 ins.offset);
Chris Mason90692182008-02-08 13:49:28 -0500144 inode->i_blocks += ins.offset >> 9;
Chris Mason5f564062008-01-22 16:47:59 -0500145 btrfs_check_file(root, inode);
Chris Mason3b951512008-04-17 11:29:12 -0400146 if (num_bytes < cur_alloc_size) {
147 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
148 cur_alloc_size);
149 break;
150 }
Chris Masonc59f8952007-12-17 20:14:04 -0500151 num_bytes -= cur_alloc_size;
152 alloc_hint = ins.objectid + ins.offset;
153 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400154 }
Chris Masond1310b22008-01-24 16:13:08 -0500155 btrfs_drop_extent_cache(inode, orig_start,
156 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500157 btrfs_add_ordered_inode(inode);
Chris Mason90692182008-02-08 13:49:28 -0500158 btrfs_update_inode(trans, root, inode);
Chris Masonb888db22007-08-27 16:49:44 -0400159out:
160 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500161 return ret;
162}
163
164static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
165{
166 u64 extent_start;
167 u64 extent_end;
168 u64 bytenr;
169 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500170 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500171 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500172 struct btrfs_root *root = BTRFS_I(inode)->root;
173 struct extent_buffer *leaf;
174 int found_type;
175 struct btrfs_path *path;
176 struct btrfs_file_extent_item *item;
177 int ret;
178 int err;
179 struct btrfs_key found_key;
180
Chris Masonc31f8832008-01-08 15:46:31 -0500181 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500182 path = btrfs_alloc_path();
183 BUG_ON(!path);
184again:
185 ret = btrfs_lookup_file_extent(NULL, root, path,
186 inode->i_ino, start, 0);
187 if (ret < 0) {
188 btrfs_free_path(path);
189 return ret;
190 }
191
192 cow_end = end;
193 if (ret != 0) {
194 if (path->slots[0] == 0)
195 goto not_found;
196 path->slots[0]--;
197 }
198
199 leaf = path->nodes[0];
200 item = btrfs_item_ptr(leaf, path->slots[0],
201 struct btrfs_file_extent_item);
202
203 /* are we inside the extent that was found? */
204 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
205 found_type = btrfs_key_type(&found_key);
206 if (found_key.objectid != inode->i_ino ||
207 found_type != BTRFS_EXTENT_DATA_KEY) {
208 goto not_found;
209 }
210
211 found_type = btrfs_file_extent_type(leaf, item);
212 extent_start = found_key.offset;
213 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500214 u64 extent_num_bytes;
215
216 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
217 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500218 err = 0;
219
Chris Mason1832a6d2007-12-21 16:27:21 -0500220 if (loops && start != extent_start)
221 goto not_found;
222
Chris Masonbe20aa92007-12-17 20:14:01 -0500223 if (start < extent_start || start >= extent_end)
224 goto not_found;
225
226 cow_end = min(end, extent_end - 1);
227 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
228 if (bytenr == 0)
229 goto not_found;
230
Chris Masonc31f8832008-01-08 15:46:31 -0500231 /*
232 * we may be called by the resizer, make sure we're inside
233 * the limits of the FS
234 */
235 if (bytenr + extent_num_bytes > total_fs_bytes)
236 goto not_found;
237
Chris Masonbe20aa92007-12-17 20:14:01 -0500238 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
239 goto not_found;
240 }
241
242 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500243 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500244 goto not_found;
245 }
246loop:
247 if (start > end) {
248 btrfs_free_path(path);
249 return 0;
250 }
251 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500252 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500253 goto again;
254
255not_found:
256 cow_file_range(inode, start, cow_end);
257 start = cow_end + 1;
258 goto loop;
259}
260
261static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
262{
263 struct btrfs_root *root = BTRFS_I(inode)->root;
264 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500265 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500266 if (btrfs_test_opt(root, NODATACOW) ||
267 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500268 ret = run_delalloc_nocow(inode, start, end);
269 else
270 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500271
Chris Masonb888db22007-08-27 16:49:44 -0400272 mutex_unlock(&root->fs_info->fs_mutex);
273 return ret;
274}
275
Chris Mason291d6732008-01-29 15:55:23 -0500276int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500277 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500278{
Chris Masonbcbfce82008-04-22 13:26:47 -0400279 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500280 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500281 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400282 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500283 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500284 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400285 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500286 }
287 return 0;
288}
289
290int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500291 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500292{
Chris Masonb0c68f82008-01-31 11:05:37 -0500293 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500294 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400295 unsigned long flags;
296
297 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500298 if (end - start + 1 > root->fs_info->delalloc_bytes) {
299 printk("warning: delalloc account %Lu %Lu\n",
300 end - start + 1, root->fs_info->delalloc_bytes);
301 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500302 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500303 } else {
304 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500305 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500306 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400307 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500308 }
309 return 0;
310}
311
Chris Mason239b14b2008-03-24 15:02:07 -0400312int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
313 size_t size, struct bio *bio)
314{
315 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
316 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400317 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400318 u64 length = 0;
319 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400320 int ret;
321
Chris Masonf2d8d742008-04-21 10:03:05 -0400322 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400323 map_tree = &root->fs_info->mapping_tree;
324 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400325 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400326 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400327
Chris Mason239b14b2008-03-24 15:02:07 -0400328 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400329 return 1;
330 }
331 return 0;
332}
333
Chris Mason44b8bd72008-04-16 11:14:51 -0400334int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400335 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500336{
Chris Mason065631f2008-02-20 12:07:25 -0500337 struct btrfs_root *root = BTRFS_I(inode)->root;
338 struct btrfs_trans_handle *trans;
339 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400340 char *sums = NULL;
341
342 ret = btrfs_csum_one_bio(root, bio, &sums);
343 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500344
Chris Mason44b8bd72008-04-16 11:14:51 -0400345 mutex_lock(&root->fs_info->fs_mutex);
346 trans = btrfs_start_transaction(root, 1);
Chris Masone0156402008-04-16 11:15:20 -0400347
Chris Mason44b8bd72008-04-16 11:14:51 -0400348 btrfs_set_trans_block_group(trans, inode);
Chris Masone0156402008-04-16 11:15:20 -0400349 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
350
Chris Mason44b8bd72008-04-16 11:14:51 -0400351 ret = btrfs_end_transaction(trans, root);
352 BUG_ON(ret);
353 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0156402008-04-16 11:15:20 -0400354
355 kfree(sums);
356
Chris Mason44b8bd72008-04-16 11:14:51 -0400357 return btrfs_map_bio(root, rw, bio, mirror_num);
358}
359
360int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
361 int mirror_num)
362{
363 struct btrfs_root *root = BTRFS_I(inode)->root;
364 int ret = 0;
365
Chris Mason22c59942008-04-09 16:28:12 -0400366 if (!(rw & (1 << BIO_RW))) {
367 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
368 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400369 goto mapit;
370 }
Chris Mason065631f2008-02-20 12:07:25 -0500371
372 if (btrfs_test_opt(root, NODATASUM) ||
Chris Mason0b86a832008-03-24 15:01:56 -0400373 btrfs_test_flag(inode, NODATASUM)) {
374 goto mapit;
375 }
Chris Mason065631f2008-02-20 12:07:25 -0500376
Chris Mason44b8bd72008-04-16 11:14:51 -0400377 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
378 inode, rw, bio, mirror_num,
379 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400380mapit:
Chris Masonf1885912008-04-09 16:28:12 -0400381 return btrfs_map_bio(root, rw, bio, mirror_num);
Chris Mason065631f2008-02-20 12:07:25 -0500382}
Chris Mason6885f302008-02-20 16:11:05 -0500383
Chris Mason07157aa2007-08-30 08:50:51 -0400384int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
385{
386 int ret = 0;
387 struct inode *inode = page->mapping->host;
388 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500389 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400390 struct btrfs_csum_item *item;
391 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400392 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400393
Yanb98b6762008-01-08 15:54:37 -0500394 if (btrfs_test_opt(root, NODATASUM) ||
395 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500396 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400397
Chris Mason07157aa2007-08-30 08:50:51 -0400398 mutex_lock(&root->fs_info->fs_mutex);
399 path = btrfs_alloc_path();
400 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
401 if (IS_ERR(item)) {
402 ret = PTR_ERR(item);
403 /* a csum that isn't present is a preallocated region. */
404 if (ret == -ENOENT || ret == -EFBIG)
405 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400406 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500407 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400408 goto out;
409 }
Chris Masonff79f812007-10-15 16:22:25 -0400410 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
411 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500412 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400413out:
414 if (path)
415 btrfs_free_path(path);
416 mutex_unlock(&root->fs_info->fs_mutex);
417 return ret;
418}
419
Chris Mason7e383262008-04-09 16:28:12 -0400420struct io_failure_record {
421 struct page *page;
422 u64 start;
423 u64 len;
424 u64 logical;
425 int last_mirror;
426};
427
428int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
429 struct page *page, u64 start, u64 end,
430 struct extent_state *state)
431{
432 struct io_failure_record *failrec = NULL;
433 u64 private;
434 struct extent_map *em;
435 struct inode *inode = page->mapping->host;
436 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400437 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400438 struct bio *bio;
439 int num_copies;
440 int ret;
441 u64 logical;
442
443 ret = get_state_private(failure_tree, start, &private);
444 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400445 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
446 if (!failrec)
447 return -ENOMEM;
448 failrec->start = start;
449 failrec->len = end - start + 1;
450 failrec->last_mirror = 0;
451
Chris Mason3b951512008-04-17 11:29:12 -0400452 spin_lock(&em_tree->lock);
453 em = lookup_extent_mapping(em_tree, start, failrec->len);
454 if (em->start > start || em->start + em->len < start) {
455 free_extent_map(em);
456 em = NULL;
457 }
458 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400459
460 if (!em || IS_ERR(em)) {
461 kfree(failrec);
462 return -EIO;
463 }
464 logical = start - em->start;
465 logical = em->block_start + logical;
466 failrec->logical = logical;
467 free_extent_map(em);
468 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
469 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400470 set_state_private(failure_tree, start,
471 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400472 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400473 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400474 }
475 num_copies = btrfs_num_copies(
476 &BTRFS_I(inode)->root->fs_info->mapping_tree,
477 failrec->logical, failrec->len);
478 failrec->last_mirror++;
479 if (!state) {
480 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
481 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
482 failrec->start,
483 EXTENT_LOCKED);
484 if (state && state->start != failrec->start)
485 state = NULL;
486 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
487 }
488 if (!state || failrec->last_mirror > num_copies) {
489 set_state_private(failure_tree, failrec->start, 0);
490 clear_extent_bits(failure_tree, failrec->start,
491 failrec->start + failrec->len - 1,
492 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
493 kfree(failrec);
494 return -EIO;
495 }
496 bio = bio_alloc(GFP_NOFS, 1);
497 bio->bi_private = state;
498 bio->bi_end_io = failed_bio->bi_end_io;
499 bio->bi_sector = failrec->logical >> 9;
500 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400501 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400502 bio_add_page(bio, page, failrec->len, start - page_offset(page));
503 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
504 return 0;
505}
506
Chris Mason70dec802008-01-29 09:59:12 -0500507int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
508 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400509{
Chris Mason35ebb932007-10-30 16:56:53 -0400510 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400511 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500512 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400513 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500514 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400515 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400516 struct btrfs_root *root = BTRFS_I(inode)->root;
517 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400518 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500519
Yanb98b6762008-01-08 15:54:37 -0500520 if (btrfs_test_opt(root, NODATASUM) ||
521 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500522 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500523 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500524 private = state->private;
525 ret = 0;
526 } else {
527 ret = get_state_private(io_tree, start, &private);
528 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400529 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400530 kaddr = kmap_atomic(page, KM_IRQ0);
531 if (ret) {
532 goto zeroit;
533 }
Chris Masonff79f812007-10-15 16:22:25 -0400534 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
535 btrfs_csum_final(csum, (char *)&csum);
536 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400537 goto zeroit;
538 }
539 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400540 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400541
542 /* if the io failure tree for this inode is non-empty,
543 * check to see if we've recovered from a failed IO
544 */
545 private = 0;
546 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
547 (u64)-1, 1, EXTENT_DIRTY)) {
548 u64 private_failure;
549 struct io_failure_record *failure;
550 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
551 start, &private_failure);
552 if (ret == 0) {
Chris Mason587f7702008-04-11 12:16:46 -0400553 failure = (struct io_failure_record *)(unsigned long)
554 private_failure;
Chris Mason7e383262008-04-09 16:28:12 -0400555 set_state_private(&BTRFS_I(inode)->io_failure_tree,
556 failure->start, 0);
557 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
558 failure->start,
559 failure->start + failure->len - 1,
560 EXTENT_DIRTY | EXTENT_LOCKED,
561 GFP_NOFS);
562 kfree(failure);
563 }
564 }
Chris Mason07157aa2007-08-30 08:50:51 -0400565 return 0;
566
567zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500568 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
569 page->mapping->host->i_ino, (unsigned long long)start, csum,
570 private);
Chris Masondb945352007-10-15 16:15:53 -0400571 memset(kaddr + offset, 1, end - start + 1);
572 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400573 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400574 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400575 if (private == 0)
576 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400577 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400578}
Chris Masonb888db22007-08-27 16:49:44 -0400579
Chris Mason39279cc2007-06-12 06:35:45 -0400580void btrfs_read_locked_inode(struct inode *inode)
581{
582 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400583 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400584 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400585 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400586 struct btrfs_root *root = BTRFS_I(inode)->root;
587 struct btrfs_key location;
588 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400589 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400590 int ret;
591
592 path = btrfs_alloc_path();
593 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400594 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400595 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500596
Chris Mason39279cc2007-06-12 06:35:45 -0400597 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400598 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400599 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400600
Chris Mason5f39d392007-10-15 16:14:19 -0400601 leaf = path->nodes[0];
602 inode_item = btrfs_item_ptr(leaf, path->slots[0],
603 struct btrfs_inode_item);
604
605 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
606 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
607 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
608 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
609 inode->i_size = btrfs_inode_size(leaf, inode_item);
610
611 tspec = btrfs_inode_atime(inode_item);
612 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
613 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
614
615 tspec = btrfs_inode_mtime(inode_item);
616 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
617 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
618
619 tspec = btrfs_inode_ctime(inode_item);
620 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
621 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
622
623 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
624 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400625 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400626 rdev = btrfs_inode_rdev(leaf, inode_item);
627
628 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400629 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
630 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500631 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500632 if (!BTRFS_I(inode)->block_group) {
633 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400634 NULL, 0,
635 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500636 }
Chris Mason39279cc2007-06-12 06:35:45 -0400637 btrfs_free_path(path);
638 inode_item = NULL;
639
640 mutex_unlock(&root->fs_info->fs_mutex);
641
642 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400643 case S_IFREG:
644 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400645 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500646 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400647 inode->i_fop = &btrfs_file_operations;
648 inode->i_op = &btrfs_file_inode_operations;
649 break;
650 case S_IFDIR:
651 inode->i_fop = &btrfs_dir_file_operations;
652 if (root == root->fs_info->tree_root)
653 inode->i_op = &btrfs_dir_ro_inode_operations;
654 else
655 inode->i_op = &btrfs_dir_inode_operations;
656 break;
657 case S_IFLNK:
658 inode->i_op = &btrfs_symlink_inode_operations;
659 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400660 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400661 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400662 default:
663 init_special_inode(inode, inode->i_mode, rdev);
664 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400665 }
666 return;
667
668make_bad:
669 btrfs_release_path(root, path);
670 btrfs_free_path(path);
671 mutex_unlock(&root->fs_info->fs_mutex);
672 make_bad_inode(inode);
673}
674
Chris Mason5f39d392007-10-15 16:14:19 -0400675static void fill_inode_item(struct extent_buffer *leaf,
676 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400677 struct inode *inode)
678{
Chris Mason5f39d392007-10-15 16:14:19 -0400679 btrfs_set_inode_uid(leaf, item, inode->i_uid);
680 btrfs_set_inode_gid(leaf, item, inode->i_gid);
681 btrfs_set_inode_size(leaf, item, inode->i_size);
682 btrfs_set_inode_mode(leaf, item, inode->i_mode);
683 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
684
685 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
686 inode->i_atime.tv_sec);
687 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
688 inode->i_atime.tv_nsec);
689
690 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
691 inode->i_mtime.tv_sec);
692 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
693 inode->i_mtime.tv_nsec);
694
695 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
696 inode->i_ctime.tv_sec);
697 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
698 inode->i_ctime.tv_nsec);
699
700 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
701 btrfs_set_inode_generation(leaf, item, inode->i_generation);
702 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500703 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400704 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400705 BTRFS_I(inode)->block_group->key.objectid);
706}
707
Chris Masona52d9a82007-08-27 16:49:44 -0400708int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400709 struct btrfs_root *root,
710 struct inode *inode)
711{
712 struct btrfs_inode_item *inode_item;
713 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400714 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400715 int ret;
716
717 path = btrfs_alloc_path();
718 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400719 ret = btrfs_lookup_inode(trans, root, path,
720 &BTRFS_I(inode)->location, 1);
721 if (ret) {
722 if (ret > 0)
723 ret = -ENOENT;
724 goto failed;
725 }
726
Chris Mason5f39d392007-10-15 16:14:19 -0400727 leaf = path->nodes[0];
728 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400729 struct btrfs_inode_item);
730
Chris Mason5f39d392007-10-15 16:14:19 -0400731 fill_inode_item(leaf, inode_item, inode);
732 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400733 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400734 ret = 0;
735failed:
736 btrfs_release_path(root, path);
737 btrfs_free_path(path);
738 return ret;
739}
740
741
742static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
743 struct btrfs_root *root,
744 struct inode *dir,
745 struct dentry *dentry)
746{
747 struct btrfs_path *path;
748 const char *name = dentry->d_name.name;
749 int name_len = dentry->d_name.len;
750 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400751 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400752 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400753 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400754
755 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400756 if (!path) {
757 ret = -ENOMEM;
758 goto err;
759 }
760
Chris Mason39279cc2007-06-12 06:35:45 -0400761 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
762 name, name_len, -1);
763 if (IS_ERR(di)) {
764 ret = PTR_ERR(di);
765 goto err;
766 }
767 if (!di) {
768 ret = -ENOENT;
769 goto err;
770 }
Chris Mason5f39d392007-10-15 16:14:19 -0400771 leaf = path->nodes[0];
772 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400773 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400774 if (ret)
775 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400776 btrfs_release_path(root, path);
777
778 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400779 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400780 if (IS_ERR(di)) {
781 ret = PTR_ERR(di);
782 goto err;
783 }
784 if (!di) {
785 ret = -ENOENT;
786 goto err;
787 }
788 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400789
790 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500791 ret = btrfs_del_inode_ref(trans, root, name, name_len,
792 dentry->d_inode->i_ino,
793 dentry->d_parent->d_inode->i_ino);
794 if (ret) {
795 printk("failed to delete reference to %.*s, "
796 "inode %lu parent %lu\n", name_len, name,
797 dentry->d_inode->i_ino,
798 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500799 }
Chris Mason39279cc2007-06-12 06:35:45 -0400800err:
801 btrfs_free_path(path);
802 if (!ret) {
803 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400804 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400805 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500806#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
807 dentry->d_inode->i_nlink--;
808#else
Chris Mason39279cc2007-06-12 06:35:45 -0400809 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500810#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400811 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400812 dir->i_sb->s_dirt = 1;
813 }
814 return ret;
815}
816
817static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
818{
819 struct btrfs_root *root;
820 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500821 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400822 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500823 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400824
825 root = BTRFS_I(dir)->root;
826 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500827
828 ret = btrfs_check_free_space(root, 1, 1);
829 if (ret)
830 goto fail;
831
Chris Mason39279cc2007-06-12 06:35:45 -0400832 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400833
Chris Mason39279cc2007-06-12 06:35:45 -0400834 btrfs_set_trans_block_group(trans, dir);
835 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400836 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400837
Chris Mason2da98f02008-01-16 11:44:43 -0500838 if (inode->i_nlink == 0) {
839 int found;
840 /* if the inode isn't linked anywhere,
841 * we don't need to worry about
842 * data=ordered
843 */
844 found = btrfs_del_ordered_inode(inode);
845 if (found == 1) {
846 atomic_dec(&inode->i_count);
847 }
848 }
849
Chris Mason39279cc2007-06-12 06:35:45 -0400850 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500851fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400852 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400853 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500854 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400855 return ret;
856}
857
858static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
859{
860 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500861 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400862 int ret;
863 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400864 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500865 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400866
Yan134d4512007-10-25 15:49:25 -0400867 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
868 return -ENOTEMPTY;
869
Chris Mason39279cc2007-06-12 06:35:45 -0400870 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500871 ret = btrfs_check_free_space(root, 1, 1);
872 if (ret)
873 goto fail;
874
Chris Mason39279cc2007-06-12 06:35:45 -0400875 trans = btrfs_start_transaction(root, 1);
876 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400877
878 /* now the directory is empty */
879 err = btrfs_unlink_trans(trans, root, dir, dentry);
880 if (!err) {
881 inode->i_size = 0;
882 }
Chris Mason39544012007-12-12 14:38:19 -0500883
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400884 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400885 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500886fail:
Yan134d4512007-10-25 15:49:25 -0400887 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400888 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500889 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500890
Chris Mason39279cc2007-06-12 06:35:45 -0400891 if (ret && !err)
892 err = ret;
893 return err;
894}
895
Chris Mason39279cc2007-06-12 06:35:45 -0400896/*
Chris Mason39279cc2007-06-12 06:35:45 -0400897 * this can truncate away extent items, csum items and directory items.
898 * It starts at a high offset and removes keys until it can't find
899 * any higher than i_size.
900 *
901 * csum items that cross the new i_size are truncated to the new size
902 * as well.
903 */
904static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
905 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500906 struct inode *inode,
907 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400908{
909 int ret;
910 struct btrfs_path *path;
911 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400912 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400913 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400914 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400915 struct btrfs_file_extent_item *fi;
916 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400917 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400918 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500919 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500920 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400921 int found_extent;
922 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500923 int pending_del_nr = 0;
924 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400925 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -0400926 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400927
Chris Mason3b951512008-04-17 11:29:12 -0400928 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400929 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400930 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400931 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400932
Chris Mason39279cc2007-06-12 06:35:45 -0400933 /* FIXME, add redo link to tree so we don't leak on crash */
934 key.objectid = inode->i_ino;
935 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400936 key.type = (u8)-1;
937
Chris Mason85e21ba2008-01-29 15:11:36 -0500938 btrfs_init_path(path);
939search_again:
940 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
941 if (ret < 0) {
942 goto error;
943 }
944 if (ret > 0) {
945 BUG_ON(path->slots[0] == 0);
946 path->slots[0]--;
947 }
948
Chris Mason39279cc2007-06-12 06:35:45 -0400949 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400950 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400951 leaf = path->nodes[0];
952 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
953 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400954
Chris Mason5f39d392007-10-15 16:14:19 -0400955 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400956 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400957
Chris Mason85e21ba2008-01-29 15:11:36 -0500958 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400959 break;
960
Chris Mason5f39d392007-10-15 16:14:19 -0400961 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400962 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400963 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400964 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400965 extent_type = btrfs_file_extent_type(leaf, fi);
966 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400967 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400968 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400969 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
970 struct btrfs_item *item = btrfs_item_nr(leaf,
971 path->slots[0]);
972 item_end += btrfs_file_extent_inline_len(leaf,
973 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400974 }
Yan008630c2007-11-07 13:31:09 -0500975 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400976 }
977 if (found_type == BTRFS_CSUM_ITEM_KEY) {
978 ret = btrfs_csum_truncate(trans, root, path,
979 inode->i_size);
980 BUG_ON(ret);
981 }
Yan008630c2007-11-07 13:31:09 -0500982 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400983 if (found_type == BTRFS_DIR_ITEM_KEY) {
984 found_type = BTRFS_INODE_ITEM_KEY;
985 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
986 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500987 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
988 found_type = BTRFS_XATTR_ITEM_KEY;
989 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
990 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400991 } else if (found_type) {
992 found_type--;
993 } else {
994 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400995 }
Yana61721d2007-09-17 11:08:38 -0400996 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500997 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400998 }
Chris Mason5f39d392007-10-15 16:14:19 -0400999 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001000 del_item = 1;
1001 else
1002 del_item = 0;
1003 found_extent = 0;
1004
1005 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001006 if (found_type != BTRFS_EXTENT_DATA_KEY)
1007 goto delete;
1008
1009 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001010 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001011 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001012 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001013 u64 orig_num_bytes =
1014 btrfs_file_extent_num_bytes(leaf, fi);
1015 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001016 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001017 extent_num_bytes = extent_num_bytes &
1018 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001019 btrfs_set_file_extent_num_bytes(leaf, fi,
1020 extent_num_bytes);
1021 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001022 extent_num_bytes);
1023 if (extent_start != 0)
1024 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001025 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001026 } else {
Chris Masondb945352007-10-15 16:15:53 -04001027 extent_num_bytes =
1028 btrfs_file_extent_disk_num_bytes(leaf,
1029 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001030 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001031 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001032 if (extent_start != 0) {
1033 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001034 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001035 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001036 root_gen = btrfs_header_generation(leaf);
1037 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001038 }
Chris Mason90692182008-02-08 13:49:28 -05001039 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1040 if (!del_item) {
1041 u32 newsize = inode->i_size - found_key.offset;
1042 dec_i_blocks(inode, item_end + 1 -
1043 found_key.offset - newsize);
1044 newsize =
1045 btrfs_file_extent_calc_inline_size(newsize);
1046 ret = btrfs_truncate_item(trans, root, path,
1047 newsize, 1);
1048 BUG_ON(ret);
1049 } else {
1050 dec_i_blocks(inode, item_end + 1 -
1051 found_key.offset);
1052 }
Chris Mason39279cc2007-06-12 06:35:45 -04001053 }
Chris Mason179e29e2007-11-01 11:28:41 -04001054delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001055 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001056 if (!pending_del_nr) {
1057 /* no pending yet, add ourselves */
1058 pending_del_slot = path->slots[0];
1059 pending_del_nr = 1;
1060 } else if (pending_del_nr &&
1061 path->slots[0] + 1 == pending_del_slot) {
1062 /* hop on the pending chunk */
1063 pending_del_nr++;
1064 pending_del_slot = path->slots[0];
1065 } else {
1066 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1067 }
Chris Mason39279cc2007-06-12 06:35:45 -04001068 } else {
1069 break;
1070 }
Chris Mason39279cc2007-06-12 06:35:45 -04001071 if (found_extent) {
1072 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001073 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001074 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001075 root_gen, inode->i_ino,
1076 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001077 BUG_ON(ret);
1078 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001079next:
1080 if (path->slots[0] == 0) {
1081 if (pending_del_nr)
1082 goto del_pending;
1083 btrfs_release_path(root, path);
1084 goto search_again;
1085 }
1086
1087 path->slots[0]--;
1088 if (pending_del_nr &&
1089 path->slots[0] + 1 != pending_del_slot) {
1090 struct btrfs_key debug;
1091del_pending:
1092 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1093 pending_del_slot);
1094 ret = btrfs_del_items(trans, root, path,
1095 pending_del_slot,
1096 pending_del_nr);
1097 BUG_ON(ret);
1098 pending_del_nr = 0;
1099 btrfs_release_path(root, path);
1100 goto search_again;
1101 }
Chris Mason39279cc2007-06-12 06:35:45 -04001102 }
1103 ret = 0;
1104error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001105 if (pending_del_nr) {
1106 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1107 pending_del_nr);
1108 }
Chris Mason39279cc2007-06-12 06:35:45 -04001109 btrfs_release_path(root, path);
1110 btrfs_free_path(path);
1111 inode->i_sb->s_dirt = 1;
1112 return ret;
1113}
1114
Chris Masonb888db22007-08-27 16:49:44 -04001115static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -04001116 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001117{
Chris Mason39279cc2007-06-12 06:35:45 -04001118 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -05001119 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001120 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001121 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -05001122 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001123
Chris Mason190662b2007-12-18 16:25:45 -05001124 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001125 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001126
Chris Masond1310b22008-01-24 16:13:08 -05001127 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001128 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -04001129 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -05001130
Chris Masona52d9a82007-08-27 16:49:44 -04001131 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -04001132 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001133 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1134 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -04001135 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001136 }
Chris Masonb888db22007-08-27 16:49:44 -04001137 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -05001138 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001139
Chris Masona52d9a82007-08-27 16:49:44 -04001140 return ret;
1141}
1142
1143/*
1144 * taken from block_truncate_page, but does cow as it zeros out
1145 * any bytes left in the last page in the file.
1146 */
1147static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1148{
1149 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001150 struct btrfs_root *root = BTRFS_I(inode)->root;
1151 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001152 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1153 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1154 struct page *page;
1155 int ret = 0;
1156 u64 page_start;
1157
1158 if ((offset & (blocksize - 1)) == 0)
1159 goto out;
1160
1161 ret = -ENOMEM;
1162 page = grab_cache_page(mapping, index);
1163 if (!page)
1164 goto out;
1165 if (!PageUptodate(page)) {
1166 ret = btrfs_readpage(NULL, page);
1167 lock_page(page);
1168 if (!PageUptodate(page)) {
1169 ret = -EIO;
1170 goto out;
1171 }
1172 }
Chris Mason35ebb932007-10-30 16:56:53 -04001173 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001174
Chris Masonb888db22007-08-27 16:49:44 -04001175 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001176
Chris Mason39279cc2007-06-12 06:35:45 -04001177 unlock_page(page);
1178 page_cache_release(page);
1179out:
1180 return ret;
1181}
1182
1183static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1184{
1185 struct inode *inode = dentry->d_inode;
1186 int err;
1187
1188 err = inode_change_ok(inode, attr);
1189 if (err)
1190 return err;
1191
1192 if (S_ISREG(inode->i_mode) &&
1193 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1194 struct btrfs_trans_handle *trans;
1195 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001196 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001197
Chris Mason5f39d392007-10-15 16:14:19 -04001198 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001199 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001200 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001201 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001202 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001203
Chris Mason1b0f7c22008-01-30 14:33:02 -05001204 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001205 goto out;
1206
Chris Mason1832a6d2007-12-21 16:27:21 -05001207 mutex_lock(&root->fs_info->fs_mutex);
1208 err = btrfs_check_free_space(root, 1, 0);
1209 mutex_unlock(&root->fs_info->fs_mutex);
1210 if (err)
1211 goto fail;
1212
Chris Mason39279cc2007-06-12 06:35:45 -04001213 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1214
Chris Mason1b0f7c22008-01-30 14:33:02 -05001215 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001216 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001217
1218 mutex_lock(&root->fs_info->fs_mutex);
1219 trans = btrfs_start_transaction(root, 1);
1220 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001221 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001222 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001223 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001224
Chris Mason179e29e2007-11-01 11:28:41 -04001225 if (alloc_hint != EXTENT_MAP_INLINE) {
1226 err = btrfs_insert_file_extent(trans, root,
1227 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001228 hole_start, 0, 0,
1229 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001230 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001231 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001232 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001233 }
Chris Mason39279cc2007-06-12 06:35:45 -04001234 btrfs_end_transaction(trans, root);
1235 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001236 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001237 if (err)
1238 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001239 }
1240out:
1241 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001242fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001243 return err;
1244}
Chris Mason61295eb2008-01-14 16:24:38 -05001245
Chris Mason2da98f02008-01-16 11:44:43 -05001246void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001247{
Chris Mason2da98f02008-01-16 11:44:43 -05001248 int ret;
1249
1250 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001251 return;
1252 }
Chris Mason2da98f02008-01-16 11:44:43 -05001253
1254 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1255 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1256 return;
1257
1258 ret = btrfs_del_ordered_inode(inode);
1259 if (ret == 1) {
1260 atomic_dec(&inode->i_count);
1261 }
Chris Mason61295eb2008-01-14 16:24:38 -05001262}
1263
Chris Mason39279cc2007-06-12 06:35:45 -04001264void btrfs_delete_inode(struct inode *inode)
1265{
1266 struct btrfs_trans_handle *trans;
1267 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001268 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001269 int ret;
1270
1271 truncate_inode_pages(&inode->i_data, 0);
1272 if (is_bad_inode(inode)) {
1273 goto no_delete;
1274 }
Chris Mason5f39d392007-10-15 16:14:19 -04001275
Chris Mason39279cc2007-06-12 06:35:45 -04001276 inode->i_size = 0;
1277 mutex_lock(&root->fs_info->fs_mutex);
1278 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001279
Chris Mason39279cc2007-06-12 06:35:45 -04001280 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001281 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001282 if (ret)
1283 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001284
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001285 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001286 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001287
Chris Mason39279cc2007-06-12 06:35:45 -04001288 btrfs_end_transaction(trans, root);
1289 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001290 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001291 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001292 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001293
1294no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001295 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001296 btrfs_end_transaction(trans, root);
1297 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001298 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001299 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001300no_delete:
1301 clear_inode(inode);
1302}
1303
1304/*
1305 * this returns the key found in the dir entry in the location pointer.
1306 * If no dir entries were found, location->objectid is 0.
1307 */
1308static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1309 struct btrfs_key *location)
1310{
1311 const char *name = dentry->d_name.name;
1312 int namelen = dentry->d_name.len;
1313 struct btrfs_dir_item *di;
1314 struct btrfs_path *path;
1315 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001316 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001317
Chris Mason39544012007-12-12 14:38:19 -05001318 if (namelen == 1 && strcmp(name, ".") == 0) {
1319 location->objectid = dir->i_ino;
1320 location->type = BTRFS_INODE_ITEM_KEY;
1321 location->offset = 0;
1322 return 0;
1323 }
Chris Mason39279cc2007-06-12 06:35:45 -04001324 path = btrfs_alloc_path();
1325 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001326
Chris Mason7a720532007-12-13 09:06:59 -05001327 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001328 struct btrfs_key key;
1329 struct extent_buffer *leaf;
1330 u32 nritems;
1331 int slot;
1332
1333 key.objectid = dir->i_ino;
1334 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1335 key.offset = 0;
1336 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1337 BUG_ON(ret == 0);
1338 ret = 0;
1339
1340 leaf = path->nodes[0];
1341 slot = path->slots[0];
1342 nritems = btrfs_header_nritems(leaf);
1343 if (slot >= nritems)
1344 goto out_err;
1345
1346 btrfs_item_key_to_cpu(leaf, &key, slot);
1347 if (key.objectid != dir->i_ino ||
1348 key.type != BTRFS_INODE_REF_KEY) {
1349 goto out_err;
1350 }
1351 location->objectid = key.offset;
1352 location->type = BTRFS_INODE_ITEM_KEY;
1353 location->offset = 0;
1354 goto out;
1355 }
1356
Chris Mason39279cc2007-06-12 06:35:45 -04001357 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1358 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001359 if (IS_ERR(di))
1360 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001361 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001362 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001363 }
Chris Mason5f39d392007-10-15 16:14:19 -04001364 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001365out:
Chris Mason39279cc2007-06-12 06:35:45 -04001366 btrfs_free_path(path);
1367 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001368out_err:
1369 location->objectid = 0;
1370 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001371}
1372
1373/*
1374 * when we hit a tree root in a directory, the btrfs part of the inode
1375 * needs to be changed to reflect the root directory of the tree root. This
1376 * is kind of like crossing a mount point.
1377 */
1378static int fixup_tree_root_location(struct btrfs_root *root,
1379 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001380 struct btrfs_root **sub_root,
1381 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001382{
1383 struct btrfs_path *path;
1384 struct btrfs_root_item *ri;
1385
1386 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1387 return 0;
1388 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1389 return 0;
1390
1391 path = btrfs_alloc_path();
1392 BUG_ON(!path);
1393 mutex_lock(&root->fs_info->fs_mutex);
1394
Josef Bacik58176a92007-08-29 15:47:34 -04001395 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1396 dentry->d_name.name,
1397 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001398 if (IS_ERR(*sub_root))
1399 return PTR_ERR(*sub_root);
1400
1401 ri = &(*sub_root)->root_item;
1402 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001403 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1404 location->offset = 0;
1405
1406 btrfs_free_path(path);
1407 mutex_unlock(&root->fs_info->fs_mutex);
1408 return 0;
1409}
1410
1411static int btrfs_init_locked_inode(struct inode *inode, void *p)
1412{
1413 struct btrfs_iget_args *args = p;
1414 inode->i_ino = args->ino;
1415 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001416 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001417 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1418 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001419 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001420 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1421 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001422 return 0;
1423}
1424
1425static int btrfs_find_actor(struct inode *inode, void *opaque)
1426{
1427 struct btrfs_iget_args *args = opaque;
1428 return (args->ino == inode->i_ino &&
1429 args->root == BTRFS_I(inode)->root);
1430}
1431
Chris Masondc17ff82008-01-08 15:46:30 -05001432struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1433 u64 root_objectid)
1434{
1435 struct btrfs_iget_args args;
1436 args.ino = objectid;
1437 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1438
1439 if (!args.root)
1440 return NULL;
1441
1442 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1443}
1444
Chris Mason39279cc2007-06-12 06:35:45 -04001445struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1446 struct btrfs_root *root)
1447{
1448 struct inode *inode;
1449 struct btrfs_iget_args args;
1450 args.ino = objectid;
1451 args.root = root;
1452
1453 inode = iget5_locked(s, objectid, btrfs_find_actor,
1454 btrfs_init_locked_inode,
1455 (void *)&args);
1456 return inode;
1457}
1458
1459static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1460 struct nameidata *nd)
1461{
1462 struct inode * inode;
1463 struct btrfs_inode *bi = BTRFS_I(dir);
1464 struct btrfs_root *root = bi->root;
1465 struct btrfs_root *sub_root = root;
1466 struct btrfs_key location;
1467 int ret;
1468
1469 if (dentry->d_name.len > BTRFS_NAME_LEN)
1470 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001471
Chris Mason39279cc2007-06-12 06:35:45 -04001472 mutex_lock(&root->fs_info->fs_mutex);
1473 ret = btrfs_inode_by_name(dir, dentry, &location);
1474 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001475
Chris Mason39279cc2007-06-12 06:35:45 -04001476 if (ret < 0)
1477 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001478
Chris Mason39279cc2007-06-12 06:35:45 -04001479 inode = NULL;
1480 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001481 ret = fixup_tree_root_location(root, &location, &sub_root,
1482 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001483 if (ret < 0)
1484 return ERR_PTR(ret);
1485 if (ret > 0)
1486 return ERR_PTR(-ENOENT);
1487 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1488 sub_root);
1489 if (!inode)
1490 return ERR_PTR(-EACCES);
1491 if (inode->i_state & I_NEW) {
1492 /* the inode and parent dir are two different roots */
1493 if (sub_root != root) {
1494 igrab(inode);
1495 sub_root->inode = inode;
1496 }
1497 BTRFS_I(inode)->root = sub_root;
1498 memcpy(&BTRFS_I(inode)->location, &location,
1499 sizeof(location));
1500 btrfs_read_locked_inode(inode);
1501 unlock_new_inode(inode);
1502 }
1503 }
1504 return d_splice_alias(inode, dentry);
1505}
1506
Chris Mason39279cc2007-06-12 06:35:45 -04001507static unsigned char btrfs_filetype_table[] = {
1508 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1509};
1510
1511static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1512{
Chris Mason6da6aba2007-12-18 16:15:09 -05001513 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001514 struct btrfs_root *root = BTRFS_I(inode)->root;
1515 struct btrfs_item *item;
1516 struct btrfs_dir_item *di;
1517 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001518 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001519 struct btrfs_path *path;
1520 int ret;
1521 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001522 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001523 int slot;
1524 int advance;
1525 unsigned char d_type;
1526 int over = 0;
1527 u32 di_cur;
1528 u32 di_total;
1529 u32 di_len;
1530 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001531 char tmp_name[32];
1532 char *name_ptr;
1533 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001534
1535 /* FIXME, use a real flag for deciding about the key type */
1536 if (root->fs_info->tree_root == root)
1537 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001538
Chris Mason39544012007-12-12 14:38:19 -05001539 /* special case for "." */
1540 if (filp->f_pos == 0) {
1541 over = filldir(dirent, ".", 1,
1542 1, inode->i_ino,
1543 DT_DIR);
1544 if (over)
1545 return 0;
1546 filp->f_pos = 1;
1547 }
1548
Chris Mason39279cc2007-06-12 06:35:45 -04001549 mutex_lock(&root->fs_info->fs_mutex);
1550 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001551 path = btrfs_alloc_path();
1552 path->reada = 2;
1553
1554 /* special case for .., just use the back ref */
1555 if (filp->f_pos == 1) {
1556 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1557 key.offset = 0;
1558 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1559 BUG_ON(ret == 0);
1560 leaf = path->nodes[0];
1561 slot = path->slots[0];
1562 nritems = btrfs_header_nritems(leaf);
1563 if (slot >= nritems) {
1564 btrfs_release_path(root, path);
1565 goto read_dir_items;
1566 }
1567 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1568 btrfs_release_path(root, path);
1569 if (found_key.objectid != key.objectid ||
1570 found_key.type != BTRFS_INODE_REF_KEY)
1571 goto read_dir_items;
1572 over = filldir(dirent, "..", 2,
1573 2, found_key.offset, DT_DIR);
1574 if (over)
1575 goto nopos;
1576 filp->f_pos = 2;
1577 }
1578
1579read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001580 btrfs_set_key_type(&key, key_type);
1581 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001582
Chris Mason39279cc2007-06-12 06:35:45 -04001583 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1584 if (ret < 0)
1585 goto err;
1586 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001587 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001588 leaf = path->nodes[0];
1589 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001590 slot = path->slots[0];
1591 if (advance || slot >= nritems) {
1592 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001593 ret = btrfs_next_leaf(root, path);
1594 if (ret)
1595 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001596 leaf = path->nodes[0];
1597 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001598 slot = path->slots[0];
1599 } else {
1600 slot++;
1601 path->slots[0]++;
1602 }
1603 }
1604 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001605 item = btrfs_item_nr(leaf, slot);
1606 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1607
1608 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001609 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001610 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001611 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001612 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001613 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001614
1615 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001616 advance = 1;
1617 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1618 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001619 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001620 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001621 struct btrfs_key location;
1622
1623 name_len = btrfs_dir_name_len(leaf, di);
1624 if (name_len < 32) {
1625 name_ptr = tmp_name;
1626 } else {
1627 name_ptr = kmalloc(name_len, GFP_NOFS);
1628 BUG_ON(!name_ptr);
1629 }
1630 read_extent_buffer(leaf, name_ptr,
1631 (unsigned long)(di + 1), name_len);
1632
1633 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1634 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001635 over = filldir(dirent, name_ptr, name_len,
1636 found_key.offset,
1637 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001638 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001639
1640 if (name_ptr != tmp_name)
1641 kfree(name_ptr);
1642
Chris Mason39279cc2007-06-12 06:35:45 -04001643 if (over)
1644 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001645 di_len = btrfs_dir_name_len(leaf, di) +
1646 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001647 di_cur += di_len;
1648 di = (struct btrfs_dir_item *)((char *)di + di_len);
1649 }
1650 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001651 if (key_type == BTRFS_DIR_INDEX_KEY)
1652 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1653 else
1654 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001655nopos:
1656 ret = 0;
1657err:
1658 btrfs_release_path(root, path);
1659 btrfs_free_path(path);
1660 mutex_unlock(&root->fs_info->fs_mutex);
1661 return ret;
1662}
1663
1664int btrfs_write_inode(struct inode *inode, int wait)
1665{
1666 struct btrfs_root *root = BTRFS_I(inode)->root;
1667 struct btrfs_trans_handle *trans;
1668 int ret = 0;
1669
1670 if (wait) {
1671 mutex_lock(&root->fs_info->fs_mutex);
1672 trans = btrfs_start_transaction(root, 1);
1673 btrfs_set_trans_block_group(trans, inode);
1674 ret = btrfs_commit_transaction(trans, root);
1675 mutex_unlock(&root->fs_info->fs_mutex);
1676 }
1677 return ret;
1678}
1679
1680/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001681 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001682 * inode changes. But, it is most likely to find the inode in cache.
1683 * FIXME, needs more benchmarking...there are no reasons other than performance
1684 * to keep or drop this code.
1685 */
1686void btrfs_dirty_inode(struct inode *inode)
1687{
1688 struct btrfs_root *root = BTRFS_I(inode)->root;
1689 struct btrfs_trans_handle *trans;
1690
1691 mutex_lock(&root->fs_info->fs_mutex);
1692 trans = btrfs_start_transaction(root, 1);
1693 btrfs_set_trans_block_group(trans, inode);
1694 btrfs_update_inode(trans, root, inode);
1695 btrfs_end_transaction(trans, root);
1696 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001697}
1698
1699static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1700 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001701 const char *name, int name_len,
1702 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001703 u64 objectid,
1704 struct btrfs_block_group_cache *group,
1705 int mode)
1706{
1707 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001708 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001709 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001710 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001711 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001712 struct btrfs_inode_ref *ref;
1713 struct btrfs_key key[2];
1714 u32 sizes[2];
1715 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001716 int ret;
1717 int owner;
1718
Chris Mason5f39d392007-10-15 16:14:19 -04001719 path = btrfs_alloc_path();
1720 BUG_ON(!path);
1721
Chris Mason39279cc2007-06-12 06:35:45 -04001722 inode = new_inode(root->fs_info->sb);
1723 if (!inode)
1724 return ERR_PTR(-ENOMEM);
1725
Chris Masond1310b22008-01-24 16:13:08 -05001726 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1727 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001728 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001729 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1730 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001731 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001732 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001733
Chris Mason39279cc2007-06-12 06:35:45 -04001734 if (mode & S_IFDIR)
1735 owner = 0;
1736 else
1737 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001738 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001739 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001740 if (!new_inode_group) {
1741 printk("find_block group failed\n");
1742 new_inode_group = group;
1743 }
1744 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001745 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001746
1747 key[0].objectid = objectid;
1748 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1749 key[0].offset = 0;
1750
1751 key[1].objectid = objectid;
1752 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1753 key[1].offset = ref_objectid;
1754
1755 sizes[0] = sizeof(struct btrfs_inode_item);
1756 sizes[1] = name_len + sizeof(*ref);
1757
1758 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1759 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001760 goto fail;
1761
Chris Mason9c583092008-01-29 15:15:18 -05001762 if (objectid > root->highest_inode)
1763 root->highest_inode = objectid;
1764
Chris Mason39279cc2007-06-12 06:35:45 -04001765 inode->i_uid = current->fsuid;
1766 inode->i_gid = current->fsgid;
1767 inode->i_mode = mode;
1768 inode->i_ino = objectid;
1769 inode->i_blocks = 0;
1770 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001771 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1772 struct btrfs_inode_item);
1773 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001774
1775 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1776 struct btrfs_inode_ref);
1777 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1778 ptr = (unsigned long)(ref + 1);
1779 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1780
Chris Mason5f39d392007-10-15 16:14:19 -04001781 btrfs_mark_buffer_dirty(path->nodes[0]);
1782 btrfs_free_path(path);
1783
Chris Mason39279cc2007-06-12 06:35:45 -04001784 location = &BTRFS_I(inode)->location;
1785 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001786 location->offset = 0;
1787 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1788
Chris Mason39279cc2007-06-12 06:35:45 -04001789 insert_inode_hash(inode);
1790 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001791fail:
1792 btrfs_free_path(path);
1793 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001794}
1795
1796static inline u8 btrfs_inode_type(struct inode *inode)
1797{
1798 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1799}
1800
1801static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001802 struct dentry *dentry, struct inode *inode,
1803 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001804{
1805 int ret;
1806 struct btrfs_key key;
1807 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001808 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001809
Chris Mason39279cc2007-06-12 06:35:45 -04001810 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001811 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1812 key.offset = 0;
1813
1814 ret = btrfs_insert_dir_item(trans, root,
1815 dentry->d_name.name, dentry->d_name.len,
1816 dentry->d_parent->d_inode->i_ino,
1817 &key, btrfs_inode_type(inode));
1818 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001819 if (add_backref) {
1820 ret = btrfs_insert_inode_ref(trans, root,
1821 dentry->d_name.name,
1822 dentry->d_name.len,
1823 inode->i_ino,
1824 dentry->d_parent->d_inode->i_ino);
1825 }
Chris Mason79c44582007-06-25 10:09:33 -04001826 parent_inode = dentry->d_parent->d_inode;
1827 parent_inode->i_size += dentry->d_name.len * 2;
1828 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001829 ret = btrfs_update_inode(trans, root,
1830 dentry->d_parent->d_inode);
1831 }
1832 return ret;
1833}
1834
1835static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001836 struct dentry *dentry, struct inode *inode,
1837 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001838{
Chris Mason9c583092008-01-29 15:15:18 -05001839 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001840 if (!err) {
1841 d_instantiate(dentry, inode);
1842 return 0;
1843 }
1844 if (err > 0)
1845 err = -EEXIST;
1846 return err;
1847}
1848
Josef Bacik618e21d2007-07-11 10:18:17 -04001849static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1850 int mode, dev_t rdev)
1851{
1852 struct btrfs_trans_handle *trans;
1853 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001854 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001855 int err;
1856 int drop_inode = 0;
1857 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001858 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001859
1860 if (!new_valid_dev(rdev))
1861 return -EINVAL;
1862
1863 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001864 err = btrfs_check_free_space(root, 1, 0);
1865 if (err)
1866 goto fail;
1867
Josef Bacik618e21d2007-07-11 10:18:17 -04001868 trans = btrfs_start_transaction(root, 1);
1869 btrfs_set_trans_block_group(trans, dir);
1870
1871 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1872 if (err) {
1873 err = -ENOSPC;
1874 goto out_unlock;
1875 }
1876
Chris Mason9c583092008-01-29 15:15:18 -05001877 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1878 dentry->d_name.len,
1879 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001880 BTRFS_I(dir)->block_group, mode);
1881 err = PTR_ERR(inode);
1882 if (IS_ERR(inode))
1883 goto out_unlock;
1884
1885 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001886 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001887 if (err)
1888 drop_inode = 1;
1889 else {
1890 inode->i_op = &btrfs_special_inode_operations;
1891 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001892 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001893 }
1894 dir->i_sb->s_dirt = 1;
1895 btrfs_update_inode_block_group(trans, inode);
1896 btrfs_update_inode_block_group(trans, dir);
1897out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001898 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001899 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001900fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001901 mutex_unlock(&root->fs_info->fs_mutex);
1902
1903 if (drop_inode) {
1904 inode_dec_link_count(inode);
1905 iput(inode);
1906 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001907 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001908 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001909 return err;
1910}
1911
Chris Mason39279cc2007-06-12 06:35:45 -04001912static int btrfs_create(struct inode *dir, struct dentry *dentry,
1913 int mode, struct nameidata *nd)
1914{
1915 struct btrfs_trans_handle *trans;
1916 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001917 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001918 int err;
1919 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001920 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001921 u64 objectid;
1922
1923 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001924 err = btrfs_check_free_space(root, 1, 0);
1925 if (err)
1926 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001927 trans = btrfs_start_transaction(root, 1);
1928 btrfs_set_trans_block_group(trans, dir);
1929
1930 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1931 if (err) {
1932 err = -ENOSPC;
1933 goto out_unlock;
1934 }
1935
Chris Mason9c583092008-01-29 15:15:18 -05001936 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1937 dentry->d_name.len,
1938 dentry->d_parent->d_inode->i_ino,
1939 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001940 err = PTR_ERR(inode);
1941 if (IS_ERR(inode))
1942 goto out_unlock;
1943
1944 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001945 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001946 if (err)
1947 drop_inode = 1;
1948 else {
1949 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001950 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001951 inode->i_fop = &btrfs_file_operations;
1952 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001953 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1954 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001955 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001956 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1957 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001958 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001959 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001960 }
1961 dir->i_sb->s_dirt = 1;
1962 btrfs_update_inode_block_group(trans, inode);
1963 btrfs_update_inode_block_group(trans, dir);
1964out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001965 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001966 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001967fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001968 mutex_unlock(&root->fs_info->fs_mutex);
1969
1970 if (drop_inode) {
1971 inode_dec_link_count(inode);
1972 iput(inode);
1973 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001974 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001975 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001976 return err;
1977}
1978
1979static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1980 struct dentry *dentry)
1981{
1982 struct btrfs_trans_handle *trans;
1983 struct btrfs_root *root = BTRFS_I(dir)->root;
1984 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001985 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001986 int err;
1987 int drop_inode = 0;
1988
1989 if (inode->i_nlink == 0)
1990 return -ENOENT;
1991
Chris Mason6da6aba2007-12-18 16:15:09 -05001992#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1993 inode->i_nlink++;
1994#else
Chris Mason39279cc2007-06-12 06:35:45 -04001995 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001996#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001997 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001998 err = btrfs_check_free_space(root, 1, 0);
1999 if (err)
2000 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002001 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002002
Chris Mason39279cc2007-06-12 06:35:45 -04002003 btrfs_set_trans_block_group(trans, dir);
2004 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05002005 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002006
Chris Mason39279cc2007-06-12 06:35:45 -04002007 if (err)
2008 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002009
Chris Mason39279cc2007-06-12 06:35:45 -04002010 dir->i_sb->s_dirt = 1;
2011 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002012 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002013
Chris Mason54aa1f42007-06-22 14:16:25 -04002014 if (err)
2015 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002016
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002017 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002018 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002019fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002020 mutex_unlock(&root->fs_info->fs_mutex);
2021
2022 if (drop_inode) {
2023 inode_dec_link_count(inode);
2024 iput(inode);
2025 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002026 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002027 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002028 return err;
2029}
2030
Chris Mason39279cc2007-06-12 06:35:45 -04002031static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2032{
2033 struct inode *inode;
2034 struct btrfs_trans_handle *trans;
2035 struct btrfs_root *root = BTRFS_I(dir)->root;
2036 int err = 0;
2037 int drop_on_err = 0;
2038 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002039 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002040
2041 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002042 err = btrfs_check_free_space(root, 1, 0);
2043 if (err)
2044 goto out_unlock;
2045
Chris Mason39279cc2007-06-12 06:35:45 -04002046 trans = btrfs_start_transaction(root, 1);
2047 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002048
Chris Mason39279cc2007-06-12 06:35:45 -04002049 if (IS_ERR(trans)) {
2050 err = PTR_ERR(trans);
2051 goto out_unlock;
2052 }
2053
2054 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2055 if (err) {
2056 err = -ENOSPC;
2057 goto out_unlock;
2058 }
2059
Chris Mason9c583092008-01-29 15:15:18 -05002060 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2061 dentry->d_name.len,
2062 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002063 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2064 if (IS_ERR(inode)) {
2065 err = PTR_ERR(inode);
2066 goto out_fail;
2067 }
Chris Mason5f39d392007-10-15 16:14:19 -04002068
Chris Mason39279cc2007-06-12 06:35:45 -04002069 drop_on_err = 1;
2070 inode->i_op = &btrfs_dir_inode_operations;
2071 inode->i_fop = &btrfs_dir_file_operations;
2072 btrfs_set_trans_block_group(trans, inode);
2073
Chris Mason39544012007-12-12 14:38:19 -05002074 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002075 err = btrfs_update_inode(trans, root, inode);
2076 if (err)
2077 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002078
Chris Mason9c583092008-01-29 15:15:18 -05002079 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002080 if (err)
2081 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002082
Chris Mason39279cc2007-06-12 06:35:45 -04002083 d_instantiate(dentry, inode);
2084 drop_on_err = 0;
2085 dir->i_sb->s_dirt = 1;
2086 btrfs_update_inode_block_group(trans, inode);
2087 btrfs_update_inode_block_group(trans, dir);
2088
2089out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002090 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002091 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002092
Chris Mason39279cc2007-06-12 06:35:45 -04002093out_unlock:
2094 mutex_unlock(&root->fs_info->fs_mutex);
2095 if (drop_on_err)
2096 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002097 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002098 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002099 return err;
2100}
2101
Chris Mason3b951512008-04-17 11:29:12 -04002102static int merge_extent_mapping(struct extent_map_tree *em_tree,
2103 struct extent_map *existing,
2104 struct extent_map *em)
2105{
2106 u64 start_diff;
2107 u64 new_end;
2108 int ret = 0;
2109 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2110
2111 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2112 goto invalid;
2113
2114 if (!real_blocks && em->block_start != existing->block_start)
2115 goto invalid;
2116
2117 new_end = max(existing->start + existing->len, em->start + em->len);
2118
2119 if (existing->start >= em->start) {
2120 if (em->start + em->len < existing->start)
2121 goto invalid;
2122
2123 start_diff = existing->start - em->start;
2124 if (real_blocks && em->block_start + start_diff !=
2125 existing->block_start)
2126 goto invalid;
2127
2128 em->len = new_end - em->start;
2129
2130 remove_extent_mapping(em_tree, existing);
2131 /* free for the tree */
2132 free_extent_map(existing);
2133 ret = add_extent_mapping(em_tree, em);
2134
2135 } else if (em->start > existing->start) {
2136
2137 if (existing->start + existing->len < em->start)
2138 goto invalid;
2139
2140 start_diff = em->start - existing->start;
2141 if (real_blocks && existing->block_start + start_diff !=
2142 em->block_start)
2143 goto invalid;
2144
2145 remove_extent_mapping(em_tree, existing);
2146 em->block_start = existing->block_start;
2147 em->start = existing->start;
2148 em->len = new_end - existing->start;
2149 free_extent_map(existing);
2150
2151 ret = add_extent_mapping(em_tree, em);
2152 } else {
2153 goto invalid;
2154 }
2155 return ret;
2156
2157invalid:
2158 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2159 existing->start, existing->len, existing->block_start,
2160 em->start, em->len, em->block_start);
2161 return -EIO;
2162}
2163
Chris Masona52d9a82007-08-27 16:49:44 -04002164struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002165 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002166 int create)
2167{
2168 int ret;
2169 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002170 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002171 u64 extent_start = 0;
2172 u64 extent_end = 0;
2173 u64 objectid = inode->i_ino;
2174 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002175 struct btrfs_path *path;
2176 struct btrfs_root *root = BTRFS_I(inode)->root;
2177 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002178 struct extent_buffer *leaf;
2179 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002180 struct extent_map *em = NULL;
2181 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002182 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002183 struct btrfs_trans_handle *trans = NULL;
2184
2185 path = btrfs_alloc_path();
2186 BUG_ON(!path);
2187 mutex_lock(&root->fs_info->fs_mutex);
2188
2189again:
Chris Masond1310b22008-01-24 16:13:08 -05002190 spin_lock(&em_tree->lock);
2191 em = lookup_extent_mapping(em_tree, start, len);
2192 spin_unlock(&em_tree->lock);
2193
Chris Masona52d9a82007-08-27 16:49:44 -04002194 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002195 if (em->start > start || em->start + em->len <= start)
2196 free_extent_map(em);
2197 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002198 free_extent_map(em);
2199 else
2200 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002201 }
Chris Masond1310b22008-01-24 16:13:08 -05002202 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002203 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002204 err = -ENOMEM;
2205 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002206 }
Chris Masond1310b22008-01-24 16:13:08 -05002207
2208 em->start = EXTENT_MAP_HOLE;
2209 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04002210 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04002211 ret = btrfs_lookup_file_extent(trans, root, path,
2212 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002213 if (ret < 0) {
2214 err = ret;
2215 goto out;
2216 }
2217
2218 if (ret != 0) {
2219 if (path->slots[0] == 0)
2220 goto not_found;
2221 path->slots[0]--;
2222 }
2223
Chris Mason5f39d392007-10-15 16:14:19 -04002224 leaf = path->nodes[0];
2225 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002226 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002227 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002228 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2229 found_type = btrfs_key_type(&found_key);
2230 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002231 found_type != BTRFS_EXTENT_DATA_KEY) {
2232 goto not_found;
2233 }
2234
Chris Mason5f39d392007-10-15 16:14:19 -04002235 found_type = btrfs_file_extent_type(leaf, item);
2236 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002237 if (found_type == BTRFS_FILE_EXTENT_REG) {
2238 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002239 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002240 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002241 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002242 em->start = start;
2243 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002244 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002245 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002246 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002247 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002248 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002249 }
2250 goto not_found_em;
2251 }
Chris Masondb945352007-10-15 16:15:53 -04002252 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2253 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002254 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002255 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002256 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002257 goto insert;
2258 }
Chris Masondb945352007-10-15 16:15:53 -04002259 bytenr += btrfs_file_extent_offset(leaf, item);
2260 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002261 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002262 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002263 goto insert;
2264 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002265 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002266 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002267 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002268 size_t size;
2269 size_t extent_offset;
2270 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002271
Chris Mason5f39d392007-10-15 16:14:19 -04002272 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2273 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002274 extent_end = (extent_start + size + root->sectorsize - 1) &
2275 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002276 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002277 em->start = start;
2278 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002279 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002280 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002281 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002282 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002283 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002284 }
2285 goto not_found_em;
2286 }
2287 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002288
2289 if (!page) {
2290 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002291 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002292 goto out;
2293 }
2294
Chris Mason70dec802008-01-29 09:59:12 -05002295 page_start = page_offset(page) + pg_offset;
2296 extent_offset = page_start - extent_start;
2297 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002298 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002299 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002300 em->len = (copy_size + root->sectorsize - 1) &
2301 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002302 map = kmap(page);
2303 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002304 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002305 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002306 copy_size);
2307 flush_dcache_page(page);
2308 } else if (create && PageUptodate(page)) {
2309 if (!trans) {
2310 kunmap(page);
2311 free_extent_map(em);
2312 em = NULL;
2313 btrfs_release_path(root, path);
2314 trans = btrfs_start_transaction(root, 1);
2315 goto again;
2316 }
Chris Mason70dec802008-01-29 09:59:12 -05002317 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002318 copy_size);
2319 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002320 }
Chris Masona52d9a82007-08-27 16:49:44 -04002321 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002322 set_extent_uptodate(io_tree, em->start,
2323 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002324 goto insert;
2325 } else {
2326 printk("unkknown found_type %d\n", found_type);
2327 WARN_ON(1);
2328 }
2329not_found:
2330 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002331 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002332not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002333 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002334insert:
2335 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002336 if (em->start > start || extent_map_end(em) <= start) {
2337 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002338 err = -EIO;
2339 goto out;
2340 }
Chris Masond1310b22008-01-24 16:13:08 -05002341
2342 err = 0;
2343 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002344 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002345 /* it is possible that someone inserted the extent into the tree
2346 * while we had the lock dropped. It is also possible that
2347 * an overlapping map exists in the tree
2348 */
Chris Masona52d9a82007-08-27 16:49:44 -04002349 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002350 struct extent_map *existing;
2351 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002352 if (existing && (existing->start > start ||
2353 existing->start + existing->len <= start)) {
2354 free_extent_map(existing);
2355 existing = NULL;
2356 }
Chris Mason3b951512008-04-17 11:29:12 -04002357 if (!existing) {
2358 existing = lookup_extent_mapping(em_tree, em->start,
2359 em->len);
2360 if (existing) {
2361 err = merge_extent_mapping(em_tree, existing,
2362 em);
2363 free_extent_map(existing);
2364 if (err) {
2365 free_extent_map(em);
2366 em = NULL;
2367 }
2368 } else {
2369 err = -EIO;
2370 printk("failing to insert %Lu %Lu\n",
2371 start, len);
2372 free_extent_map(em);
2373 em = NULL;
2374 }
2375 } else {
2376 free_extent_map(em);
2377 em = existing;
Chris Masona52d9a82007-08-27 16:49:44 -04002378 }
Chris Masona52d9a82007-08-27 16:49:44 -04002379 }
Chris Masond1310b22008-01-24 16:13:08 -05002380 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002381out:
2382 btrfs_free_path(path);
2383 if (trans) {
2384 ret = btrfs_end_transaction(trans, root);
2385 if (!err)
2386 err = ret;
2387 }
2388 mutex_unlock(&root->fs_info->fs_mutex);
2389 if (err) {
2390 free_extent_map(em);
2391 WARN_ON(1);
2392 return ERR_PTR(err);
2393 }
2394 return em;
2395}
2396
Chris Masone1c4b742008-04-22 13:26:46 -04002397#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002398static int btrfs_get_block(struct inode *inode, sector_t iblock,
2399 struct buffer_head *bh_result, int create)
2400{
2401 struct extent_map *em;
2402 u64 start = (u64)iblock << inode->i_blkbits;
2403 struct btrfs_multi_bio *multi = NULL;
2404 struct btrfs_root *root = BTRFS_I(inode)->root;
2405 u64 len;
2406 u64 logical;
2407 u64 map_length;
2408 int ret = 0;
2409
2410 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2411
2412 if (!em || IS_ERR(em))
2413 goto out;
2414
Chris Masone1c4b742008-04-22 13:26:46 -04002415 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002416 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002417 }
Chris Mason16432982008-04-10 10:23:21 -04002418
2419 if (em->block_start == EXTENT_MAP_INLINE) {
2420 ret = -EINVAL;
2421 goto out;
2422 }
2423
Chris Mason16432982008-04-10 10:23:21 -04002424 len = em->start + em->len - start;
2425 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2426
Chris Masone1c4b742008-04-22 13:26:46 -04002427 if (em->block_start == EXTENT_MAP_HOLE ||
2428 em->block_start == EXTENT_MAP_DELALLOC) {
2429 bh_result->b_size = len;
2430 goto out;
2431 }
2432
Chris Mason16432982008-04-10 10:23:21 -04002433 logical = start - em->start;
2434 logical = em->block_start + logical;
2435
2436 map_length = len;
2437 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2438 logical, &map_length, &multi, 0);
2439 BUG_ON(ret);
2440 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2441 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002442
Chris Mason16432982008-04-10 10:23:21 -04002443 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2444 set_buffer_mapped(bh_result);
2445 kfree(multi);
2446out:
2447 free_extent_map(em);
2448 return ret;
2449}
Chris Masone1c4b742008-04-22 13:26:46 -04002450#endif
Chris Mason16432982008-04-10 10:23:21 -04002451
2452static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2453 const struct iovec *iov, loff_t offset,
2454 unsigned long nr_segs)
2455{
Chris Masone1c4b742008-04-22 13:26:46 -04002456 return -EINVAL;
2457#if 0
Chris Mason16432982008-04-10 10:23:21 -04002458 struct file *file = iocb->ki_filp;
2459 struct inode *inode = file->f_mapping->host;
2460
2461 if (rw == WRITE)
2462 return -EINVAL;
2463
2464 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2465 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002466#endif
Chris Mason16432982008-04-10 10:23:21 -04002467}
2468
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002469static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002470{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002471 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002472}
2473
Chris Mason9ebefb182007-06-15 13:50:00 -04002474int btrfs_readpage(struct file *file, struct page *page)
2475{
Chris Masond1310b22008-01-24 16:13:08 -05002476 struct extent_io_tree *tree;
2477 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002478 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002479}
Chris Mason1832a6d2007-12-21 16:27:21 -05002480
Chris Mason39279cc2007-06-12 06:35:45 -04002481static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2482{
Chris Masond1310b22008-01-24 16:13:08 -05002483 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002484
2485
2486 if (current->flags & PF_MEMALLOC) {
2487 redirty_page_for_writepage(wbc, page);
2488 unlock_page(page);
2489 return 0;
2490 }
Chris Masond1310b22008-01-24 16:13:08 -05002491 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002492 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2493}
Chris Mason39279cc2007-06-12 06:35:45 -04002494
Chris Masonb293f022007-11-01 19:45:34 -04002495static int btrfs_writepages(struct address_space *mapping,
2496 struct writeback_control *wbc)
2497{
Chris Masond1310b22008-01-24 16:13:08 -05002498 struct extent_io_tree *tree;
2499 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002500 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2501}
2502
Chris Mason3ab2fb52007-11-08 10:59:22 -05002503static int
2504btrfs_readpages(struct file *file, struct address_space *mapping,
2505 struct list_head *pages, unsigned nr_pages)
2506{
Chris Masond1310b22008-01-24 16:13:08 -05002507 struct extent_io_tree *tree;
2508 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002509 return extent_readpages(tree, mapping, pages, nr_pages,
2510 btrfs_get_extent);
2511}
2512
Chris Mason70dec802008-01-29 09:59:12 -05002513static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002514{
Chris Masond1310b22008-01-24 16:13:08 -05002515 struct extent_io_tree *tree;
2516 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002517 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002518
Chris Masond1310b22008-01-24 16:13:08 -05002519 tree = &BTRFS_I(page->mapping->host)->io_tree;
2520 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002521 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002522 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002523 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002524 ClearPagePrivate(page);
2525 set_page_private(page, 0);
2526 page_cache_release(page);
2527 }
2528 return ret;
2529}
Chris Mason39279cc2007-06-12 06:35:45 -04002530
Chris Masona52d9a82007-08-27 16:49:44 -04002531static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2532{
Chris Masond1310b22008-01-24 16:13:08 -05002533 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002534
Chris Masond1310b22008-01-24 16:13:08 -05002535 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002536 extent_invalidatepage(tree, page, offset);
2537 btrfs_releasepage(page, GFP_NOFS);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002538 if (PagePrivate(page)) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002539 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002540 ClearPagePrivate(page);
2541 set_page_private(page, 0);
2542 page_cache_release(page);
2543 }
Chris Mason39279cc2007-06-12 06:35:45 -04002544}
2545
Chris Mason9ebefb182007-06-15 13:50:00 -04002546/*
2547 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2548 * called from a page fault handler when a page is first dirtied. Hence we must
2549 * be careful to check for EOF conditions here. We set the page up correctly
2550 * for a written page which means we get ENOSPC checking when writing into
2551 * holes and correct delalloc and unwritten extent mapping on filesystems that
2552 * support these features.
2553 *
2554 * We are not allowed to take the i_mutex here so we have to play games to
2555 * protect against truncate races as the page could now be beyond EOF. Because
2556 * vmtruncate() writes the inode size before removing pages, once we have the
2557 * page lock we can determine safely if the page is beyond EOF. If it is not
2558 * beyond EOF, then the page is guaranteed safe against truncation until we
2559 * unlock the page.
2560 */
2561int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2562{
Chris Mason6da6aba2007-12-18 16:15:09 -05002563 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002564 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002565 unsigned long end;
2566 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002567 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002568 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002569
Chris Mason1832a6d2007-12-21 16:27:21 -05002570 mutex_lock(&root->fs_info->fs_mutex);
2571 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002572 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002573 if (ret)
2574 goto out;
2575
2576 ret = -EINVAL;
2577
Chris Mason9ebefb182007-06-15 13:50:00 -04002578 lock_page(page);
2579 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002580 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002581 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002582
Chris Mason9ebefb182007-06-15 13:50:00 -04002583 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002584 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002585 /* page got truncated out from underneath us */
2586 goto out_unlock;
2587 }
2588
2589 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002590 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002591 end = size & ~PAGE_CACHE_MASK;
2592 else
2593 end = PAGE_CACHE_SIZE;
2594
Chris Masonb888db22007-08-27 16:49:44 -04002595 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002596
2597out_unlock:
2598 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002599out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002600 return ret;
2601}
2602
Chris Mason39279cc2007-06-12 06:35:45 -04002603static void btrfs_truncate(struct inode *inode)
2604{
2605 struct btrfs_root *root = BTRFS_I(inode)->root;
2606 int ret;
2607 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002608 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002609
2610 if (!S_ISREG(inode->i_mode))
2611 return;
2612 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2613 return;
2614
2615 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2616
2617 mutex_lock(&root->fs_info->fs_mutex);
2618 trans = btrfs_start_transaction(root, 1);
2619 btrfs_set_trans_block_group(trans, inode);
2620
2621 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002622 ret = btrfs_truncate_in_trans(trans, root, inode,
2623 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002624 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002625 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002626
Chris Mason39279cc2007-06-12 06:35:45 -04002627 ret = btrfs_end_transaction(trans, root);
2628 BUG_ON(ret);
2629 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002630 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002631 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002632}
2633
Chris Mason4313b392008-01-03 09:08:48 -05002634static int noinline create_subvol(struct btrfs_root *root, char *name,
2635 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002636{
2637 struct btrfs_trans_handle *trans;
2638 struct btrfs_key key;
2639 struct btrfs_root_item root_item;
2640 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002641 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002642 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002643 struct inode *inode;
2644 struct inode *dir;
2645 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002646 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002647 u64 objectid;
2648 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002649 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002650
2651 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002652 ret = btrfs_check_free_space(root, 1, 0);
2653 if (ret)
2654 goto fail_commit;
2655
Chris Mason39279cc2007-06-12 06:35:45 -04002656 trans = btrfs_start_transaction(root, 1);
2657 BUG_ON(!trans);
2658
Chris Mason7bb86312007-12-11 09:25:06 -05002659 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2660 0, &objectid);
2661 if (ret)
2662 goto fail;
2663
2664 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2665 objectid, trans->transid, 0, 0,
2666 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002667 if (IS_ERR(leaf))
2668 return PTR_ERR(leaf);
2669
2670 btrfs_set_header_nritems(leaf, 0);
2671 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002672 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002673 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002674 btrfs_set_header_owner(leaf, objectid);
2675
Chris Mason5f39d392007-10-15 16:14:19 -04002676 write_extent_buffer(leaf, root->fs_info->fsid,
2677 (unsigned long)btrfs_header_fsid(leaf),
2678 BTRFS_FSID_SIZE);
2679 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002680
2681 inode_item = &root_item.inode;
2682 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002683 inode_item->generation = cpu_to_le64(1);
2684 inode_item->size = cpu_to_le64(3);
2685 inode_item->nlink = cpu_to_le32(1);
2686 inode_item->nblocks = cpu_to_le64(1);
2687 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002688
Chris Masondb945352007-10-15 16:15:53 -04002689 btrfs_set_root_bytenr(&root_item, leaf->start);
2690 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002691 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002692 btrfs_set_root_used(&root_item, 0);
2693
Chris Mason5eda7b52007-06-22 14:16:25 -04002694 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2695 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002696
2697 free_extent_buffer(leaf);
2698 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002699
Chris Mason39279cc2007-06-12 06:35:45 -04002700 btrfs_set_root_dirid(&root_item, new_dirid);
2701
2702 key.objectid = objectid;
2703 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002704 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2705 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2706 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002707 if (ret)
2708 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002709
2710 /*
2711 * insert the directory item
2712 */
2713 key.offset = (u64)-1;
2714 dir = root->fs_info->sb->s_root->d_inode;
2715 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2716 name, namelen, dir->i_ino, &key,
2717 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002718 if (ret)
2719 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002720
Chris Mason39544012007-12-12 14:38:19 -05002721 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2722 name, namelen, objectid,
2723 root->fs_info->sb->s_root->d_inode->i_ino);
2724 if (ret)
2725 goto fail;
2726
Chris Mason39279cc2007-06-12 06:35:45 -04002727 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002728 if (ret)
2729 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002730
Josef Bacik58176a92007-08-29 15:47:34 -04002731 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002732 BUG_ON(!new_root);
2733
2734 trans = btrfs_start_transaction(new_root, 1);
2735 BUG_ON(!trans);
2736
Chris Mason9c583092008-01-29 15:15:18 -05002737 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2738 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002739 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002740 if (IS_ERR(inode))
2741 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002742 inode->i_op = &btrfs_dir_inode_operations;
2743 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002744 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002745
Chris Mason39544012007-12-12 14:38:19 -05002746 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2747 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002748 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002749 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002750 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002751 if (ret)
2752 goto fail;
2753fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002754 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002755 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002756 if (err && !ret)
2757 ret = err;
2758fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002759 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002760 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002761 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002762 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002763}
2764
2765static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2766{
Chris Mason3063d292008-01-08 15:46:30 -05002767 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002768 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002769 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002770 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002771 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002772
2773 if (!root->ref_cows)
2774 return -EINVAL;
2775
2776 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002777 ret = btrfs_check_free_space(root, 1, 0);
2778 if (ret)
2779 goto fail_unlock;
2780
Chris Mason3063d292008-01-08 15:46:30 -05002781 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2782 if (!pending_snapshot) {
2783 ret = -ENOMEM;
2784 goto fail_unlock;
2785 }
Yanfb4bc1e2008-01-17 11:59:51 -05002786 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002787 if (!pending_snapshot->name) {
2788 ret = -ENOMEM;
2789 kfree(pending_snapshot);
2790 goto fail_unlock;
2791 }
Yanfb4bc1e2008-01-17 11:59:51 -05002792 memcpy(pending_snapshot->name, name, namelen);
2793 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002794 trans = btrfs_start_transaction(root, 1);
2795 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002796 pending_snapshot->root = root;
2797 list_add(&pending_snapshot->list,
2798 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002799 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002800 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002801
Chris Mason1832a6d2007-12-21 16:27:21 -05002802fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002803 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002804 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002805 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002806 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002807}
2808
Chris Masonedbd8d42007-12-21 16:27:24 -05002809unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002810 struct file_ra_state *ra, struct file *file,
2811 pgoff_t offset, pgoff_t last_index)
2812{
2813 pgoff_t req_size;
2814
2815#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2816 req_size = last_index - offset + 1;
2817 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2818 return offset;
2819#else
2820 req_size = min(last_index - offset + 1, (pgoff_t)128);
2821 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2822 return offset + req_size;
2823#endif
2824}
2825
2826int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002827 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002828 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002829 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002830 struct page *page;
2831 unsigned long last_index;
2832 unsigned long ra_index = 0;
2833 u64 page_start;
2834 u64 page_end;
2835 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002836 int ret;
2837
2838 mutex_lock(&root->fs_info->fs_mutex);
2839 ret = btrfs_check_free_space(root, inode->i_size, 0);
2840 mutex_unlock(&root->fs_info->fs_mutex);
2841 if (ret)
2842 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002843
2844 mutex_lock(&inode->i_mutex);
2845 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2846 for (i = 0; i <= last_index; i++) {
2847 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002848 ra_index = btrfs_force_ra(inode->i_mapping,
2849 &file->f_ra,
2850 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002851 }
2852 page = grab_cache_page(inode->i_mapping, i);
2853 if (!page)
2854 goto out_unlock;
2855 if (!PageUptodate(page)) {
2856 btrfs_readpage(NULL, page);
2857 lock_page(page);
2858 if (!PageUptodate(page)) {
2859 unlock_page(page);
2860 page_cache_release(page);
2861 goto out_unlock;
2862 }
2863 }
Chris Mason35ebb932007-10-30 16:56:53 -04002864 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002865 page_end = page_start + PAGE_CACHE_SIZE - 1;
2866
Chris Masond1310b22008-01-24 16:13:08 -05002867 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002868 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002869 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002870
Chris Masond1310b22008-01-24 16:13:08 -05002871 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002872 set_page_dirty(page);
2873 unlock_page(page);
2874 page_cache_release(page);
2875 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2876 }
2877
2878out_unlock:
2879 mutex_unlock(&inode->i_mutex);
2880 return 0;
2881}
2882
Chris Masonedbd8d42007-12-21 16:27:24 -05002883static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2884{
2885 u64 new_size;
2886 u64 old_size;
2887 struct btrfs_ioctl_vol_args *vol_args;
2888 struct btrfs_trans_handle *trans;
2889 char *sizestr;
2890 int ret = 0;
2891 int namelen;
2892 int mod = 0;
2893
2894 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2895
2896 if (!vol_args)
2897 return -ENOMEM;
2898
2899 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2900 ret = -EFAULT;
2901 goto out;
2902 }
2903 namelen = strlen(vol_args->name);
2904 if (namelen > BTRFS_VOL_NAME_MAX) {
2905 ret = -EINVAL;
2906 goto out;
2907 }
2908
2909 sizestr = vol_args->name;
2910 if (!strcmp(sizestr, "max"))
2911 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2912 else {
2913 if (sizestr[0] == '-') {
2914 mod = -1;
2915 sizestr++;
2916 } else if (sizestr[0] == '+') {
2917 mod = 1;
2918 sizestr++;
2919 }
2920 new_size = btrfs_parse_size(sizestr);
2921 if (new_size == 0) {
2922 ret = -EINVAL;
2923 goto out;
2924 }
2925 }
2926
2927 mutex_lock(&root->fs_info->fs_mutex);
2928 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2929
2930 if (mod < 0) {
2931 if (new_size > old_size) {
2932 ret = -EINVAL;
2933 goto out_unlock;
2934 }
2935 new_size = old_size - new_size;
2936 } else if (mod > 0) {
2937 new_size = old_size + new_size;
2938 }
2939
2940 if (new_size < 256 * 1024 * 1024) {
2941 ret = -EINVAL;
2942 goto out_unlock;
2943 }
2944 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2945 ret = -EFBIG;
2946 goto out_unlock;
2947 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002948
2949 do_div(new_size, root->sectorsize);
2950 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002951
2952printk("new size is %Lu\n", new_size);
2953 if (new_size > old_size) {
2954 trans = btrfs_start_transaction(root, 1);
2955 ret = btrfs_grow_extent_tree(trans, root, new_size);
2956 btrfs_commit_transaction(trans, root);
2957 } else {
2958 ret = btrfs_shrink_extent_tree(root, new_size);
2959 }
2960
2961out_unlock:
2962 mutex_unlock(&root->fs_info->fs_mutex);
2963out:
2964 kfree(vol_args);
2965 return ret;
2966}
2967
Chris Mason4313b392008-01-03 09:08:48 -05002968static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2969 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002970{
Chris Mason4aec2b52007-12-18 16:25:45 -05002971 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002972 struct btrfs_dir_item *di;
2973 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002974 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002975 int namelen;
2976 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002977
Chris Mason4aec2b52007-12-18 16:25:45 -05002978 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002979
Chris Mason4aec2b52007-12-18 16:25:45 -05002980 if (!vol_args)
2981 return -ENOMEM;
2982
2983 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2984 ret = -EFAULT;
2985 goto out;
2986 }
2987
2988 namelen = strlen(vol_args->name);
2989 if (namelen > BTRFS_VOL_NAME_MAX) {
2990 ret = -EINVAL;
2991 goto out;
2992 }
2993 if (strchr(vol_args->name, '/')) {
2994 ret = -EINVAL;
2995 goto out;
2996 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002997
2998 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002999 if (!path) {
3000 ret = -ENOMEM;
3001 goto out;
3002 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003003
3004 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
3005 mutex_lock(&root->fs_info->fs_mutex);
3006 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
3007 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05003008 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003009 mutex_unlock(&root->fs_info->fs_mutex);
3010 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05003011
3012 if (di && !IS_ERR(di)) {
3013 ret = -EEXIST;
3014 goto out;
3015 }
3016
3017 if (IS_ERR(di)) {
3018 ret = PTR_ERR(di);
3019 goto out;
3020 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003021
3022 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05003023 ret = create_subvol(root, vol_args->name, namelen);
3024 else
3025 ret = create_snapshot(root, vol_args->name, namelen);
3026out:
3027 kfree(vol_args);
3028 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003029}
3030
3031static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04003032{
Chris Mason6da6aba2007-12-18 16:15:09 -05003033 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003034 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003035
3036 switch (inode->i_mode & S_IFMT) {
3037 case S_IFDIR:
3038 mutex_lock(&root->fs_info->fs_mutex);
3039 btrfs_defrag_root(root, 0);
3040 btrfs_defrag_root(root->fs_info->extent_root, 0);
3041 mutex_unlock(&root->fs_info->fs_mutex);
3042 break;
3043 case S_IFREG:
3044 btrfs_defrag_file(file);
3045 break;
3046 }
3047
3048 return 0;
3049}
3050
3051long btrfs_ioctl(struct file *file, unsigned int
3052 cmd, unsigned long arg)
3053{
Chris Mason6da6aba2007-12-18 16:15:09 -05003054 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003055
3056 switch (cmd) {
3057 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003058 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04003059 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003060 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05003061 case BTRFS_IOC_RESIZE:
3062 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04003063 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003064
3065 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04003066}
3067
Chris Mason39279cc2007-06-12 06:35:45 -04003068/*
3069 * Called inside transaction, so use GFP_NOFS
3070 */
3071struct inode *btrfs_alloc_inode(struct super_block *sb)
3072{
3073 struct btrfs_inode *ei;
3074
3075 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3076 if (!ei)
3077 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003078 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05003079 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003080 return &ei->vfs_inode;
3081}
3082
3083void btrfs_destroy_inode(struct inode *inode)
3084{
3085 WARN_ON(!list_empty(&inode->i_dentry));
3086 WARN_ON(inode->i_data.nrpages);
3087
Chris Mason8c416c92008-01-14 15:10:26 -05003088 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003089 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3090}
3091
Chris Mason44ec0b72007-10-29 10:55:05 -04003092#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3093static void init_once(struct kmem_cache * cachep, void *foo)
3094#else
Chris Mason39279cc2007-06-12 06:35:45 -04003095static void init_once(void * foo, struct kmem_cache * cachep,
3096 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003097#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003098{
3099 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3100
3101 inode_init_once(&ei->vfs_inode);
3102}
3103
3104void btrfs_destroy_cachep(void)
3105{
3106 if (btrfs_inode_cachep)
3107 kmem_cache_destroy(btrfs_inode_cachep);
3108 if (btrfs_trans_handle_cachep)
3109 kmem_cache_destroy(btrfs_trans_handle_cachep);
3110 if (btrfs_transaction_cachep)
3111 kmem_cache_destroy(btrfs_transaction_cachep);
3112 if (btrfs_bit_radix_cachep)
3113 kmem_cache_destroy(btrfs_bit_radix_cachep);
3114 if (btrfs_path_cachep)
3115 kmem_cache_destroy(btrfs_path_cachep);
3116}
3117
Chris Mason86479a02007-09-10 19:58:16 -04003118struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003119 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04003120#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3121 void (*ctor)(struct kmem_cache *, void *)
3122#else
Chris Mason92fee662007-07-25 12:31:35 -04003123 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003124 unsigned long)
3125#endif
3126 )
Chris Mason92fee662007-07-25 12:31:35 -04003127{
3128 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3129 SLAB_MEM_SPREAD | extra_flags), ctor
3130#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3131 ,NULL
3132#endif
3133 );
3134}
3135
Chris Mason39279cc2007-06-12 06:35:45 -04003136int btrfs_init_cachep(void)
3137{
Chris Mason86479a02007-09-10 19:58:16 -04003138 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003139 sizeof(struct btrfs_inode),
3140 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003141 if (!btrfs_inode_cachep)
3142 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003143 btrfs_trans_handle_cachep =
3144 btrfs_cache_create("btrfs_trans_handle_cache",
3145 sizeof(struct btrfs_trans_handle),
3146 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003147 if (!btrfs_trans_handle_cachep)
3148 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003149 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003150 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003151 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003152 if (!btrfs_transaction_cachep)
3153 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003154 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003155 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003156 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003157 if (!btrfs_path_cachep)
3158 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003159 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003160 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003161 if (!btrfs_bit_radix_cachep)
3162 goto fail;
3163 return 0;
3164fail:
3165 btrfs_destroy_cachep();
3166 return -ENOMEM;
3167}
3168
3169static int btrfs_getattr(struct vfsmount *mnt,
3170 struct dentry *dentry, struct kstat *stat)
3171{
3172 struct inode *inode = dentry->d_inode;
3173 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003174 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003175 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003176 return 0;
3177}
3178
3179static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3180 struct inode * new_dir,struct dentry *new_dentry)
3181{
3182 struct btrfs_trans_handle *trans;
3183 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3184 struct inode *new_inode = new_dentry->d_inode;
3185 struct inode *old_inode = old_dentry->d_inode;
3186 struct timespec ctime = CURRENT_TIME;
3187 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003188 int ret;
3189
3190 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3191 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3192 return -ENOTEMPTY;
3193 }
Chris Mason5f39d392007-10-15 16:14:19 -04003194
Chris Mason39279cc2007-06-12 06:35:45 -04003195 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003196 ret = btrfs_check_free_space(root, 1, 0);
3197 if (ret)
3198 goto out_unlock;
3199
Chris Mason39279cc2007-06-12 06:35:45 -04003200 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003201
Chris Mason39279cc2007-06-12 06:35:45 -04003202 btrfs_set_trans_block_group(trans, new_dir);
3203 path = btrfs_alloc_path();
3204 if (!path) {
3205 ret = -ENOMEM;
3206 goto out_fail;
3207 }
3208
3209 old_dentry->d_inode->i_nlink++;
3210 old_dir->i_ctime = old_dir->i_mtime = ctime;
3211 new_dir->i_ctime = new_dir->i_mtime = ctime;
3212 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003213
Chris Mason39279cc2007-06-12 06:35:45 -04003214 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3215 if (ret)
3216 goto out_fail;
3217
3218 if (new_inode) {
3219 new_inode->i_ctime = CURRENT_TIME;
3220 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3221 if (ret)
3222 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003223 }
Chris Mason9c583092008-01-29 15:15:18 -05003224 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003225 if (ret)
3226 goto out_fail;
3227
3228out_fail:
3229 btrfs_free_path(path);
3230 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003231out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003232 mutex_unlock(&root->fs_info->fs_mutex);
3233 return ret;
3234}
3235
3236static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3237 const char *symname)
3238{
3239 struct btrfs_trans_handle *trans;
3240 struct btrfs_root *root = BTRFS_I(dir)->root;
3241 struct btrfs_path *path;
3242 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003243 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003244 int err;
3245 int drop_inode = 0;
3246 u64 objectid;
3247 int name_len;
3248 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003249 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003250 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003251 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003252 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003253
3254 name_len = strlen(symname) + 1;
3255 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3256 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003257
Chris Mason39279cc2007-06-12 06:35:45 -04003258 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003259 err = btrfs_check_free_space(root, 1, 0);
3260 if (err)
3261 goto out_fail;
3262
Chris Mason39279cc2007-06-12 06:35:45 -04003263 trans = btrfs_start_transaction(root, 1);
3264 btrfs_set_trans_block_group(trans, dir);
3265
3266 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3267 if (err) {
3268 err = -ENOSPC;
3269 goto out_unlock;
3270 }
3271
Chris Mason9c583092008-01-29 15:15:18 -05003272 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3273 dentry->d_name.len,
3274 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003275 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3276 err = PTR_ERR(inode);
3277 if (IS_ERR(inode))
3278 goto out_unlock;
3279
3280 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003281 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003282 if (err)
3283 drop_inode = 1;
3284 else {
3285 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003286 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003287 inode->i_fop = &btrfs_file_operations;
3288 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003289 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3290 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003291 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003292 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3293 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05003294 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003295 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003296 }
3297 dir->i_sb->s_dirt = 1;
3298 btrfs_update_inode_block_group(trans, inode);
3299 btrfs_update_inode_block_group(trans, dir);
3300 if (drop_inode)
3301 goto out_unlock;
3302
3303 path = btrfs_alloc_path();
3304 BUG_ON(!path);
3305 key.objectid = inode->i_ino;
3306 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003307 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3308 datasize = btrfs_file_extent_calc_inline_size(name_len);
3309 err = btrfs_insert_empty_item(trans, root, path, &key,
3310 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003311 if (err) {
3312 drop_inode = 1;
3313 goto out_unlock;
3314 }
Chris Mason5f39d392007-10-15 16:14:19 -04003315 leaf = path->nodes[0];
3316 ei = btrfs_item_ptr(leaf, path->slots[0],
3317 struct btrfs_file_extent_item);
3318 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3319 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003320 BTRFS_FILE_EXTENT_INLINE);
3321 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003322 write_extent_buffer(leaf, symname, ptr, name_len);
3323 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003324 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003325
Chris Mason39279cc2007-06-12 06:35:45 -04003326 inode->i_op = &btrfs_symlink_inode_operations;
3327 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003328 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003329 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04003330 err = btrfs_update_inode(trans, root, inode);
3331 if (err)
3332 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003333
3334out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003335 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04003336 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003337out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003338 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04003339 if (drop_inode) {
3340 inode_dec_link_count(inode);
3341 iput(inode);
3342 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003343 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05003344 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003345 return err;
3346}
Chris Mason16432982008-04-10 10:23:21 -04003347
Yanfdebe2b2008-01-14 13:26:08 -05003348static int btrfs_permission(struct inode *inode, int mask,
3349 struct nameidata *nd)
3350{
3351 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3352 return -EACCES;
3353 return generic_permission(inode, mask, NULL);
3354}
Chris Mason39279cc2007-06-12 06:35:45 -04003355
3356static struct inode_operations btrfs_dir_inode_operations = {
3357 .lookup = btrfs_lookup,
3358 .create = btrfs_create,
3359 .unlink = btrfs_unlink,
3360 .link = btrfs_link,
3361 .mkdir = btrfs_mkdir,
3362 .rmdir = btrfs_rmdir,
3363 .rename = btrfs_rename,
3364 .symlink = btrfs_symlink,
3365 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003366 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003367 .setxattr = generic_setxattr,
3368 .getxattr = generic_getxattr,
3369 .listxattr = btrfs_listxattr,
3370 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003371 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003372};
Chris Mason39279cc2007-06-12 06:35:45 -04003373static struct inode_operations btrfs_dir_ro_inode_operations = {
3374 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003375 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003376};
Chris Mason39279cc2007-06-12 06:35:45 -04003377static struct file_operations btrfs_dir_file_operations = {
3378 .llseek = generic_file_llseek,
3379 .read = generic_read_dir,
3380 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003381 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003382#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003383 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003384#endif
3385};
3386
Chris Masond1310b22008-01-24 16:13:08 -05003387static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003388 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003389 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003390 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003391 .readpage_io_hook = btrfs_readpage_io_hook,
3392 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Mason7e383262008-04-09 16:28:12 -04003393 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003394 .set_bit_hook = btrfs_set_bit_hook,
3395 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003396};
3397
Chris Mason39279cc2007-06-12 06:35:45 -04003398static struct address_space_operations btrfs_aops = {
3399 .readpage = btrfs_readpage,
3400 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003401 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003402 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003403 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003404 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003405 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003406 .invalidatepage = btrfs_invalidatepage,
3407 .releasepage = btrfs_releasepage,
3408 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003409};
3410
3411static struct address_space_operations btrfs_symlink_aops = {
3412 .readpage = btrfs_readpage,
3413 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003414 .invalidatepage = btrfs_invalidatepage,
3415 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003416};
3417
3418static struct inode_operations btrfs_file_inode_operations = {
3419 .truncate = btrfs_truncate,
3420 .getattr = btrfs_getattr,
3421 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003422 .setxattr = generic_setxattr,
3423 .getxattr = generic_getxattr,
3424 .listxattr = btrfs_listxattr,
3425 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003426 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003427};
Josef Bacik618e21d2007-07-11 10:18:17 -04003428static struct inode_operations btrfs_special_inode_operations = {
3429 .getattr = btrfs_getattr,
3430 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003431 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003432};
Chris Mason39279cc2007-06-12 06:35:45 -04003433static struct inode_operations btrfs_symlink_inode_operations = {
3434 .readlink = generic_readlink,
3435 .follow_link = page_follow_link_light,
3436 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003437 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003438};