blob: cf27b5984627fc7b92fe2f41c5d32fe62e94e62b [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 Masond3c2fdc2007-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 Mason39279cc2007-06-12 06:35:45 -0400858 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500859fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -0400860 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500861 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400862 return ret;
863}
864
865static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
866{
867 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500868 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400869 int ret;
870 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400871 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500872 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400873
Chris Mason925baed2008-06-25 16:01:30 -0400874 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -0400875 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -0400876 }
Yan134d4512007-10-25 15:49:25 -0400877
Chris Mason1832a6d2007-12-21 16:27:21 -0500878 ret = btrfs_check_free_space(root, 1, 1);
879 if (ret)
880 goto fail;
881
Chris Mason39279cc2007-06-12 06:35:45 -0400882 trans = btrfs_start_transaction(root, 1);
883 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400884
885 /* now the directory is empty */
886 err = btrfs_unlink_trans(trans, root, dir, dentry);
887 if (!err) {
888 inode->i_size = 0;
889 }
Chris Mason39544012007-12-12 14:38:19 -0500890
Chris Masond3c2fdc2007-09-17 10:58:06 -0400891 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400892 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500893fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -0400894 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500895 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500896
Chris Mason39279cc2007-06-12 06:35:45 -0400897 if (ret && !err)
898 err = ret;
899 return err;
900}
901
Chris Mason39279cc2007-06-12 06:35:45 -0400902/*
Chris Mason39279cc2007-06-12 06:35:45 -0400903 * this can truncate away extent items, csum items and directory items.
904 * It starts at a high offset and removes keys until it can't find
905 * any higher than i_size.
906 *
907 * csum items that cross the new i_size are truncated to the new size
908 * as well.
909 */
910static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
911 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500912 struct inode *inode,
913 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400914{
915 int ret;
916 struct btrfs_path *path;
917 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400918 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400919 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400920 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400921 struct btrfs_file_extent_item *fi;
922 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400923 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400924 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500925 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500926 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400927 int found_extent;
928 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500929 int pending_del_nr = 0;
930 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400931 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -0400932 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400933
Chris Mason3b951512008-04-17 11:29:12 -0400934 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400935 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400936 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400937 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400938
Chris Mason39279cc2007-06-12 06:35:45 -0400939 /* FIXME, add redo link to tree so we don't leak on crash */
940 key.objectid = inode->i_ino;
941 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400942 key.type = (u8)-1;
943
Chris Mason85e21ba2008-01-29 15:11:36 -0500944 btrfs_init_path(path);
945search_again:
946 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
947 if (ret < 0) {
948 goto error;
949 }
950 if (ret > 0) {
951 BUG_ON(path->slots[0] == 0);
952 path->slots[0]--;
953 }
954
Chris Mason39279cc2007-06-12 06:35:45 -0400955 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400956 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400957 leaf = path->nodes[0];
958 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
959 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400960
Chris Mason5f39d392007-10-15 16:14:19 -0400961 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400962 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400963
Chris Mason85e21ba2008-01-29 15:11:36 -0500964 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400965 break;
966
Chris Mason5f39d392007-10-15 16:14:19 -0400967 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400968 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400969 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400970 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400971 extent_type = btrfs_file_extent_type(leaf, fi);
972 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400973 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400974 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400975 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
976 struct btrfs_item *item = btrfs_item_nr(leaf,
977 path->slots[0]);
978 item_end += btrfs_file_extent_inline_len(leaf,
979 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400980 }
Yan008630c2007-11-07 13:31:09 -0500981 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400982 }
983 if (found_type == BTRFS_CSUM_ITEM_KEY) {
984 ret = btrfs_csum_truncate(trans, root, path,
985 inode->i_size);
986 BUG_ON(ret);
987 }
Yan008630c2007-11-07 13:31:09 -0500988 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400989 if (found_type == BTRFS_DIR_ITEM_KEY) {
990 found_type = BTRFS_INODE_ITEM_KEY;
991 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
992 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500993 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
994 found_type = BTRFS_XATTR_ITEM_KEY;
995 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
996 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400997 } else if (found_type) {
998 found_type--;
999 } else {
1000 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001001 }
Yana61721d2007-09-17 11:08:38 -04001002 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001003 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001004 }
Chris Mason5f39d392007-10-15 16:14:19 -04001005 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001006 del_item = 1;
1007 else
1008 del_item = 0;
1009 found_extent = 0;
1010
1011 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001012 if (found_type != BTRFS_EXTENT_DATA_KEY)
1013 goto delete;
1014
1015 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001016 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001017 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001018 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001019 u64 orig_num_bytes =
1020 btrfs_file_extent_num_bytes(leaf, fi);
1021 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001022 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -05001023 extent_num_bytes = extent_num_bytes &
1024 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001025 btrfs_set_file_extent_num_bytes(leaf, fi,
1026 extent_num_bytes);
1027 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001028 extent_num_bytes);
1029 if (extent_start != 0)
1030 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001031 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001032 } else {
Chris Masondb945352007-10-15 16:15:53 -04001033 extent_num_bytes =
1034 btrfs_file_extent_disk_num_bytes(leaf,
1035 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001036 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001037 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001038 if (extent_start != 0) {
1039 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001040 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001041 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001042 root_gen = btrfs_header_generation(leaf);
1043 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001044 }
Chris Mason90692182008-02-08 13:49:28 -05001045 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1046 if (!del_item) {
1047 u32 newsize = inode->i_size - found_key.offset;
1048 dec_i_blocks(inode, item_end + 1 -
1049 found_key.offset - newsize);
1050 newsize =
1051 btrfs_file_extent_calc_inline_size(newsize);
1052 ret = btrfs_truncate_item(trans, root, path,
1053 newsize, 1);
1054 BUG_ON(ret);
1055 } else {
1056 dec_i_blocks(inode, item_end + 1 -
1057 found_key.offset);
1058 }
Chris Mason39279cc2007-06-12 06:35:45 -04001059 }
Chris Mason179e29e2007-11-01 11:28:41 -04001060delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001061 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001062 if (!pending_del_nr) {
1063 /* no pending yet, add ourselves */
1064 pending_del_slot = path->slots[0];
1065 pending_del_nr = 1;
1066 } else if (pending_del_nr &&
1067 path->slots[0] + 1 == pending_del_slot) {
1068 /* hop on the pending chunk */
1069 pending_del_nr++;
1070 pending_del_slot = path->slots[0];
1071 } else {
1072 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1073 }
Chris Mason39279cc2007-06-12 06:35:45 -04001074 } else {
1075 break;
1076 }
Chris Mason39279cc2007-06-12 06:35:45 -04001077 if (found_extent) {
1078 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001079 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001080 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001081 root_gen, inode->i_ino,
1082 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001083 BUG_ON(ret);
1084 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001085next:
1086 if (path->slots[0] == 0) {
1087 if (pending_del_nr)
1088 goto del_pending;
1089 btrfs_release_path(root, path);
1090 goto search_again;
1091 }
1092
1093 path->slots[0]--;
1094 if (pending_del_nr &&
1095 path->slots[0] + 1 != pending_del_slot) {
1096 struct btrfs_key debug;
1097del_pending:
1098 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1099 pending_del_slot);
1100 ret = btrfs_del_items(trans, root, path,
1101 pending_del_slot,
1102 pending_del_nr);
1103 BUG_ON(ret);
1104 pending_del_nr = 0;
1105 btrfs_release_path(root, path);
1106 goto search_again;
1107 }
Chris Mason39279cc2007-06-12 06:35:45 -04001108 }
1109 ret = 0;
1110error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001111 if (pending_del_nr) {
1112 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1113 pending_del_nr);
1114 }
Chris Mason39279cc2007-06-12 06:35:45 -04001115 btrfs_free_path(path);
1116 inode->i_sb->s_dirt = 1;
1117 return ret;
1118}
1119
Chris Masonb888db22007-08-27 16:49:44 -04001120static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -04001121 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001122{
Chris Mason39279cc2007-06-12 06:35:45 -04001123 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -05001124 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001125 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001126 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -05001127 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001128
Chris Mason190662b2007-12-18 16:25:45 -05001129 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001130 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001131
Chris Masond1310b22008-01-24 16:13:08 -05001132 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001133 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -04001134 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -05001135
Chris Masona52d9a82007-08-27 16:49:44 -04001136 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -04001137 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001138 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1139 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -04001140 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001141 }
Chris Masonb888db22007-08-27 16:49:44 -04001142 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -05001143 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001144
Chris Masona52d9a82007-08-27 16:49:44 -04001145 return ret;
1146}
1147
1148/*
1149 * taken from block_truncate_page, but does cow as it zeros out
1150 * any bytes left in the last page in the file.
1151 */
1152static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1153{
1154 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001155 struct btrfs_root *root = BTRFS_I(inode)->root;
1156 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001157 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1158 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1159 struct page *page;
1160 int ret = 0;
1161 u64 page_start;
1162
1163 if ((offset & (blocksize - 1)) == 0)
1164 goto out;
1165
1166 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001167again:
Chris Masona52d9a82007-08-27 16:49:44 -04001168 page = grab_cache_page(mapping, index);
1169 if (!page)
1170 goto out;
1171 if (!PageUptodate(page)) {
1172 ret = btrfs_readpage(NULL, page);
1173 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001174 if (page->mapping != mapping) {
1175 unlock_page(page);
1176 page_cache_release(page);
1177 goto again;
1178 }
Chris Masona52d9a82007-08-27 16:49:44 -04001179 if (!PageUptodate(page)) {
1180 ret = -EIO;
1181 goto out;
1182 }
1183 }
Chris Masona52d9a82007-08-27 16:49:44 -04001184
Chris Mason211c17f2008-05-15 09:13:45 -04001185 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1186 wait_on_page_writeback(page);
Chris Masonb888db22007-08-27 16:49:44 -04001187 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001188
Chris Mason39279cc2007-06-12 06:35:45 -04001189 unlock_page(page);
1190 page_cache_release(page);
1191out:
1192 return ret;
1193}
1194
1195static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1196{
1197 struct inode *inode = dentry->d_inode;
1198 int err;
1199
1200 err = inode_change_ok(inode, attr);
1201 if (err)
1202 return err;
1203
1204 if (S_ISREG(inode->i_mode) &&
1205 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1206 struct btrfs_trans_handle *trans;
1207 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001208 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001209
Chris Mason5f39d392007-10-15 16:14:19 -04001210 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001211 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001212 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001213 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001214 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001215
Chris Mason1b0f7c22008-01-30 14:33:02 -05001216 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001217 goto out;
1218
Chris Mason1832a6d2007-12-21 16:27:21 -05001219 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001220 if (err)
1221 goto fail;
1222
Chris Mason39279cc2007-06-12 06:35:45 -04001223 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1224
Chris Mason1b0f7c22008-01-30 14:33:02 -05001225 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001226 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001227
Chris Mason39279cc2007-06-12 06:35:45 -04001228 trans = btrfs_start_transaction(root, 1);
1229 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001230 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001231 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001232 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001233
Chris Mason179e29e2007-11-01 11:28:41 -04001234 if (alloc_hint != EXTENT_MAP_INLINE) {
1235 err = btrfs_insert_file_extent(trans, root,
1236 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001237 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001238 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001239 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001240 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001241 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001242 }
Chris Mason39279cc2007-06-12 06:35:45 -04001243 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001244 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001245 if (err)
1246 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001247 }
1248out:
1249 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001250fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001251 return err;
1252}
Chris Mason61295eb2008-01-14 16:24:38 -05001253
Chris Mason39279cc2007-06-12 06:35:45 -04001254void btrfs_delete_inode(struct inode *inode)
1255{
1256 struct btrfs_trans_handle *trans;
1257 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001258 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001259 int ret;
1260
1261 truncate_inode_pages(&inode->i_data, 0);
1262 if (is_bad_inode(inode)) {
1263 goto no_delete;
1264 }
Chris Mason5f39d392007-10-15 16:14:19 -04001265
Chris Mason39279cc2007-06-12 06:35:45 -04001266 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001267 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001268
Chris Mason39279cc2007-06-12 06:35:45 -04001269 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001270 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001271 if (ret)
1272 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001273
Chris Masond3c2fdc2007-09-17 10:58:06 -04001274 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001275 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001276
Chris Mason39279cc2007-06-12 06:35:45 -04001277 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001278 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001279 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001280
1281no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001282 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001283 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001284 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001285no_delete:
1286 clear_inode(inode);
1287}
1288
1289/*
1290 * this returns the key found in the dir entry in the location pointer.
1291 * If no dir entries were found, location->objectid is 0.
1292 */
1293static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1294 struct btrfs_key *location)
1295{
1296 const char *name = dentry->d_name.name;
1297 int namelen = dentry->d_name.len;
1298 struct btrfs_dir_item *di;
1299 struct btrfs_path *path;
1300 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001301 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001302
Chris Mason39544012007-12-12 14:38:19 -05001303 if (namelen == 1 && strcmp(name, ".") == 0) {
1304 location->objectid = dir->i_ino;
1305 location->type = BTRFS_INODE_ITEM_KEY;
1306 location->offset = 0;
1307 return 0;
1308 }
Chris Mason39279cc2007-06-12 06:35:45 -04001309 path = btrfs_alloc_path();
1310 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001311
Chris Mason7a720532007-12-13 09:06:59 -05001312 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001313 struct btrfs_key key;
1314 struct extent_buffer *leaf;
1315 u32 nritems;
1316 int slot;
1317
1318 key.objectid = dir->i_ino;
1319 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1320 key.offset = 0;
1321 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1322 BUG_ON(ret == 0);
1323 ret = 0;
1324
1325 leaf = path->nodes[0];
1326 slot = path->slots[0];
1327 nritems = btrfs_header_nritems(leaf);
1328 if (slot >= nritems)
1329 goto out_err;
1330
1331 btrfs_item_key_to_cpu(leaf, &key, slot);
1332 if (key.objectid != dir->i_ino ||
1333 key.type != BTRFS_INODE_REF_KEY) {
1334 goto out_err;
1335 }
1336 location->objectid = key.offset;
1337 location->type = BTRFS_INODE_ITEM_KEY;
1338 location->offset = 0;
1339 goto out;
1340 }
1341
Chris Mason39279cc2007-06-12 06:35:45 -04001342 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1343 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001344 if (IS_ERR(di))
1345 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001346 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001347 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001348 }
Chris Mason5f39d392007-10-15 16:14:19 -04001349 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001350out:
Chris Mason39279cc2007-06-12 06:35:45 -04001351 btrfs_free_path(path);
1352 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001353out_err:
1354 location->objectid = 0;
1355 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001356}
1357
1358/*
1359 * when we hit a tree root in a directory, the btrfs part of the inode
1360 * needs to be changed to reflect the root directory of the tree root. This
1361 * is kind of like crossing a mount point.
1362 */
1363static int fixup_tree_root_location(struct btrfs_root *root,
1364 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001365 struct btrfs_root **sub_root,
1366 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001367{
1368 struct btrfs_path *path;
1369 struct btrfs_root_item *ri;
1370
1371 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1372 return 0;
1373 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1374 return 0;
1375
1376 path = btrfs_alloc_path();
1377 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001378
Josef Bacik58176a92007-08-29 15:47:34 -04001379 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1380 dentry->d_name.name,
1381 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001382 if (IS_ERR(*sub_root))
1383 return PTR_ERR(*sub_root);
1384
1385 ri = &(*sub_root)->root_item;
1386 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001387 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1388 location->offset = 0;
1389
1390 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001391 return 0;
1392}
1393
1394static int btrfs_init_locked_inode(struct inode *inode, void *p)
1395{
1396 struct btrfs_iget_args *args = p;
1397 inode->i_ino = args->ino;
1398 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001399 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001400 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1401 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001402 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001403 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1404 inode->i_mapping, GFP_NOFS);
Chris Mason81d7ed22008-04-25 08:51:48 -04001405 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001406 return 0;
1407}
1408
1409static int btrfs_find_actor(struct inode *inode, void *opaque)
1410{
1411 struct btrfs_iget_args *args = opaque;
1412 return (args->ino == inode->i_ino &&
1413 args->root == BTRFS_I(inode)->root);
1414}
1415
Chris Masondc17ff82008-01-08 15:46:30 -05001416struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1417 u64 root_objectid)
1418{
1419 struct btrfs_iget_args args;
1420 args.ino = objectid;
1421 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1422
1423 if (!args.root)
1424 return NULL;
1425
1426 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1427}
1428
Chris Mason39279cc2007-06-12 06:35:45 -04001429struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1430 struct btrfs_root *root)
1431{
1432 struct inode *inode;
1433 struct btrfs_iget_args args;
1434 args.ino = objectid;
1435 args.root = root;
1436
1437 inode = iget5_locked(s, objectid, btrfs_find_actor,
1438 btrfs_init_locked_inode,
1439 (void *)&args);
1440 return inode;
1441}
1442
1443static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1444 struct nameidata *nd)
1445{
1446 struct inode * inode;
1447 struct btrfs_inode *bi = BTRFS_I(dir);
1448 struct btrfs_root *root = bi->root;
1449 struct btrfs_root *sub_root = root;
1450 struct btrfs_key location;
1451 int ret;
1452
1453 if (dentry->d_name.len > BTRFS_NAME_LEN)
1454 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001455
Chris Mason39279cc2007-06-12 06:35:45 -04001456 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001457
Chris Mason39279cc2007-06-12 06:35:45 -04001458 if (ret < 0)
1459 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001460
Chris Mason39279cc2007-06-12 06:35:45 -04001461 inode = NULL;
1462 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001463 ret = fixup_tree_root_location(root, &location, &sub_root,
1464 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001465 if (ret < 0)
1466 return ERR_PTR(ret);
1467 if (ret > 0)
1468 return ERR_PTR(-ENOENT);
1469 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1470 sub_root);
1471 if (!inode)
1472 return ERR_PTR(-EACCES);
1473 if (inode->i_state & I_NEW) {
1474 /* the inode and parent dir are two different roots */
1475 if (sub_root != root) {
1476 igrab(inode);
1477 sub_root->inode = inode;
1478 }
1479 BTRFS_I(inode)->root = sub_root;
1480 memcpy(&BTRFS_I(inode)->location, &location,
1481 sizeof(location));
1482 btrfs_read_locked_inode(inode);
1483 unlock_new_inode(inode);
1484 }
1485 }
1486 return d_splice_alias(inode, dentry);
1487}
1488
Chris Mason39279cc2007-06-12 06:35:45 -04001489static unsigned char btrfs_filetype_table[] = {
1490 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1491};
1492
1493static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1494{
Chris Mason6da6aba2007-12-18 16:15:09 -05001495 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001496 struct btrfs_root *root = BTRFS_I(inode)->root;
1497 struct btrfs_item *item;
1498 struct btrfs_dir_item *di;
1499 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001500 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001501 struct btrfs_path *path;
1502 int ret;
1503 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001504 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001505 int slot;
1506 int advance;
1507 unsigned char d_type;
1508 int over = 0;
1509 u32 di_cur;
1510 u32 di_total;
1511 u32 di_len;
1512 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001513 char tmp_name[32];
1514 char *name_ptr;
1515 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001516
1517 /* FIXME, use a real flag for deciding about the key type */
1518 if (root->fs_info->tree_root == root)
1519 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001520
Chris Mason39544012007-12-12 14:38:19 -05001521 /* special case for "." */
1522 if (filp->f_pos == 0) {
1523 over = filldir(dirent, ".", 1,
1524 1, inode->i_ino,
1525 DT_DIR);
1526 if (over)
1527 return 0;
1528 filp->f_pos = 1;
1529 }
1530
Chris Mason39279cc2007-06-12 06:35:45 -04001531 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001532 path = btrfs_alloc_path();
1533 path->reada = 2;
1534
1535 /* special case for .., just use the back ref */
1536 if (filp->f_pos == 1) {
1537 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1538 key.offset = 0;
1539 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1540 BUG_ON(ret == 0);
1541 leaf = path->nodes[0];
1542 slot = path->slots[0];
1543 nritems = btrfs_header_nritems(leaf);
1544 if (slot >= nritems) {
1545 btrfs_release_path(root, path);
1546 goto read_dir_items;
1547 }
1548 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1549 btrfs_release_path(root, path);
1550 if (found_key.objectid != key.objectid ||
1551 found_key.type != BTRFS_INODE_REF_KEY)
1552 goto read_dir_items;
1553 over = filldir(dirent, "..", 2,
1554 2, found_key.offset, DT_DIR);
1555 if (over)
1556 goto nopos;
1557 filp->f_pos = 2;
1558 }
1559
1560read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001561 btrfs_set_key_type(&key, key_type);
1562 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001563
Chris Mason39279cc2007-06-12 06:35:45 -04001564 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1565 if (ret < 0)
1566 goto err;
1567 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001568 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001569 leaf = path->nodes[0];
1570 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001571 slot = path->slots[0];
1572 if (advance || slot >= nritems) {
1573 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001574 ret = btrfs_next_leaf(root, path);
1575 if (ret)
1576 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001577 leaf = path->nodes[0];
1578 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001579 slot = path->slots[0];
1580 } else {
1581 slot++;
1582 path->slots[0]++;
1583 }
1584 }
1585 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001586 item = btrfs_item_nr(leaf, slot);
1587 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1588
1589 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001590 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001591 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001592 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001593 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001594 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001595
1596 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001597 advance = 1;
1598 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1599 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001600 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001601 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001602 struct btrfs_key location;
1603
1604 name_len = btrfs_dir_name_len(leaf, di);
1605 if (name_len < 32) {
1606 name_ptr = tmp_name;
1607 } else {
1608 name_ptr = kmalloc(name_len, GFP_NOFS);
1609 BUG_ON(!name_ptr);
1610 }
1611 read_extent_buffer(leaf, name_ptr,
1612 (unsigned long)(di + 1), name_len);
1613
1614 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1615 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001616 over = filldir(dirent, name_ptr, name_len,
1617 found_key.offset,
1618 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001619 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001620
1621 if (name_ptr != tmp_name)
1622 kfree(name_ptr);
1623
Chris Mason39279cc2007-06-12 06:35:45 -04001624 if (over)
1625 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001626 di_len = btrfs_dir_name_len(leaf, di) +
1627 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001628 di_cur += di_len;
1629 di = (struct btrfs_dir_item *)((char *)di + di_len);
1630 }
1631 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001632 if (key_type == BTRFS_DIR_INDEX_KEY)
1633 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1634 else
1635 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001636nopos:
1637 ret = 0;
1638err:
Chris Mason39279cc2007-06-12 06:35:45 -04001639 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001640 return ret;
1641}
1642
1643int btrfs_write_inode(struct inode *inode, int wait)
1644{
1645 struct btrfs_root *root = BTRFS_I(inode)->root;
1646 struct btrfs_trans_handle *trans;
1647 int ret = 0;
1648
1649 if (wait) {
Chris Mason39279cc2007-06-12 06:35:45 -04001650 trans = btrfs_start_transaction(root, 1);
1651 btrfs_set_trans_block_group(trans, inode);
1652 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001653 }
1654 return ret;
1655}
1656
1657/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001658 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001659 * inode changes. But, it is most likely to find the inode in cache.
1660 * FIXME, needs more benchmarking...there are no reasons other than performance
1661 * to keep or drop this code.
1662 */
1663void btrfs_dirty_inode(struct inode *inode)
1664{
1665 struct btrfs_root *root = BTRFS_I(inode)->root;
1666 struct btrfs_trans_handle *trans;
1667
Chris Mason39279cc2007-06-12 06:35:45 -04001668 trans = btrfs_start_transaction(root, 1);
1669 btrfs_set_trans_block_group(trans, inode);
1670 btrfs_update_inode(trans, root, inode);
1671 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001672}
1673
1674static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1675 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001676 const char *name, int name_len,
1677 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001678 u64 objectid,
1679 struct btrfs_block_group_cache *group,
1680 int mode)
1681{
1682 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001683 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001684 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001685 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001686 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001687 struct btrfs_inode_ref *ref;
1688 struct btrfs_key key[2];
1689 u32 sizes[2];
1690 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001691 int ret;
1692 int owner;
1693
Chris Mason5f39d392007-10-15 16:14:19 -04001694 path = btrfs_alloc_path();
1695 BUG_ON(!path);
1696
Chris Mason39279cc2007-06-12 06:35:45 -04001697 inode = new_inode(root->fs_info->sb);
1698 if (!inode)
1699 return ERR_PTR(-ENOMEM);
1700
Chris Masond1310b22008-01-24 16:13:08 -05001701 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1702 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001703 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001704 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1705 inode->i_mapping, GFP_NOFS);
Chris Mason81d7ed22008-04-25 08:51:48 -04001706 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Mason90692182008-02-08 13:49:28 -05001707 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001708 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001709
Chris Mason39279cc2007-06-12 06:35:45 -04001710 if (mode & S_IFDIR)
1711 owner = 0;
1712 else
1713 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001714 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001715 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001716 if (!new_inode_group) {
1717 printk("find_block group failed\n");
1718 new_inode_group = group;
1719 }
1720 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001721 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001722
1723 key[0].objectid = objectid;
1724 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1725 key[0].offset = 0;
1726
1727 key[1].objectid = objectid;
1728 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1729 key[1].offset = ref_objectid;
1730
1731 sizes[0] = sizeof(struct btrfs_inode_item);
1732 sizes[1] = name_len + sizeof(*ref);
1733
1734 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1735 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001736 goto fail;
1737
Chris Mason9c583092008-01-29 15:15:18 -05001738 if (objectid > root->highest_inode)
1739 root->highest_inode = objectid;
1740
Chris Mason39279cc2007-06-12 06:35:45 -04001741 inode->i_uid = current->fsuid;
1742 inode->i_gid = current->fsgid;
1743 inode->i_mode = mode;
1744 inode->i_ino = objectid;
1745 inode->i_blocks = 0;
1746 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001747 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1748 struct btrfs_inode_item);
1749 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001750
1751 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1752 struct btrfs_inode_ref);
1753 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1754 ptr = (unsigned long)(ref + 1);
1755 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1756
Chris Mason5f39d392007-10-15 16:14:19 -04001757 btrfs_mark_buffer_dirty(path->nodes[0]);
1758 btrfs_free_path(path);
1759
Chris Mason39279cc2007-06-12 06:35:45 -04001760 location = &BTRFS_I(inode)->location;
1761 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001762 location->offset = 0;
1763 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1764
Chris Mason39279cc2007-06-12 06:35:45 -04001765 insert_inode_hash(inode);
1766 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001767fail:
1768 btrfs_free_path(path);
1769 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001770}
1771
1772static inline u8 btrfs_inode_type(struct inode *inode)
1773{
1774 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1775}
1776
1777static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001778 struct dentry *dentry, struct inode *inode,
1779 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001780{
1781 int ret;
1782 struct btrfs_key key;
1783 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001784 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001785
Chris Mason39279cc2007-06-12 06:35:45 -04001786 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001787 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1788 key.offset = 0;
1789
1790 ret = btrfs_insert_dir_item(trans, root,
1791 dentry->d_name.name, dentry->d_name.len,
1792 dentry->d_parent->d_inode->i_ino,
1793 &key, btrfs_inode_type(inode));
1794 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001795 if (add_backref) {
1796 ret = btrfs_insert_inode_ref(trans, root,
1797 dentry->d_name.name,
1798 dentry->d_name.len,
1799 inode->i_ino,
1800 dentry->d_parent->d_inode->i_ino);
1801 }
Chris Mason79c44582007-06-25 10:09:33 -04001802 parent_inode = dentry->d_parent->d_inode;
1803 parent_inode->i_size += dentry->d_name.len * 2;
1804 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001805 ret = btrfs_update_inode(trans, root,
1806 dentry->d_parent->d_inode);
1807 }
1808 return ret;
1809}
1810
1811static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001812 struct dentry *dentry, struct inode *inode,
1813 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001814{
Chris Mason9c583092008-01-29 15:15:18 -05001815 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001816 if (!err) {
1817 d_instantiate(dentry, inode);
1818 return 0;
1819 }
1820 if (err > 0)
1821 err = -EEXIST;
1822 return err;
1823}
1824
Josef Bacik618e21d2007-07-11 10:18:17 -04001825static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1826 int mode, dev_t rdev)
1827{
1828 struct btrfs_trans_handle *trans;
1829 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001830 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001831 int err;
1832 int drop_inode = 0;
1833 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001834 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001835
1836 if (!new_valid_dev(rdev))
1837 return -EINVAL;
1838
Chris Mason1832a6d2007-12-21 16:27:21 -05001839 err = btrfs_check_free_space(root, 1, 0);
1840 if (err)
1841 goto fail;
1842
Josef Bacik618e21d2007-07-11 10:18:17 -04001843 trans = btrfs_start_transaction(root, 1);
1844 btrfs_set_trans_block_group(trans, dir);
1845
1846 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1847 if (err) {
1848 err = -ENOSPC;
1849 goto out_unlock;
1850 }
1851
Chris Mason9c583092008-01-29 15:15:18 -05001852 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1853 dentry->d_name.len,
1854 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001855 BTRFS_I(dir)->block_group, mode);
1856 err = PTR_ERR(inode);
1857 if (IS_ERR(inode))
1858 goto out_unlock;
1859
1860 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001861 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001862 if (err)
1863 drop_inode = 1;
1864 else {
1865 inode->i_op = &btrfs_special_inode_operations;
1866 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001867 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001868 }
1869 dir->i_sb->s_dirt = 1;
1870 btrfs_update_inode_block_group(trans, inode);
1871 btrfs_update_inode_block_group(trans, dir);
1872out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001873 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001874 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001875fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001876 if (drop_inode) {
1877 inode_dec_link_count(inode);
1878 iput(inode);
1879 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001880 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001881 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001882 return err;
1883}
1884
Chris Mason39279cc2007-06-12 06:35:45 -04001885static int btrfs_create(struct inode *dir, struct dentry *dentry,
1886 int mode, struct nameidata *nd)
1887{
1888 struct btrfs_trans_handle *trans;
1889 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001890 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001891 int err;
1892 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001893 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001894 u64 objectid;
1895
Chris Mason1832a6d2007-12-21 16:27:21 -05001896 err = btrfs_check_free_space(root, 1, 0);
1897 if (err)
1898 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001899 trans = btrfs_start_transaction(root, 1);
1900 btrfs_set_trans_block_group(trans, dir);
1901
1902 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1903 if (err) {
1904 err = -ENOSPC;
1905 goto out_unlock;
1906 }
1907
Chris Mason9c583092008-01-29 15:15:18 -05001908 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1909 dentry->d_name.len,
1910 dentry->d_parent->d_inode->i_ino,
1911 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001912 err = PTR_ERR(inode);
1913 if (IS_ERR(inode))
1914 goto out_unlock;
1915
1916 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001917 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001918 if (err)
1919 drop_inode = 1;
1920 else {
1921 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001922 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001923 inode->i_fop = &btrfs_file_operations;
1924 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001925 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1926 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001927 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001928 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1929 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001930 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason81d7ed22008-04-25 08:51:48 -04001931 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001932 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001933 }
1934 dir->i_sb->s_dirt = 1;
1935 btrfs_update_inode_block_group(trans, inode);
1936 btrfs_update_inode_block_group(trans, dir);
1937out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001938 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001939 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001940fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001941 if (drop_inode) {
1942 inode_dec_link_count(inode);
1943 iput(inode);
1944 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001945 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001946 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001947 return err;
1948}
1949
1950static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1951 struct dentry *dentry)
1952{
1953 struct btrfs_trans_handle *trans;
1954 struct btrfs_root *root = BTRFS_I(dir)->root;
1955 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001956 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001957 int err;
1958 int drop_inode = 0;
1959
1960 if (inode->i_nlink == 0)
1961 return -ENOENT;
1962
Chris Mason6da6aba2007-12-18 16:15:09 -05001963#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1964 inode->i_nlink++;
1965#else
Chris Mason39279cc2007-06-12 06:35:45 -04001966 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001967#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05001968 err = btrfs_check_free_space(root, 1, 0);
1969 if (err)
1970 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001971 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001972
Chris Mason39279cc2007-06-12 06:35:45 -04001973 btrfs_set_trans_block_group(trans, dir);
1974 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05001975 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001976
Chris Mason39279cc2007-06-12 06:35:45 -04001977 if (err)
1978 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001979
Chris Mason39279cc2007-06-12 06:35:45 -04001980 dir->i_sb->s_dirt = 1;
1981 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001982 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001983
Chris Mason54aa1f42007-06-22 14:16:25 -04001984 if (err)
1985 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001986
Chris Masond3c2fdc2007-09-17 10:58:06 -04001987 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001988 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001989fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001990 if (drop_inode) {
1991 inode_dec_link_count(inode);
1992 iput(inode);
1993 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001994 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001995 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001996 return err;
1997}
1998
Chris Mason39279cc2007-06-12 06:35:45 -04001999static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2000{
Chris Masonb9d86662008-05-02 16:13:49 -04002001 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002002 struct btrfs_trans_handle *trans;
2003 struct btrfs_root *root = BTRFS_I(dir)->root;
2004 int err = 0;
2005 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002006 u64 objectid = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002007 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002008
Chris Mason1832a6d2007-12-21 16:27:21 -05002009 err = btrfs_check_free_space(root, 1, 0);
2010 if (err)
2011 goto out_unlock;
2012
Chris Mason39279cc2007-06-12 06:35:45 -04002013 trans = btrfs_start_transaction(root, 1);
2014 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002015
Chris Mason39279cc2007-06-12 06:35:45 -04002016 if (IS_ERR(trans)) {
2017 err = PTR_ERR(trans);
2018 goto out_unlock;
2019 }
2020
2021 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2022 if (err) {
2023 err = -ENOSPC;
2024 goto out_unlock;
2025 }
2026
Chris Mason9c583092008-01-29 15:15:18 -05002027 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2028 dentry->d_name.len,
2029 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002030 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2031 if (IS_ERR(inode)) {
2032 err = PTR_ERR(inode);
2033 goto out_fail;
2034 }
Chris Mason5f39d392007-10-15 16:14:19 -04002035
Chris Mason39279cc2007-06-12 06:35:45 -04002036 drop_on_err = 1;
2037 inode->i_op = &btrfs_dir_inode_operations;
2038 inode->i_fop = &btrfs_dir_file_operations;
2039 btrfs_set_trans_block_group(trans, inode);
2040
Chris Mason39544012007-12-12 14:38:19 -05002041 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002042 err = btrfs_update_inode(trans, root, inode);
2043 if (err)
2044 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002045
Chris Mason9c583092008-01-29 15:15:18 -05002046 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002047 if (err)
2048 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002049
Chris Mason39279cc2007-06-12 06:35:45 -04002050 d_instantiate(dentry, inode);
2051 drop_on_err = 0;
2052 dir->i_sb->s_dirt = 1;
2053 btrfs_update_inode_block_group(trans, inode);
2054 btrfs_update_inode_block_group(trans, dir);
2055
2056out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002057 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002058 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002059
Chris Mason39279cc2007-06-12 06:35:45 -04002060out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002061 if (drop_on_err)
2062 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002063 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002064 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002065 return err;
2066}
2067
Chris Mason3b951512008-04-17 11:29:12 -04002068static int merge_extent_mapping(struct extent_map_tree *em_tree,
2069 struct extent_map *existing,
2070 struct extent_map *em)
2071{
2072 u64 start_diff;
2073 u64 new_end;
2074 int ret = 0;
2075 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2076
2077 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2078 goto invalid;
2079
2080 if (!real_blocks && em->block_start != existing->block_start)
2081 goto invalid;
2082
2083 new_end = max(existing->start + existing->len, em->start + em->len);
2084
2085 if (existing->start >= em->start) {
2086 if (em->start + em->len < existing->start)
2087 goto invalid;
2088
2089 start_diff = existing->start - em->start;
2090 if (real_blocks && em->block_start + start_diff !=
2091 existing->block_start)
2092 goto invalid;
2093
2094 em->len = new_end - em->start;
2095
2096 remove_extent_mapping(em_tree, existing);
2097 /* free for the tree */
2098 free_extent_map(existing);
2099 ret = add_extent_mapping(em_tree, em);
2100
2101 } else if (em->start > existing->start) {
2102
2103 if (existing->start + existing->len < em->start)
2104 goto invalid;
2105
2106 start_diff = em->start - existing->start;
2107 if (real_blocks && existing->block_start + start_diff !=
2108 em->block_start)
2109 goto invalid;
2110
2111 remove_extent_mapping(em_tree, existing);
2112 em->block_start = existing->block_start;
2113 em->start = existing->start;
2114 em->len = new_end - existing->start;
2115 free_extent_map(existing);
2116
2117 ret = add_extent_mapping(em_tree, em);
2118 } else {
2119 goto invalid;
2120 }
2121 return ret;
2122
2123invalid:
2124 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2125 existing->start, existing->len, existing->block_start,
2126 em->start, em->len, em->block_start);
2127 return -EIO;
2128}
2129
Chris Masona52d9a82007-08-27 16:49:44 -04002130struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002131 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002132 int create)
2133{
2134 int ret;
2135 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002136 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002137 u64 extent_start = 0;
2138 u64 extent_end = 0;
2139 u64 objectid = inode->i_ino;
2140 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002141 struct btrfs_path *path;
2142 struct btrfs_root *root = BTRFS_I(inode)->root;
2143 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002144 struct extent_buffer *leaf;
2145 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002146 struct extent_map *em = NULL;
2147 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002148 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002149 struct btrfs_trans_handle *trans = NULL;
2150
2151 path = btrfs_alloc_path();
2152 BUG_ON(!path);
Chris Masona52d9a82007-08-27 16:49:44 -04002153
2154again:
Chris Masond1310b22008-01-24 16:13:08 -05002155 spin_lock(&em_tree->lock);
2156 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002157 if (em)
2158 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002159 spin_unlock(&em_tree->lock);
2160
Chris Masona52d9a82007-08-27 16:49:44 -04002161 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002162 if (em->start > start || em->start + em->len <= start)
2163 free_extent_map(em);
2164 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002165 free_extent_map(em);
2166 else
2167 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002168 }
Chris Masond1310b22008-01-24 16:13:08 -05002169 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002170 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002171 err = -ENOMEM;
2172 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002173 }
Chris Masond1310b22008-01-24 16:13:08 -05002174
2175 em->start = EXTENT_MAP_HOLE;
2176 em->len = (u64)-1;
Chris Masona061fc82008-05-07 11:43:44 -04002177 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04002178 ret = btrfs_lookup_file_extent(trans, root, path,
2179 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002180 if (ret < 0) {
2181 err = ret;
2182 goto out;
2183 }
2184
2185 if (ret != 0) {
2186 if (path->slots[0] == 0)
2187 goto not_found;
2188 path->slots[0]--;
2189 }
2190
Chris Mason5f39d392007-10-15 16:14:19 -04002191 leaf = path->nodes[0];
2192 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002193 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002194 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002195 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2196 found_type = btrfs_key_type(&found_key);
2197 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002198 found_type != BTRFS_EXTENT_DATA_KEY) {
2199 goto not_found;
2200 }
2201
Chris Mason5f39d392007-10-15 16:14:19 -04002202 found_type = btrfs_file_extent_type(leaf, item);
2203 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002204 if (found_type == BTRFS_FILE_EXTENT_REG) {
2205 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002206 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002207 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002208 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002209 em->start = start;
2210 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002211 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002212 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002213 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002214 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002215 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002216 }
2217 goto not_found_em;
2218 }
Chris Masondb945352007-10-15 16:15:53 -04002219 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2220 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002221 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002222 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002223 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002224 goto insert;
2225 }
Chris Masondb945352007-10-15 16:15:53 -04002226 bytenr += btrfs_file_extent_offset(leaf, item);
2227 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002228 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002229 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002230 goto insert;
2231 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002232 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002233 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002234 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002235 size_t size;
2236 size_t extent_offset;
2237 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002238
Chris Mason5f39d392007-10-15 16:14:19 -04002239 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2240 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002241 extent_end = (extent_start + size + root->sectorsize - 1) &
2242 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002243 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002244 em->start = start;
2245 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002246 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002247 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002248 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002249 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002250 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002251 }
2252 goto not_found_em;
2253 }
2254 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002255
2256 if (!page) {
2257 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002258 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002259 goto out;
2260 }
2261
Chris Mason70dec802008-01-29 09:59:12 -05002262 page_start = page_offset(page) + pg_offset;
2263 extent_offset = page_start - extent_start;
2264 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002265 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002266 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002267 em->len = (copy_size + root->sectorsize - 1) &
2268 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002269 map = kmap(page);
2270 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002271 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002272 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002273 copy_size);
2274 flush_dcache_page(page);
2275 } else if (create && PageUptodate(page)) {
2276 if (!trans) {
2277 kunmap(page);
2278 free_extent_map(em);
2279 em = NULL;
2280 btrfs_release_path(root, path);
2281 trans = btrfs_start_transaction(root, 1);
2282 goto again;
2283 }
Chris Mason70dec802008-01-29 09:59:12 -05002284 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002285 copy_size);
2286 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002287 }
Chris Masona52d9a82007-08-27 16:49:44 -04002288 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002289 set_extent_uptodate(io_tree, em->start,
2290 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002291 goto insert;
2292 } else {
2293 printk("unkknown found_type %d\n", found_type);
2294 WARN_ON(1);
2295 }
2296not_found:
2297 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002298 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002299not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002300 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002301insert:
2302 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002303 if (em->start > start || extent_map_end(em) <= start) {
2304 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002305 err = -EIO;
2306 goto out;
2307 }
Chris Masond1310b22008-01-24 16:13:08 -05002308
2309 err = 0;
2310 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002311 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002312 /* it is possible that someone inserted the extent into the tree
2313 * while we had the lock dropped. It is also possible that
2314 * an overlapping map exists in the tree
2315 */
Chris Masona52d9a82007-08-27 16:49:44 -04002316 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002317 struct extent_map *existing;
2318 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002319 if (existing && (existing->start > start ||
2320 existing->start + existing->len <= start)) {
2321 free_extent_map(existing);
2322 existing = NULL;
2323 }
Chris Mason3b951512008-04-17 11:29:12 -04002324 if (!existing) {
2325 existing = lookup_extent_mapping(em_tree, em->start,
2326 em->len);
2327 if (existing) {
2328 err = merge_extent_mapping(em_tree, existing,
2329 em);
2330 free_extent_map(existing);
2331 if (err) {
2332 free_extent_map(em);
2333 em = NULL;
2334 }
2335 } else {
2336 err = -EIO;
2337 printk("failing to insert %Lu %Lu\n",
2338 start, len);
2339 free_extent_map(em);
2340 em = NULL;
2341 }
2342 } else {
2343 free_extent_map(em);
2344 em = existing;
Chris Masona52d9a82007-08-27 16:49:44 -04002345 }
Chris Masona52d9a82007-08-27 16:49:44 -04002346 }
Chris Masond1310b22008-01-24 16:13:08 -05002347 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002348out:
2349 btrfs_free_path(path);
2350 if (trans) {
2351 ret = btrfs_end_transaction(trans, root);
2352 if (!err)
2353 err = ret;
2354 }
Chris Masona52d9a82007-08-27 16:49:44 -04002355 if (err) {
2356 free_extent_map(em);
2357 WARN_ON(1);
2358 return ERR_PTR(err);
2359 }
2360 return em;
2361}
2362
Chris Masone1c4b742008-04-22 13:26:46 -04002363#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002364static int btrfs_get_block(struct inode *inode, sector_t iblock,
2365 struct buffer_head *bh_result, int create)
2366{
2367 struct extent_map *em;
2368 u64 start = (u64)iblock << inode->i_blkbits;
2369 struct btrfs_multi_bio *multi = NULL;
2370 struct btrfs_root *root = BTRFS_I(inode)->root;
2371 u64 len;
2372 u64 logical;
2373 u64 map_length;
2374 int ret = 0;
2375
2376 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2377
2378 if (!em || IS_ERR(em))
2379 goto out;
2380
Chris Masone1c4b742008-04-22 13:26:46 -04002381 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002382 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002383 }
Chris Mason16432982008-04-10 10:23:21 -04002384
2385 if (em->block_start == EXTENT_MAP_INLINE) {
2386 ret = -EINVAL;
2387 goto out;
2388 }
2389
Chris Mason16432982008-04-10 10:23:21 -04002390 len = em->start + em->len - start;
2391 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2392
Chris Masone1c4b742008-04-22 13:26:46 -04002393 if (em->block_start == EXTENT_MAP_HOLE ||
2394 em->block_start == EXTENT_MAP_DELALLOC) {
2395 bh_result->b_size = len;
2396 goto out;
2397 }
2398
Chris Mason16432982008-04-10 10:23:21 -04002399 logical = start - em->start;
2400 logical = em->block_start + logical;
2401
2402 map_length = len;
2403 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2404 logical, &map_length, &multi, 0);
2405 BUG_ON(ret);
2406 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2407 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002408
Chris Mason16432982008-04-10 10:23:21 -04002409 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2410 set_buffer_mapped(bh_result);
2411 kfree(multi);
2412out:
2413 free_extent_map(em);
2414 return ret;
2415}
Chris Masone1c4b742008-04-22 13:26:46 -04002416#endif
Chris Mason16432982008-04-10 10:23:21 -04002417
2418static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2419 const struct iovec *iov, loff_t offset,
2420 unsigned long nr_segs)
2421{
Chris Masone1c4b742008-04-22 13:26:46 -04002422 return -EINVAL;
2423#if 0
Chris Mason16432982008-04-10 10:23:21 -04002424 struct file *file = iocb->ki_filp;
2425 struct inode *inode = file->f_mapping->host;
2426
2427 if (rw == WRITE)
2428 return -EINVAL;
2429
2430 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2431 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002432#endif
Chris Mason16432982008-04-10 10:23:21 -04002433}
2434
Christoph Hellwigd396c6f52007-09-10 20:02:30 -04002435static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002436{
Christoph Hellwigd396c6f52007-09-10 20:02:30 -04002437 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002438}
2439
Chris Mason9ebefb182007-06-15 13:50:00 -04002440int btrfs_readpage(struct file *file, struct page *page)
2441{
Chris Masond1310b22008-01-24 16:13:08 -05002442 struct extent_io_tree *tree;
2443 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002444 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002445}
Chris Mason1832a6d2007-12-21 16:27:21 -05002446
Chris Mason39279cc2007-06-12 06:35:45 -04002447static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2448{
Chris Masond1310b22008-01-24 16:13:08 -05002449 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002450
2451
2452 if (current->flags & PF_MEMALLOC) {
2453 redirty_page_for_writepage(wbc, page);
2454 unlock_page(page);
2455 return 0;
2456 }
Chris Masond1310b22008-01-24 16:13:08 -05002457 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002458 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2459}
Chris Mason39279cc2007-06-12 06:35:45 -04002460
Chris Masonb293f022007-11-01 19:45:34 -04002461static int btrfs_writepages(struct address_space *mapping,
2462 struct writeback_control *wbc)
2463{
Chris Masond1310b22008-01-24 16:13:08 -05002464 struct extent_io_tree *tree;
2465 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002466 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2467}
2468
Chris Mason3ab2fb52007-11-08 10:59:22 -05002469static int
2470btrfs_readpages(struct file *file, struct address_space *mapping,
2471 struct list_head *pages, unsigned nr_pages)
2472{
Chris Masond1310b22008-01-24 16:13:08 -05002473 struct extent_io_tree *tree;
2474 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002475 return extent_readpages(tree, mapping, pages, nr_pages,
2476 btrfs_get_extent);
2477}
2478
Chris Mason70dec802008-01-29 09:59:12 -05002479static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002480{
Chris Masond1310b22008-01-24 16:13:08 -05002481 struct extent_io_tree *tree;
2482 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002483 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002484
Chris Masond1310b22008-01-24 16:13:08 -05002485 tree = &BTRFS_I(page->mapping->host)->io_tree;
2486 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002487 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002488 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002489 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002490 ClearPagePrivate(page);
2491 set_page_private(page, 0);
2492 page_cache_release(page);
2493 }
2494 return ret;
2495}
Chris Mason39279cc2007-06-12 06:35:45 -04002496
Chris Masona52d9a82007-08-27 16:49:44 -04002497static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2498{
Chris Masond1310b22008-01-24 16:13:08 -05002499 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002500
Chris Masond1310b22008-01-24 16:13:08 -05002501 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002502 extent_invalidatepage(tree, page, offset);
2503 btrfs_releasepage(page, GFP_NOFS);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002504 if (PagePrivate(page)) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002505 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002506 ClearPagePrivate(page);
2507 set_page_private(page, 0);
2508 page_cache_release(page);
2509 }
Chris Mason39279cc2007-06-12 06:35:45 -04002510}
2511
Chris Mason9ebefb182007-06-15 13:50:00 -04002512/*
2513 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2514 * called from a page fault handler when a page is first dirtied. Hence we must
2515 * be careful to check for EOF conditions here. We set the page up correctly
2516 * for a written page which means we get ENOSPC checking when writing into
2517 * holes and correct delalloc and unwritten extent mapping on filesystems that
2518 * support these features.
2519 *
2520 * We are not allowed to take the i_mutex here so we have to play games to
2521 * protect against truncate races as the page could now be beyond EOF. Because
2522 * vmtruncate() writes the inode size before removing pages, once we have the
2523 * page lock we can determine safely if the page is beyond EOF. If it is not
2524 * beyond EOF, then the page is guaranteed safe against truncation until we
2525 * unlock the page.
2526 */
2527int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2528{
Chris Mason6da6aba2007-12-18 16:15:09 -05002529 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002530 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002531 unsigned long end;
2532 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002533 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002534 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002535
Chris Mason1832a6d2007-12-21 16:27:21 -05002536 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05002537 if (ret)
2538 goto out;
2539
2540 ret = -EINVAL;
2541
Chris Mason9ebefb182007-06-15 13:50:00 -04002542 lock_page(page);
2543 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002544 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002545 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002546
Chris Mason9ebefb182007-06-15 13:50:00 -04002547 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002548 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002549 /* page got truncated out from underneath us */
2550 goto out_unlock;
2551 }
2552
2553 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002554 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002555 end = size & ~PAGE_CACHE_MASK;
2556 else
2557 end = PAGE_CACHE_SIZE;
2558
Chris Masonb888db22007-08-27 16:49:44 -04002559 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002560
2561out_unlock:
2562 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002563out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002564 return ret;
2565}
2566
Chris Mason39279cc2007-06-12 06:35:45 -04002567static void btrfs_truncate(struct inode *inode)
2568{
2569 struct btrfs_root *root = BTRFS_I(inode)->root;
2570 int ret;
2571 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002572 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002573
2574 if (!S_ISREG(inode->i_mode))
2575 return;
2576 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2577 return;
2578
2579 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2580
Chris Mason39279cc2007-06-12 06:35:45 -04002581 trans = btrfs_start_transaction(root, 1);
2582 btrfs_set_trans_block_group(trans, inode);
2583
2584 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002585 ret = btrfs_truncate_in_trans(trans, root, inode,
2586 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002587 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002588 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002589
Chris Mason39279cc2007-06-12 06:35:45 -04002590 ret = btrfs_end_transaction(trans, root);
2591 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002592 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002593 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002594}
2595
Sven Wegener3b963622008-06-09 21:57:42 -04002596/*
2597 * Invalidate a single dcache entry at the root of the filesystem.
2598 * Needed after creation of snapshot or subvolume.
2599 */
2600void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2601 int namelen)
2602{
2603 struct dentry *alias, *entry;
2604 struct qstr qstr;
2605
2606 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2607 if (alias) {
2608 qstr.name = name;
2609 qstr.len = namelen;
2610 /* change me if btrfs ever gets a d_hash operation */
2611 qstr.hash = full_name_hash(qstr.name, qstr.len);
2612 entry = d_lookup(alias, &qstr);
2613 dput(alias);
2614 if (entry) {
2615 d_invalidate(entry);
2616 dput(entry);
2617 }
2618 }
2619}
2620
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002621int btrfs_create_subvol_root(struct btrfs_root *new_root,
2622 struct btrfs_trans_handle *trans, u64 new_dirid,
2623 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04002624{
Chris Mason39279cc2007-06-12 06:35:45 -04002625 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002626 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002627
Chris Mason9c583092008-01-29 15:15:18 -05002628 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002629 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002630 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002631 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002632 inode->i_op = &btrfs_dir_inode_operations;
2633 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002634 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002635
Chris Mason39544012007-12-12 14:38:19 -05002636 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2637 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002638 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002639 inode->i_size = 0;
Sven Wegener3b963622008-06-09 21:57:42 -04002640
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002641 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002642}
2643
Chris Masonedbd8d42007-12-21 16:27:24 -05002644unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002645 struct file_ra_state *ra, struct file *file,
2646 pgoff_t offset, pgoff_t last_index)
2647{
Chris Mason8e7bf942008-04-28 09:02:36 -04002648 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04002649
2650#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04002651 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2652 return offset;
2653#else
Chris Mason86479a02007-09-10 19:58:16 -04002654 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2655 return offset + req_size;
2656#endif
2657}
2658
Chris Mason39279cc2007-06-12 06:35:45 -04002659struct inode *btrfs_alloc_inode(struct super_block *sb)
2660{
2661 struct btrfs_inode *ei;
2662
2663 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2664 if (!ei)
2665 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002666 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002667 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002668 return &ei->vfs_inode;
2669}
2670
2671void btrfs_destroy_inode(struct inode *inode)
2672{
2673 WARN_ON(!list_empty(&inode->i_dentry));
2674 WARN_ON(inode->i_data.nrpages);
2675
Chris Mason8c416c92008-01-14 15:10:26 -05002676 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002677 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2678}
2679
Chris Mason44ec0b72007-10-29 10:55:05 -04002680#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2681static void init_once(struct kmem_cache * cachep, void *foo)
2682#else
Chris Mason39279cc2007-06-12 06:35:45 -04002683static void init_once(void * foo, struct kmem_cache * cachep,
2684 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002685#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002686{
2687 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2688
2689 inode_init_once(&ei->vfs_inode);
2690}
2691
2692void btrfs_destroy_cachep(void)
2693{
2694 if (btrfs_inode_cachep)
2695 kmem_cache_destroy(btrfs_inode_cachep);
2696 if (btrfs_trans_handle_cachep)
2697 kmem_cache_destroy(btrfs_trans_handle_cachep);
2698 if (btrfs_transaction_cachep)
2699 kmem_cache_destroy(btrfs_transaction_cachep);
2700 if (btrfs_bit_radix_cachep)
2701 kmem_cache_destroy(btrfs_bit_radix_cachep);
2702 if (btrfs_path_cachep)
2703 kmem_cache_destroy(btrfs_path_cachep);
2704}
2705
Chris Mason86479a02007-09-10 19:58:16 -04002706struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002707 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002708#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2709 void (*ctor)(struct kmem_cache *, void *)
2710#else
Chris Mason92fee662007-07-25 12:31:35 -04002711 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002712 unsigned long)
2713#endif
2714 )
Chris Mason92fee662007-07-25 12:31:35 -04002715{
2716 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2717 SLAB_MEM_SPREAD | extra_flags), ctor
2718#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2719 ,NULL
2720#endif
2721 );
2722}
2723
Chris Mason39279cc2007-06-12 06:35:45 -04002724int btrfs_init_cachep(void)
2725{
Chris Mason86479a02007-09-10 19:58:16 -04002726 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002727 sizeof(struct btrfs_inode),
2728 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002729 if (!btrfs_inode_cachep)
2730 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002731 btrfs_trans_handle_cachep =
2732 btrfs_cache_create("btrfs_trans_handle_cache",
2733 sizeof(struct btrfs_trans_handle),
2734 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002735 if (!btrfs_trans_handle_cachep)
2736 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002737 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002738 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002739 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002740 if (!btrfs_transaction_cachep)
2741 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002742 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002743 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002744 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002745 if (!btrfs_path_cachep)
2746 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002747 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002748 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002749 if (!btrfs_bit_radix_cachep)
2750 goto fail;
2751 return 0;
2752fail:
2753 btrfs_destroy_cachep();
2754 return -ENOMEM;
2755}
2756
2757static int btrfs_getattr(struct vfsmount *mnt,
2758 struct dentry *dentry, struct kstat *stat)
2759{
2760 struct inode *inode = dentry->d_inode;
2761 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002762 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05002763 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04002764 return 0;
2765}
2766
2767static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2768 struct inode * new_dir,struct dentry *new_dentry)
2769{
2770 struct btrfs_trans_handle *trans;
2771 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2772 struct inode *new_inode = new_dentry->d_inode;
2773 struct inode *old_inode = old_dentry->d_inode;
2774 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002775 int ret;
2776
2777 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2778 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2779 return -ENOTEMPTY;
2780 }
Chris Mason5f39d392007-10-15 16:14:19 -04002781
Chris Mason1832a6d2007-12-21 16:27:21 -05002782 ret = btrfs_check_free_space(root, 1, 0);
2783 if (ret)
2784 goto out_unlock;
2785
Chris Mason39279cc2007-06-12 06:35:45 -04002786 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002787
Chris Mason39279cc2007-06-12 06:35:45 -04002788 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002789
2790 old_dentry->d_inode->i_nlink++;
2791 old_dir->i_ctime = old_dir->i_mtime = ctime;
2792 new_dir->i_ctime = new_dir->i_mtime = ctime;
2793 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002794
Chris Mason39279cc2007-06-12 06:35:45 -04002795 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2796 if (ret)
2797 goto out_fail;
2798
2799 if (new_inode) {
2800 new_inode->i_ctime = CURRENT_TIME;
2801 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2802 if (ret)
2803 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002804 }
Chris Mason9c583092008-01-29 15:15:18 -05002805 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002806 if (ret)
2807 goto out_fail;
2808
2809out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002810 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002811out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002812 return ret;
2813}
2814
2815static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2816 const char *symname)
2817{
2818 struct btrfs_trans_handle *trans;
2819 struct btrfs_root *root = BTRFS_I(dir)->root;
2820 struct btrfs_path *path;
2821 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002822 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002823 int err;
2824 int drop_inode = 0;
2825 u64 objectid;
2826 int name_len;
2827 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002828 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002829 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002830 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002831 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002832
2833 name_len = strlen(symname) + 1;
2834 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2835 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002836
Chris Mason1832a6d2007-12-21 16:27:21 -05002837 err = btrfs_check_free_space(root, 1, 0);
2838 if (err)
2839 goto out_fail;
2840
Chris Mason39279cc2007-06-12 06:35:45 -04002841 trans = btrfs_start_transaction(root, 1);
2842 btrfs_set_trans_block_group(trans, dir);
2843
2844 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2845 if (err) {
2846 err = -ENOSPC;
2847 goto out_unlock;
2848 }
2849
Chris Mason9c583092008-01-29 15:15:18 -05002850 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2851 dentry->d_name.len,
2852 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002853 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2854 err = PTR_ERR(inode);
2855 if (IS_ERR(inode))
2856 goto out_unlock;
2857
2858 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002859 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002860 if (err)
2861 drop_inode = 1;
2862 else {
2863 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002864 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002865 inode->i_fop = &btrfs_file_operations;
2866 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002867 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2868 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002869 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002870 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2871 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05002872 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason81d7ed22008-04-25 08:51:48 -04002873 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Masond1310b22008-01-24 16:13:08 -05002874 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002875 }
2876 dir->i_sb->s_dirt = 1;
2877 btrfs_update_inode_block_group(trans, inode);
2878 btrfs_update_inode_block_group(trans, dir);
2879 if (drop_inode)
2880 goto out_unlock;
2881
2882 path = btrfs_alloc_path();
2883 BUG_ON(!path);
2884 key.objectid = inode->i_ino;
2885 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002886 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2887 datasize = btrfs_file_extent_calc_inline_size(name_len);
2888 err = btrfs_insert_empty_item(trans, root, path, &key,
2889 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002890 if (err) {
2891 drop_inode = 1;
2892 goto out_unlock;
2893 }
Chris Mason5f39d392007-10-15 16:14:19 -04002894 leaf = path->nodes[0];
2895 ei = btrfs_item_ptr(leaf, path->slots[0],
2896 struct btrfs_file_extent_item);
2897 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2898 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002899 BTRFS_FILE_EXTENT_INLINE);
2900 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002901 write_extent_buffer(leaf, symname, ptr, name_len);
2902 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002903 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002904
Chris Mason39279cc2007-06-12 06:35:45 -04002905 inode->i_op = &btrfs_symlink_inode_operations;
2906 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04002907 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002908 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002909 err = btrfs_update_inode(trans, root, inode);
2910 if (err)
2911 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002912
2913out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002914 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002915 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002916out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002917 if (drop_inode) {
2918 inode_dec_link_count(inode);
2919 iput(inode);
2920 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002921 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002922 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002923 return err;
2924}
Chris Mason16432982008-04-10 10:23:21 -04002925
Yanfdebe2b2008-01-14 13:26:08 -05002926static int btrfs_permission(struct inode *inode, int mask,
2927 struct nameidata *nd)
2928{
2929 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2930 return -EACCES;
2931 return generic_permission(inode, mask, NULL);
2932}
Chris Mason39279cc2007-06-12 06:35:45 -04002933
2934static struct inode_operations btrfs_dir_inode_operations = {
2935 .lookup = btrfs_lookup,
2936 .create = btrfs_create,
2937 .unlink = btrfs_unlink,
2938 .link = btrfs_link,
2939 .mkdir = btrfs_mkdir,
2940 .rmdir = btrfs_rmdir,
2941 .rename = btrfs_rename,
2942 .symlink = btrfs_symlink,
2943 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04002944 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05002945 .setxattr = generic_setxattr,
2946 .getxattr = generic_getxattr,
2947 .listxattr = btrfs_listxattr,
2948 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05002949 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002950};
Chris Mason39279cc2007-06-12 06:35:45 -04002951static struct inode_operations btrfs_dir_ro_inode_operations = {
2952 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05002953 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04002954};
Chris Mason39279cc2007-06-12 06:35:45 -04002955static struct file_operations btrfs_dir_file_operations = {
2956 .llseek = generic_file_llseek,
2957 .read = generic_read_dir,
2958 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002959 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002960#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04002961 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04002962#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04002963 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04002964};
2965
Chris Masond1310b22008-01-24 16:13:08 -05002966static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04002967 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05002968 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04002969 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04002970 .readpage_io_hook = btrfs_readpage_io_hook,
2971 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04002972 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05002973 .set_bit_hook = btrfs_set_bit_hook,
2974 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04002975};
2976
Chris Mason39279cc2007-06-12 06:35:45 -04002977static struct address_space_operations btrfs_aops = {
2978 .readpage = btrfs_readpage,
2979 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04002980 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05002981 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04002982 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04002983 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04002984 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04002985 .invalidatepage = btrfs_invalidatepage,
2986 .releasepage = btrfs_releasepage,
2987 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04002988};
2989
2990static struct address_space_operations btrfs_symlink_aops = {
2991 .readpage = btrfs_readpage,
2992 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04002993 .invalidatepage = btrfs_invalidatepage,
2994 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04002995};
2996
2997static struct inode_operations btrfs_file_inode_operations = {
2998 .truncate = btrfs_truncate,
2999 .getattr = btrfs_getattr,
3000 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003001 .setxattr = generic_setxattr,
3002 .getxattr = generic_getxattr,
3003 .listxattr = btrfs_listxattr,
3004 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003005 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003006};
Josef Bacik618e21d2007-07-11 10:18:17 -04003007static struct inode_operations btrfs_special_inode_operations = {
3008 .getattr = btrfs_getattr,
3009 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003010 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003011};
Chris Mason39279cc2007-06-12 06:35:45 -04003012static struct inode_operations btrfs_symlink_inode_operations = {
3013 .readlink = generic_readlink,
3014 .follow_link = page_follow_link_light,
3015 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003016 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003017};