blob: 6c778043207d967b67412febc10f8f71b1af34c7 [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>
Josef Bacik33268ea2008-07-24 12:16:36 -040039#include <linux/posix_acl.h>
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040046#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040047#include "ordered-data.h"
Chris Mason39279cc2007-06-12 06:35:45 -040048
49struct btrfs_iget_args {
50 u64 ino;
51 struct btrfs_root *root;
52};
53
54static struct inode_operations btrfs_dir_inode_operations;
55static struct inode_operations btrfs_symlink_inode_operations;
56static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040057static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040058static struct inode_operations btrfs_file_inode_operations;
59static struct address_space_operations btrfs_aops;
60static struct address_space_operations btrfs_symlink_aops;
61static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050062static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040063
64static struct kmem_cache *btrfs_inode_cachep;
65struct kmem_cache *btrfs_trans_handle_cachep;
66struct kmem_cache *btrfs_transaction_cachep;
67struct kmem_cache *btrfs_bit_radix_cachep;
68struct kmem_cache *btrfs_path_cachep;
69
70#define S_SHIFT 12
71static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
72 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
73 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
74 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
75 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
76 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
77 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
78 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
79};
80
Josef Bacik7b128762008-07-24 12:17:14 -040081static void btrfs_truncate(struct inode *inode);
82
Chris Mason1832a6d2007-12-21 16:27:21 -050083int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
84 int for_del)
85{
Chris Masona2135012008-06-25 16:01:30 -040086 u64 total;
87 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050088 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040089 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050090 int ret = 0;
91
Chris Masona2135012008-06-25 16:01:30 -040092 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
93 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
94 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050095 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050096 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050097 else
Chris Masonf9ef6602008-01-03 09:22:38 -050098 thresh = total * 85;
99
100 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500101
Chris Mason1832a6d2007-12-21 16:27:21 -0500102 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
103 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400104 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500105 return ret;
106}
107
Chris Masonbe20aa92007-12-17 20:14:01 -0500108static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400109{
110 struct btrfs_root *root = BTRFS_I(inode)->root;
111 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400112 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400113 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500114 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400115 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500116 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500117 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400118 struct extent_map *em;
119 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
120 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400121
Chris Masonf9295742008-07-17 12:54:14 -0400122 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400123 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500124 btrfs_set_trans_block_group(trans, inode);
125
Chris Masondb945352007-10-15 16:15:53 -0400126 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500127 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500128 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400129
Chris Mason179e29e2007-11-01 11:28:41 -0400130 if (alloc_hint == EXTENT_MAP_INLINE)
131 goto out;
132
Chris Mason3b951512008-04-17 11:29:12 -0400133 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone5a22172008-07-18 20:42:20 -0400134 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400135 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Masone5a22172008-07-18 20:42:20 -0400136 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400137
Chris Masonc59f8952007-12-17 20:14:04 -0500138 while(num_bytes > 0) {
139 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400140 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
141 root->sectorsize, 0, 0,
142 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500143 if (ret) {
144 WARN_ON(1);
145 goto out;
146 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400147 em = alloc_extent_map(GFP_NOFS);
148 em->start = start;
149 em->len = ins.offset;
150 em->block_start = ins.objectid;
151 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masone5a22172008-07-18 20:42:20 -0400152 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400153 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400154 while(1) {
155 spin_lock(&em_tree->lock);
156 ret = add_extent_mapping(em_tree, em);
157 spin_unlock(&em_tree->lock);
158 if (ret != -EEXIST) {
159 free_extent_map(em);
160 break;
161 }
162 btrfs_drop_extent_cache(inode, start,
163 start + ins.offset - 1);
164 }
Chris Masone5a22172008-07-18 20:42:20 -0400165 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400166
Chris Mason98d20f62008-04-14 09:46:10 -0400167 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400168 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Yan Zheng7ea394f2008-08-05 13:05:02 -0400169 ins.offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400170 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400171 if (num_bytes < cur_alloc_size) {
172 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
173 cur_alloc_size);
174 break;
175 }
Chris Masonc59f8952007-12-17 20:14:04 -0500176 num_bytes -= cur_alloc_size;
177 alloc_hint = ins.objectid + ins.offset;
178 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400179 }
Chris Masonb888db22007-08-27 16:49:44 -0400180out:
181 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500182 return ret;
183}
184
185static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
186{
187 u64 extent_start;
188 u64 extent_end;
189 u64 bytenr;
Chris Mason1832a6d2007-12-21 16:27:21 -0500190 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500191 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500192 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400193 struct btrfs_block_group_cache *block_group;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400194 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500195 struct extent_buffer *leaf;
196 int found_type;
197 struct btrfs_path *path;
198 struct btrfs_file_extent_item *item;
199 int ret;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400200 int err = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500201 struct btrfs_key found_key;
202
Chris Masonc31f8832008-01-08 15:46:31 -0500203 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500204 path = btrfs_alloc_path();
205 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400206 trans = btrfs_join_transaction(root, 1);
207 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500208again:
209 ret = btrfs_lookup_file_extent(NULL, root, path,
210 inode->i_ino, start, 0);
211 if (ret < 0) {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400212 err = ret;
213 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500214 }
215
Chris Masonbe20aa92007-12-17 20:14:01 -0500216 if (ret != 0) {
217 if (path->slots[0] == 0)
218 goto not_found;
219 path->slots[0]--;
220 }
221
222 leaf = path->nodes[0];
223 item = btrfs_item_ptr(leaf, path->slots[0],
224 struct btrfs_file_extent_item);
225
226 /* are we inside the extent that was found? */
227 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
228 found_type = btrfs_key_type(&found_key);
229 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400230 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500231 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500232
233 found_type = btrfs_file_extent_type(leaf, item);
234 extent_start = found_key.offset;
235 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500236 u64 extent_num_bytes;
237
238 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
239 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500240 err = 0;
241
Chris Mason1832a6d2007-12-21 16:27:21 -0500242 if (loops && start != extent_start)
243 goto not_found;
244
Chris Masonbe20aa92007-12-17 20:14:01 -0500245 if (start < extent_start || start >= extent_end)
246 goto not_found;
247
Chris Masonbe20aa92007-12-17 20:14:01 -0500248 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
249 if (bytenr == 0)
250 goto not_found;
251
Yan Zheng7ea394f2008-08-05 13:05:02 -0400252 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
Chris Masona68d5932008-05-08 14:11:56 -0400253 goto not_found;
Chris Masonc31f8832008-01-08 15:46:31 -0500254 /*
255 * we may be called by the resizer, make sure we're inside
256 * the limits of the FS
257 */
Chris Masona68d5932008-05-08 14:11:56 -0400258 block_group = btrfs_lookup_block_group(root->fs_info,
259 bytenr);
260 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500261 goto not_found;
262
Yan Zheng7ea394f2008-08-05 13:05:02 -0400263 bytenr += btrfs_file_extent_offset(leaf, item);
264 extent_num_bytes = min(end + 1, extent_end) - start;
265 ret = btrfs_add_ordered_extent(inode, start, bytenr,
266 extent_num_bytes, 1);
267 if (ret) {
268 err = ret;
269 goto out;
270 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500271
Yan Zheng7ea394f2008-08-05 13:05:02 -0400272 btrfs_release_path(root, path);
273 start = extent_end;
274 if (start <= end) {
275 loops++;
276 goto again;
277 }
278 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500279not_found:
Yan Zheng7ea394f2008-08-05 13:05:02 -0400280 btrfs_end_transaction(trans, root);
281 btrfs_free_path(path);
282 return cow_file_range(inode, start, end);
283 }
284out:
285 WARN_ON(err);
286 btrfs_end_transaction(trans, root);
287 btrfs_free_path(path);
288 return err;
Chris Masonbe20aa92007-12-17 20:14:01 -0500289}
290
291static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
292{
293 struct btrfs_root *root = BTRFS_I(inode)->root;
294 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400295
Yanb98b6762008-01-08 15:54:37 -0500296 if (btrfs_test_opt(root, NODATACOW) ||
297 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500298 ret = run_delalloc_nocow(inode, start, end);
299 else
300 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500301
Chris Masonb888db22007-08-27 16:49:44 -0400302 return ret;
303}
304
Chris Mason291d6732008-01-29 15:55:23 -0500305int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500306 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500307{
Chris Masonbcbfce82008-04-22 13:26:47 -0400308 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500309 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500310 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400311 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500312 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500313 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -0400314 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
315 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
316 &root->fs_info->delalloc_inodes);
317 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400318 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500319 }
320 return 0;
321}
322
323int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500324 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500325{
Chris Masonb0c68f82008-01-31 11:05:37 -0500326 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500327 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400328 unsigned long flags;
329
330 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500331 if (end - start + 1 > root->fs_info->delalloc_bytes) {
332 printk("warning: delalloc account %Lu %Lu\n",
333 end - start + 1, root->fs_info->delalloc_bytes);
334 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500335 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500336 } else {
337 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500338 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500339 }
Chris Masonea8c2812008-08-04 23:17:27 -0400340 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
341 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
342 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
343 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400344 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500345 }
346 return 0;
347}
348
Chris Mason239b14b2008-03-24 15:02:07 -0400349int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
350 size_t size, struct bio *bio)
351{
352 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
353 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400354 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400355 u64 length = 0;
356 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400357 int ret;
358
Chris Masonf2d8d742008-04-21 10:03:05 -0400359 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400360 map_tree = &root->fs_info->mapping_tree;
361 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400362 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400363 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400364
Chris Mason239b14b2008-03-24 15:02:07 -0400365 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400366 return 1;
367 }
368 return 0;
369}
370
Chris Mason44b8bd72008-04-16 11:14:51 -0400371int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400372 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500373{
Chris Mason065631f2008-02-20 12:07:25 -0500374 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500375 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400376
Chris Mason3edf7d32008-07-18 06:17:13 -0400377 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400378 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400379
Chris Mason8b712842008-06-11 16:50:36 -0400380 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400381}
382
383int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
384 int mirror_num)
385{
386 struct btrfs_root *root = BTRFS_I(inode)->root;
387 int ret = 0;
388
Chris Masone6dcd2d2008-07-17 12:53:50 -0400389 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
390 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500391
Chris Masone6dcd2d2008-07-17 12:53:50 -0400392 if (!(rw & (1 << BIO_RW))) {
Chris Mason0b86a832008-03-24 15:01:56 -0400393 goto mapit;
394 }
Chris Mason065631f2008-02-20 12:07:25 -0500395
Yan Zheng7ea394f2008-08-05 13:05:02 -0400396 if (btrfs_test_opt(root, NODATASUM) ||
397 btrfs_test_flag(inode, NODATASUM)) {
398 goto mapit;
399 }
400
Chris Mason44b8bd72008-04-16 11:14:51 -0400401 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
402 inode, rw, bio, mirror_num,
403 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400404mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400405 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500406}
Chris Mason6885f302008-02-20 16:11:05 -0500407
Chris Masonba1da2f2008-07-17 12:54:15 -0400408static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400409 struct inode *inode, u64 file_offset,
410 struct list_head *list)
411{
412 struct list_head *cur;
413 struct btrfs_ordered_sum *sum;
414
415 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400416 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400417 sum = list_entry(cur, struct btrfs_ordered_sum, list);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400418 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
419 inode, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400420 }
421 return 0;
422}
423
Chris Masonea8c2812008-08-04 23:17:27 -0400424int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
425{
426 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
427 GFP_NOFS);
428}
429
Chris Mason247e7432008-07-17 12:53:51 -0400430struct btrfs_writepage_fixup {
431 struct page *page;
432 struct btrfs_work work;
433};
434
435/* see btrfs_writepage_start_hook for details on why this is required */
436void btrfs_writepage_fixup_worker(struct btrfs_work *work)
437{
438 struct btrfs_writepage_fixup *fixup;
439 struct btrfs_ordered_extent *ordered;
440 struct page *page;
441 struct inode *inode;
442 u64 page_start;
443 u64 page_end;
444
445 fixup = container_of(work, struct btrfs_writepage_fixup, work);
446 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400447again:
Chris Mason247e7432008-07-17 12:53:51 -0400448 lock_page(page);
449 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
450 ClearPageChecked(page);
451 goto out_page;
452 }
453
454 inode = page->mapping->host;
455 page_start = page_offset(page);
456 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
457
458 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400459
460 /* already ordered? We're done */
461 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
462 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400463 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400464 }
465
466 ordered = btrfs_lookup_ordered_extent(inode, page_start);
467 if (ordered) {
468 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
469 page_end, GFP_NOFS);
470 unlock_page(page);
471 btrfs_start_ordered_extent(inode, ordered, 1);
472 goto again;
473 }
Chris Mason247e7432008-07-17 12:53:51 -0400474
Chris Masonea8c2812008-08-04 23:17:27 -0400475 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -0400476 ClearPageChecked(page);
477out:
478 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
479out_page:
480 unlock_page(page);
481 page_cache_release(page);
482}
483
484/*
485 * There are a few paths in the higher layers of the kernel that directly
486 * set the page dirty bit without asking the filesystem if it is a
487 * good idea. This causes problems because we want to make sure COW
488 * properly happens and the data=ordered rules are followed.
489 *
490 * In our case any range that doesn't have the EXTENT_ORDERED bit set
491 * hasn't been properly setup for IO. We kick off an async process
492 * to fix it up. The async helper will wait for ordered extents, set
493 * the delalloc bit and make it safe to write the page.
494 */
495int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
496{
497 struct inode *inode = page->mapping->host;
498 struct btrfs_writepage_fixup *fixup;
499 struct btrfs_root *root = BTRFS_I(inode)->root;
500 int ret;
501
502 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
503 EXTENT_ORDERED, 0);
504 if (ret)
505 return 0;
506
507 if (PageChecked(page))
508 return -EAGAIN;
509
510 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
511 if (!fixup)
512 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400513
Chris Mason247e7432008-07-17 12:53:51 -0400514 SetPageChecked(page);
515 page_cache_get(page);
516 fixup->work.func = btrfs_writepage_fixup_worker;
517 fixup->page = page;
518 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
519 return -EAGAIN;
520}
521
Chris Mason211f90e2008-07-18 11:56:15 -0400522static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400523{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400524 struct btrfs_root *root = BTRFS_I(inode)->root;
525 struct btrfs_trans_handle *trans;
526 struct btrfs_ordered_extent *ordered_extent;
527 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
528 u64 alloc_hint = 0;
529 struct list_head list;
530 struct btrfs_key ins;
531 int ret;
532
533 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400534 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400535 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400536
Chris Masonf9295742008-07-17 12:54:14 -0400537 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400538
539 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
540 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400541 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
542 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400543
544 lock_extent(io_tree, ordered_extent->file_offset,
545 ordered_extent->file_offset + ordered_extent->len - 1,
546 GFP_NOFS);
547
548 INIT_LIST_HEAD(&list);
549
550 ins.objectid = ordered_extent->start;
551 ins.offset = ordered_extent->len;
552 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400553
Chris Masone6dcd2d2008-07-17 12:53:50 -0400554 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
555 trans->transid, inode->i_ino,
556 ordered_extent->file_offset, &ins);
557 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400558
559 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400560
Chris Masone6dcd2d2008-07-17 12:53:50 -0400561 ret = btrfs_drop_extents(trans, root, inode,
562 ordered_extent->file_offset,
563 ordered_extent->file_offset +
564 ordered_extent->len,
565 ordered_extent->file_offset, &alloc_hint);
566 BUG_ON(ret);
567 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
568 ordered_extent->file_offset,
569 ordered_extent->start,
570 ordered_extent->len,
571 ordered_extent->len, 0);
572 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400573
Chris Masone6dcd2d2008-07-17 12:53:50 -0400574 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
575 ordered_extent->file_offset +
576 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400577 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
578
Chris Masone6dcd2d2008-07-17 12:53:50 -0400579 inode->i_blocks += ordered_extent->len >> 9;
580 unlock_extent(io_tree, ordered_extent->file_offset,
581 ordered_extent->file_offset + ordered_extent->len - 1,
582 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400583nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -0400584 add_pending_csums(trans, inode, ordered_extent->file_offset,
585 &ordered_extent->list);
586
Chris Masondbe674a2008-07-17 12:54:05 -0400587 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400588 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400589
Chris Masone6dcd2d2008-07-17 12:53:50 -0400590 /* once for us */
591 btrfs_put_ordered_extent(ordered_extent);
592 /* once for the tree */
593 btrfs_put_ordered_extent(ordered_extent);
594
595 btrfs_update_inode(trans, root, inode);
596 btrfs_end_transaction(trans, root);
597 return 0;
598}
599
Chris Mason211f90e2008-07-18 11:56:15 -0400600int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
601 struct extent_state *state, int uptodate)
602{
603 return btrfs_finish_ordered_io(page->mapping->host, start, end);
604}
605
Chris Mason3de9d6b2008-08-04 23:17:27 -0400606int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
607{
608 int ret = 0;
609 struct inode *inode = page->mapping->host;
610 struct btrfs_root *root = BTRFS_I(inode)->root;
611 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
612 struct btrfs_csum_item *item;
613 struct btrfs_path *path = NULL;
614 u32 csum;
615
616 if (btrfs_test_opt(root, NODATASUM) ||
617 btrfs_test_flag(inode, NODATASUM))
618 return 0;
619
620 /*
621 * It is possible there is an ordered extent that has
622 * not yet finished for this range in the file. If so,
623 * that extent will have a csum cached, and it will insert
624 * the sum after all the blocks in the extent are fully
625 * on disk. So, look for an ordered extent and use the
626 * sum if found. We have to do this before looking in the
627 * btree because csum items are pre-inserted based on
628 * the file size. btrfs_lookup_csum might find an item
629 * that still hasn't been fully filled.
630 */
631 ret = btrfs_find_ordered_sum(inode, start, &csum);
632 if (ret == 0)
633 goto found;
634
635 ret = 0;
636 path = btrfs_alloc_path();
637 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
638 if (IS_ERR(item)) {
639 ret = PTR_ERR(item);
640 /* a csum that isn't present is a preallocated region. */
641 if (ret == -ENOENT || ret == -EFBIG)
642 ret = 0;
643 csum = 0;
Chris Mason7c2fe322008-08-20 08:51:50 -0400644 if (printk_ratelimit())
645 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
646 start);
Chris Mason3de9d6b2008-08-04 23:17:27 -0400647 goto out;
648 }
649 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
650 BTRFS_CRC32_SIZE);
651found:
652 set_state_private(io_tree, start, csum);
653out:
654 if (path)
655 btrfs_free_path(path);
656 return ret;
657}
658
Chris Mason7e383262008-04-09 16:28:12 -0400659struct io_failure_record {
660 struct page *page;
661 u64 start;
662 u64 len;
663 u64 logical;
664 int last_mirror;
665};
666
Chris Mason1259ab72008-05-12 13:39:03 -0400667int btrfs_io_failed_hook(struct bio *failed_bio,
668 struct page *page, u64 start, u64 end,
669 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400670{
671 struct io_failure_record *failrec = NULL;
672 u64 private;
673 struct extent_map *em;
674 struct inode *inode = page->mapping->host;
675 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400676 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400677 struct bio *bio;
678 int num_copies;
679 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400680 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400681 u64 logical;
682
683 ret = get_state_private(failure_tree, start, &private);
684 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400685 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
686 if (!failrec)
687 return -ENOMEM;
688 failrec->start = start;
689 failrec->len = end - start + 1;
690 failrec->last_mirror = 0;
691
Chris Mason3b951512008-04-17 11:29:12 -0400692 spin_lock(&em_tree->lock);
693 em = lookup_extent_mapping(em_tree, start, failrec->len);
694 if (em->start > start || em->start + em->len < start) {
695 free_extent_map(em);
696 em = NULL;
697 }
698 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400699
700 if (!em || IS_ERR(em)) {
701 kfree(failrec);
702 return -EIO;
703 }
704 logical = start - em->start;
705 logical = em->block_start + logical;
706 failrec->logical = logical;
707 free_extent_map(em);
708 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
709 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400710 set_state_private(failure_tree, start,
711 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400712 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400713 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400714 }
715 num_copies = btrfs_num_copies(
716 &BTRFS_I(inode)->root->fs_info->mapping_tree,
717 failrec->logical, failrec->len);
718 failrec->last_mirror++;
719 if (!state) {
720 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
721 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
722 failrec->start,
723 EXTENT_LOCKED);
724 if (state && state->start != failrec->start)
725 state = NULL;
726 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
727 }
728 if (!state || failrec->last_mirror > num_copies) {
729 set_state_private(failure_tree, failrec->start, 0);
730 clear_extent_bits(failure_tree, failrec->start,
731 failrec->start + failrec->len - 1,
732 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
733 kfree(failrec);
734 return -EIO;
735 }
736 bio = bio_alloc(GFP_NOFS, 1);
737 bio->bi_private = state;
738 bio->bi_end_io = failed_bio->bi_end_io;
739 bio->bi_sector = failrec->logical >> 9;
740 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400741 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400742 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400743 if (failed_bio->bi_rw & (1 << BIO_RW))
744 rw = WRITE;
745 else
746 rw = READ;
747
748 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
749 failrec->last_mirror);
750 return 0;
751}
752
753int btrfs_clean_io_failures(struct inode *inode, u64 start)
754{
755 u64 private;
756 u64 private_failure;
757 struct io_failure_record *failure;
758 int ret;
759
760 private = 0;
761 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
762 (u64)-1, 1, EXTENT_DIRTY)) {
763 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
764 start, &private_failure);
765 if (ret == 0) {
766 failure = (struct io_failure_record *)(unsigned long)
767 private_failure;
768 set_state_private(&BTRFS_I(inode)->io_failure_tree,
769 failure->start, 0);
770 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
771 failure->start,
772 failure->start + failure->len - 1,
773 EXTENT_DIRTY | EXTENT_LOCKED,
774 GFP_NOFS);
775 kfree(failure);
776 }
777 }
Chris Mason7e383262008-04-09 16:28:12 -0400778 return 0;
779}
780
Chris Mason70dec802008-01-29 09:59:12 -0500781int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
782 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400783{
Chris Mason35ebb932007-10-30 16:56:53 -0400784 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400785 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500786 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400787 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500788 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400789 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400790 struct btrfs_root *root = BTRFS_I(inode)->root;
791 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400792 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500793
Yanb98b6762008-01-08 15:54:37 -0500794 if (btrfs_test_opt(root, NODATASUM) ||
795 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500796 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500797 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500798 private = state->private;
799 ret = 0;
800 } else {
801 ret = get_state_private(io_tree, start, &private);
802 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400803 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400804 kaddr = kmap_atomic(page, KM_IRQ0);
805 if (ret) {
806 goto zeroit;
807 }
Chris Masonff79f812007-10-15 16:22:25 -0400808 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
809 btrfs_csum_final(csum, (char *)&csum);
810 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400811 goto zeroit;
812 }
813 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400814 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400815
816 /* if the io failure tree for this inode is non-empty,
817 * check to see if we've recovered from a failed IO
818 */
Chris Mason1259ab72008-05-12 13:39:03 -0400819 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400820 return 0;
821
822zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500823 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
824 page->mapping->host->i_ino, (unsigned long long)start, csum,
825 private);
Chris Masondb945352007-10-15 16:15:53 -0400826 memset(kaddr + offset, 1, end - start + 1);
827 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400828 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400829 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400830 if (private == 0)
831 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400832 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400833}
Chris Masonb888db22007-08-27 16:49:44 -0400834
Josef Bacik7b128762008-07-24 12:17:14 -0400835/*
836 * This creates an orphan entry for the given inode in case something goes
837 * wrong in the middle of an unlink/truncate.
838 */
839int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
840{
841 struct btrfs_root *root = BTRFS_I(inode)->root;
842 int ret = 0;
843
Yanbcc63ab2008-07-30 16:29:20 -0400844 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400845
846 /* already on the orphan list, we're good */
847 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400848 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400849 return 0;
850 }
851
852 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
853
Yanbcc63ab2008-07-30 16:29:20 -0400854 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400855
856 /*
857 * insert an orphan item to track this unlinked/truncated file
858 */
859 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
860
861 return ret;
862}
863
864/*
865 * We have done the truncate/delete so we can go ahead and remove the orphan
866 * item for this particular inode.
867 */
868int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
869{
870 struct btrfs_root *root = BTRFS_I(inode)->root;
871 int ret = 0;
872
Yanbcc63ab2008-07-30 16:29:20 -0400873 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400874
875 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400876 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400877 return 0;
878 }
879
880 list_del_init(&BTRFS_I(inode)->i_orphan);
881 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400882 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400883 return 0;
884 }
885
Yanbcc63ab2008-07-30 16:29:20 -0400886 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400887
888 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
889
890 return ret;
891}
892
893/*
894 * this cleans up any orphans that may be left on the list from the last use
895 * of this root.
896 */
897void btrfs_orphan_cleanup(struct btrfs_root *root)
898{
899 struct btrfs_path *path;
900 struct extent_buffer *leaf;
901 struct btrfs_item *item;
902 struct btrfs_key key, found_key;
903 struct btrfs_trans_handle *trans;
904 struct inode *inode;
905 int ret = 0, nr_unlink = 0, nr_truncate = 0;
906
907 /* don't do orphan cleanup if the fs is readonly. */
908 if (root->inode->i_sb->s_flags & MS_RDONLY)
909 return;
910
911 path = btrfs_alloc_path();
912 if (!path)
913 return;
914 path->reada = -1;
915
916 key.objectid = BTRFS_ORPHAN_OBJECTID;
917 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
918 key.offset = (u64)-1;
919
920 trans = btrfs_start_transaction(root, 1);
921 btrfs_set_trans_block_group(trans, root->inode);
922
923 while (1) {
924 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
925 if (ret < 0) {
926 printk(KERN_ERR "Error searching slot for orphan: %d"
927 "\n", ret);
928 break;
929 }
930
931 /*
932 * if ret == 0 means we found what we were searching for, which
933 * is weird, but possible, so only screw with path if we didnt
934 * find the key and see if we have stuff that matches
935 */
936 if (ret > 0) {
937 if (path->slots[0] == 0)
938 break;
939 path->slots[0]--;
940 }
941
942 /* pull out the item */
943 leaf = path->nodes[0];
944 item = btrfs_item_nr(leaf, path->slots[0]);
945 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
946
947 /* make sure the item matches what we want */
948 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
949 break;
950 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
951 break;
952
953 /* release the path since we're done with it */
954 btrfs_release_path(root, path);
955
956 /*
957 * this is where we are basically btrfs_lookup, without the
958 * crossing root thing. we store the inode number in the
959 * offset of the orphan item.
960 */
961 inode = btrfs_iget_locked(root->inode->i_sb,
962 found_key.offset, root);
963 if (!inode)
964 break;
965
966 if (inode->i_state & I_NEW) {
967 BTRFS_I(inode)->root = root;
968
969 /* have to set the location manually */
970 BTRFS_I(inode)->location.objectid = inode->i_ino;
971 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
972 BTRFS_I(inode)->location.offset = 0;
973
974 btrfs_read_locked_inode(inode);
975 unlock_new_inode(inode);
976 }
977
978 /*
979 * add this inode to the orphan list so btrfs_orphan_del does
980 * the proper thing when we hit it
981 */
Yanbcc63ab2008-07-30 16:29:20 -0400982 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400983 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -0400984 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400985
986 /*
987 * if this is a bad inode, means we actually succeeded in
988 * removing the inode, but not the orphan record, which means
989 * we need to manually delete the orphan since iput will just
990 * do a destroy_inode
991 */
992 if (is_bad_inode(inode)) {
993 btrfs_orphan_del(trans, inode);
994 iput(inode);
995 continue;
996 }
997
998 /* if we have links, this was a truncate, lets do that */
999 if (inode->i_nlink) {
1000 nr_truncate++;
1001 btrfs_truncate(inode);
1002 } else {
1003 nr_unlink++;
1004 }
1005
1006 /* this will do delete_inode and everything for us */
1007 iput(inode);
1008 }
1009
1010 if (nr_unlink)
1011 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1012 if (nr_truncate)
1013 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1014
1015 btrfs_free_path(path);
1016 btrfs_end_transaction(trans, root);
1017}
1018
Chris Mason39279cc2007-06-12 06:35:45 -04001019void btrfs_read_locked_inode(struct inode *inode)
1020{
1021 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001022 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001023 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001024 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001025 struct btrfs_root *root = BTRFS_I(inode)->root;
1026 struct btrfs_key location;
1027 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001028 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001029 int ret;
1030
1031 path = btrfs_alloc_path();
1032 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001033 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001034
Chris Mason39279cc2007-06-12 06:35:45 -04001035 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001036 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001037 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001038
Chris Mason5f39d392007-10-15 16:14:19 -04001039 leaf = path->nodes[0];
1040 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1041 struct btrfs_inode_item);
1042
1043 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1044 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1045 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1046 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001047 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001048
1049 tspec = btrfs_inode_atime(inode_item);
1050 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1051 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1052
1053 tspec = btrfs_inode_mtime(inode_item);
1054 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1055 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1056
1057 tspec = btrfs_inode_ctime(inode_item);
1058 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1059 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1060
1061 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1062 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -04001063 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001064 rdev = btrfs_inode_rdev(leaf, inode_item);
1065
Josef Bacikaec74772008-07-24 12:12:38 -04001066 BTRFS_I(inode)->index_cnt = (u64)-1;
1067
Chris Mason5f39d392007-10-15 16:14:19 -04001068 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001069 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1070 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001071 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001072 if (!BTRFS_I(inode)->block_group) {
1073 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001074 NULL, 0,
1075 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001076 }
Chris Mason39279cc2007-06-12 06:35:45 -04001077 btrfs_free_path(path);
1078 inode_item = NULL;
1079
Chris Mason39279cc2007-06-12 06:35:45 -04001080 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001081 case S_IFREG:
1082 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001083 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001084 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001085 inode->i_fop = &btrfs_file_operations;
1086 inode->i_op = &btrfs_file_inode_operations;
1087 break;
1088 case S_IFDIR:
1089 inode->i_fop = &btrfs_dir_file_operations;
1090 if (root == root->fs_info->tree_root)
1091 inode->i_op = &btrfs_dir_ro_inode_operations;
1092 else
1093 inode->i_op = &btrfs_dir_inode_operations;
1094 break;
1095 case S_IFLNK:
1096 inode->i_op = &btrfs_symlink_inode_operations;
1097 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001098 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001099 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001100 default:
1101 init_special_inode(inode, inode->i_mode, rdev);
1102 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001103 }
1104 return;
1105
1106make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001107 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001108 make_bad_inode(inode);
1109}
1110
Chris Mason5f39d392007-10-15 16:14:19 -04001111static void fill_inode_item(struct extent_buffer *leaf,
1112 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001113 struct inode *inode)
1114{
Chris Mason5f39d392007-10-15 16:14:19 -04001115 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1116 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001117 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001118 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1119 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1120
1121 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1122 inode->i_atime.tv_sec);
1123 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1124 inode->i_atime.tv_nsec);
1125
1126 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1127 inode->i_mtime.tv_sec);
1128 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1129 inode->i_mtime.tv_nsec);
1130
1131 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1132 inode->i_ctime.tv_sec);
1133 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1134 inode->i_ctime.tv_nsec);
1135
1136 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1137 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1138 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001139 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001140 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001141 BTRFS_I(inode)->block_group->key.objectid);
1142}
1143
Chris Masonba1da2f2008-07-17 12:54:15 -04001144int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001145 struct btrfs_root *root,
1146 struct inode *inode)
1147{
1148 struct btrfs_inode_item *inode_item;
1149 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001150 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001151 int ret;
1152
1153 path = btrfs_alloc_path();
1154 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001155 ret = btrfs_lookup_inode(trans, root, path,
1156 &BTRFS_I(inode)->location, 1);
1157 if (ret) {
1158 if (ret > 0)
1159 ret = -ENOENT;
1160 goto failed;
1161 }
1162
Chris Mason5f39d392007-10-15 16:14:19 -04001163 leaf = path->nodes[0];
1164 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001165 struct btrfs_inode_item);
1166
Chris Mason5f39d392007-10-15 16:14:19 -04001167 fill_inode_item(leaf, inode_item, inode);
1168 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001169 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001170 ret = 0;
1171failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001172 btrfs_free_path(path);
1173 return ret;
1174}
1175
1176
1177static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1178 struct btrfs_root *root,
1179 struct inode *dir,
1180 struct dentry *dentry)
1181{
1182 struct btrfs_path *path;
1183 const char *name = dentry->d_name.name;
1184 int name_len = dentry->d_name.len;
1185 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001186 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001187 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001188 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001189 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001190
1191 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001192 if (!path) {
1193 ret = -ENOMEM;
1194 goto err;
1195 }
1196
Chris Mason39279cc2007-06-12 06:35:45 -04001197 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1198 name, name_len, -1);
1199 if (IS_ERR(di)) {
1200 ret = PTR_ERR(di);
1201 goto err;
1202 }
1203 if (!di) {
1204 ret = -ENOENT;
1205 goto err;
1206 }
Chris Mason5f39d392007-10-15 16:14:19 -04001207 leaf = path->nodes[0];
1208 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001209 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001210 if (ret)
1211 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001212 btrfs_release_path(root, path);
1213
Josef Bacikaec74772008-07-24 12:12:38 -04001214 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1215 dentry->d_inode->i_ino,
1216 dentry->d_parent->d_inode->i_ino, &index);
1217 if (ret) {
1218 printk("failed to delete reference to %.*s, "
1219 "inode %lu parent %lu\n", name_len, name,
1220 dentry->d_inode->i_ino,
1221 dentry->d_parent->d_inode->i_ino);
1222 goto err;
1223 }
1224
Chris Mason39279cc2007-06-12 06:35:45 -04001225 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001226 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001227 if (IS_ERR(di)) {
1228 ret = PTR_ERR(di);
1229 goto err;
1230 }
1231 if (!di) {
1232 ret = -ENOENT;
1233 goto err;
1234 }
1235 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001236 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001237
1238 dentry->d_inode->i_ctime = dir->i_ctime;
1239err:
1240 btrfs_free_path(path);
1241 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001242 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001243 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001244 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001245#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1246 dentry->d_inode->i_nlink--;
1247#else
Chris Mason39279cc2007-06-12 06:35:45 -04001248 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001249#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001250 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001251 dir->i_sb->s_dirt = 1;
1252 }
1253 return ret;
1254}
1255
1256static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1257{
1258 struct btrfs_root *root;
1259 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001260 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001261 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001262 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001263
1264 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001265
1266 ret = btrfs_check_free_space(root, 1, 1);
1267 if (ret)
1268 goto fail;
1269
Chris Mason39279cc2007-06-12 06:35:45 -04001270 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001271
Chris Mason39279cc2007-06-12 06:35:45 -04001272 btrfs_set_trans_block_group(trans, dir);
1273 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Josef Bacik7b128762008-07-24 12:17:14 -04001274
1275 if (inode->i_nlink == 0)
1276 ret = btrfs_orphan_add(trans, inode);
1277
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001278 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001279
Chris Mason89ce8a62008-06-25 16:01:31 -04001280 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001281fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001282 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001283 return ret;
1284}
1285
1286static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1287{
1288 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001289 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001290 int ret;
1291 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001292 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001293 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001294
Chris Mason925baed2008-06-25 16:01:30 -04001295 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001296 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001297 }
Yan134d4512007-10-25 15:49:25 -04001298
Chris Mason1832a6d2007-12-21 16:27:21 -05001299 ret = btrfs_check_free_space(root, 1, 1);
1300 if (ret)
1301 goto fail;
1302
Chris Mason39279cc2007-06-12 06:35:45 -04001303 trans = btrfs_start_transaction(root, 1);
1304 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001305
Josef Bacik7b128762008-07-24 12:17:14 -04001306 err = btrfs_orphan_add(trans, inode);
1307 if (err)
1308 goto fail_trans;
1309
Chris Mason39279cc2007-06-12 06:35:45 -04001310 /* now the directory is empty */
1311 err = btrfs_unlink_trans(trans, root, dir, dentry);
1312 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001313 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001314 }
Chris Mason39544012007-12-12 14:38:19 -05001315
Josef Bacik7b128762008-07-24 12:17:14 -04001316fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001317 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001318 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001319fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001320 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001321
Chris Mason39279cc2007-06-12 06:35:45 -04001322 if (ret && !err)
1323 err = ret;
1324 return err;
1325}
1326
Chris Mason39279cc2007-06-12 06:35:45 -04001327/*
Chris Mason39279cc2007-06-12 06:35:45 -04001328 * this can truncate away extent items, csum items and directory items.
1329 * It starts at a high offset and removes keys until it can't find
1330 * any higher than i_size.
1331 *
1332 * csum items that cross the new i_size are truncated to the new size
1333 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001334 *
1335 * min_type is the minimum key type to truncate down to. If set to 0, this
1336 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001337 */
1338static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1339 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001340 struct inode *inode,
1341 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001342{
1343 int ret;
1344 struct btrfs_path *path;
1345 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001346 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001347 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001348 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001349 struct btrfs_file_extent_item *fi;
1350 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001351 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001352 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001353 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001354 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001355 int found_extent;
1356 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001357 int pending_del_nr = 0;
1358 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001359 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001360 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001361
Chris Mason3b951512008-04-17 11:29:12 -04001362 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001363 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001364 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001365 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001366
Chris Mason39279cc2007-06-12 06:35:45 -04001367 /* FIXME, add redo link to tree so we don't leak on crash */
1368 key.objectid = inode->i_ino;
1369 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001370 key.type = (u8)-1;
1371
Chris Mason85e21ba2008-01-29 15:11:36 -05001372 btrfs_init_path(path);
1373search_again:
1374 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1375 if (ret < 0) {
1376 goto error;
1377 }
1378 if (ret > 0) {
1379 BUG_ON(path->slots[0] == 0);
1380 path->slots[0]--;
1381 }
1382
Chris Mason39279cc2007-06-12 06:35:45 -04001383 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001384 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001385 leaf = path->nodes[0];
1386 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1387 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001388
Chris Mason5f39d392007-10-15 16:14:19 -04001389 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001390 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001391
Chris Mason85e21ba2008-01-29 15:11:36 -05001392 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001393 break;
1394
Chris Mason5f39d392007-10-15 16:14:19 -04001395 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001396 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001397 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001398 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001399 extent_type = btrfs_file_extent_type(leaf, fi);
1400 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001401 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001402 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001403 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1404 struct btrfs_item *item = btrfs_item_nr(leaf,
1405 path->slots[0]);
1406 item_end += btrfs_file_extent_inline_len(leaf,
1407 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001408 }
Yan008630c2007-11-07 13:31:09 -05001409 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001410 }
1411 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1412 ret = btrfs_csum_truncate(trans, root, path,
1413 inode->i_size);
1414 BUG_ON(ret);
1415 }
Yan008630c2007-11-07 13:31:09 -05001416 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001417 if (found_type == BTRFS_DIR_ITEM_KEY) {
1418 found_type = BTRFS_INODE_ITEM_KEY;
1419 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1420 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001421 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1422 found_type = BTRFS_XATTR_ITEM_KEY;
1423 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1424 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001425 } else if (found_type) {
1426 found_type--;
1427 } else {
1428 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001429 }
Yana61721d2007-09-17 11:08:38 -04001430 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001431 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001432 }
Chris Mason5f39d392007-10-15 16:14:19 -04001433 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001434 del_item = 1;
1435 else
1436 del_item = 0;
1437 found_extent = 0;
1438
1439 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001440 if (found_type != BTRFS_EXTENT_DATA_KEY)
1441 goto delete;
1442
1443 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001444 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001445 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001446 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001447 u64 orig_num_bytes =
1448 btrfs_file_extent_num_bytes(leaf, fi);
1449 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001450 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001451 extent_num_bytes = extent_num_bytes &
1452 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001453 btrfs_set_file_extent_num_bytes(leaf, fi,
1454 extent_num_bytes);
1455 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001456 extent_num_bytes);
1457 if (extent_start != 0)
1458 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001459 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001460 } else {
Chris Masondb945352007-10-15 16:15:53 -04001461 extent_num_bytes =
1462 btrfs_file_extent_disk_num_bytes(leaf,
1463 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001464 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001465 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001466 if (extent_start != 0) {
1467 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001468 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001469 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001470 root_gen = btrfs_header_generation(leaf);
1471 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001472 }
Chris Mason90692182008-02-08 13:49:28 -05001473 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1474 if (!del_item) {
1475 u32 newsize = inode->i_size - found_key.offset;
1476 dec_i_blocks(inode, item_end + 1 -
1477 found_key.offset - newsize);
1478 newsize =
1479 btrfs_file_extent_calc_inline_size(newsize);
1480 ret = btrfs_truncate_item(trans, root, path,
1481 newsize, 1);
1482 BUG_ON(ret);
1483 } else {
1484 dec_i_blocks(inode, item_end + 1 -
1485 found_key.offset);
1486 }
Chris Mason39279cc2007-06-12 06:35:45 -04001487 }
Chris Mason179e29e2007-11-01 11:28:41 -04001488delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001489 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001490 if (!pending_del_nr) {
1491 /* no pending yet, add ourselves */
1492 pending_del_slot = path->slots[0];
1493 pending_del_nr = 1;
1494 } else if (pending_del_nr &&
1495 path->slots[0] + 1 == pending_del_slot) {
1496 /* hop on the pending chunk */
1497 pending_del_nr++;
1498 pending_del_slot = path->slots[0];
1499 } else {
1500 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1501 }
Chris Mason39279cc2007-06-12 06:35:45 -04001502 } else {
1503 break;
1504 }
Chris Mason39279cc2007-06-12 06:35:45 -04001505 if (found_extent) {
1506 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001507 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001508 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001509 root_gen, inode->i_ino,
1510 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001511 BUG_ON(ret);
1512 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001513next:
1514 if (path->slots[0] == 0) {
1515 if (pending_del_nr)
1516 goto del_pending;
1517 btrfs_release_path(root, path);
1518 goto search_again;
1519 }
1520
1521 path->slots[0]--;
1522 if (pending_del_nr &&
1523 path->slots[0] + 1 != pending_del_slot) {
1524 struct btrfs_key debug;
1525del_pending:
1526 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1527 pending_del_slot);
1528 ret = btrfs_del_items(trans, root, path,
1529 pending_del_slot,
1530 pending_del_nr);
1531 BUG_ON(ret);
1532 pending_del_nr = 0;
1533 btrfs_release_path(root, path);
1534 goto search_again;
1535 }
Chris Mason39279cc2007-06-12 06:35:45 -04001536 }
1537 ret = 0;
1538error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001539 if (pending_del_nr) {
1540 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1541 pending_del_nr);
1542 }
Chris Mason39279cc2007-06-12 06:35:45 -04001543 btrfs_free_path(path);
1544 inode->i_sb->s_dirt = 1;
1545 return ret;
1546}
1547
Chris Masona52d9a82007-08-27 16:49:44 -04001548/*
1549 * taken from block_truncate_page, but does cow as it zeros out
1550 * any bytes left in the last page in the file.
1551 */
1552static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1553{
1554 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001555 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001556 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1557 struct btrfs_ordered_extent *ordered;
1558 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001559 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001560 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1561 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1562 struct page *page;
1563 int ret = 0;
1564 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001565 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001566
1567 if ((offset & (blocksize - 1)) == 0)
1568 goto out;
1569
1570 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001571again:
Chris Masona52d9a82007-08-27 16:49:44 -04001572 page = grab_cache_page(mapping, index);
1573 if (!page)
1574 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001575
1576 page_start = page_offset(page);
1577 page_end = page_start + PAGE_CACHE_SIZE - 1;
1578
Chris Masona52d9a82007-08-27 16:49:44 -04001579 if (!PageUptodate(page)) {
1580 ret = btrfs_readpage(NULL, page);
1581 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001582 if (page->mapping != mapping) {
1583 unlock_page(page);
1584 page_cache_release(page);
1585 goto again;
1586 }
Chris Masona52d9a82007-08-27 16:49:44 -04001587 if (!PageUptodate(page)) {
1588 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001589 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001590 }
1591 }
Chris Mason211c17f2008-05-15 09:13:45 -04001592 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001593
1594 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1595 set_page_extent_mapped(page);
1596
1597 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1598 if (ordered) {
1599 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1600 unlock_page(page);
1601 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001602 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001603 btrfs_put_ordered_extent(ordered);
1604 goto again;
1605 }
1606
Chris Masonea8c2812008-08-04 23:17:27 -04001607 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001608 ret = 0;
1609 if (offset != PAGE_CACHE_SIZE) {
1610 kaddr = kmap(page);
1611 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1612 flush_dcache_page(page);
1613 kunmap(page);
1614 }
Chris Mason247e7432008-07-17 12:53:51 -04001615 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001616 set_page_dirty(page);
1617 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001618
Chris Mason89642222008-07-24 09:41:53 -04001619out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001620 unlock_page(page);
1621 page_cache_release(page);
1622out:
1623 return ret;
1624}
1625
1626static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1627{
1628 struct inode *inode = dentry->d_inode;
1629 int err;
1630
1631 err = inode_change_ok(inode, attr);
1632 if (err)
1633 return err;
1634
1635 if (S_ISREG(inode->i_mode) &&
1636 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1637 struct btrfs_trans_handle *trans;
1638 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001639 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001640
Chris Mason5f39d392007-10-15 16:14:19 -04001641 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001642 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001643 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001644 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001645 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001646
Chris Mason1b0f7c22008-01-30 14:33:02 -05001647 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001648 goto out;
1649
Chris Mason1832a6d2007-12-21 16:27:21 -05001650 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001651 if (err)
1652 goto fail;
1653
Chris Mason39279cc2007-06-12 06:35:45 -04001654 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1655
Chris Mason5f564062008-01-22 16:47:59 -05001656 hole_size = block_end - hole_start;
Chris Mason7c2fe322008-08-20 08:51:50 -04001657 while(1) {
1658 struct btrfs_ordered_extent *ordered;
1659 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1660
1661 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1662 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
1663 if (ordered) {
1664 unlock_extent(io_tree, hole_start,
1665 block_end - 1, GFP_NOFS);
1666 btrfs_put_ordered_extent(ordered);
1667 } else {
1668 break;
1669 }
1670 }
Chris Mason39279cc2007-06-12 06:35:45 -04001671
Chris Mason39279cc2007-06-12 06:35:45 -04001672 trans = btrfs_start_transaction(root, 1);
1673 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001674 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001675 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001676 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001677 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001678
Chris Mason179e29e2007-11-01 11:28:41 -04001679 if (alloc_hint != EXTENT_MAP_INLINE) {
1680 err = btrfs_insert_file_extent(trans, root,
1681 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001682 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001683 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001684 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001685 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001686 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001687 }
Chris Masonee6e6502008-07-17 12:54:40 -04001688 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001689 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001690 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001691 if (err)
1692 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001693 }
1694out:
1695 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001696
1697 if (!err && ((attr->ia_valid & ATTR_MODE)))
1698 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001699fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001700 return err;
1701}
Chris Mason61295eb2008-01-14 16:24:38 -05001702
Chris Mason39279cc2007-06-12 06:35:45 -04001703void btrfs_delete_inode(struct inode *inode)
1704{
1705 struct btrfs_trans_handle *trans;
1706 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001707 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001708 int ret;
1709
1710 truncate_inode_pages(&inode->i_data, 0);
1711 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001712 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001713 goto no_delete;
1714 }
Chris Mason4a096752008-07-21 10:29:44 -04001715 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001716
Chris Masondbe674a2008-07-17 12:54:05 -04001717 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001718 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001719
Chris Mason39279cc2007-06-12 06:35:45 -04001720 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001721 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001722 if (ret) {
1723 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001724 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001725 }
1726
1727 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001728
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001729 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001730 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001731
Chris Mason39279cc2007-06-12 06:35:45 -04001732 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001733 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001734 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001735
1736no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001737 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001738 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001739 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001740no_delete:
1741 clear_inode(inode);
1742}
1743
1744/*
1745 * this returns the key found in the dir entry in the location pointer.
1746 * If no dir entries were found, location->objectid is 0.
1747 */
1748static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1749 struct btrfs_key *location)
1750{
1751 const char *name = dentry->d_name.name;
1752 int namelen = dentry->d_name.len;
1753 struct btrfs_dir_item *di;
1754 struct btrfs_path *path;
1755 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001756 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001757
Chris Mason39544012007-12-12 14:38:19 -05001758 if (namelen == 1 && strcmp(name, ".") == 0) {
1759 location->objectid = dir->i_ino;
1760 location->type = BTRFS_INODE_ITEM_KEY;
1761 location->offset = 0;
1762 return 0;
1763 }
Chris Mason39279cc2007-06-12 06:35:45 -04001764 path = btrfs_alloc_path();
1765 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001766
Chris Mason7a720532007-12-13 09:06:59 -05001767 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001768 struct btrfs_key key;
1769 struct extent_buffer *leaf;
Chris Mason39544012007-12-12 14:38:19 -05001770 int slot;
1771
1772 key.objectid = dir->i_ino;
Yan445dceb2008-07-24 12:19:32 -04001773 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001774 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001775 if (ret < 0 || path->slots[0] == 0)
1776 goto out_err;
Chris Mason39544012007-12-12 14:38:19 -05001777 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1778 BUG_ON(ret == 0);
1779 ret = 0;
Chris Mason39544012007-12-12 14:38:19 -05001780 leaf = path->nodes[0];
Yan445dceb2008-07-24 12:19:32 -04001781 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001782
1783 btrfs_item_key_to_cpu(leaf, &key, slot);
1784 if (key.objectid != dir->i_ino ||
1785 key.type != BTRFS_INODE_REF_KEY) {
1786 goto out_err;
1787 }
1788 location->objectid = key.offset;
1789 location->type = BTRFS_INODE_ITEM_KEY;
1790 location->offset = 0;
1791 goto out;
1792 }
1793
Chris Mason39279cc2007-06-12 06:35:45 -04001794 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1795 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001796 if (IS_ERR(di))
1797 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001798 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001799 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001800 }
Chris Mason5f39d392007-10-15 16:14:19 -04001801 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001802out:
Chris Mason39279cc2007-06-12 06:35:45 -04001803 btrfs_free_path(path);
1804 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001805out_err:
1806 location->objectid = 0;
1807 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001808}
1809
1810/*
1811 * when we hit a tree root in a directory, the btrfs part of the inode
1812 * needs to be changed to reflect the root directory of the tree root. This
1813 * is kind of like crossing a mount point.
1814 */
1815static int fixup_tree_root_location(struct btrfs_root *root,
1816 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001817 struct btrfs_root **sub_root,
1818 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001819{
Chris Mason39279cc2007-06-12 06:35:45 -04001820 struct btrfs_root_item *ri;
1821
1822 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1823 return 0;
1824 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1825 return 0;
1826
Josef Bacik58176a92007-08-29 15:47:34 -04001827 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1828 dentry->d_name.name,
1829 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001830 if (IS_ERR(*sub_root))
1831 return PTR_ERR(*sub_root);
1832
1833 ri = &(*sub_root)->root_item;
1834 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001835 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1836 location->offset = 0;
1837
Chris Mason39279cc2007-06-12 06:35:45 -04001838 return 0;
1839}
1840
1841static int btrfs_init_locked_inode(struct inode *inode, void *p)
1842{
1843 struct btrfs_iget_args *args = p;
1844 inode->i_ino = args->ino;
1845 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001846 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04001847 inode->i_mapping->writeback_index = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001848 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001849 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001850 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1851 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001852 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001853 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1854 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001855 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04001856 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001857 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001858 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001859 return 0;
1860}
1861
1862static int btrfs_find_actor(struct inode *inode, void *opaque)
1863{
1864 struct btrfs_iget_args *args = opaque;
1865 return (args->ino == inode->i_ino &&
1866 args->root == BTRFS_I(inode)->root);
1867}
1868
Chris Masondc17ff82008-01-08 15:46:30 -05001869struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1870 u64 root_objectid)
1871{
1872 struct btrfs_iget_args args;
1873 args.ino = objectid;
1874 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1875
1876 if (!args.root)
1877 return NULL;
1878
1879 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1880}
1881
Chris Mason39279cc2007-06-12 06:35:45 -04001882struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1883 struct btrfs_root *root)
1884{
1885 struct inode *inode;
1886 struct btrfs_iget_args args;
1887 args.ino = objectid;
1888 args.root = root;
1889
1890 inode = iget5_locked(s, objectid, btrfs_find_actor,
1891 btrfs_init_locked_inode,
1892 (void *)&args);
1893 return inode;
1894}
1895
1896static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1897 struct nameidata *nd)
1898{
1899 struct inode * inode;
1900 struct btrfs_inode *bi = BTRFS_I(dir);
1901 struct btrfs_root *root = bi->root;
1902 struct btrfs_root *sub_root = root;
1903 struct btrfs_key location;
Josef Bacik7b128762008-07-24 12:17:14 -04001904 int ret, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001905
1906 if (dentry->d_name.len > BTRFS_NAME_LEN)
1907 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001908
Chris Mason39279cc2007-06-12 06:35:45 -04001909 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001910
Chris Mason39279cc2007-06-12 06:35:45 -04001911 if (ret < 0)
1912 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001913
Chris Mason39279cc2007-06-12 06:35:45 -04001914 inode = NULL;
1915 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001916 ret = fixup_tree_root_location(root, &location, &sub_root,
1917 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001918 if (ret < 0)
1919 return ERR_PTR(ret);
1920 if (ret > 0)
1921 return ERR_PTR(-ENOENT);
Josef Bacik7b128762008-07-24 12:17:14 -04001922
Chris Mason39279cc2007-06-12 06:35:45 -04001923 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1924 sub_root);
1925 if (!inode)
1926 return ERR_PTR(-EACCES);
1927 if (inode->i_state & I_NEW) {
1928 /* the inode and parent dir are two different roots */
1929 if (sub_root != root) {
1930 igrab(inode);
1931 sub_root->inode = inode;
Josef Bacik7b128762008-07-24 12:17:14 -04001932 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001933 }
1934 BTRFS_I(inode)->root = sub_root;
1935 memcpy(&BTRFS_I(inode)->location, &location,
1936 sizeof(location));
1937 btrfs_read_locked_inode(inode);
1938 unlock_new_inode(inode);
1939 }
1940 }
Josef Bacik7b128762008-07-24 12:17:14 -04001941
1942 if (unlikely(do_orphan))
1943 btrfs_orphan_cleanup(sub_root);
1944
Chris Mason39279cc2007-06-12 06:35:45 -04001945 return d_splice_alias(inode, dentry);
1946}
1947
Chris Mason39279cc2007-06-12 06:35:45 -04001948static unsigned char btrfs_filetype_table[] = {
1949 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1950};
1951
1952static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1953{
Chris Mason6da6aba2007-12-18 16:15:09 -05001954 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001955 struct btrfs_root *root = BTRFS_I(inode)->root;
1956 struct btrfs_item *item;
1957 struct btrfs_dir_item *di;
1958 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001959 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001960 struct btrfs_path *path;
1961 int ret;
1962 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001963 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001964 int slot;
1965 int advance;
1966 unsigned char d_type;
1967 int over = 0;
1968 u32 di_cur;
1969 u32 di_total;
1970 u32 di_len;
1971 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001972 char tmp_name[32];
1973 char *name_ptr;
1974 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001975
1976 /* FIXME, use a real flag for deciding about the key type */
1977 if (root->fs_info->tree_root == root)
1978 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001979
Chris Mason39544012007-12-12 14:38:19 -05001980 /* special case for "." */
1981 if (filp->f_pos == 0) {
1982 over = filldir(dirent, ".", 1,
1983 1, inode->i_ino,
1984 DT_DIR);
1985 if (over)
1986 return 0;
1987 filp->f_pos = 1;
1988 }
1989
Chris Mason39279cc2007-06-12 06:35:45 -04001990 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001991 path = btrfs_alloc_path();
1992 path->reada = 2;
1993
1994 /* special case for .., just use the back ref */
1995 if (filp->f_pos == 1) {
1996 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001997 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001998 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan445dceb2008-07-24 12:19:32 -04001999 if (ret < 0 || path->slots[0] == 0) {
Chris Mason39544012007-12-12 14:38:19 -05002000 btrfs_release_path(root, path);
2001 goto read_dir_items;
2002 }
Yan445dceb2008-07-24 12:19:32 -04002003 BUG_ON(ret == 0);
2004 leaf = path->nodes[0];
2005 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05002006 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2007 btrfs_release_path(root, path);
2008 if (found_key.objectid != key.objectid ||
2009 found_key.type != BTRFS_INODE_REF_KEY)
2010 goto read_dir_items;
2011 over = filldir(dirent, "..", 2,
2012 2, found_key.offset, DT_DIR);
2013 if (over)
2014 goto nopos;
2015 filp->f_pos = 2;
2016 }
2017
2018read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04002019 btrfs_set_key_type(&key, key_type);
2020 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04002021
Chris Mason39279cc2007-06-12 06:35:45 -04002022 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2023 if (ret < 0)
2024 goto err;
2025 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002026 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002027 leaf = path->nodes[0];
2028 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002029 slot = path->slots[0];
2030 if (advance || slot >= nritems) {
2031 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002032 ret = btrfs_next_leaf(root, path);
2033 if (ret)
2034 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002035 leaf = path->nodes[0];
2036 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002037 slot = path->slots[0];
2038 } else {
2039 slot++;
2040 path->slots[0]++;
2041 }
2042 }
2043 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002044 item = btrfs_item_nr(leaf, slot);
2045 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2046
2047 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04002048 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002049 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002050 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002051 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04002052 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002053
2054 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002055 advance = 1;
2056 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2057 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002058 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04002059 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04002060 struct btrfs_key location;
2061
2062 name_len = btrfs_dir_name_len(leaf, di);
2063 if (name_len < 32) {
2064 name_ptr = tmp_name;
2065 } else {
2066 name_ptr = kmalloc(name_len, GFP_NOFS);
2067 BUG_ON(!name_ptr);
2068 }
2069 read_extent_buffer(leaf, name_ptr,
2070 (unsigned long)(di + 1), name_len);
2071
2072 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2073 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002074 over = filldir(dirent, name_ptr, name_len,
2075 found_key.offset,
2076 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002077 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002078
2079 if (name_ptr != tmp_name)
2080 kfree(name_ptr);
2081
Chris Mason39279cc2007-06-12 06:35:45 -04002082 if (over)
2083 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05002084 di_len = btrfs_dir_name_len(leaf, di) +
2085 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002086 di_cur += di_len;
2087 di = (struct btrfs_dir_item *)((char *)di + di_len);
2088 }
2089 }
Yan Zheng5e591a02008-02-19 11:41:02 -05002090 if (key_type == BTRFS_DIR_INDEX_KEY)
2091 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2092 else
2093 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002094nopos:
2095 ret = 0;
2096err:
Chris Mason39279cc2007-06-12 06:35:45 -04002097 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002098 return ret;
2099}
2100
2101int btrfs_write_inode(struct inode *inode, int wait)
2102{
2103 struct btrfs_root *root = BTRFS_I(inode)->root;
2104 struct btrfs_trans_handle *trans;
2105 int ret = 0;
2106
Chris Mason4ca8b412008-08-05 13:30:48 -04002107 if (root->fs_info->closing > 1)
2108 return 0;
2109
Chris Mason39279cc2007-06-12 06:35:45 -04002110 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002111 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002112 btrfs_set_trans_block_group(trans, inode);
2113 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002114 }
2115 return ret;
2116}
2117
2118/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002119 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002120 * inode changes. But, it is most likely to find the inode in cache.
2121 * FIXME, needs more benchmarking...there are no reasons other than performance
2122 * to keep or drop this code.
2123 */
2124void btrfs_dirty_inode(struct inode *inode)
2125{
2126 struct btrfs_root *root = BTRFS_I(inode)->root;
2127 struct btrfs_trans_handle *trans;
2128
Chris Masonf9295742008-07-17 12:54:14 -04002129 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002130 btrfs_set_trans_block_group(trans, inode);
2131 btrfs_update_inode(trans, root, inode);
2132 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002133}
2134
Josef Bacikaec74772008-07-24 12:12:38 -04002135static int btrfs_set_inode_index_count(struct inode *inode)
2136{
2137 struct btrfs_root *root = BTRFS_I(inode)->root;
2138 struct btrfs_key key, found_key;
2139 struct btrfs_path *path;
2140 struct extent_buffer *leaf;
2141 int ret;
2142
2143 key.objectid = inode->i_ino;
2144 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2145 key.offset = (u64)-1;
2146
2147 path = btrfs_alloc_path();
2148 if (!path)
2149 return -ENOMEM;
2150
2151 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2152 if (ret < 0)
2153 goto out;
2154 /* FIXME: we should be able to handle this */
2155 if (ret == 0)
2156 goto out;
2157 ret = 0;
2158
2159 /*
2160 * MAGIC NUMBER EXPLANATION:
2161 * since we search a directory based on f_pos we have to start at 2
2162 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2163 * else has to start at 2
2164 */
2165 if (path->slots[0] == 0) {
2166 BTRFS_I(inode)->index_cnt = 2;
2167 goto out;
2168 }
2169
2170 path->slots[0]--;
2171
2172 leaf = path->nodes[0];
2173 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2174
2175 if (found_key.objectid != inode->i_ino ||
2176 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2177 BTRFS_I(inode)->index_cnt = 2;
2178 goto out;
2179 }
2180
2181 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2182out:
2183 btrfs_free_path(path);
2184 return ret;
2185}
2186
Chris Mason00e4e6b2008-08-05 11:18:09 -04002187static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2188 u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04002189{
2190 int ret = 0;
2191
2192 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2193 ret = btrfs_set_inode_index_count(dir);
2194 if (ret)
2195 return ret;
2196 }
2197
Chris Mason00e4e6b2008-08-05 11:18:09 -04002198 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04002199 BTRFS_I(dir)->index_cnt++;
2200
2201 return ret;
2202}
2203
Chris Mason39279cc2007-06-12 06:35:45 -04002204static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2205 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002206 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002207 const char *name, int name_len,
2208 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002209 u64 objectid,
2210 struct btrfs_block_group_cache *group,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002211 int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04002212{
2213 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002214 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002215 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002216 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002217 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002218 struct btrfs_inode_ref *ref;
2219 struct btrfs_key key[2];
2220 u32 sizes[2];
2221 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002222 int ret;
2223 int owner;
2224
Chris Mason5f39d392007-10-15 16:14:19 -04002225 path = btrfs_alloc_path();
2226 BUG_ON(!path);
2227
Chris Mason39279cc2007-06-12 06:35:45 -04002228 inode = new_inode(root->fs_info->sb);
2229 if (!inode)
2230 return ERR_PTR(-ENOMEM);
2231
Josef Bacikaec74772008-07-24 12:12:38 -04002232 if (dir) {
Chris Mason00e4e6b2008-08-05 11:18:09 -04002233 ret = btrfs_set_inode_index(dir, inode, index);
Josef Bacikaec74772008-07-24 12:12:38 -04002234 if (ret)
2235 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002236 }
2237 /*
2238 * index_cnt is ignored for everything but a dir,
2239 * btrfs_get_inode_index_count has an explanation for the magic
2240 * number
2241 */
2242 BTRFS_I(inode)->index_cnt = 2;
2243
Chris Masond1310b22008-01-24 16:13:08 -05002244 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2245 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002246 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002247 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2248 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002249 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonea8c2812008-08-04 23:17:27 -04002250 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04002251 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002252 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002253 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04002254 inode->i_mapping->writeback_index = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002255 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002256 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002257
Chris Mason39279cc2007-06-12 06:35:45 -04002258 if (mode & S_IFDIR)
2259 owner = 0;
2260 else
2261 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002262 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002263 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002264 if (!new_inode_group) {
2265 printk("find_block group failed\n");
2266 new_inode_group = group;
2267 }
2268 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002269 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002270
2271 key[0].objectid = objectid;
2272 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2273 key[0].offset = 0;
2274
2275 key[1].objectid = objectid;
2276 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2277 key[1].offset = ref_objectid;
2278
2279 sizes[0] = sizeof(struct btrfs_inode_item);
2280 sizes[1] = name_len + sizeof(*ref);
2281
2282 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2283 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002284 goto fail;
2285
Chris Mason9c583092008-01-29 15:15:18 -05002286 if (objectid > root->highest_inode)
2287 root->highest_inode = objectid;
2288
Chris Mason39279cc2007-06-12 06:35:45 -04002289 inode->i_uid = current->fsuid;
2290 inode->i_gid = current->fsgid;
2291 inode->i_mode = mode;
2292 inode->i_ino = objectid;
2293 inode->i_blocks = 0;
2294 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002295 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2296 struct btrfs_inode_item);
2297 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002298
2299 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2300 struct btrfs_inode_ref);
2301 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002302 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05002303 ptr = (unsigned long)(ref + 1);
2304 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2305
Chris Mason5f39d392007-10-15 16:14:19 -04002306 btrfs_mark_buffer_dirty(path->nodes[0]);
2307 btrfs_free_path(path);
2308
Chris Mason39279cc2007-06-12 06:35:45 -04002309 location = &BTRFS_I(inode)->location;
2310 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002311 location->offset = 0;
2312 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2313
Chris Mason39279cc2007-06-12 06:35:45 -04002314 insert_inode_hash(inode);
2315 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002316fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002317 if (dir)
2318 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002319 btrfs_free_path(path);
2320 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002321}
2322
2323static inline u8 btrfs_inode_type(struct inode *inode)
2324{
2325 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2326}
2327
2328static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002329 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002330 int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002331{
2332 int ret;
2333 struct btrfs_key key;
2334 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002335 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002336
Chris Mason39279cc2007-06-12 06:35:45 -04002337 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002338 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2339 key.offset = 0;
2340
2341 ret = btrfs_insert_dir_item(trans, root,
2342 dentry->d_name.name, dentry->d_name.len,
2343 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002344 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04002345 index);
Chris Mason39279cc2007-06-12 06:35:45 -04002346 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002347 if (add_backref) {
2348 ret = btrfs_insert_inode_ref(trans, root,
2349 dentry->d_name.name,
2350 dentry->d_name.len,
2351 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002352 parent_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002353 index);
Chris Mason9c583092008-01-29 15:15:18 -05002354 }
Chris Masondbe674a2008-07-17 12:54:05 -04002355 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2356 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002357 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002358 ret = btrfs_update_inode(trans, root,
2359 dentry->d_parent->d_inode);
2360 }
2361 return ret;
2362}
2363
2364static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002365 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002366 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002367{
Chris Mason00e4e6b2008-08-05 11:18:09 -04002368 int err = btrfs_add_link(trans, dentry, inode, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002369 if (!err) {
2370 d_instantiate(dentry, inode);
2371 return 0;
2372 }
2373 if (err > 0)
2374 err = -EEXIST;
2375 return err;
2376}
2377
Josef Bacik618e21d2007-07-11 10:18:17 -04002378static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2379 int mode, dev_t rdev)
2380{
2381 struct btrfs_trans_handle *trans;
2382 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002383 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002384 int err;
2385 int drop_inode = 0;
2386 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002387 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002388 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002389
2390 if (!new_valid_dev(rdev))
2391 return -EINVAL;
2392
Chris Mason1832a6d2007-12-21 16:27:21 -05002393 err = btrfs_check_free_space(root, 1, 0);
2394 if (err)
2395 goto fail;
2396
Josef Bacik618e21d2007-07-11 10:18:17 -04002397 trans = btrfs_start_transaction(root, 1);
2398 btrfs_set_trans_block_group(trans, dir);
2399
2400 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2401 if (err) {
2402 err = -ENOSPC;
2403 goto out_unlock;
2404 }
2405
Josef Bacikaec74772008-07-24 12:12:38 -04002406 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002407 dentry->d_name.len,
2408 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002409 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002410 err = PTR_ERR(inode);
2411 if (IS_ERR(inode))
2412 goto out_unlock;
2413
Josef Bacik33268ea2008-07-24 12:16:36 -04002414 err = btrfs_init_acl(inode, dir);
2415 if (err) {
2416 drop_inode = 1;
2417 goto out_unlock;
2418 }
2419
Josef Bacik618e21d2007-07-11 10:18:17 -04002420 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002421 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002422 if (err)
2423 drop_inode = 1;
2424 else {
2425 inode->i_op = &btrfs_special_inode_operations;
2426 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002427 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002428 }
2429 dir->i_sb->s_dirt = 1;
2430 btrfs_update_inode_block_group(trans, inode);
2431 btrfs_update_inode_block_group(trans, dir);
2432out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002433 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002434 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002435fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002436 if (drop_inode) {
2437 inode_dec_link_count(inode);
2438 iput(inode);
2439 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002440 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002441 return err;
2442}
2443
Chris Mason39279cc2007-06-12 06:35:45 -04002444static int btrfs_create(struct inode *dir, struct dentry *dentry,
2445 int mode, struct nameidata *nd)
2446{
2447 struct btrfs_trans_handle *trans;
2448 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002449 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002450 int err;
2451 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002452 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002453 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002454 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002455
Chris Mason1832a6d2007-12-21 16:27:21 -05002456 err = btrfs_check_free_space(root, 1, 0);
2457 if (err)
2458 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002459 trans = btrfs_start_transaction(root, 1);
2460 btrfs_set_trans_block_group(trans, dir);
2461
2462 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2463 if (err) {
2464 err = -ENOSPC;
2465 goto out_unlock;
2466 }
2467
Josef Bacikaec74772008-07-24 12:12:38 -04002468 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002469 dentry->d_name.len,
2470 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002471 objectid, BTRFS_I(dir)->block_group, mode,
2472 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002473 err = PTR_ERR(inode);
2474 if (IS_ERR(inode))
2475 goto out_unlock;
2476
Josef Bacik33268ea2008-07-24 12:16:36 -04002477 err = btrfs_init_acl(inode, dir);
2478 if (err) {
2479 drop_inode = 1;
2480 goto out_unlock;
2481 }
2482
Chris Mason39279cc2007-06-12 06:35:45 -04002483 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002484 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002485 if (err)
2486 drop_inode = 1;
2487 else {
2488 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002489 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002490 inode->i_fop = &btrfs_file_operations;
2491 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002492 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2493 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002494 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002495 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2496 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04002497 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04002498 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002499 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002500 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002501 BTRFS_I(inode)->disk_i_size = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04002502 inode->i_mapping->writeback_index = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002503 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002504 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002505 }
2506 dir->i_sb->s_dirt = 1;
2507 btrfs_update_inode_block_group(trans, inode);
2508 btrfs_update_inode_block_group(trans, dir);
2509out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002510 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002511 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002512fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002513 if (drop_inode) {
2514 inode_dec_link_count(inode);
2515 iput(inode);
2516 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002517 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002518 return err;
2519}
2520
2521static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2522 struct dentry *dentry)
2523{
2524 struct btrfs_trans_handle *trans;
2525 struct btrfs_root *root = BTRFS_I(dir)->root;
2526 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002527 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05002528 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002529 int err;
2530 int drop_inode = 0;
2531
2532 if (inode->i_nlink == 0)
2533 return -ENOENT;
2534
Chris Mason6da6aba2007-12-18 16:15:09 -05002535#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2536 inode->i_nlink++;
2537#else
Chris Mason39279cc2007-06-12 06:35:45 -04002538 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002539#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002540 err = btrfs_check_free_space(root, 1, 0);
2541 if (err)
2542 goto fail;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002543 err = btrfs_set_inode_index(dir, inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002544 if (err)
2545 goto fail;
2546
Chris Mason39279cc2007-06-12 06:35:45 -04002547 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002548
Chris Mason39279cc2007-06-12 06:35:45 -04002549 btrfs_set_trans_block_group(trans, dir);
2550 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002551
Chris Mason00e4e6b2008-08-05 11:18:09 -04002552 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04002553
Chris Mason39279cc2007-06-12 06:35:45 -04002554 if (err)
2555 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002556
Chris Mason39279cc2007-06-12 06:35:45 -04002557 dir->i_sb->s_dirt = 1;
2558 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002559 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002560
Chris Mason54aa1f42007-06-22 14:16:25 -04002561 if (err)
2562 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002563
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002564 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002565 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002566fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002567 if (drop_inode) {
2568 inode_dec_link_count(inode);
2569 iput(inode);
2570 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002571 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002572 return err;
2573}
2574
Chris Mason39279cc2007-06-12 06:35:45 -04002575static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2576{
Chris Masonb9d86662008-05-02 16:13:49 -04002577 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002578 struct btrfs_trans_handle *trans;
2579 struct btrfs_root *root = BTRFS_I(dir)->root;
2580 int err = 0;
2581 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002582 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002583 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002584 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002585
Chris Mason1832a6d2007-12-21 16:27:21 -05002586 err = btrfs_check_free_space(root, 1, 0);
2587 if (err)
2588 goto out_unlock;
2589
Chris Mason39279cc2007-06-12 06:35:45 -04002590 trans = btrfs_start_transaction(root, 1);
2591 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002592
Chris Mason39279cc2007-06-12 06:35:45 -04002593 if (IS_ERR(trans)) {
2594 err = PTR_ERR(trans);
2595 goto out_unlock;
2596 }
2597
2598 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2599 if (err) {
2600 err = -ENOSPC;
2601 goto out_unlock;
2602 }
2603
Josef Bacikaec74772008-07-24 12:12:38 -04002604 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002605 dentry->d_name.len,
2606 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002607 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2608 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002609 if (IS_ERR(inode)) {
2610 err = PTR_ERR(inode);
2611 goto out_fail;
2612 }
Chris Mason5f39d392007-10-15 16:14:19 -04002613
Chris Mason39279cc2007-06-12 06:35:45 -04002614 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002615
2616 err = btrfs_init_acl(inode, dir);
2617 if (err)
2618 goto out_fail;
2619
Chris Mason39279cc2007-06-12 06:35:45 -04002620 inode->i_op = &btrfs_dir_inode_operations;
2621 inode->i_fop = &btrfs_dir_file_operations;
2622 btrfs_set_trans_block_group(trans, inode);
2623
Chris Masondbe674a2008-07-17 12:54:05 -04002624 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002625 err = btrfs_update_inode(trans, root, inode);
2626 if (err)
2627 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002628
Chris Mason00e4e6b2008-08-05 11:18:09 -04002629 err = btrfs_add_link(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002630 if (err)
2631 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002632
Chris Mason39279cc2007-06-12 06:35:45 -04002633 d_instantiate(dentry, inode);
2634 drop_on_err = 0;
2635 dir->i_sb->s_dirt = 1;
2636 btrfs_update_inode_block_group(trans, inode);
2637 btrfs_update_inode_block_group(trans, dir);
2638
2639out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002640 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002641 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002642
Chris Mason39279cc2007-06-12 06:35:45 -04002643out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002644 if (drop_on_err)
2645 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002646 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002647 return err;
2648}
2649
Chris Mason3b951512008-04-17 11:29:12 -04002650static int merge_extent_mapping(struct extent_map_tree *em_tree,
2651 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002652 struct extent_map *em,
2653 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002654{
2655 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002656
Chris Masone6dcd2d2008-07-17 12:53:50 -04002657 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2658 start_diff = map_start - em->start;
2659 em->start = map_start;
2660 em->len = map_len;
2661 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2662 em->block_start += start_diff;
2663 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002664}
2665
Chris Masona52d9a82007-08-27 16:49:44 -04002666struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002667 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002668 int create)
2669{
2670 int ret;
2671 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002672 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002673 u64 extent_start = 0;
2674 u64 extent_end = 0;
2675 u64 objectid = inode->i_ino;
2676 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002677 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002678 struct btrfs_root *root = BTRFS_I(inode)->root;
2679 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002680 struct extent_buffer *leaf;
2681 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002682 struct extent_map *em = NULL;
2683 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002684 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002685 struct btrfs_trans_handle *trans = NULL;
2686
Chris Masona52d9a82007-08-27 16:49:44 -04002687again:
Chris Masond1310b22008-01-24 16:13:08 -05002688 spin_lock(&em_tree->lock);
2689 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002690 if (em)
2691 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002692 spin_unlock(&em_tree->lock);
2693
Chris Masona52d9a82007-08-27 16:49:44 -04002694 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002695 if (em->start > start || em->start + em->len <= start)
2696 free_extent_map(em);
2697 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002698 free_extent_map(em);
2699 else
2700 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002701 }
Chris Masond1310b22008-01-24 16:13:08 -05002702 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002703 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002704 err = -ENOMEM;
2705 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002706 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002707 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002708 em->start = EXTENT_MAP_HOLE;
2709 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002710
2711 if (!path) {
2712 path = btrfs_alloc_path();
2713 BUG_ON(!path);
2714 }
2715
Chris Mason179e29e2007-11-01 11:28:41 -04002716 ret = btrfs_lookup_file_extent(trans, root, path,
2717 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002718 if (ret < 0) {
2719 err = ret;
2720 goto out;
2721 }
2722
2723 if (ret != 0) {
2724 if (path->slots[0] == 0)
2725 goto not_found;
2726 path->slots[0]--;
2727 }
2728
Chris Mason5f39d392007-10-15 16:14:19 -04002729 leaf = path->nodes[0];
2730 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002731 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002732 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002733 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2734 found_type = btrfs_key_type(&found_key);
2735 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002736 found_type != BTRFS_EXTENT_DATA_KEY) {
2737 goto not_found;
2738 }
2739
Chris Mason5f39d392007-10-15 16:14:19 -04002740 found_type = btrfs_file_extent_type(leaf, item);
2741 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002742 if (found_type == BTRFS_FILE_EXTENT_REG) {
2743 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002744 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002745 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002746 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002747 em->start = start;
2748 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002749 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002750 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002751 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002752 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002753 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002754 }
2755 goto not_found_em;
2756 }
Chris Masondb945352007-10-15 16:15:53 -04002757 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2758 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002759 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002760 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002761 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002762 goto insert;
2763 }
Chris Masondb945352007-10-15 16:15:53 -04002764 bytenr += btrfs_file_extent_offset(leaf, item);
2765 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002766 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002767 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002768 goto insert;
2769 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002770 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002771 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002772 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002773 size_t size;
2774 size_t extent_offset;
2775 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002776
Chris Mason5f39d392007-10-15 16:14:19 -04002777 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2778 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002779 extent_end = (extent_start + size + root->sectorsize - 1) &
2780 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002781 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002782 em->start = start;
2783 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002784 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002785 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002786 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002787 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002788 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002789 }
2790 goto not_found_em;
2791 }
2792 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002793
2794 if (!page) {
2795 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002796 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002797 goto out;
2798 }
2799
Chris Mason70dec802008-01-29 09:59:12 -05002800 page_start = page_offset(page) + pg_offset;
2801 extent_offset = page_start - extent_start;
2802 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002803 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002804 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002805 em->len = (copy_size + root->sectorsize - 1) &
2806 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002807 map = kmap(page);
2808 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002809 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002810 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002811 copy_size);
2812 flush_dcache_page(page);
2813 } else if (create && PageUptodate(page)) {
2814 if (!trans) {
2815 kunmap(page);
2816 free_extent_map(em);
2817 em = NULL;
2818 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002819 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002820 goto again;
2821 }
Chris Mason70dec802008-01-29 09:59:12 -05002822 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002823 copy_size);
2824 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002825 }
Chris Masona52d9a82007-08-27 16:49:44 -04002826 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002827 set_extent_uptodate(io_tree, em->start,
2828 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002829 goto insert;
2830 } else {
2831 printk("unkknown found_type %d\n", found_type);
2832 WARN_ON(1);
2833 }
2834not_found:
2835 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002836 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002837not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002838 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002839insert:
2840 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002841 if (em->start > start || extent_map_end(em) <= start) {
2842 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002843 err = -EIO;
2844 goto out;
2845 }
Chris Masond1310b22008-01-24 16:13:08 -05002846
2847 err = 0;
2848 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002849 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002850 /* it is possible that someone inserted the extent into the tree
2851 * while we had the lock dropped. It is also possible that
2852 * an overlapping map exists in the tree
2853 */
Chris Masona52d9a82007-08-27 16:49:44 -04002854 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002855 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002856
2857 ret = 0;
2858
Chris Mason3b951512008-04-17 11:29:12 -04002859 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002860 if (existing && (existing->start > start ||
2861 existing->start + existing->len <= start)) {
2862 free_extent_map(existing);
2863 existing = NULL;
2864 }
Chris Mason3b951512008-04-17 11:29:12 -04002865 if (!existing) {
2866 existing = lookup_extent_mapping(em_tree, em->start,
2867 em->len);
2868 if (existing) {
2869 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002870 em, start,
2871 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002872 free_extent_map(existing);
2873 if (err) {
2874 free_extent_map(em);
2875 em = NULL;
2876 }
2877 } else {
2878 err = -EIO;
2879 printk("failing to insert %Lu %Lu\n",
2880 start, len);
2881 free_extent_map(em);
2882 em = NULL;
2883 }
2884 } else {
2885 free_extent_map(em);
2886 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002887 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002888 }
Chris Masona52d9a82007-08-27 16:49:44 -04002889 }
Chris Masond1310b22008-01-24 16:13:08 -05002890 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002891out:
Chris Masonf4219502008-07-22 11:18:09 -04002892 if (path)
2893 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002894 if (trans) {
2895 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002896 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002897 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002898 }
Chris Masona52d9a82007-08-27 16:49:44 -04002899 }
Chris Masona52d9a82007-08-27 16:49:44 -04002900 if (err) {
2901 free_extent_map(em);
2902 WARN_ON(1);
2903 return ERR_PTR(err);
2904 }
2905 return em;
2906}
2907
Chris Masone1c4b742008-04-22 13:26:46 -04002908#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002909static int btrfs_get_block(struct inode *inode, sector_t iblock,
2910 struct buffer_head *bh_result, int create)
2911{
2912 struct extent_map *em;
2913 u64 start = (u64)iblock << inode->i_blkbits;
2914 struct btrfs_multi_bio *multi = NULL;
2915 struct btrfs_root *root = BTRFS_I(inode)->root;
2916 u64 len;
2917 u64 logical;
2918 u64 map_length;
2919 int ret = 0;
2920
2921 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2922
2923 if (!em || IS_ERR(em))
2924 goto out;
2925
Chris Masone1c4b742008-04-22 13:26:46 -04002926 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002927 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002928 }
Chris Mason16432982008-04-10 10:23:21 -04002929
2930 if (em->block_start == EXTENT_MAP_INLINE) {
2931 ret = -EINVAL;
2932 goto out;
2933 }
2934
Chris Mason16432982008-04-10 10:23:21 -04002935 len = em->start + em->len - start;
2936 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2937
Chris Masone1c4b742008-04-22 13:26:46 -04002938 if (em->block_start == EXTENT_MAP_HOLE ||
2939 em->block_start == EXTENT_MAP_DELALLOC) {
2940 bh_result->b_size = len;
2941 goto out;
2942 }
2943
Chris Mason16432982008-04-10 10:23:21 -04002944 logical = start - em->start;
2945 logical = em->block_start + logical;
2946
2947 map_length = len;
2948 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2949 logical, &map_length, &multi, 0);
2950 BUG_ON(ret);
2951 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2952 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002953
Chris Mason16432982008-04-10 10:23:21 -04002954 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2955 set_buffer_mapped(bh_result);
2956 kfree(multi);
2957out:
2958 free_extent_map(em);
2959 return ret;
2960}
Chris Masone1c4b742008-04-22 13:26:46 -04002961#endif
Chris Mason16432982008-04-10 10:23:21 -04002962
2963static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2964 const struct iovec *iov, loff_t offset,
2965 unsigned long nr_segs)
2966{
Chris Masone1c4b742008-04-22 13:26:46 -04002967 return -EINVAL;
2968#if 0
Chris Mason16432982008-04-10 10:23:21 -04002969 struct file *file = iocb->ki_filp;
2970 struct inode *inode = file->f_mapping->host;
2971
2972 if (rw == WRITE)
2973 return -EINVAL;
2974
2975 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2976 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002977#endif
Chris Mason16432982008-04-10 10:23:21 -04002978}
2979
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002980static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002981{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002982 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002983}
2984
Chris Mason9ebefb182007-06-15 13:50:00 -04002985int btrfs_readpage(struct file *file, struct page *page)
2986{
Chris Masond1310b22008-01-24 16:13:08 -05002987 struct extent_io_tree *tree;
2988 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002989 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002990}
Chris Mason1832a6d2007-12-21 16:27:21 -05002991
Chris Mason39279cc2007-06-12 06:35:45 -04002992static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2993{
Chris Masond1310b22008-01-24 16:13:08 -05002994 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002995
2996
2997 if (current->flags & PF_MEMALLOC) {
2998 redirty_page_for_writepage(wbc, page);
2999 unlock_page(page);
3000 return 0;
3001 }
Chris Masond1310b22008-01-24 16:13:08 -05003002 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003003 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3004}
Chris Mason39279cc2007-06-12 06:35:45 -04003005
Chris Masonf4219502008-07-22 11:18:09 -04003006int btrfs_writepages(struct address_space *mapping,
3007 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04003008{
Chris Masond1310b22008-01-24 16:13:08 -05003009 struct extent_io_tree *tree;
3010 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04003011 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3012}
3013
Chris Mason3ab2fb52007-11-08 10:59:22 -05003014static int
3015btrfs_readpages(struct file *file, struct address_space *mapping,
3016 struct list_head *pages, unsigned nr_pages)
3017{
Chris Masond1310b22008-01-24 16:13:08 -05003018 struct extent_io_tree *tree;
3019 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05003020 return extent_readpages(tree, mapping, pages, nr_pages,
3021 btrfs_get_extent);
3022}
Chris Masone6dcd2d2008-07-17 12:53:50 -04003023static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04003024{
Chris Masond1310b22008-01-24 16:13:08 -05003025 struct extent_io_tree *tree;
3026 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04003027 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003028
Chris Masond1310b22008-01-24 16:13:08 -05003029 tree = &BTRFS_I(page->mapping->host)->io_tree;
3030 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05003031 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003032 if (ret == 1) {
3033 ClearPagePrivate(page);
3034 set_page_private(page, 0);
3035 page_cache_release(page);
3036 }
3037 return ret;
3038}
Chris Mason39279cc2007-06-12 06:35:45 -04003039
Chris Masone6dcd2d2008-07-17 12:53:50 -04003040static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3041{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003042 return __btrfs_releasepage(page, gfp_flags);
3043}
3044
Chris Masona52d9a82007-08-27 16:49:44 -04003045static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3046{
Chris Masond1310b22008-01-24 16:13:08 -05003047 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003048 struct btrfs_ordered_extent *ordered;
3049 u64 page_start = page_offset(page);
3050 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003051
Chris Masone6dcd2d2008-07-17 12:53:50 -04003052 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003053 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003054 if (offset) {
3055 btrfs_releasepage(page, GFP_NOFS);
3056 return;
3057 }
3058
3059 lock_extent(tree, page_start, page_end, GFP_NOFS);
3060 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3061 page_offset(page));
3062 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003063 /*
3064 * IO on this page will never be started, so we need
3065 * to account for any ordered extents now
3066 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003067 clear_extent_bit(tree, page_start, page_end,
3068 EXTENT_DIRTY | EXTENT_DELALLOC |
3069 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003070 btrfs_finish_ordered_io(page->mapping->host,
3071 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003072 btrfs_put_ordered_extent(ordered);
3073 lock_extent(tree, page_start, page_end, GFP_NOFS);
3074 }
3075 clear_extent_bit(tree, page_start, page_end,
3076 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3077 EXTENT_ORDERED,
3078 1, 1, GFP_NOFS);
3079 __btrfs_releasepage(page, GFP_NOFS);
3080
Chris Mason4a096752008-07-21 10:29:44 -04003081 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003082 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003083 ClearPagePrivate(page);
3084 set_page_private(page, 0);
3085 page_cache_release(page);
3086 }
Chris Mason39279cc2007-06-12 06:35:45 -04003087}
3088
Chris Mason9ebefb182007-06-15 13:50:00 -04003089/*
3090 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3091 * called from a page fault handler when a page is first dirtied. Hence we must
3092 * be careful to check for EOF conditions here. We set the page up correctly
3093 * for a written page which means we get ENOSPC checking when writing into
3094 * holes and correct delalloc and unwritten extent mapping on filesystems that
3095 * support these features.
3096 *
3097 * We are not allowed to take the i_mutex here so we have to play games to
3098 * protect against truncate races as the page could now be beyond EOF. Because
3099 * vmtruncate() writes the inode size before removing pages, once we have the
3100 * page lock we can determine safely if the page is beyond EOF. If it is not
3101 * beyond EOF, then the page is guaranteed safe against truncation until we
3102 * unlock the page.
3103 */
3104int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3105{
Chris Mason6da6aba2007-12-18 16:15:09 -05003106 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003107 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003108 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3109 struct btrfs_ordered_extent *ordered;
3110 char *kaddr;
3111 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003112 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003113 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003114 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003115 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003116
Chris Mason1832a6d2007-12-21 16:27:21 -05003117 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003118 if (ret)
3119 goto out;
3120
3121 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003122again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003123 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003124 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003125 page_start = page_offset(page);
3126 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003127
Chris Mason9ebefb182007-06-15 13:50:00 -04003128 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003129 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003130 /* page got truncated out from underneath us */
3131 goto out_unlock;
3132 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003133 wait_on_page_writeback(page);
3134
3135 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3136 set_page_extent_mapped(page);
3137
Chris Masoneb84ae02008-07-17 13:53:27 -04003138 /*
3139 * we can't set the delalloc bits if there are pending ordered
3140 * extents. Drop our locks and wait for them to finish
3141 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003142 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3143 if (ordered) {
3144 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3145 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003146 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003147 btrfs_put_ordered_extent(ordered);
3148 goto again;
3149 }
3150
Chris Masonea8c2812008-08-04 23:17:27 -04003151 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003152 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003153
3154 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003155 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003156 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003157 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003158 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003159
Chris Masone6dcd2d2008-07-17 12:53:50 -04003160 if (zero_start != PAGE_CACHE_SIZE) {
3161 kaddr = kmap(page);
3162 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3163 flush_dcache_page(page);
3164 kunmap(page);
3165 }
Chris Mason247e7432008-07-17 12:53:51 -04003166 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003167 set_page_dirty(page);
3168 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003169
3170out_unlock:
3171 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003172out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003173 return ret;
3174}
3175
Chris Mason39279cc2007-06-12 06:35:45 -04003176static void btrfs_truncate(struct inode *inode)
3177{
3178 struct btrfs_root *root = BTRFS_I(inode)->root;
3179 int ret;
3180 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003181 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003182 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003183
3184 if (!S_ISREG(inode->i_mode))
3185 return;
3186 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3187 return;
3188
3189 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003190 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003191
Chris Mason39279cc2007-06-12 06:35:45 -04003192 trans = btrfs_start_transaction(root, 1);
3193 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003194 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003195
Josef Bacik7b128762008-07-24 12:17:14 -04003196 ret = btrfs_orphan_add(trans, inode);
3197 if (ret)
3198 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003199 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05003200 ret = btrfs_truncate_in_trans(trans, root, inode,
3201 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003202 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003203
Josef Bacik7b128762008-07-24 12:17:14 -04003204 ret = btrfs_orphan_del(trans, inode);
3205 BUG_ON(ret);
3206
3207out:
3208 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003209 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003210 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003211 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003212}
3213
Sven Wegener3b963622008-06-09 21:57:42 -04003214/*
3215 * Invalidate a single dcache entry at the root of the filesystem.
3216 * Needed after creation of snapshot or subvolume.
3217 */
3218void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3219 int namelen)
3220{
3221 struct dentry *alias, *entry;
3222 struct qstr qstr;
3223
3224 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3225 if (alias) {
3226 qstr.name = name;
3227 qstr.len = namelen;
3228 /* change me if btrfs ever gets a d_hash operation */
3229 qstr.hash = full_name_hash(qstr.name, qstr.len);
3230 entry = d_lookup(alias, &qstr);
3231 dput(alias);
3232 if (entry) {
3233 d_invalidate(entry);
3234 dput(entry);
3235 }
3236 }
3237}
3238
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003239int btrfs_create_subvol_root(struct btrfs_root *new_root,
3240 struct btrfs_trans_handle *trans, u64 new_dirid,
3241 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003242{
Chris Mason39279cc2007-06-12 06:35:45 -04003243 struct inode *inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003244 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003245
Josef Bacikaec74772008-07-24 12:12:38 -04003246 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003247 new_dirid, block_group, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04003248 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003249 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003250 inode->i_op = &btrfs_dir_inode_operations;
3251 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003252 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003253
Chris Mason39279cc2007-06-12 06:35:45 -04003254 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003255 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003256
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003257 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003258}
3259
Chris Masonedbd8d42007-12-21 16:27:24 -05003260unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003261 struct file_ra_state *ra, struct file *file,
3262 pgoff_t offset, pgoff_t last_index)
3263{
Chris Mason8e7bf942008-04-28 09:02:36 -04003264 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003265
3266#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003267 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3268 return offset;
3269#else
Chris Mason86479a02007-09-10 19:58:16 -04003270 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3271 return offset + req_size;
3272#endif
3273}
3274
Chris Mason39279cc2007-06-12 06:35:45 -04003275struct inode *btrfs_alloc_inode(struct super_block *sb)
3276{
3277 struct btrfs_inode *ei;
3278
3279 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3280 if (!ei)
3281 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003282 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003283 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003284 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3285 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003286 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003287 return &ei->vfs_inode;
3288}
3289
3290void btrfs_destroy_inode(struct inode *inode)
3291{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003292 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003293 WARN_ON(!list_empty(&inode->i_dentry));
3294 WARN_ON(inode->i_data.nrpages);
3295
Josef Bacik33268ea2008-07-24 12:16:36 -04003296 if (BTRFS_I(inode)->i_acl &&
3297 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3298 posix_acl_release(BTRFS_I(inode)->i_acl);
3299 if (BTRFS_I(inode)->i_default_acl &&
3300 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3301 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3302
Yanbcc63ab2008-07-30 16:29:20 -04003303 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003304 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3305 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3306 " list\n", inode->i_ino);
3307 dump_stack();
3308 }
Yanbcc63ab2008-07-30 16:29:20 -04003309 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003310
Chris Masone6dcd2d2008-07-17 12:53:50 -04003311 while(1) {
3312 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3313 if (!ordered)
3314 break;
3315 else {
3316 printk("found ordered extent %Lu %Lu\n",
3317 ordered->file_offset, ordered->len);
3318 btrfs_remove_ordered_extent(inode, ordered);
3319 btrfs_put_ordered_extent(ordered);
3320 btrfs_put_ordered_extent(ordered);
3321 }
3322 }
Chris Mason8c416c92008-01-14 15:10:26 -05003323 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003324 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3325}
3326
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003327#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3328static void init_once(void *foo)
3329#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003330static void init_once(struct kmem_cache * cachep, void *foo)
3331#else
Chris Mason39279cc2007-06-12 06:35:45 -04003332static void init_once(void * foo, struct kmem_cache * cachep,
3333 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003334#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003335{
3336 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3337
3338 inode_init_once(&ei->vfs_inode);
3339}
3340
3341void btrfs_destroy_cachep(void)
3342{
3343 if (btrfs_inode_cachep)
3344 kmem_cache_destroy(btrfs_inode_cachep);
3345 if (btrfs_trans_handle_cachep)
3346 kmem_cache_destroy(btrfs_trans_handle_cachep);
3347 if (btrfs_transaction_cachep)
3348 kmem_cache_destroy(btrfs_transaction_cachep);
3349 if (btrfs_bit_radix_cachep)
3350 kmem_cache_destroy(btrfs_bit_radix_cachep);
3351 if (btrfs_path_cachep)
3352 kmem_cache_destroy(btrfs_path_cachep);
3353}
3354
Chris Mason86479a02007-09-10 19:58:16 -04003355struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003356 unsigned long extra_flags,
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003357#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3358 void (*ctor)(void *)
3359#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003360 void (*ctor)(struct kmem_cache *, void *)
3361#else
Chris Mason92fee662007-07-25 12:31:35 -04003362 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003363 unsigned long)
3364#endif
3365 )
Chris Mason92fee662007-07-25 12:31:35 -04003366{
3367 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3368 SLAB_MEM_SPREAD | extra_flags), ctor
3369#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3370 ,NULL
3371#endif
3372 );
3373}
3374
Chris Mason39279cc2007-06-12 06:35:45 -04003375int btrfs_init_cachep(void)
3376{
Chris Mason86479a02007-09-10 19:58:16 -04003377 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003378 sizeof(struct btrfs_inode),
3379 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003380 if (!btrfs_inode_cachep)
3381 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003382 btrfs_trans_handle_cachep =
3383 btrfs_cache_create("btrfs_trans_handle_cache",
3384 sizeof(struct btrfs_trans_handle),
3385 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003386 if (!btrfs_trans_handle_cachep)
3387 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003388 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003389 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003390 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003391 if (!btrfs_transaction_cachep)
3392 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003393 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003394 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003395 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003396 if (!btrfs_path_cachep)
3397 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003398 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003399 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003400 if (!btrfs_bit_radix_cachep)
3401 goto fail;
3402 return 0;
3403fail:
3404 btrfs_destroy_cachep();
3405 return -ENOMEM;
3406}
3407
3408static int btrfs_getattr(struct vfsmount *mnt,
3409 struct dentry *dentry, struct kstat *stat)
3410{
3411 struct inode *inode = dentry->d_inode;
3412 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003413 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003414 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003415 return 0;
3416}
3417
3418static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3419 struct inode * new_dir,struct dentry *new_dentry)
3420{
3421 struct btrfs_trans_handle *trans;
3422 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3423 struct inode *new_inode = new_dentry->d_inode;
3424 struct inode *old_inode = old_dentry->d_inode;
3425 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003426 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003427 int ret;
3428
3429 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3430 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3431 return -ENOTEMPTY;
3432 }
Chris Mason5f39d392007-10-15 16:14:19 -04003433
Chris Mason1832a6d2007-12-21 16:27:21 -05003434 ret = btrfs_check_free_space(root, 1, 0);
3435 if (ret)
3436 goto out_unlock;
3437
Chris Mason39279cc2007-06-12 06:35:45 -04003438 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003439
Chris Mason39279cc2007-06-12 06:35:45 -04003440 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003441
3442 old_dentry->d_inode->i_nlink++;
3443 old_dir->i_ctime = old_dir->i_mtime = ctime;
3444 new_dir->i_ctime = new_dir->i_mtime = ctime;
3445 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003446
Chris Mason39279cc2007-06-12 06:35:45 -04003447 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3448 if (ret)
3449 goto out_fail;
3450
3451 if (new_inode) {
3452 new_inode->i_ctime = CURRENT_TIME;
3453 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3454 if (ret)
3455 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003456 if (new_inode->i_nlink == 0) {
3457 ret = btrfs_orphan_add(trans, new_inode);
3458 if (ret)
3459 goto out_fail;
3460 }
Chris Mason39279cc2007-06-12 06:35:45 -04003461 }
Chris Mason00e4e6b2008-08-05 11:18:09 -04003462 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003463 if (ret)
3464 goto out_fail;
3465
Chris Mason00e4e6b2008-08-05 11:18:09 -04003466 ret = btrfs_add_link(trans, new_dentry, old_inode, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003467 if (ret)
3468 goto out_fail;
3469
3470out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003471 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003472out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003473 return ret;
3474}
3475
Chris Masonea8c2812008-08-04 23:17:27 -04003476int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3477{
3478 struct list_head *head = &root->fs_info->delalloc_inodes;
3479 struct btrfs_inode *binode;
3480 unsigned long flags;
3481
3482 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3483 while(!list_empty(head)) {
3484 binode = list_entry(head->next, struct btrfs_inode,
3485 delalloc_inodes);
3486 atomic_inc(&binode->vfs_inode.i_count);
3487 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3488 filemap_write_and_wait(binode->vfs_inode.i_mapping);
3489 iput(&binode->vfs_inode);
3490 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3491 }
3492 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3493 return 0;
3494}
3495
Chris Mason39279cc2007-06-12 06:35:45 -04003496static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3497 const char *symname)
3498{
3499 struct btrfs_trans_handle *trans;
3500 struct btrfs_root *root = BTRFS_I(dir)->root;
3501 struct btrfs_path *path;
3502 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003503 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003504 int err;
3505 int drop_inode = 0;
3506 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003507 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04003508 int name_len;
3509 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003510 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003511 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003512 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003513 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003514
3515 name_len = strlen(symname) + 1;
3516 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3517 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003518
Chris Mason1832a6d2007-12-21 16:27:21 -05003519 err = btrfs_check_free_space(root, 1, 0);
3520 if (err)
3521 goto out_fail;
3522
Chris Mason39279cc2007-06-12 06:35:45 -04003523 trans = btrfs_start_transaction(root, 1);
3524 btrfs_set_trans_block_group(trans, dir);
3525
3526 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3527 if (err) {
3528 err = -ENOSPC;
3529 goto out_unlock;
3530 }
3531
Josef Bacikaec74772008-07-24 12:12:38 -04003532 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003533 dentry->d_name.len,
3534 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003535 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3536 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003537 err = PTR_ERR(inode);
3538 if (IS_ERR(inode))
3539 goto out_unlock;
3540
Josef Bacik33268ea2008-07-24 12:16:36 -04003541 err = btrfs_init_acl(inode, dir);
3542 if (err) {
3543 drop_inode = 1;
3544 goto out_unlock;
3545 }
3546
Chris Mason39279cc2007-06-12 06:35:45 -04003547 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003548 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003549 if (err)
3550 drop_inode = 1;
3551 else {
3552 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003553 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003554 inode->i_fop = &btrfs_file_operations;
3555 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003556 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3557 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003558 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003559 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3560 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003561 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04003562 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003563 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003564 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003565 BTRFS_I(inode)->disk_i_size = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04003566 inode->i_mapping->writeback_index = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003567 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003568 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003569 }
3570 dir->i_sb->s_dirt = 1;
3571 btrfs_update_inode_block_group(trans, inode);
3572 btrfs_update_inode_block_group(trans, dir);
3573 if (drop_inode)
3574 goto out_unlock;
3575
3576 path = btrfs_alloc_path();
3577 BUG_ON(!path);
3578 key.objectid = inode->i_ino;
3579 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003580 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3581 datasize = btrfs_file_extent_calc_inline_size(name_len);
3582 err = btrfs_insert_empty_item(trans, root, path, &key,
3583 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003584 if (err) {
3585 drop_inode = 1;
3586 goto out_unlock;
3587 }
Chris Mason5f39d392007-10-15 16:14:19 -04003588 leaf = path->nodes[0];
3589 ei = btrfs_item_ptr(leaf, path->slots[0],
3590 struct btrfs_file_extent_item);
3591 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3592 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003593 BTRFS_FILE_EXTENT_INLINE);
3594 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003595 write_extent_buffer(leaf, symname, ptr, name_len);
3596 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003597 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003598
Chris Mason39279cc2007-06-12 06:35:45 -04003599 inode->i_op = &btrfs_symlink_inode_operations;
3600 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003601 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003602 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003603 err = btrfs_update_inode(trans, root, inode);
3604 if (err)
3605 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003606
3607out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003608 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003609 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003610out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003611 if (drop_inode) {
3612 inode_dec_link_count(inode);
3613 iput(inode);
3614 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003615 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003616 return err;
3617}
Chris Mason16432982008-04-10 10:23:21 -04003618
Chris Masone6dcd2d2008-07-17 12:53:50 -04003619static int btrfs_set_page_dirty(struct page *page)
3620{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003621 return __set_page_dirty_nobuffers(page);
3622}
3623
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003624#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3625static int btrfs_permission(struct inode *inode, int mask)
3626#else
Yanfdebe2b2008-01-14 13:26:08 -05003627static int btrfs_permission(struct inode *inode, int mask,
3628 struct nameidata *nd)
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003629#endif
Yanfdebe2b2008-01-14 13:26:08 -05003630{
3631 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3632 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003633 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003634}
Chris Mason39279cc2007-06-12 06:35:45 -04003635
3636static struct inode_operations btrfs_dir_inode_operations = {
3637 .lookup = btrfs_lookup,
3638 .create = btrfs_create,
3639 .unlink = btrfs_unlink,
3640 .link = btrfs_link,
3641 .mkdir = btrfs_mkdir,
3642 .rmdir = btrfs_rmdir,
3643 .rename = btrfs_rename,
3644 .symlink = btrfs_symlink,
3645 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003646 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003647 .setxattr = generic_setxattr,
3648 .getxattr = generic_getxattr,
3649 .listxattr = btrfs_listxattr,
3650 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003651 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003652};
Chris Mason39279cc2007-06-12 06:35:45 -04003653static struct inode_operations btrfs_dir_ro_inode_operations = {
3654 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003655 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003656};
Chris Mason39279cc2007-06-12 06:35:45 -04003657static struct file_operations btrfs_dir_file_operations = {
3658 .llseek = generic_file_llseek,
3659 .read = generic_read_dir,
3660 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003661 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003662#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003663 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003664#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003665 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003666};
3667
Chris Masond1310b22008-01-24 16:13:08 -05003668static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003669 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003670 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003671 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason3de9d6b2008-08-04 23:17:27 -04003672 .readpage_io_hook = btrfs_readpage_io_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003673 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003674 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003675 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003676 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003677 .set_bit_hook = btrfs_set_bit_hook,
3678 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003679};
3680
Chris Mason39279cc2007-06-12 06:35:45 -04003681static struct address_space_operations btrfs_aops = {
3682 .readpage = btrfs_readpage,
3683 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003684 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003685 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003686 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003687 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003688 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003689 .invalidatepage = btrfs_invalidatepage,
3690 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003691 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003692};
3693
3694static struct address_space_operations btrfs_symlink_aops = {
3695 .readpage = btrfs_readpage,
3696 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003697 .invalidatepage = btrfs_invalidatepage,
3698 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003699};
3700
3701static struct inode_operations btrfs_file_inode_operations = {
3702 .truncate = btrfs_truncate,
3703 .getattr = btrfs_getattr,
3704 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003705 .setxattr = generic_setxattr,
3706 .getxattr = generic_getxattr,
3707 .listxattr = btrfs_listxattr,
3708 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003709 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003710};
Josef Bacik618e21d2007-07-11 10:18:17 -04003711static struct inode_operations btrfs_special_inode_operations = {
3712 .getattr = btrfs_getattr,
3713 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003714 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -04003715 .setxattr = generic_setxattr,
3716 .getxattr = generic_getxattr,
3717 .listxattr = btrfs_listxattr,
3718 .removexattr = generic_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003719};
Chris Mason39279cc2007-06-12 06:35:45 -04003720static struct inode_operations btrfs_symlink_inode_operations = {
3721 .readlink = generic_readlink,
3722 .follow_link = page_follow_link_light,
3723 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003724 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003725};