blob: bbba3350d0232dd7aef11cc456fd7de7d56b0f75 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040037#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050038#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040039#include "ctree.h"
40#include "disk-io.h"
41#include "transaction.h"
42#include "btrfs_inode.h"
43#include "ioctl.h"
44#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040045#include "volumes.h"
Chris Mason39279cc2007-06-12 06:35:45 -040046
47struct btrfs_iget_args {
48 u64 ino;
49 struct btrfs_root *root;
50};
51
52static struct inode_operations btrfs_dir_inode_operations;
53static struct inode_operations btrfs_symlink_inode_operations;
54static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040055static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040056static struct inode_operations btrfs_file_inode_operations;
57static struct address_space_operations btrfs_aops;
58static struct address_space_operations btrfs_symlink_aops;
59static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050060static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040061
62static struct kmem_cache *btrfs_inode_cachep;
63struct kmem_cache *btrfs_trans_handle_cachep;
64struct kmem_cache *btrfs_transaction_cachep;
65struct kmem_cache *btrfs_bit_radix_cachep;
66struct kmem_cache *btrfs_path_cachep;
67
68#define S_SHIFT 12
69static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
70 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
71 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
72 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
73 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
74 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
75 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
76 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
77};
78
Chris Mason1832a6d2007-12-21 16:27:21 -050079int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
80 int for_del)
81{
Chris Masona2135012008-06-25 16:01:30 -040082 u64 total;
83 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050084 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040085 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050086 int ret = 0;
87
Chris Masona2135012008-06-25 16:01:30 -040088 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
89 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
90 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050091 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050092 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050093 else
Chris Masonf9ef6602008-01-03 09:22:38 -050094 thresh = total * 85;
95
96 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050097
Chris Mason1832a6d2007-12-21 16:27:21 -050098 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
99 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400100 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500101 return ret;
102}
103
Chris Masonbe20aa92007-12-17 20:14:01 -0500104static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400105{
106 struct btrfs_root *root = BTRFS_I(inode)->root;
107 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400108 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400109 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500110 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400111 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500112 u64 orig_start = start;
113 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500114 struct btrfs_key ins;
115 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400116
Chris Masonb888db22007-08-27 16:49:44 -0400117 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400118 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500119 btrfs_set_trans_block_group(trans, inode);
120
Chris Masondb945352007-10-15 16:15:53 -0400121 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500122 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400123 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400124 start, start + num_bytes, start, &alloc_hint);
Chris Masond1310b22008-01-24 16:13:08 -0500125 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400126
Chris Mason179e29e2007-11-01 11:28:41 -0400127 if (alloc_hint == EXTENT_MAP_INLINE)
128 goto out;
129
Chris Mason3b951512008-04-17 11:29:12 -0400130 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
131
Chris Masonc59f8952007-12-17 20:14:04 -0500132 while(num_bytes > 0) {
133 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
134 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
Chris Mason98d20f62008-04-14 09:46:10 -0400135 root->sectorsize,
Chris Masonc59f8952007-12-17 20:14:04 -0500136 root->root_key.objectid,
137 trans->transid,
138 inode->i_ino, start, 0,
139 alloc_hint, (u64)-1, &ins, 1);
140 if (ret) {
141 WARN_ON(1);
142 goto out;
143 }
Chris Mason98d20f62008-04-14 09:46:10 -0400144 cur_alloc_size = ins.offset;
Chris Masonc59f8952007-12-17 20:14:04 -0500145 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
146 start, ins.objectid, ins.offset,
Sage Weilf2eb0a22008-05-02 14:43:14 -0400147 ins.offset, 0);
Chris Mason90692182008-02-08 13:49:28 -0500148 inode->i_blocks += ins.offset >> 9;
Chris Mason5f564062008-01-22 16:47:59 -0500149 btrfs_check_file(root, inode);
Chris Mason3b951512008-04-17 11:29:12 -0400150 if (num_bytes < cur_alloc_size) {
151 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
152 cur_alloc_size);
153 break;
154 }
Chris Masonc59f8952007-12-17 20:14:04 -0500155 num_bytes -= cur_alloc_size;
156 alloc_hint = ins.objectid + ins.offset;
157 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400158 }
Chris Masond1310b22008-01-24 16:13:08 -0500159 btrfs_drop_extent_cache(inode, orig_start,
160 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500161 btrfs_add_ordered_inode(inode);
Chris Mason90692182008-02-08 13:49:28 -0500162 btrfs_update_inode(trans, root, inode);
Chris Masonb888db22007-08-27 16:49:44 -0400163out:
164 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500165 return ret;
166}
167
168static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
169{
170 u64 extent_start;
171 u64 extent_end;
172 u64 bytenr;
173 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500174 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500175 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500176 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400177 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500178 struct extent_buffer *leaf;
179 int found_type;
180 struct btrfs_path *path;
181 struct btrfs_file_extent_item *item;
182 int ret;
183 int err;
184 struct btrfs_key found_key;
185
Chris Masonc31f8832008-01-08 15:46:31 -0500186 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500187 path = btrfs_alloc_path();
188 BUG_ON(!path);
189again:
190 ret = btrfs_lookup_file_extent(NULL, root, path,
191 inode->i_ino, start, 0);
192 if (ret < 0) {
193 btrfs_free_path(path);
194 return ret;
195 }
196
197 cow_end = end;
198 if (ret != 0) {
199 if (path->slots[0] == 0)
200 goto not_found;
201 path->slots[0]--;
202 }
203
204 leaf = path->nodes[0];
205 item = btrfs_item_ptr(leaf, path->slots[0],
206 struct btrfs_file_extent_item);
207
208 /* are we inside the extent that was found? */
209 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
210 found_type = btrfs_key_type(&found_key);
211 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400212 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500213 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500214
215 found_type = btrfs_file_extent_type(leaf, item);
216 extent_start = found_key.offset;
217 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500218 u64 extent_num_bytes;
219
220 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
221 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500222 err = 0;
223
Chris Mason1832a6d2007-12-21 16:27:21 -0500224 if (loops && start != extent_start)
225 goto not_found;
226
Chris Masonbe20aa92007-12-17 20:14:01 -0500227 if (start < extent_start || start >= extent_end)
228 goto not_found;
229
230 cow_end = min(end, extent_end - 1);
231 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
232 if (bytenr == 0)
233 goto not_found;
234
Chris Masona68d5932008-05-08 14:11:56 -0400235 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
236 bytenr) != 1) {
237 goto not_found;
238 }
239
Chris Masonc31f8832008-01-08 15:46:31 -0500240 /*
241 * we may be called by the resizer, make sure we're inside
242 * the limits of the FS
243 */
Chris Masona68d5932008-05-08 14:11:56 -0400244 block_group = btrfs_lookup_block_group(root->fs_info,
245 bytenr);
246 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500247 goto not_found;
248
Chris Masonbe20aa92007-12-17 20:14:01 -0500249 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500250 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500251 goto not_found;
252 }
253loop:
254 if (start > end) {
255 btrfs_free_path(path);
256 return 0;
257 }
258 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500259 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500260 goto again;
261
262not_found:
Chris Masonbbaf5492008-05-08 16:31:21 -0400263 cow_file_range(inode, start, end);
264 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500265 goto loop;
266}
267
268static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
269{
270 struct btrfs_root *root = BTRFS_I(inode)->root;
271 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400272
Yanb98b6762008-01-08 15:54:37 -0500273 if (btrfs_test_opt(root, NODATACOW) ||
274 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500275 ret = run_delalloc_nocow(inode, start, end);
276 else
277 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500278
Chris Masonb888db22007-08-27 16:49:44 -0400279 return ret;
280}
281
Chris Mason291d6732008-01-29 15:55:23 -0500282int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500283 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500284{
Chris Masonbcbfce82008-04-22 13:26:47 -0400285 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500286 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500287 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400288 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500289 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500290 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400291 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500292 }
293 return 0;
294}
295
296int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500297 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500298{
Chris Masonb0c68f82008-01-31 11:05:37 -0500299 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500300 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400301 unsigned long flags;
302
303 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500304 if (end - start + 1 > root->fs_info->delalloc_bytes) {
305 printk("warning: delalloc account %Lu %Lu\n",
306 end - start + 1, root->fs_info->delalloc_bytes);
307 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500308 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500309 } else {
310 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500311 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500312 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400313 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500314 }
315 return 0;
316}
317
Chris Mason239b14b2008-03-24 15:02:07 -0400318int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
319 size_t size, struct bio *bio)
320{
321 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
322 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400323 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400324 u64 length = 0;
325 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400326 int ret;
327
Chris Masonf2d8d742008-04-21 10:03:05 -0400328 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400329 map_tree = &root->fs_info->mapping_tree;
330 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400331 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400332 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400333
Chris Mason239b14b2008-03-24 15:02:07 -0400334 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400335 return 1;
336 }
337 return 0;
338}
339
Chris Mason44b8bd72008-04-16 11:14:51 -0400340int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400341 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500342{
Chris Mason065631f2008-02-20 12:07:25 -0500343 struct btrfs_root *root = BTRFS_I(inode)->root;
344 struct btrfs_trans_handle *trans;
345 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400346 char *sums = NULL;
347
348 ret = btrfs_csum_one_bio(root, bio, &sums);
349 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500350
Chris Mason44b8bd72008-04-16 11:14:51 -0400351 trans = btrfs_start_transaction(root, 1);
Chris Masone0156402008-04-16 11:15:20 -0400352
Chris Mason44b8bd72008-04-16 11:14:51 -0400353 btrfs_set_trans_block_group(trans, inode);
Chris Masone0156402008-04-16 11:15:20 -0400354 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
355
Chris Mason44b8bd72008-04-16 11:14:51 -0400356 ret = btrfs_end_transaction(trans, root);
357 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400358
359 kfree(sums);
360
Chris Mason8b712842008-06-11 16:50:36 -0400361 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400362}
363
364int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
365 int mirror_num)
366{
367 struct btrfs_root *root = BTRFS_I(inode)->root;
368 int ret = 0;
369
Chris Mason22c59942008-04-09 16:28:12 -0400370 if (!(rw & (1 << BIO_RW))) {
371 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
372 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400373 goto mapit;
374 }
Chris Mason065631f2008-02-20 12:07:25 -0500375
376 if (btrfs_test_opt(root, NODATASUM) ||
Chris Mason0b86a832008-03-24 15:01:56 -0400377 btrfs_test_flag(inode, NODATASUM)) {
378 goto mapit;
379 }
Chris Mason065631f2008-02-20 12:07:25 -0500380
Chris Mason44b8bd72008-04-16 11:14:51 -0400381 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
382 inode, rw, bio, mirror_num,
383 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400384mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400385 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500386}
Chris Mason6885f302008-02-20 16:11:05 -0500387
Chris Mason07157aa2007-08-30 08:50:51 -0400388int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
389{
390 int ret = 0;
391 struct inode *inode = page->mapping->host;
392 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500393 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400394 struct btrfs_csum_item *item;
395 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400396 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400397
Yanb98b6762008-01-08 15:54:37 -0500398 if (btrfs_test_opt(root, NODATASUM) ||
399 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500400 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400401
Chris Mason07157aa2007-08-30 08:50:51 -0400402 path = btrfs_alloc_path();
403 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
404 if (IS_ERR(item)) {
405 ret = PTR_ERR(item);
406 /* a csum that isn't present is a preallocated region. */
407 if (ret == -ENOENT || ret == -EFBIG)
408 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400409 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500410 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400411 goto out;
412 }
Chris Masonff79f812007-10-15 16:22:25 -0400413 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
414 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500415 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400416out:
417 if (path)
418 btrfs_free_path(path);
Chris Mason07157aa2007-08-30 08:50:51 -0400419 return ret;
420}
421
Chris Mason7e383262008-04-09 16:28:12 -0400422struct io_failure_record {
423 struct page *page;
424 u64 start;
425 u64 len;
426 u64 logical;
427 int last_mirror;
428};
429
Chris Mason1259ab72008-05-12 13:39:03 -0400430int btrfs_io_failed_hook(struct bio *failed_bio,
431 struct page *page, u64 start, u64 end,
432 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400433{
434 struct io_failure_record *failrec = NULL;
435 u64 private;
436 struct extent_map *em;
437 struct inode *inode = page->mapping->host;
438 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400439 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400440 struct bio *bio;
441 int num_copies;
442 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400443 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400444 u64 logical;
445
446 ret = get_state_private(failure_tree, start, &private);
447 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400448 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
449 if (!failrec)
450 return -ENOMEM;
451 failrec->start = start;
452 failrec->len = end - start + 1;
453 failrec->last_mirror = 0;
454
Chris Mason3b951512008-04-17 11:29:12 -0400455 spin_lock(&em_tree->lock);
456 em = lookup_extent_mapping(em_tree, start, failrec->len);
457 if (em->start > start || em->start + em->len < start) {
458 free_extent_map(em);
459 em = NULL;
460 }
461 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400462
463 if (!em || IS_ERR(em)) {
464 kfree(failrec);
465 return -EIO;
466 }
467 logical = start - em->start;
468 logical = em->block_start + logical;
469 failrec->logical = logical;
470 free_extent_map(em);
471 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
472 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400473 set_state_private(failure_tree, start,
474 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400475 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400476 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400477 }
478 num_copies = btrfs_num_copies(
479 &BTRFS_I(inode)->root->fs_info->mapping_tree,
480 failrec->logical, failrec->len);
481 failrec->last_mirror++;
482 if (!state) {
483 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
484 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
485 failrec->start,
486 EXTENT_LOCKED);
487 if (state && state->start != failrec->start)
488 state = NULL;
489 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
490 }
491 if (!state || failrec->last_mirror > num_copies) {
492 set_state_private(failure_tree, failrec->start, 0);
493 clear_extent_bits(failure_tree, failrec->start,
494 failrec->start + failrec->len - 1,
495 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
496 kfree(failrec);
497 return -EIO;
498 }
499 bio = bio_alloc(GFP_NOFS, 1);
500 bio->bi_private = state;
501 bio->bi_end_io = failed_bio->bi_end_io;
502 bio->bi_sector = failrec->logical >> 9;
503 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400504 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400505 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400506 if (failed_bio->bi_rw & (1 << BIO_RW))
507 rw = WRITE;
508 else
509 rw = READ;
510
511 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
512 failrec->last_mirror);
513 return 0;
514}
515
516int btrfs_clean_io_failures(struct inode *inode, u64 start)
517{
518 u64 private;
519 u64 private_failure;
520 struct io_failure_record *failure;
521 int ret;
522
523 private = 0;
524 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
525 (u64)-1, 1, EXTENT_DIRTY)) {
526 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
527 start, &private_failure);
528 if (ret == 0) {
529 failure = (struct io_failure_record *)(unsigned long)
530 private_failure;
531 set_state_private(&BTRFS_I(inode)->io_failure_tree,
532 failure->start, 0);
533 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
534 failure->start,
535 failure->start + failure->len - 1,
536 EXTENT_DIRTY | EXTENT_LOCKED,
537 GFP_NOFS);
538 kfree(failure);
539 }
540 }
Chris Mason7e383262008-04-09 16:28:12 -0400541 return 0;
542}
543
Chris Mason70dec802008-01-29 09:59:12 -0500544int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
545 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400546{
Chris Mason35ebb932007-10-30 16:56:53 -0400547 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400548 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500549 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400550 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500551 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400552 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400553 struct btrfs_root *root = BTRFS_I(inode)->root;
554 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400555 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500556
Yanb98b6762008-01-08 15:54:37 -0500557 if (btrfs_test_opt(root, NODATASUM) ||
558 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500559 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500560 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500561 private = state->private;
562 ret = 0;
563 } else {
564 ret = get_state_private(io_tree, start, &private);
565 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400566 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400567 kaddr = kmap_atomic(page, KM_IRQ0);
568 if (ret) {
569 goto zeroit;
570 }
Chris Masonff79f812007-10-15 16:22:25 -0400571 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
572 btrfs_csum_final(csum, (char *)&csum);
573 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400574 goto zeroit;
575 }
576 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400577 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400578
579 /* if the io failure tree for this inode is non-empty,
580 * check to see if we've recovered from a failed IO
581 */
Chris Mason1259ab72008-05-12 13:39:03 -0400582 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400583 return 0;
584
585zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500586 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
587 page->mapping->host->i_ino, (unsigned long long)start, csum,
588 private);
Chris Masondb945352007-10-15 16:15:53 -0400589 memset(kaddr + offset, 1, end - start + 1);
590 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400591 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400592 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400593 if (private == 0)
594 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400595 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400596}
Chris Masonb888db22007-08-27 16:49:44 -0400597
Chris Mason39279cc2007-06-12 06:35:45 -0400598void btrfs_read_locked_inode(struct inode *inode)
599{
600 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400601 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400602 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400603 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400604 struct btrfs_root *root = BTRFS_I(inode)->root;
605 struct btrfs_key location;
606 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400607 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400608 int ret;
609
610 path = btrfs_alloc_path();
611 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400612 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500613
Chris Mason39279cc2007-06-12 06:35:45 -0400614 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400615 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400616 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400617
Chris Mason5f39d392007-10-15 16:14:19 -0400618 leaf = path->nodes[0];
619 inode_item = btrfs_item_ptr(leaf, path->slots[0],
620 struct btrfs_inode_item);
621
622 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
623 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
624 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
625 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
626 inode->i_size = btrfs_inode_size(leaf, inode_item);
627
628 tspec = btrfs_inode_atime(inode_item);
629 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
630 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
631
632 tspec = btrfs_inode_mtime(inode_item);
633 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
634 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
635
636 tspec = btrfs_inode_ctime(inode_item);
637 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
638 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
639
640 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
641 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400642 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400643 rdev = btrfs_inode_rdev(leaf, inode_item);
644
645 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400646 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
647 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500648 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500649 if (!BTRFS_I(inode)->block_group) {
650 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400651 NULL, 0,
652 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500653 }
Chris Mason39279cc2007-06-12 06:35:45 -0400654 btrfs_free_path(path);
655 inode_item = NULL;
656
Chris Mason39279cc2007-06-12 06:35:45 -0400657 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400658 case S_IFREG:
659 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400660 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500661 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400662 inode->i_fop = &btrfs_file_operations;
663 inode->i_op = &btrfs_file_inode_operations;
664 break;
665 case S_IFDIR:
666 inode->i_fop = &btrfs_dir_file_operations;
667 if (root == root->fs_info->tree_root)
668 inode->i_op = &btrfs_dir_ro_inode_operations;
669 else
670 inode->i_op = &btrfs_dir_inode_operations;
671 break;
672 case S_IFLNK:
673 inode->i_op = &btrfs_symlink_inode_operations;
674 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400675 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400676 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400677 default:
678 init_special_inode(inode, inode->i_mode, rdev);
679 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400680 }
681 return;
682
683make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -0400684 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -0400685 make_bad_inode(inode);
686}
687
Chris Mason5f39d392007-10-15 16:14:19 -0400688static void fill_inode_item(struct extent_buffer *leaf,
689 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400690 struct inode *inode)
691{
Chris Mason5f39d392007-10-15 16:14:19 -0400692 btrfs_set_inode_uid(leaf, item, inode->i_uid);
693 btrfs_set_inode_gid(leaf, item, inode->i_gid);
694 btrfs_set_inode_size(leaf, item, inode->i_size);
695 btrfs_set_inode_mode(leaf, item, inode->i_mode);
696 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
697
698 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
699 inode->i_atime.tv_sec);
700 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
701 inode->i_atime.tv_nsec);
702
703 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
704 inode->i_mtime.tv_sec);
705 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
706 inode->i_mtime.tv_nsec);
707
708 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
709 inode->i_ctime.tv_sec);
710 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
711 inode->i_ctime.tv_nsec);
712
713 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
714 btrfs_set_inode_generation(leaf, item, inode->i_generation);
715 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500716 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400717 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400718 BTRFS_I(inode)->block_group->key.objectid);
719}
720
Chris Masona52d9a82007-08-27 16:49:44 -0400721int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400722 struct btrfs_root *root,
723 struct inode *inode)
724{
725 struct btrfs_inode_item *inode_item;
726 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400727 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400728 int ret;
729
730 path = btrfs_alloc_path();
731 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400732 ret = btrfs_lookup_inode(trans, root, path,
733 &BTRFS_I(inode)->location, 1);
734 if (ret) {
735 if (ret > 0)
736 ret = -ENOENT;
737 goto failed;
738 }
739
Chris Mason5f39d392007-10-15 16:14:19 -0400740 leaf = path->nodes[0];
741 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400742 struct btrfs_inode_item);
743
Chris Mason5f39d392007-10-15 16:14:19 -0400744 fill_inode_item(leaf, inode_item, inode);
745 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400746 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400747 ret = 0;
748failed:
Chris Mason39279cc2007-06-12 06:35:45 -0400749 btrfs_free_path(path);
750 return ret;
751}
752
753
754static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
755 struct btrfs_root *root,
756 struct inode *dir,
757 struct dentry *dentry)
758{
759 struct btrfs_path *path;
760 const char *name = dentry->d_name.name;
761 int name_len = dentry->d_name.len;
762 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400763 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400764 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400765 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400766
767 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400768 if (!path) {
769 ret = -ENOMEM;
770 goto err;
771 }
772
Chris Mason39279cc2007-06-12 06:35:45 -0400773 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
774 name, name_len, -1);
775 if (IS_ERR(di)) {
776 ret = PTR_ERR(di);
777 goto err;
778 }
779 if (!di) {
780 ret = -ENOENT;
781 goto err;
782 }
Chris Mason5f39d392007-10-15 16:14:19 -0400783 leaf = path->nodes[0];
784 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400785 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400786 if (ret)
787 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400788 btrfs_release_path(root, path);
789
790 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400791 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400792 if (IS_ERR(di)) {
793 ret = PTR_ERR(di);
794 goto err;
795 }
796 if (!di) {
797 ret = -ENOENT;
798 goto err;
799 }
800 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -0400801 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400802
803 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500804 ret = btrfs_del_inode_ref(trans, root, name, name_len,
805 dentry->d_inode->i_ino,
806 dentry->d_parent->d_inode->i_ino);
807 if (ret) {
808 printk("failed to delete reference to %.*s, "
809 "inode %lu parent %lu\n", name_len, name,
810 dentry->d_inode->i_ino,
811 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500812 }
Chris Mason39279cc2007-06-12 06:35:45 -0400813err:
814 btrfs_free_path(path);
815 if (!ret) {
816 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400817 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400818 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500819#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
820 dentry->d_inode->i_nlink--;
821#else
Chris Mason39279cc2007-06-12 06:35:45 -0400822 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500823#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400824 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400825 dir->i_sb->s_dirt = 1;
826 }
827 return ret;
828}
829
830static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
831{
832 struct btrfs_root *root;
833 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500834 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400835 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500836 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400837
838 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -0500839
840 ret = btrfs_check_free_space(root, 1, 1);
841 if (ret)
842 goto fail;
843
Chris Mason39279cc2007-06-12 06:35:45 -0400844 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400845
Chris Mason39279cc2007-06-12 06:35:45 -0400846 btrfs_set_trans_block_group(trans, dir);
847 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400848 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400849
Chris Mason2da98f02008-01-16 11:44:43 -0500850 if (inode->i_nlink == 0) {
Chris Mason2da98f02008-01-16 11:44:43 -0500851 /* if the inode isn't linked anywhere,
852 * we don't need to worry about
853 * data=ordered
854 */
Chris Mason594a24e2008-06-25 16:01:30 -0400855 btrfs_del_ordered_inode(inode, 1);
Chris Mason2da98f02008-01-16 11:44:43 -0500856 }
857
Chris Mason89ce8a62008-06-25 16:01:31 -0400858 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500859fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400860 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -0400861 return ret;
862}
863
864static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
865{
866 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500867 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400868 int ret;
869 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400870 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500871 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400872
Chris Mason925baed2008-06-25 16:01:30 -0400873 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -0400874 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -0400875 }
Yan134d4512007-10-25 15:49:25 -0400876
Chris Mason1832a6d2007-12-21 16:27:21 -0500877 ret = btrfs_check_free_space(root, 1, 1);
878 if (ret)
879 goto fail;
880
Chris Mason39279cc2007-06-12 06:35:45 -0400881 trans = btrfs_start_transaction(root, 1);
882 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400883
884 /* now the directory is empty */
885 err = btrfs_unlink_trans(trans, root, dir, dentry);
886 if (!err) {
887 inode->i_size = 0;
888 }
Chris Mason39544012007-12-12 14:38:19 -0500889
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400890 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -0400891 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500892fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400893 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -0500894
Chris Mason39279cc2007-06-12 06:35:45 -0400895 if (ret && !err)
896 err = ret;
897 return err;
898}
899
Chris Mason39279cc2007-06-12 06:35:45 -0400900/*
Chris Mason39279cc2007-06-12 06:35:45 -0400901 * this can truncate away extent items, csum items and directory items.
902 * It starts at a high offset and removes keys until it can't find
903 * any higher than i_size.
904 *
905 * csum items that cross the new i_size are truncated to the new size
906 * as well.
907 */
908static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
909 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500910 struct inode *inode,
911 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400912{
913 int ret;
914 struct btrfs_path *path;
915 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400916 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400917 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400918 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400919 struct btrfs_file_extent_item *fi;
920 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400921 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400922 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500923 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500924 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400925 int found_extent;
926 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500927 int pending_del_nr = 0;
928 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400929 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -0400930 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400931
Chris Mason3b951512008-04-17 11:29:12 -0400932 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400933 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400934 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400935 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400936
Chris Mason39279cc2007-06-12 06:35:45 -0400937 /* FIXME, add redo link to tree so we don't leak on crash */
938 key.objectid = inode->i_ino;
939 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400940 key.type = (u8)-1;
941
Chris Mason85e21ba2008-01-29 15:11:36 -0500942 btrfs_init_path(path);
943search_again:
944 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
945 if (ret < 0) {
946 goto error;
947 }
948 if (ret > 0) {
949 BUG_ON(path->slots[0] == 0);
950 path->slots[0]--;
951 }
952
Chris Mason39279cc2007-06-12 06:35:45 -0400953 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400954 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400955 leaf = path->nodes[0];
956 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
957 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400958
Chris Mason5f39d392007-10-15 16:14:19 -0400959 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400960 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400961
Chris Mason85e21ba2008-01-29 15:11:36 -0500962 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400963 break;
964
Chris Mason5f39d392007-10-15 16:14:19 -0400965 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400966 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400967 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400968 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400969 extent_type = btrfs_file_extent_type(leaf, fi);
970 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400971 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400972 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400973 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
974 struct btrfs_item *item = btrfs_item_nr(leaf,
975 path->slots[0]);
976 item_end += btrfs_file_extent_inline_len(leaf,
977 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400978 }
Yan008630c2007-11-07 13:31:09 -0500979 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400980 }
981 if (found_type == BTRFS_CSUM_ITEM_KEY) {
982 ret = btrfs_csum_truncate(trans, root, path,
983 inode->i_size);
984 BUG_ON(ret);
985 }
Yan008630c2007-11-07 13:31:09 -0500986 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400987 if (found_type == BTRFS_DIR_ITEM_KEY) {
988 found_type = BTRFS_INODE_ITEM_KEY;
989 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
990 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500991 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
992 found_type = BTRFS_XATTR_ITEM_KEY;
993 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
994 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400995 } else if (found_type) {
996 found_type--;
997 } else {
998 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400999 }
Yana61721d2007-09-17 11:08:38 -04001000 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001001 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001002 }
Chris Mason5f39d392007-10-15 16:14:19 -04001003 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001004 del_item = 1;
1005 else
1006 del_item = 0;
1007 found_extent = 0;
1008
1009 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001010 if (found_type != BTRFS_EXTENT_DATA_KEY)
1011 goto delete;
1012
1013 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001014 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001015 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001016 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001017 u64 orig_num_bytes =
1018 btrfs_file_extent_num_bytes(leaf, fi);
1019 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001020 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001021 extent_num_bytes = extent_num_bytes &
1022 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001023 btrfs_set_file_extent_num_bytes(leaf, fi,
1024 extent_num_bytes);
1025 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001026 extent_num_bytes);
1027 if (extent_start != 0)
1028 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001029 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001030 } else {
Chris Masondb945352007-10-15 16:15:53 -04001031 extent_num_bytes =
1032 btrfs_file_extent_disk_num_bytes(leaf,
1033 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001034 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001035 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001036 if (extent_start != 0) {
1037 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001038 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001039 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001040 root_gen = btrfs_header_generation(leaf);
1041 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001042 }
Chris Mason90692182008-02-08 13:49:28 -05001043 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1044 if (!del_item) {
1045 u32 newsize = inode->i_size - found_key.offset;
1046 dec_i_blocks(inode, item_end + 1 -
1047 found_key.offset - newsize);
1048 newsize =
1049 btrfs_file_extent_calc_inline_size(newsize);
1050 ret = btrfs_truncate_item(trans, root, path,
1051 newsize, 1);
1052 BUG_ON(ret);
1053 } else {
1054 dec_i_blocks(inode, item_end + 1 -
1055 found_key.offset);
1056 }
Chris Mason39279cc2007-06-12 06:35:45 -04001057 }
Chris Mason179e29e2007-11-01 11:28:41 -04001058delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001059 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001060 if (!pending_del_nr) {
1061 /* no pending yet, add ourselves */
1062 pending_del_slot = path->slots[0];
1063 pending_del_nr = 1;
1064 } else if (pending_del_nr &&
1065 path->slots[0] + 1 == pending_del_slot) {
1066 /* hop on the pending chunk */
1067 pending_del_nr++;
1068 pending_del_slot = path->slots[0];
1069 } else {
1070 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1071 }
Chris Mason39279cc2007-06-12 06:35:45 -04001072 } else {
1073 break;
1074 }
Chris Mason39279cc2007-06-12 06:35:45 -04001075 if (found_extent) {
1076 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001077 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001078 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001079 root_gen, inode->i_ino,
1080 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001081 BUG_ON(ret);
1082 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001083next:
1084 if (path->slots[0] == 0) {
1085 if (pending_del_nr)
1086 goto del_pending;
1087 btrfs_release_path(root, path);
1088 goto search_again;
1089 }
1090
1091 path->slots[0]--;
1092 if (pending_del_nr &&
1093 path->slots[0] + 1 != pending_del_slot) {
1094 struct btrfs_key debug;
1095del_pending:
1096 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1097 pending_del_slot);
1098 ret = btrfs_del_items(trans, root, path,
1099 pending_del_slot,
1100 pending_del_nr);
1101 BUG_ON(ret);
1102 pending_del_nr = 0;
1103 btrfs_release_path(root, path);
1104 goto search_again;
1105 }
Chris Mason39279cc2007-06-12 06:35:45 -04001106 }
1107 ret = 0;
1108error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001109 if (pending_del_nr) {
1110 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1111 pending_del_nr);
1112 }
Chris Mason39279cc2007-06-12 06:35:45 -04001113 btrfs_free_path(path);
1114 inode->i_sb->s_dirt = 1;
1115 return ret;
1116}
1117
Chris Masonb888db22007-08-27 16:49:44 -04001118static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -04001119 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001120{
Chris Mason39279cc2007-06-12 06:35:45 -04001121 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -05001122 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001123 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001124 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -05001125 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001126
Chris Mason190662b2007-12-18 16:25:45 -05001127 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001128 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001129
Chris Masond1310b22008-01-24 16:13:08 -05001130 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001131 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -04001132 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -05001133
Chris Masona52d9a82007-08-27 16:49:44 -04001134 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -04001135 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001136 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1137 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -04001138 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001139 }
Chris Masonb888db22007-08-27 16:49:44 -04001140 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -05001141 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001142
Chris Masona52d9a82007-08-27 16:49:44 -04001143 return ret;
1144}
1145
1146/*
1147 * taken from block_truncate_page, but does cow as it zeros out
1148 * any bytes left in the last page in the file.
1149 */
1150static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1151{
1152 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001153 struct btrfs_root *root = BTRFS_I(inode)->root;
1154 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001155 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1156 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1157 struct page *page;
1158 int ret = 0;
1159 u64 page_start;
1160
1161 if ((offset & (blocksize - 1)) == 0)
1162 goto out;
1163
1164 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001165again:
Chris Masona52d9a82007-08-27 16:49:44 -04001166 page = grab_cache_page(mapping, index);
1167 if (!page)
1168 goto out;
1169 if (!PageUptodate(page)) {
1170 ret = btrfs_readpage(NULL, page);
1171 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001172 if (page->mapping != mapping) {
1173 unlock_page(page);
1174 page_cache_release(page);
1175 goto again;
1176 }
Chris Masona52d9a82007-08-27 16:49:44 -04001177 if (!PageUptodate(page)) {
1178 ret = -EIO;
1179 goto out;
1180 }
1181 }
Chris Masona52d9a82007-08-27 16:49:44 -04001182
Chris Mason211c17f2008-05-15 09:13:45 -04001183 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1184 wait_on_page_writeback(page);
Chris Masonb888db22007-08-27 16:49:44 -04001185 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001186
Chris Mason39279cc2007-06-12 06:35:45 -04001187 unlock_page(page);
1188 page_cache_release(page);
1189out:
1190 return ret;
1191}
1192
1193static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1194{
1195 struct inode *inode = dentry->d_inode;
1196 int err;
1197
1198 err = inode_change_ok(inode, attr);
1199 if (err)
1200 return err;
1201
1202 if (S_ISREG(inode->i_mode) &&
1203 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1204 struct btrfs_trans_handle *trans;
1205 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001206 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001207
Chris Mason5f39d392007-10-15 16:14:19 -04001208 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001209 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001210 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001211 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001212 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001213
Chris Mason1b0f7c22008-01-30 14:33:02 -05001214 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001215 goto out;
1216
Chris Mason1832a6d2007-12-21 16:27:21 -05001217 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001218 if (err)
1219 goto fail;
1220
Chris Mason39279cc2007-06-12 06:35:45 -04001221 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1222
Chris Mason1b0f7c22008-01-30 14:33:02 -05001223 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001224 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001225
Chris Mason39279cc2007-06-12 06:35:45 -04001226 trans = btrfs_start_transaction(root, 1);
1227 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001228 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001229 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001230 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001231
Chris Mason179e29e2007-11-01 11:28:41 -04001232 if (alloc_hint != EXTENT_MAP_INLINE) {
1233 err = btrfs_insert_file_extent(trans, root,
1234 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001235 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001236 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001237 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001238 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001239 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001240 }
Chris Mason39279cc2007-06-12 06:35:45 -04001241 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001242 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001243 if (err)
1244 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001245 }
1246out:
1247 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001248fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001249 return err;
1250}
Chris Mason61295eb2008-01-14 16:24:38 -05001251
Chris Mason39279cc2007-06-12 06:35:45 -04001252void btrfs_delete_inode(struct inode *inode)
1253{
1254 struct btrfs_trans_handle *trans;
1255 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001256 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001257 int ret;
1258
1259 truncate_inode_pages(&inode->i_data, 0);
1260 if (is_bad_inode(inode)) {
1261 goto no_delete;
1262 }
Chris Mason5f39d392007-10-15 16:14:19 -04001263
Chris Mason39279cc2007-06-12 06:35:45 -04001264 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001265 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001266
Chris Mason39279cc2007-06-12 06:35:45 -04001267 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001268 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001269 if (ret)
1270 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001271
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001272 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001273 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001274
Chris Mason39279cc2007-06-12 06:35:45 -04001275 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001276 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001277 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001278
1279no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001280 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001281 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001282 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001283no_delete:
1284 clear_inode(inode);
1285}
1286
1287/*
1288 * this returns the key found in the dir entry in the location pointer.
1289 * If no dir entries were found, location->objectid is 0.
1290 */
1291static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1292 struct btrfs_key *location)
1293{
1294 const char *name = dentry->d_name.name;
1295 int namelen = dentry->d_name.len;
1296 struct btrfs_dir_item *di;
1297 struct btrfs_path *path;
1298 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001299 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001300
Chris Mason39544012007-12-12 14:38:19 -05001301 if (namelen == 1 && strcmp(name, ".") == 0) {
1302 location->objectid = dir->i_ino;
1303 location->type = BTRFS_INODE_ITEM_KEY;
1304 location->offset = 0;
1305 return 0;
1306 }
Chris Mason39279cc2007-06-12 06:35:45 -04001307 path = btrfs_alloc_path();
1308 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001309
Chris Mason7a720532007-12-13 09:06:59 -05001310 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001311 struct btrfs_key key;
1312 struct extent_buffer *leaf;
1313 u32 nritems;
1314 int slot;
1315
1316 key.objectid = dir->i_ino;
1317 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1318 key.offset = 0;
1319 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1320 BUG_ON(ret == 0);
1321 ret = 0;
1322
1323 leaf = path->nodes[0];
1324 slot = path->slots[0];
1325 nritems = btrfs_header_nritems(leaf);
1326 if (slot >= nritems)
1327 goto out_err;
1328
1329 btrfs_item_key_to_cpu(leaf, &key, slot);
1330 if (key.objectid != dir->i_ino ||
1331 key.type != BTRFS_INODE_REF_KEY) {
1332 goto out_err;
1333 }
1334 location->objectid = key.offset;
1335 location->type = BTRFS_INODE_ITEM_KEY;
1336 location->offset = 0;
1337 goto out;
1338 }
1339
Chris Mason39279cc2007-06-12 06:35:45 -04001340 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1341 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001342 if (IS_ERR(di))
1343 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001344 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001345 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001346 }
Chris Mason5f39d392007-10-15 16:14:19 -04001347 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001348out:
Chris Mason39279cc2007-06-12 06:35:45 -04001349 btrfs_free_path(path);
1350 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001351out_err:
1352 location->objectid = 0;
1353 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001354}
1355
1356/*
1357 * when we hit a tree root in a directory, the btrfs part of the inode
1358 * needs to be changed to reflect the root directory of the tree root. This
1359 * is kind of like crossing a mount point.
1360 */
1361static int fixup_tree_root_location(struct btrfs_root *root,
1362 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001363 struct btrfs_root **sub_root,
1364 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001365{
1366 struct btrfs_path *path;
1367 struct btrfs_root_item *ri;
1368
1369 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1370 return 0;
1371 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1372 return 0;
1373
1374 path = btrfs_alloc_path();
1375 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001376
Josef Bacik58176a92007-08-29 15:47:34 -04001377 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1378 dentry->d_name.name,
1379 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001380 if (IS_ERR(*sub_root))
1381 return PTR_ERR(*sub_root);
1382
1383 ri = &(*sub_root)->root_item;
1384 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001385 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1386 location->offset = 0;
1387
1388 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001389 return 0;
1390}
1391
1392static int btrfs_init_locked_inode(struct inode *inode, void *p)
1393{
1394 struct btrfs_iget_args *args = p;
1395 inode->i_ino = args->ino;
1396 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001397 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001398 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1399 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001400 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001401 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1402 inode->i_mapping, GFP_NOFS);
Chris Mason81d7ed22008-04-25 08:51:48 -04001403 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001404 return 0;
1405}
1406
1407static int btrfs_find_actor(struct inode *inode, void *opaque)
1408{
1409 struct btrfs_iget_args *args = opaque;
1410 return (args->ino == inode->i_ino &&
1411 args->root == BTRFS_I(inode)->root);
1412}
1413
Chris Masondc17ff82008-01-08 15:46:30 -05001414struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1415 u64 root_objectid)
1416{
1417 struct btrfs_iget_args args;
1418 args.ino = objectid;
1419 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1420
1421 if (!args.root)
1422 return NULL;
1423
1424 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1425}
1426
Chris Mason39279cc2007-06-12 06:35:45 -04001427struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1428 struct btrfs_root *root)
1429{
1430 struct inode *inode;
1431 struct btrfs_iget_args args;
1432 args.ino = objectid;
1433 args.root = root;
1434
1435 inode = iget5_locked(s, objectid, btrfs_find_actor,
1436 btrfs_init_locked_inode,
1437 (void *)&args);
1438 return inode;
1439}
1440
1441static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1442 struct nameidata *nd)
1443{
1444 struct inode * inode;
1445 struct btrfs_inode *bi = BTRFS_I(dir);
1446 struct btrfs_root *root = bi->root;
1447 struct btrfs_root *sub_root = root;
1448 struct btrfs_key location;
1449 int ret;
1450
1451 if (dentry->d_name.len > BTRFS_NAME_LEN)
1452 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001453
Chris Mason39279cc2007-06-12 06:35:45 -04001454 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001455
Chris Mason39279cc2007-06-12 06:35:45 -04001456 if (ret < 0)
1457 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001458
Chris Mason39279cc2007-06-12 06:35:45 -04001459 inode = NULL;
1460 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001461 ret = fixup_tree_root_location(root, &location, &sub_root,
1462 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001463 if (ret < 0)
1464 return ERR_PTR(ret);
1465 if (ret > 0)
1466 return ERR_PTR(-ENOENT);
1467 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1468 sub_root);
1469 if (!inode)
1470 return ERR_PTR(-EACCES);
1471 if (inode->i_state & I_NEW) {
1472 /* the inode and parent dir are two different roots */
1473 if (sub_root != root) {
1474 igrab(inode);
1475 sub_root->inode = inode;
1476 }
1477 BTRFS_I(inode)->root = sub_root;
1478 memcpy(&BTRFS_I(inode)->location, &location,
1479 sizeof(location));
1480 btrfs_read_locked_inode(inode);
1481 unlock_new_inode(inode);
1482 }
1483 }
1484 return d_splice_alias(inode, dentry);
1485}
1486
Chris Mason39279cc2007-06-12 06:35:45 -04001487static unsigned char btrfs_filetype_table[] = {
1488 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1489};
1490
1491static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1492{
Chris Mason6da6aba2007-12-18 16:15:09 -05001493 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001494 struct btrfs_root *root = BTRFS_I(inode)->root;
1495 struct btrfs_item *item;
1496 struct btrfs_dir_item *di;
1497 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001498 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001499 struct btrfs_path *path;
1500 int ret;
1501 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001502 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001503 int slot;
1504 int advance;
1505 unsigned char d_type;
1506 int over = 0;
1507 u32 di_cur;
1508 u32 di_total;
1509 u32 di_len;
1510 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001511 char tmp_name[32];
1512 char *name_ptr;
1513 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001514
1515 /* FIXME, use a real flag for deciding about the key type */
1516 if (root->fs_info->tree_root == root)
1517 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001518
Chris Mason39544012007-12-12 14:38:19 -05001519 /* special case for "." */
1520 if (filp->f_pos == 0) {
1521 over = filldir(dirent, ".", 1,
1522 1, inode->i_ino,
1523 DT_DIR);
1524 if (over)
1525 return 0;
1526 filp->f_pos = 1;
1527 }
1528
Chris Mason39279cc2007-06-12 06:35:45 -04001529 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001530 path = btrfs_alloc_path();
1531 path->reada = 2;
1532
1533 /* special case for .., just use the back ref */
1534 if (filp->f_pos == 1) {
1535 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1536 key.offset = 0;
1537 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1538 BUG_ON(ret == 0);
1539 leaf = path->nodes[0];
1540 slot = path->slots[0];
1541 nritems = btrfs_header_nritems(leaf);
1542 if (slot >= nritems) {
1543 btrfs_release_path(root, path);
1544 goto read_dir_items;
1545 }
1546 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1547 btrfs_release_path(root, path);
1548 if (found_key.objectid != key.objectid ||
1549 found_key.type != BTRFS_INODE_REF_KEY)
1550 goto read_dir_items;
1551 over = filldir(dirent, "..", 2,
1552 2, found_key.offset, DT_DIR);
1553 if (over)
1554 goto nopos;
1555 filp->f_pos = 2;
1556 }
1557
1558read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001559 btrfs_set_key_type(&key, key_type);
1560 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001561
Chris Mason39279cc2007-06-12 06:35:45 -04001562 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1563 if (ret < 0)
1564 goto err;
1565 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001566 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001567 leaf = path->nodes[0];
1568 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001569 slot = path->slots[0];
1570 if (advance || slot >= nritems) {
1571 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001572 ret = btrfs_next_leaf(root, path);
1573 if (ret)
1574 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001575 leaf = path->nodes[0];
1576 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001577 slot = path->slots[0];
1578 } else {
1579 slot++;
1580 path->slots[0]++;
1581 }
1582 }
1583 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001584 item = btrfs_item_nr(leaf, slot);
1585 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1586
1587 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001588 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001589 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001590 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001591 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001592 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001593
1594 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001595 advance = 1;
1596 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1597 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001598 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001599 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001600 struct btrfs_key location;
1601
1602 name_len = btrfs_dir_name_len(leaf, di);
1603 if (name_len < 32) {
1604 name_ptr = tmp_name;
1605 } else {
1606 name_ptr = kmalloc(name_len, GFP_NOFS);
1607 BUG_ON(!name_ptr);
1608 }
1609 read_extent_buffer(leaf, name_ptr,
1610 (unsigned long)(di + 1), name_len);
1611
1612 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1613 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001614 over = filldir(dirent, name_ptr, name_len,
1615 found_key.offset,
1616 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001617 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001618
1619 if (name_ptr != tmp_name)
1620 kfree(name_ptr);
1621
Chris Mason39279cc2007-06-12 06:35:45 -04001622 if (over)
1623 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001624 di_len = btrfs_dir_name_len(leaf, di) +
1625 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001626 di_cur += di_len;
1627 di = (struct btrfs_dir_item *)((char *)di + di_len);
1628 }
1629 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001630 if (key_type == BTRFS_DIR_INDEX_KEY)
1631 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1632 else
1633 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001634nopos:
1635 ret = 0;
1636err:
Chris Mason39279cc2007-06-12 06:35:45 -04001637 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001638 return ret;
1639}
1640
1641int btrfs_write_inode(struct inode *inode, int wait)
1642{
1643 struct btrfs_root *root = BTRFS_I(inode)->root;
1644 struct btrfs_trans_handle *trans;
1645 int ret = 0;
1646
1647 if (wait) {
Chris Mason39279cc2007-06-12 06:35:45 -04001648 trans = btrfs_start_transaction(root, 1);
1649 btrfs_set_trans_block_group(trans, inode);
1650 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001651 }
1652 return ret;
1653}
1654
1655/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001656 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001657 * inode changes. But, it is most likely to find the inode in cache.
1658 * FIXME, needs more benchmarking...there are no reasons other than performance
1659 * to keep or drop this code.
1660 */
1661void btrfs_dirty_inode(struct inode *inode)
1662{
1663 struct btrfs_root *root = BTRFS_I(inode)->root;
1664 struct btrfs_trans_handle *trans;
1665
Chris Mason39279cc2007-06-12 06:35:45 -04001666 trans = btrfs_start_transaction(root, 1);
1667 btrfs_set_trans_block_group(trans, inode);
1668 btrfs_update_inode(trans, root, inode);
1669 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001670}
1671
1672static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1673 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001674 const char *name, int name_len,
1675 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001676 u64 objectid,
1677 struct btrfs_block_group_cache *group,
1678 int mode)
1679{
1680 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001681 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001682 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001683 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001684 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001685 struct btrfs_inode_ref *ref;
1686 struct btrfs_key key[2];
1687 u32 sizes[2];
1688 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001689 int ret;
1690 int owner;
1691
Chris Mason5f39d392007-10-15 16:14:19 -04001692 path = btrfs_alloc_path();
1693 BUG_ON(!path);
1694
Chris Mason39279cc2007-06-12 06:35:45 -04001695 inode = new_inode(root->fs_info->sb);
1696 if (!inode)
1697 return ERR_PTR(-ENOMEM);
1698
Chris Masond1310b22008-01-24 16:13:08 -05001699 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1700 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001701 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001702 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1703 inode->i_mapping, GFP_NOFS);
Chris Mason81d7ed22008-04-25 08:51:48 -04001704 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Mason90692182008-02-08 13:49:28 -05001705 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001706 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001707
Chris Mason39279cc2007-06-12 06:35:45 -04001708 if (mode & S_IFDIR)
1709 owner = 0;
1710 else
1711 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001712 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001713 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001714 if (!new_inode_group) {
1715 printk("find_block group failed\n");
1716 new_inode_group = group;
1717 }
1718 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001719 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001720
1721 key[0].objectid = objectid;
1722 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1723 key[0].offset = 0;
1724
1725 key[1].objectid = objectid;
1726 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1727 key[1].offset = ref_objectid;
1728
1729 sizes[0] = sizeof(struct btrfs_inode_item);
1730 sizes[1] = name_len + sizeof(*ref);
1731
1732 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1733 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001734 goto fail;
1735
Chris Mason9c583092008-01-29 15:15:18 -05001736 if (objectid > root->highest_inode)
1737 root->highest_inode = objectid;
1738
Chris Mason39279cc2007-06-12 06:35:45 -04001739 inode->i_uid = current->fsuid;
1740 inode->i_gid = current->fsgid;
1741 inode->i_mode = mode;
1742 inode->i_ino = objectid;
1743 inode->i_blocks = 0;
1744 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001745 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1746 struct btrfs_inode_item);
1747 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001748
1749 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1750 struct btrfs_inode_ref);
1751 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1752 ptr = (unsigned long)(ref + 1);
1753 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1754
Chris Mason5f39d392007-10-15 16:14:19 -04001755 btrfs_mark_buffer_dirty(path->nodes[0]);
1756 btrfs_free_path(path);
1757
Chris Mason39279cc2007-06-12 06:35:45 -04001758 location = &BTRFS_I(inode)->location;
1759 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001760 location->offset = 0;
1761 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1762
Chris Mason39279cc2007-06-12 06:35:45 -04001763 insert_inode_hash(inode);
1764 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001765fail:
1766 btrfs_free_path(path);
1767 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001768}
1769
1770static inline u8 btrfs_inode_type(struct inode *inode)
1771{
1772 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1773}
1774
1775static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001776 struct dentry *dentry, struct inode *inode,
1777 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001778{
1779 int ret;
1780 struct btrfs_key key;
1781 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001782 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001783
Chris Mason39279cc2007-06-12 06:35:45 -04001784 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001785 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1786 key.offset = 0;
1787
1788 ret = btrfs_insert_dir_item(trans, root,
1789 dentry->d_name.name, dentry->d_name.len,
1790 dentry->d_parent->d_inode->i_ino,
1791 &key, btrfs_inode_type(inode));
1792 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001793 if (add_backref) {
1794 ret = btrfs_insert_inode_ref(trans, root,
1795 dentry->d_name.name,
1796 dentry->d_name.len,
1797 inode->i_ino,
1798 dentry->d_parent->d_inode->i_ino);
1799 }
Chris Mason79c44582007-06-25 10:09:33 -04001800 parent_inode = dentry->d_parent->d_inode;
1801 parent_inode->i_size += dentry->d_name.len * 2;
1802 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001803 ret = btrfs_update_inode(trans, root,
1804 dentry->d_parent->d_inode);
1805 }
1806 return ret;
1807}
1808
1809static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001810 struct dentry *dentry, struct inode *inode,
1811 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001812{
Chris Mason9c583092008-01-29 15:15:18 -05001813 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001814 if (!err) {
1815 d_instantiate(dentry, inode);
1816 return 0;
1817 }
1818 if (err > 0)
1819 err = -EEXIST;
1820 return err;
1821}
1822
Josef Bacik618e21d2007-07-11 10:18:17 -04001823static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1824 int mode, dev_t rdev)
1825{
1826 struct btrfs_trans_handle *trans;
1827 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001828 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001829 int err;
1830 int drop_inode = 0;
1831 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001832 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001833
1834 if (!new_valid_dev(rdev))
1835 return -EINVAL;
1836
Chris Mason1832a6d2007-12-21 16:27:21 -05001837 err = btrfs_check_free_space(root, 1, 0);
1838 if (err)
1839 goto fail;
1840
Josef Bacik618e21d2007-07-11 10:18:17 -04001841 trans = btrfs_start_transaction(root, 1);
1842 btrfs_set_trans_block_group(trans, dir);
1843
1844 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1845 if (err) {
1846 err = -ENOSPC;
1847 goto out_unlock;
1848 }
1849
Chris Mason9c583092008-01-29 15:15:18 -05001850 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1851 dentry->d_name.len,
1852 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001853 BTRFS_I(dir)->block_group, mode);
1854 err = PTR_ERR(inode);
1855 if (IS_ERR(inode))
1856 goto out_unlock;
1857
1858 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001859 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001860 if (err)
1861 drop_inode = 1;
1862 else {
1863 inode->i_op = &btrfs_special_inode_operations;
1864 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001865 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001866 }
1867 dir->i_sb->s_dirt = 1;
1868 btrfs_update_inode_block_group(trans, inode);
1869 btrfs_update_inode_block_group(trans, dir);
1870out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001871 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001872 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001873fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001874 if (drop_inode) {
1875 inode_dec_link_count(inode);
1876 iput(inode);
1877 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001878 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04001879 return err;
1880}
1881
Chris Mason39279cc2007-06-12 06:35:45 -04001882static int btrfs_create(struct inode *dir, struct dentry *dentry,
1883 int mode, struct nameidata *nd)
1884{
1885 struct btrfs_trans_handle *trans;
1886 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001887 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001888 int err;
1889 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001890 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001891 u64 objectid;
1892
Chris Mason1832a6d2007-12-21 16:27:21 -05001893 err = btrfs_check_free_space(root, 1, 0);
1894 if (err)
1895 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001896 trans = btrfs_start_transaction(root, 1);
1897 btrfs_set_trans_block_group(trans, dir);
1898
1899 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1900 if (err) {
1901 err = -ENOSPC;
1902 goto out_unlock;
1903 }
1904
Chris Mason9c583092008-01-29 15:15:18 -05001905 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1906 dentry->d_name.len,
1907 dentry->d_parent->d_inode->i_ino,
1908 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001909 err = PTR_ERR(inode);
1910 if (IS_ERR(inode))
1911 goto out_unlock;
1912
1913 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001914 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001915 if (err)
1916 drop_inode = 1;
1917 else {
1918 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001919 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001920 inode->i_fop = &btrfs_file_operations;
1921 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001922 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1923 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001924 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001925 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1926 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001927 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason81d7ed22008-04-25 08:51:48 -04001928 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001929 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001930 }
1931 dir->i_sb->s_dirt = 1;
1932 btrfs_update_inode_block_group(trans, inode);
1933 btrfs_update_inode_block_group(trans, dir);
1934out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001935 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001936 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001937fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001938 if (drop_inode) {
1939 inode_dec_link_count(inode);
1940 iput(inode);
1941 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001942 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001943 return err;
1944}
1945
1946static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1947 struct dentry *dentry)
1948{
1949 struct btrfs_trans_handle *trans;
1950 struct btrfs_root *root = BTRFS_I(dir)->root;
1951 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001952 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001953 int err;
1954 int drop_inode = 0;
1955
1956 if (inode->i_nlink == 0)
1957 return -ENOENT;
1958
Chris Mason6da6aba2007-12-18 16:15:09 -05001959#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1960 inode->i_nlink++;
1961#else
Chris Mason39279cc2007-06-12 06:35:45 -04001962 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001963#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05001964 err = btrfs_check_free_space(root, 1, 0);
1965 if (err)
1966 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001967 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001968
Chris Mason39279cc2007-06-12 06:35:45 -04001969 btrfs_set_trans_block_group(trans, dir);
1970 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05001971 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001972
Chris Mason39279cc2007-06-12 06:35:45 -04001973 if (err)
1974 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001975
Chris Mason39279cc2007-06-12 06:35:45 -04001976 dir->i_sb->s_dirt = 1;
1977 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001978 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001979
Chris Mason54aa1f42007-06-22 14:16:25 -04001980 if (err)
1981 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001982
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001983 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001984 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001985fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001986 if (drop_inode) {
1987 inode_dec_link_count(inode);
1988 iput(inode);
1989 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001990 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001991 return err;
1992}
1993
Chris Mason39279cc2007-06-12 06:35:45 -04001994static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1995{
Chris Masonb9d86662008-05-02 16:13:49 -04001996 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001997 struct btrfs_trans_handle *trans;
1998 struct btrfs_root *root = BTRFS_I(dir)->root;
1999 int err = 0;
2000 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002001 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002002 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002003
Chris Mason1832a6d2007-12-21 16:27:21 -05002004 err = btrfs_check_free_space(root, 1, 0);
2005 if (err)
2006 goto out_unlock;
2007
Chris Mason39279cc2007-06-12 06:35:45 -04002008 trans = btrfs_start_transaction(root, 1);
2009 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002010
Chris Mason39279cc2007-06-12 06:35:45 -04002011 if (IS_ERR(trans)) {
2012 err = PTR_ERR(trans);
2013 goto out_unlock;
2014 }
2015
2016 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2017 if (err) {
2018 err = -ENOSPC;
2019 goto out_unlock;
2020 }
2021
Chris Mason9c583092008-01-29 15:15:18 -05002022 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2023 dentry->d_name.len,
2024 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002025 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2026 if (IS_ERR(inode)) {
2027 err = PTR_ERR(inode);
2028 goto out_fail;
2029 }
Chris Mason5f39d392007-10-15 16:14:19 -04002030
Chris Mason39279cc2007-06-12 06:35:45 -04002031 drop_on_err = 1;
2032 inode->i_op = &btrfs_dir_inode_operations;
2033 inode->i_fop = &btrfs_dir_file_operations;
2034 btrfs_set_trans_block_group(trans, inode);
2035
Chris Mason39544012007-12-12 14:38:19 -05002036 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002037 err = btrfs_update_inode(trans, root, inode);
2038 if (err)
2039 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002040
Chris Mason9c583092008-01-29 15:15:18 -05002041 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002042 if (err)
2043 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002044
Chris Mason39279cc2007-06-12 06:35:45 -04002045 d_instantiate(dentry, inode);
2046 drop_on_err = 0;
2047 dir->i_sb->s_dirt = 1;
2048 btrfs_update_inode_block_group(trans, inode);
2049 btrfs_update_inode_block_group(trans, dir);
2050
2051out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002052 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002053 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002054
Chris Mason39279cc2007-06-12 06:35:45 -04002055out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002056 if (drop_on_err)
2057 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002058 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002059 return err;
2060}
2061
Chris Mason3b951512008-04-17 11:29:12 -04002062static int merge_extent_mapping(struct extent_map_tree *em_tree,
2063 struct extent_map *existing,
2064 struct extent_map *em)
2065{
2066 u64 start_diff;
2067 u64 new_end;
2068 int ret = 0;
2069 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2070
2071 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2072 goto invalid;
2073
2074 if (!real_blocks && em->block_start != existing->block_start)
2075 goto invalid;
2076
2077 new_end = max(existing->start + existing->len, em->start + em->len);
2078
2079 if (existing->start >= em->start) {
2080 if (em->start + em->len < existing->start)
2081 goto invalid;
2082
2083 start_diff = existing->start - em->start;
2084 if (real_blocks && em->block_start + start_diff !=
2085 existing->block_start)
2086 goto invalid;
2087
2088 em->len = new_end - em->start;
2089
2090 remove_extent_mapping(em_tree, existing);
2091 /* free for the tree */
2092 free_extent_map(existing);
2093 ret = add_extent_mapping(em_tree, em);
2094
2095 } else if (em->start > existing->start) {
2096
2097 if (existing->start + existing->len < em->start)
2098 goto invalid;
2099
2100 start_diff = em->start - existing->start;
2101 if (real_blocks && existing->block_start + start_diff !=
2102 em->block_start)
2103 goto invalid;
2104
2105 remove_extent_mapping(em_tree, existing);
2106 em->block_start = existing->block_start;
2107 em->start = existing->start;
2108 em->len = new_end - existing->start;
2109 free_extent_map(existing);
2110
2111 ret = add_extent_mapping(em_tree, em);
2112 } else {
2113 goto invalid;
2114 }
2115 return ret;
2116
2117invalid:
2118 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2119 existing->start, existing->len, existing->block_start,
2120 em->start, em->len, em->block_start);
2121 return -EIO;
2122}
2123
Chris Masona52d9a82007-08-27 16:49:44 -04002124struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002125 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002126 int create)
2127{
2128 int ret;
2129 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002130 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002131 u64 extent_start = 0;
2132 u64 extent_end = 0;
2133 u64 objectid = inode->i_ino;
2134 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002135 struct btrfs_path *path;
2136 struct btrfs_root *root = BTRFS_I(inode)->root;
2137 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002138 struct extent_buffer *leaf;
2139 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002140 struct extent_map *em = NULL;
2141 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002142 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002143 struct btrfs_trans_handle *trans = NULL;
2144
2145 path = btrfs_alloc_path();
2146 BUG_ON(!path);
Chris Masona52d9a82007-08-27 16:49:44 -04002147
2148again:
Chris Masond1310b22008-01-24 16:13:08 -05002149 spin_lock(&em_tree->lock);
2150 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002151 if (em)
2152 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002153 spin_unlock(&em_tree->lock);
2154
Chris Masona52d9a82007-08-27 16:49:44 -04002155 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002156 if (em->start > start || em->start + em->len <= start)
2157 free_extent_map(em);
2158 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002159 free_extent_map(em);
2160 else
2161 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002162 }
Chris Masond1310b22008-01-24 16:13:08 -05002163 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002164 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002165 err = -ENOMEM;
2166 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002167 }
Chris Masond1310b22008-01-24 16:13:08 -05002168
2169 em->start = EXTENT_MAP_HOLE;
2170 em->len = (u64)-1;
Chris Masona061fc82008-05-07 11:43:44 -04002171 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04002172 ret = btrfs_lookup_file_extent(trans, root, path,
2173 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002174 if (ret < 0) {
2175 err = ret;
2176 goto out;
2177 }
2178
2179 if (ret != 0) {
2180 if (path->slots[0] == 0)
2181 goto not_found;
2182 path->slots[0]--;
2183 }
2184
Chris Mason5f39d392007-10-15 16:14:19 -04002185 leaf = path->nodes[0];
2186 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002187 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002188 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002189 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2190 found_type = btrfs_key_type(&found_key);
2191 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002192 found_type != BTRFS_EXTENT_DATA_KEY) {
2193 goto not_found;
2194 }
2195
Chris Mason5f39d392007-10-15 16:14:19 -04002196 found_type = btrfs_file_extent_type(leaf, item);
2197 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002198 if (found_type == BTRFS_FILE_EXTENT_REG) {
2199 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002200 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002201 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002202 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002203 em->start = start;
2204 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002205 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002206 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002207 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002208 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002209 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002210 }
2211 goto not_found_em;
2212 }
Chris Masondb945352007-10-15 16:15:53 -04002213 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2214 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002215 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002216 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002217 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002218 goto insert;
2219 }
Chris Masondb945352007-10-15 16:15:53 -04002220 bytenr += btrfs_file_extent_offset(leaf, item);
2221 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002222 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002223 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002224 goto insert;
2225 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002226 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002227 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002228 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002229 size_t size;
2230 size_t extent_offset;
2231 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002232
Chris Mason5f39d392007-10-15 16:14:19 -04002233 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2234 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002235 extent_end = (extent_start + size + root->sectorsize - 1) &
2236 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002237 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002238 em->start = start;
2239 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002240 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002241 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002242 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002243 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002244 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002245 }
2246 goto not_found_em;
2247 }
2248 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002249
2250 if (!page) {
2251 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002252 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002253 goto out;
2254 }
2255
Chris Mason70dec802008-01-29 09:59:12 -05002256 page_start = page_offset(page) + pg_offset;
2257 extent_offset = page_start - extent_start;
2258 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002259 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002260 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002261 em->len = (copy_size + root->sectorsize - 1) &
2262 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002263 map = kmap(page);
2264 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002265 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002266 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002267 copy_size);
2268 flush_dcache_page(page);
2269 } else if (create && PageUptodate(page)) {
2270 if (!trans) {
2271 kunmap(page);
2272 free_extent_map(em);
2273 em = NULL;
2274 btrfs_release_path(root, path);
2275 trans = btrfs_start_transaction(root, 1);
2276 goto again;
2277 }
Chris Mason70dec802008-01-29 09:59:12 -05002278 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002279 copy_size);
2280 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002281 }
Chris Masona52d9a82007-08-27 16:49:44 -04002282 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002283 set_extent_uptodate(io_tree, em->start,
2284 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002285 goto insert;
2286 } else {
2287 printk("unkknown found_type %d\n", found_type);
2288 WARN_ON(1);
2289 }
2290not_found:
2291 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002292 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002293not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002294 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002295insert:
2296 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002297 if (em->start > start || extent_map_end(em) <= start) {
2298 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002299 err = -EIO;
2300 goto out;
2301 }
Chris Masond1310b22008-01-24 16:13:08 -05002302
2303 err = 0;
2304 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002305 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002306 /* it is possible that someone inserted the extent into the tree
2307 * while we had the lock dropped. It is also possible that
2308 * an overlapping map exists in the tree
2309 */
Chris Masona52d9a82007-08-27 16:49:44 -04002310 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002311 struct extent_map *existing;
2312 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002313 if (existing && (existing->start > start ||
2314 existing->start + existing->len <= start)) {
2315 free_extent_map(existing);
2316 existing = NULL;
2317 }
Chris Mason3b951512008-04-17 11:29:12 -04002318 if (!existing) {
2319 existing = lookup_extent_mapping(em_tree, em->start,
2320 em->len);
2321 if (existing) {
2322 err = merge_extent_mapping(em_tree, existing,
2323 em);
2324 free_extent_map(existing);
2325 if (err) {
2326 free_extent_map(em);
2327 em = NULL;
2328 }
2329 } else {
2330 err = -EIO;
2331 printk("failing to insert %Lu %Lu\n",
2332 start, len);
2333 free_extent_map(em);
2334 em = NULL;
2335 }
2336 } else {
2337 free_extent_map(em);
2338 em = existing;
Chris Masona52d9a82007-08-27 16:49:44 -04002339 }
Chris Masona52d9a82007-08-27 16:49:44 -04002340 }
Chris Masond1310b22008-01-24 16:13:08 -05002341 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002342out:
2343 btrfs_free_path(path);
2344 if (trans) {
2345 ret = btrfs_end_transaction(trans, root);
2346 if (!err)
2347 err = ret;
2348 }
Chris Masona52d9a82007-08-27 16:49:44 -04002349 if (err) {
2350 free_extent_map(em);
2351 WARN_ON(1);
2352 return ERR_PTR(err);
2353 }
2354 return em;
2355}
2356
Chris Masone1c4b742008-04-22 13:26:46 -04002357#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002358static int btrfs_get_block(struct inode *inode, sector_t iblock,
2359 struct buffer_head *bh_result, int create)
2360{
2361 struct extent_map *em;
2362 u64 start = (u64)iblock << inode->i_blkbits;
2363 struct btrfs_multi_bio *multi = NULL;
2364 struct btrfs_root *root = BTRFS_I(inode)->root;
2365 u64 len;
2366 u64 logical;
2367 u64 map_length;
2368 int ret = 0;
2369
2370 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2371
2372 if (!em || IS_ERR(em))
2373 goto out;
2374
Chris Masone1c4b742008-04-22 13:26:46 -04002375 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002376 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002377 }
Chris Mason16432982008-04-10 10:23:21 -04002378
2379 if (em->block_start == EXTENT_MAP_INLINE) {
2380 ret = -EINVAL;
2381 goto out;
2382 }
2383
Chris Mason16432982008-04-10 10:23:21 -04002384 len = em->start + em->len - start;
2385 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2386
Chris Masone1c4b742008-04-22 13:26:46 -04002387 if (em->block_start == EXTENT_MAP_HOLE ||
2388 em->block_start == EXTENT_MAP_DELALLOC) {
2389 bh_result->b_size = len;
2390 goto out;
2391 }
2392
Chris Mason16432982008-04-10 10:23:21 -04002393 logical = start - em->start;
2394 logical = em->block_start + logical;
2395
2396 map_length = len;
2397 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2398 logical, &map_length, &multi, 0);
2399 BUG_ON(ret);
2400 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2401 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002402
Chris Mason16432982008-04-10 10:23:21 -04002403 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2404 set_buffer_mapped(bh_result);
2405 kfree(multi);
2406out:
2407 free_extent_map(em);
2408 return ret;
2409}
Chris Masone1c4b742008-04-22 13:26:46 -04002410#endif
Chris Mason16432982008-04-10 10:23:21 -04002411
2412static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2413 const struct iovec *iov, loff_t offset,
2414 unsigned long nr_segs)
2415{
Chris Masone1c4b742008-04-22 13:26:46 -04002416 return -EINVAL;
2417#if 0
Chris Mason16432982008-04-10 10:23:21 -04002418 struct file *file = iocb->ki_filp;
2419 struct inode *inode = file->f_mapping->host;
2420
2421 if (rw == WRITE)
2422 return -EINVAL;
2423
2424 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2425 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002426#endif
Chris Mason16432982008-04-10 10:23:21 -04002427}
2428
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002429static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002430{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002431 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002432}
2433
Chris Mason9ebefb182007-06-15 13:50:00 -04002434int btrfs_readpage(struct file *file, struct page *page)
2435{
Chris Masond1310b22008-01-24 16:13:08 -05002436 struct extent_io_tree *tree;
2437 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002438 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002439}
Chris Mason1832a6d2007-12-21 16:27:21 -05002440
Chris Mason39279cc2007-06-12 06:35:45 -04002441static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2442{
Chris Masond1310b22008-01-24 16:13:08 -05002443 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002444
2445
2446 if (current->flags & PF_MEMALLOC) {
2447 redirty_page_for_writepage(wbc, page);
2448 unlock_page(page);
2449 return 0;
2450 }
Chris Masond1310b22008-01-24 16:13:08 -05002451 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002452 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2453}
Chris Mason39279cc2007-06-12 06:35:45 -04002454
Chris Masonb293f022007-11-01 19:45:34 -04002455static int btrfs_writepages(struct address_space *mapping,
2456 struct writeback_control *wbc)
2457{
Chris Masond1310b22008-01-24 16:13:08 -05002458 struct extent_io_tree *tree;
2459 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002460 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2461}
2462
Chris Mason3ab2fb52007-11-08 10:59:22 -05002463static int
2464btrfs_readpages(struct file *file, struct address_space *mapping,
2465 struct list_head *pages, unsigned nr_pages)
2466{
Chris Masond1310b22008-01-24 16:13:08 -05002467 struct extent_io_tree *tree;
2468 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002469 return extent_readpages(tree, mapping, pages, nr_pages,
2470 btrfs_get_extent);
2471}
2472
Chris Mason70dec802008-01-29 09:59:12 -05002473static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002474{
Chris Masond1310b22008-01-24 16:13:08 -05002475 struct extent_io_tree *tree;
2476 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002477 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002478
Chris Masond1310b22008-01-24 16:13:08 -05002479 tree = &BTRFS_I(page->mapping->host)->io_tree;
2480 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002481 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002482 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002483 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002484 ClearPagePrivate(page);
2485 set_page_private(page, 0);
2486 page_cache_release(page);
2487 }
2488 return ret;
2489}
Chris Mason39279cc2007-06-12 06:35:45 -04002490
Chris Masona52d9a82007-08-27 16:49:44 -04002491static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2492{
Chris Masond1310b22008-01-24 16:13:08 -05002493 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002494
Chris Masond1310b22008-01-24 16:13:08 -05002495 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002496 extent_invalidatepage(tree, page, offset);
2497 btrfs_releasepage(page, GFP_NOFS);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002498 if (PagePrivate(page)) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002499 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002500 ClearPagePrivate(page);
2501 set_page_private(page, 0);
2502 page_cache_release(page);
2503 }
Chris Mason39279cc2007-06-12 06:35:45 -04002504}
2505
Chris Mason9ebefb182007-06-15 13:50:00 -04002506/*
2507 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2508 * called from a page fault handler when a page is first dirtied. Hence we must
2509 * be careful to check for EOF conditions here. We set the page up correctly
2510 * for a written page which means we get ENOSPC checking when writing into
2511 * holes and correct delalloc and unwritten extent mapping on filesystems that
2512 * support these features.
2513 *
2514 * We are not allowed to take the i_mutex here so we have to play games to
2515 * protect against truncate races as the page could now be beyond EOF. Because
2516 * vmtruncate() writes the inode size before removing pages, once we have the
2517 * page lock we can determine safely if the page is beyond EOF. If it is not
2518 * beyond EOF, then the page is guaranteed safe against truncation until we
2519 * unlock the page.
2520 */
2521int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2522{
Chris Mason6da6aba2007-12-18 16:15:09 -05002523 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002524 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002525 unsigned long end;
2526 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002527 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002528 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002529
Chris Mason1832a6d2007-12-21 16:27:21 -05002530 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05002531 if (ret)
2532 goto out;
2533
2534 ret = -EINVAL;
2535
Chris Mason9ebefb182007-06-15 13:50:00 -04002536 lock_page(page);
2537 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002538 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002539 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002540
Chris Mason9ebefb182007-06-15 13:50:00 -04002541 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002542 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002543 /* page got truncated out from underneath us */
2544 goto out_unlock;
2545 }
2546
2547 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002548 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002549 end = size & ~PAGE_CACHE_MASK;
2550 else
2551 end = PAGE_CACHE_SIZE;
2552
Chris Masonb888db22007-08-27 16:49:44 -04002553 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002554
2555out_unlock:
2556 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002557out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002558 return ret;
2559}
2560
Chris Mason39279cc2007-06-12 06:35:45 -04002561static void btrfs_truncate(struct inode *inode)
2562{
2563 struct btrfs_root *root = BTRFS_I(inode)->root;
2564 int ret;
2565 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002566 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002567
2568 if (!S_ISREG(inode->i_mode))
2569 return;
2570 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2571 return;
2572
2573 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2574
Chris Mason39279cc2007-06-12 06:35:45 -04002575 trans = btrfs_start_transaction(root, 1);
2576 btrfs_set_trans_block_group(trans, inode);
2577
2578 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002579 ret = btrfs_truncate_in_trans(trans, root, inode,
2580 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002581 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002582 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002583
Chris Mason89ce8a62008-06-25 16:01:31 -04002584 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002585 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002586 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002587}
2588
Sven Wegener3b963622008-06-09 21:57:42 -04002589/*
2590 * Invalidate a single dcache entry at the root of the filesystem.
2591 * Needed after creation of snapshot or subvolume.
2592 */
2593void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2594 int namelen)
2595{
2596 struct dentry *alias, *entry;
2597 struct qstr qstr;
2598
2599 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2600 if (alias) {
2601 qstr.name = name;
2602 qstr.len = namelen;
2603 /* change me if btrfs ever gets a d_hash operation */
2604 qstr.hash = full_name_hash(qstr.name, qstr.len);
2605 entry = d_lookup(alias, &qstr);
2606 dput(alias);
2607 if (entry) {
2608 d_invalidate(entry);
2609 dput(entry);
2610 }
2611 }
2612}
2613
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002614int btrfs_create_subvol_root(struct btrfs_root *new_root,
2615 struct btrfs_trans_handle *trans, u64 new_dirid,
2616 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04002617{
Chris Mason39279cc2007-06-12 06:35:45 -04002618 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002619 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002620
Chris Mason9c583092008-01-29 15:15:18 -05002621 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002622 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002623 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002624 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002625 inode->i_op = &btrfs_dir_inode_operations;
2626 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002627 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002628
Chris Mason39544012007-12-12 14:38:19 -05002629 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2630 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002631 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002632 inode->i_size = 0;
Sven Wegener3b963622008-06-09 21:57:42 -04002633
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002634 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002635}
2636
Chris Masonedbd8d42007-12-21 16:27:24 -05002637unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002638 struct file_ra_state *ra, struct file *file,
2639 pgoff_t offset, pgoff_t last_index)
2640{
Chris Mason8e7bf942008-04-28 09:02:36 -04002641 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04002642
2643#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04002644 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2645 return offset;
2646#else
Chris Mason86479a02007-09-10 19:58:16 -04002647 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2648 return offset + req_size;
2649#endif
2650}
2651
Chris Mason39279cc2007-06-12 06:35:45 -04002652struct inode *btrfs_alloc_inode(struct super_block *sb)
2653{
2654 struct btrfs_inode *ei;
2655
2656 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2657 if (!ei)
2658 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002659 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002660 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002661 return &ei->vfs_inode;
2662}
2663
2664void btrfs_destroy_inode(struct inode *inode)
2665{
2666 WARN_ON(!list_empty(&inode->i_dentry));
2667 WARN_ON(inode->i_data.nrpages);
2668
Chris Mason8c416c92008-01-14 15:10:26 -05002669 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002670 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2671}
2672
Chris Mason44ec0b72007-10-29 10:55:05 -04002673#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2674static void init_once(struct kmem_cache * cachep, void *foo)
2675#else
Chris Mason39279cc2007-06-12 06:35:45 -04002676static void init_once(void * foo, struct kmem_cache * cachep,
2677 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002678#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002679{
2680 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2681
2682 inode_init_once(&ei->vfs_inode);
2683}
2684
2685void btrfs_destroy_cachep(void)
2686{
2687 if (btrfs_inode_cachep)
2688 kmem_cache_destroy(btrfs_inode_cachep);
2689 if (btrfs_trans_handle_cachep)
2690 kmem_cache_destroy(btrfs_trans_handle_cachep);
2691 if (btrfs_transaction_cachep)
2692 kmem_cache_destroy(btrfs_transaction_cachep);
2693 if (btrfs_bit_radix_cachep)
2694 kmem_cache_destroy(btrfs_bit_radix_cachep);
2695 if (btrfs_path_cachep)
2696 kmem_cache_destroy(btrfs_path_cachep);
2697}
2698
Chris Mason86479a02007-09-10 19:58:16 -04002699struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002700 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002701#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2702 void (*ctor)(struct kmem_cache *, void *)
2703#else
Chris Mason92fee662007-07-25 12:31:35 -04002704 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002705 unsigned long)
2706#endif
2707 )
Chris Mason92fee662007-07-25 12:31:35 -04002708{
2709 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2710 SLAB_MEM_SPREAD | extra_flags), ctor
2711#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2712 ,NULL
2713#endif
2714 );
2715}
2716
Chris Mason39279cc2007-06-12 06:35:45 -04002717int btrfs_init_cachep(void)
2718{
Chris Mason86479a02007-09-10 19:58:16 -04002719 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002720 sizeof(struct btrfs_inode),
2721 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002722 if (!btrfs_inode_cachep)
2723 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002724 btrfs_trans_handle_cachep =
2725 btrfs_cache_create("btrfs_trans_handle_cache",
2726 sizeof(struct btrfs_trans_handle),
2727 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002728 if (!btrfs_trans_handle_cachep)
2729 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002730 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002731 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002732 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002733 if (!btrfs_transaction_cachep)
2734 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002735 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002736 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002737 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002738 if (!btrfs_path_cachep)
2739 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002740 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002741 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002742 if (!btrfs_bit_radix_cachep)
2743 goto fail;
2744 return 0;
2745fail:
2746 btrfs_destroy_cachep();
2747 return -ENOMEM;
2748}
2749
2750static int btrfs_getattr(struct vfsmount *mnt,
2751 struct dentry *dentry, struct kstat *stat)
2752{
2753 struct inode *inode = dentry->d_inode;
2754 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002755 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05002756 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04002757 return 0;
2758}
2759
2760static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2761 struct inode * new_dir,struct dentry *new_dentry)
2762{
2763 struct btrfs_trans_handle *trans;
2764 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2765 struct inode *new_inode = new_dentry->d_inode;
2766 struct inode *old_inode = old_dentry->d_inode;
2767 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002768 int ret;
2769
2770 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2771 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2772 return -ENOTEMPTY;
2773 }
Chris Mason5f39d392007-10-15 16:14:19 -04002774
Chris Mason1832a6d2007-12-21 16:27:21 -05002775 ret = btrfs_check_free_space(root, 1, 0);
2776 if (ret)
2777 goto out_unlock;
2778
Chris Mason39279cc2007-06-12 06:35:45 -04002779 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002780
Chris Mason39279cc2007-06-12 06:35:45 -04002781 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002782
2783 old_dentry->d_inode->i_nlink++;
2784 old_dir->i_ctime = old_dir->i_mtime = ctime;
2785 new_dir->i_ctime = new_dir->i_mtime = ctime;
2786 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002787
Chris Mason39279cc2007-06-12 06:35:45 -04002788 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2789 if (ret)
2790 goto out_fail;
2791
2792 if (new_inode) {
2793 new_inode->i_ctime = CURRENT_TIME;
2794 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2795 if (ret)
2796 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002797 }
Chris Mason9c583092008-01-29 15:15:18 -05002798 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002799 if (ret)
2800 goto out_fail;
2801
2802out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002803 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002804out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002805 return ret;
2806}
2807
2808static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2809 const char *symname)
2810{
2811 struct btrfs_trans_handle *trans;
2812 struct btrfs_root *root = BTRFS_I(dir)->root;
2813 struct btrfs_path *path;
2814 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002815 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002816 int err;
2817 int drop_inode = 0;
2818 u64 objectid;
2819 int name_len;
2820 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002821 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002822 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002823 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002824 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002825
2826 name_len = strlen(symname) + 1;
2827 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2828 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002829
Chris Mason1832a6d2007-12-21 16:27:21 -05002830 err = btrfs_check_free_space(root, 1, 0);
2831 if (err)
2832 goto out_fail;
2833
Chris Mason39279cc2007-06-12 06:35:45 -04002834 trans = btrfs_start_transaction(root, 1);
2835 btrfs_set_trans_block_group(trans, dir);
2836
2837 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2838 if (err) {
2839 err = -ENOSPC;
2840 goto out_unlock;
2841 }
2842
Chris Mason9c583092008-01-29 15:15:18 -05002843 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2844 dentry->d_name.len,
2845 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002846 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2847 err = PTR_ERR(inode);
2848 if (IS_ERR(inode))
2849 goto out_unlock;
2850
2851 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002852 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002853 if (err)
2854 drop_inode = 1;
2855 else {
2856 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002857 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002858 inode->i_fop = &btrfs_file_operations;
2859 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002860 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2861 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002862 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002863 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2864 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05002865 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason81d7ed22008-04-25 08:51:48 -04002866 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002867 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002868 }
2869 dir->i_sb->s_dirt = 1;
2870 btrfs_update_inode_block_group(trans, inode);
2871 btrfs_update_inode_block_group(trans, dir);
2872 if (drop_inode)
2873 goto out_unlock;
2874
2875 path = btrfs_alloc_path();
2876 BUG_ON(!path);
2877 key.objectid = inode->i_ino;
2878 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002879 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2880 datasize = btrfs_file_extent_calc_inline_size(name_len);
2881 err = btrfs_insert_empty_item(trans, root, path, &key,
2882 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002883 if (err) {
2884 drop_inode = 1;
2885 goto out_unlock;
2886 }
Chris Mason5f39d392007-10-15 16:14:19 -04002887 leaf = path->nodes[0];
2888 ei = btrfs_item_ptr(leaf, path->slots[0],
2889 struct btrfs_file_extent_item);
2890 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2891 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002892 BTRFS_FILE_EXTENT_INLINE);
2893 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002894 write_extent_buffer(leaf, symname, ptr, name_len);
2895 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002896 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002897
Chris Mason39279cc2007-06-12 06:35:45 -04002898 inode->i_op = &btrfs_symlink_inode_operations;
2899 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002900 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002901 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002902 err = btrfs_update_inode(trans, root, inode);
2903 if (err)
2904 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002905
2906out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002907 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002908 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002909out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002910 if (drop_inode) {
2911 inode_dec_link_count(inode);
2912 iput(inode);
2913 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002914 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002915 return err;
2916}
Chris Mason16432982008-04-10 10:23:21 -04002917
Yanfdebe2b2008-01-14 13:26:08 -05002918static int btrfs_permission(struct inode *inode, int mask,
2919 struct nameidata *nd)
2920{
2921 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2922 return -EACCES;
2923 return generic_permission(inode, mask, NULL);
2924}
Chris Mason39279cc2007-06-12 06:35:45 -04002925
2926static struct inode_operations btrfs_dir_inode_operations = {
2927 .lookup = btrfs_lookup,
2928 .create = btrfs_create,
2929 .unlink = btrfs_unlink,
2930 .link = btrfs_link,
2931 .mkdir = btrfs_mkdir,
2932 .rmdir = btrfs_rmdir,
2933 .rename = btrfs_rename,
2934 .symlink = btrfs_symlink,
2935 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002936 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002937 .setxattr = generic_setxattr,
2938 .getxattr = generic_getxattr,
2939 .listxattr = btrfs_listxattr,
2940 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002941 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002942};
Chris Mason39279cc2007-06-12 06:35:45 -04002943static struct inode_operations btrfs_dir_ro_inode_operations = {
2944 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05002945 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002946};
Chris Mason39279cc2007-06-12 06:35:45 -04002947static struct file_operations btrfs_dir_file_operations = {
2948 .llseek = generic_file_llseek,
2949 .read = generic_read_dir,
2950 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002951 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002952#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002953 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002954#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04002955 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04002956};
2957
Chris Masond1310b22008-01-24 16:13:08 -05002958static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04002959 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05002960 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04002961 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04002962 .readpage_io_hook = btrfs_readpage_io_hook,
2963 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04002964 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05002965 .set_bit_hook = btrfs_set_bit_hook,
2966 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04002967};
2968
Chris Mason39279cc2007-06-12 06:35:45 -04002969static struct address_space_operations btrfs_aops = {
2970 .readpage = btrfs_readpage,
2971 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002972 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002973 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002974 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04002975 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04002976 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04002977 .invalidatepage = btrfs_invalidatepage,
2978 .releasepage = btrfs_releasepage,
2979 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002980};
2981
2982static struct address_space_operations btrfs_symlink_aops = {
2983 .readpage = btrfs_readpage,
2984 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002985 .invalidatepage = btrfs_invalidatepage,
2986 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002987};
2988
2989static struct inode_operations btrfs_file_inode_operations = {
2990 .truncate = btrfs_truncate,
2991 .getattr = btrfs_getattr,
2992 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05002993 .setxattr = generic_setxattr,
2994 .getxattr = generic_getxattr,
2995 .listxattr = btrfs_listxattr,
2996 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002997 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002998};
Josef Bacik618e21d2007-07-11 10:18:17 -04002999static struct inode_operations btrfs_special_inode_operations = {
3000 .getattr = btrfs_getattr,
3001 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003002 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003003};
Chris Mason39279cc2007-06-12 06:35:45 -04003004static struct inode_operations btrfs_symlink_inode_operations = {
3005 .readlink = generic_readlink,
3006 .follow_link = page_follow_link_light,
3007 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003008 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003009};