blob: 33b990878d84f40a2095786c4b9b04f620431867 [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);
418 mutex_lock(&BTRFS_I(inode)->csum_mutex);
419 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
420 inode, sum);
421 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400422 }
423 return 0;
424}
425
Chris Masonea8c2812008-08-04 23:17:27 -0400426int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
427{
428 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
429 GFP_NOFS);
430}
431
Chris Mason247e7432008-07-17 12:53:51 -0400432struct btrfs_writepage_fixup {
433 struct page *page;
434 struct btrfs_work work;
435};
436
437/* see btrfs_writepage_start_hook for details on why this is required */
438void btrfs_writepage_fixup_worker(struct btrfs_work *work)
439{
440 struct btrfs_writepage_fixup *fixup;
441 struct btrfs_ordered_extent *ordered;
442 struct page *page;
443 struct inode *inode;
444 u64 page_start;
445 u64 page_end;
446
447 fixup = container_of(work, struct btrfs_writepage_fixup, work);
448 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400449again:
Chris Mason247e7432008-07-17 12:53:51 -0400450 lock_page(page);
451 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
452 ClearPageChecked(page);
453 goto out_page;
454 }
455
456 inode = page->mapping->host;
457 page_start = page_offset(page);
458 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
459
460 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400461
462 /* already ordered? We're done */
463 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
464 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400465 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400466 }
467
468 ordered = btrfs_lookup_ordered_extent(inode, page_start);
469 if (ordered) {
470 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
471 page_end, GFP_NOFS);
472 unlock_page(page);
473 btrfs_start_ordered_extent(inode, ordered, 1);
474 goto again;
475 }
Chris Mason247e7432008-07-17 12:53:51 -0400476
Chris Masonea8c2812008-08-04 23:17:27 -0400477 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -0400478 ClearPageChecked(page);
479out:
480 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
481out_page:
482 unlock_page(page);
483 page_cache_release(page);
484}
485
486/*
487 * There are a few paths in the higher layers of the kernel that directly
488 * set the page dirty bit without asking the filesystem if it is a
489 * good idea. This causes problems because we want to make sure COW
490 * properly happens and the data=ordered rules are followed.
491 *
492 * In our case any range that doesn't have the EXTENT_ORDERED bit set
493 * hasn't been properly setup for IO. We kick off an async process
494 * to fix it up. The async helper will wait for ordered extents, set
495 * the delalloc bit and make it safe to write the page.
496 */
497int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
498{
499 struct inode *inode = page->mapping->host;
500 struct btrfs_writepage_fixup *fixup;
501 struct btrfs_root *root = BTRFS_I(inode)->root;
502 int ret;
503
504 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
505 EXTENT_ORDERED, 0);
506 if (ret)
507 return 0;
508
509 if (PageChecked(page))
510 return -EAGAIN;
511
512 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
513 if (!fixup)
514 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400515
Chris Mason247e7432008-07-17 12:53:51 -0400516 SetPageChecked(page);
517 page_cache_get(page);
518 fixup->work.func = btrfs_writepage_fixup_worker;
519 fixup->page = page;
520 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
521 return -EAGAIN;
522}
523
Chris Mason211f90e2008-07-18 11:56:15 -0400524static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400525{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400526 struct btrfs_root *root = BTRFS_I(inode)->root;
527 struct btrfs_trans_handle *trans;
528 struct btrfs_ordered_extent *ordered_extent;
529 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
530 u64 alloc_hint = 0;
531 struct list_head list;
532 struct btrfs_key ins;
533 int ret;
534
535 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400536 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400537 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400538
Chris Masonf9295742008-07-17 12:54:14 -0400539 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400540
541 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
542 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400543 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
544 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400545
546 lock_extent(io_tree, ordered_extent->file_offset,
547 ordered_extent->file_offset + ordered_extent->len - 1,
548 GFP_NOFS);
549
550 INIT_LIST_HEAD(&list);
551
552 ins.objectid = ordered_extent->start;
553 ins.offset = ordered_extent->len;
554 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400555
Chris Masone6dcd2d2008-07-17 12:53:50 -0400556 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
557 trans->transid, inode->i_ino,
558 ordered_extent->file_offset, &ins);
559 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400560
561 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400562
Chris Masone6dcd2d2008-07-17 12:53:50 -0400563 ret = btrfs_drop_extents(trans, root, inode,
564 ordered_extent->file_offset,
565 ordered_extent->file_offset +
566 ordered_extent->len,
567 ordered_extent->file_offset, &alloc_hint);
568 BUG_ON(ret);
569 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
570 ordered_extent->file_offset,
571 ordered_extent->start,
572 ordered_extent->len,
573 ordered_extent->len, 0);
574 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400575
Chris Masone6dcd2d2008-07-17 12:53:50 -0400576 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
577 ordered_extent->file_offset +
578 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400579 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
580
Chris Masone6dcd2d2008-07-17 12:53:50 -0400581 inode->i_blocks += ordered_extent->len >> 9;
582 unlock_extent(io_tree, ordered_extent->file_offset,
583 ordered_extent->file_offset + ordered_extent->len - 1,
584 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400585nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -0400586 add_pending_csums(trans, inode, ordered_extent->file_offset,
587 &ordered_extent->list);
588
Chris Masondbe674a2008-07-17 12:54:05 -0400589 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400590 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400591
Chris Masone6dcd2d2008-07-17 12:53:50 -0400592 /* once for us */
593 btrfs_put_ordered_extent(ordered_extent);
594 /* once for the tree */
595 btrfs_put_ordered_extent(ordered_extent);
596
597 btrfs_update_inode(trans, root, inode);
598 btrfs_end_transaction(trans, root);
599 return 0;
600}
601
Chris Mason211f90e2008-07-18 11:56:15 -0400602int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
603 struct extent_state *state, int uptodate)
604{
605 return btrfs_finish_ordered_io(page->mapping->host, start, end);
606}
607
Chris Mason3de9d6b2008-08-04 23:17:27 -0400608int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
609{
610 int ret = 0;
611 struct inode *inode = page->mapping->host;
612 struct btrfs_root *root = BTRFS_I(inode)->root;
613 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
614 struct btrfs_csum_item *item;
615 struct btrfs_path *path = NULL;
616 u32 csum;
617
618 if (btrfs_test_opt(root, NODATASUM) ||
619 btrfs_test_flag(inode, NODATASUM))
620 return 0;
621
622 /*
623 * It is possible there is an ordered extent that has
624 * not yet finished for this range in the file. If so,
625 * that extent will have a csum cached, and it will insert
626 * the sum after all the blocks in the extent are fully
627 * on disk. So, look for an ordered extent and use the
628 * sum if found. We have to do this before looking in the
629 * btree because csum items are pre-inserted based on
630 * the file size. btrfs_lookup_csum might find an item
631 * that still hasn't been fully filled.
632 */
633 ret = btrfs_find_ordered_sum(inode, start, &csum);
634 if (ret == 0)
635 goto found;
636
637 ret = 0;
638 path = btrfs_alloc_path();
639 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
640 if (IS_ERR(item)) {
641 ret = PTR_ERR(item);
642 /* a csum that isn't present is a preallocated region. */
643 if (ret == -ENOENT || ret == -EFBIG)
644 ret = 0;
645 csum = 0;
646 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
647 start);
648 goto out;
649 }
650 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
651 BTRFS_CRC32_SIZE);
652found:
653 set_state_private(io_tree, start, csum);
654out:
655 if (path)
656 btrfs_free_path(path);
657 return ret;
658}
659
Chris Mason7e383262008-04-09 16:28:12 -0400660struct io_failure_record {
661 struct page *page;
662 u64 start;
663 u64 len;
664 u64 logical;
665 int last_mirror;
666};
667
Chris Mason1259ab72008-05-12 13:39:03 -0400668int btrfs_io_failed_hook(struct bio *failed_bio,
669 struct page *page, u64 start, u64 end,
670 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400671{
672 struct io_failure_record *failrec = NULL;
673 u64 private;
674 struct extent_map *em;
675 struct inode *inode = page->mapping->host;
676 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400677 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400678 struct bio *bio;
679 int num_copies;
680 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400681 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400682 u64 logical;
683
684 ret = get_state_private(failure_tree, start, &private);
685 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400686 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
687 if (!failrec)
688 return -ENOMEM;
689 failrec->start = start;
690 failrec->len = end - start + 1;
691 failrec->last_mirror = 0;
692
Chris Mason3b951512008-04-17 11:29:12 -0400693 spin_lock(&em_tree->lock);
694 em = lookup_extent_mapping(em_tree, start, failrec->len);
695 if (em->start > start || em->start + em->len < start) {
696 free_extent_map(em);
697 em = NULL;
698 }
699 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400700
701 if (!em || IS_ERR(em)) {
702 kfree(failrec);
703 return -EIO;
704 }
705 logical = start - em->start;
706 logical = em->block_start + logical;
707 failrec->logical = logical;
708 free_extent_map(em);
709 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
710 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400711 set_state_private(failure_tree, start,
712 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400713 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400714 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400715 }
716 num_copies = btrfs_num_copies(
717 &BTRFS_I(inode)->root->fs_info->mapping_tree,
718 failrec->logical, failrec->len);
719 failrec->last_mirror++;
720 if (!state) {
721 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
722 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
723 failrec->start,
724 EXTENT_LOCKED);
725 if (state && state->start != failrec->start)
726 state = NULL;
727 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
728 }
729 if (!state || failrec->last_mirror > num_copies) {
730 set_state_private(failure_tree, failrec->start, 0);
731 clear_extent_bits(failure_tree, failrec->start,
732 failrec->start + failrec->len - 1,
733 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
734 kfree(failrec);
735 return -EIO;
736 }
737 bio = bio_alloc(GFP_NOFS, 1);
738 bio->bi_private = state;
739 bio->bi_end_io = failed_bio->bi_end_io;
740 bio->bi_sector = failrec->logical >> 9;
741 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400742 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400743 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400744 if (failed_bio->bi_rw & (1 << BIO_RW))
745 rw = WRITE;
746 else
747 rw = READ;
748
749 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
750 failrec->last_mirror);
751 return 0;
752}
753
754int btrfs_clean_io_failures(struct inode *inode, u64 start)
755{
756 u64 private;
757 u64 private_failure;
758 struct io_failure_record *failure;
759 int ret;
760
761 private = 0;
762 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
763 (u64)-1, 1, EXTENT_DIRTY)) {
764 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
765 start, &private_failure);
766 if (ret == 0) {
767 failure = (struct io_failure_record *)(unsigned long)
768 private_failure;
769 set_state_private(&BTRFS_I(inode)->io_failure_tree,
770 failure->start, 0);
771 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
772 failure->start,
773 failure->start + failure->len - 1,
774 EXTENT_DIRTY | EXTENT_LOCKED,
775 GFP_NOFS);
776 kfree(failure);
777 }
778 }
Chris Mason7e383262008-04-09 16:28:12 -0400779 return 0;
780}
781
Chris Mason70dec802008-01-29 09:59:12 -0500782int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
783 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400784{
Chris Mason35ebb932007-10-30 16:56:53 -0400785 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400786 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500787 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400788 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500789 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400790 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400791 struct btrfs_root *root = BTRFS_I(inode)->root;
792 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400793 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500794
Yanb98b6762008-01-08 15:54:37 -0500795 if (btrfs_test_opt(root, NODATASUM) ||
796 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500797 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500798 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500799 private = state->private;
800 ret = 0;
801 } else {
802 ret = get_state_private(io_tree, start, &private);
803 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400804 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400805 kaddr = kmap_atomic(page, KM_IRQ0);
806 if (ret) {
807 goto zeroit;
808 }
Chris Masonff79f812007-10-15 16:22:25 -0400809 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
810 btrfs_csum_final(csum, (char *)&csum);
811 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400812 goto zeroit;
813 }
814 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400815 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400816
817 /* if the io failure tree for this inode is non-empty,
818 * check to see if we've recovered from a failed IO
819 */
Chris Mason1259ab72008-05-12 13:39:03 -0400820 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400821 return 0;
822
823zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500824 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
825 page->mapping->host->i_ino, (unsigned long long)start, csum,
826 private);
Chris Masondb945352007-10-15 16:15:53 -0400827 memset(kaddr + offset, 1, end - start + 1);
828 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400829 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400830 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400831 if (private == 0)
832 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400833 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400834}
Chris Masonb888db22007-08-27 16:49:44 -0400835
Josef Bacik7b128762008-07-24 12:17:14 -0400836/*
837 * This creates an orphan entry for the given inode in case something goes
838 * wrong in the middle of an unlink/truncate.
839 */
840int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
841{
842 struct btrfs_root *root = BTRFS_I(inode)->root;
843 int ret = 0;
844
Yanbcc63ab2008-07-30 16:29:20 -0400845 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400846
847 /* already on the orphan list, we're good */
848 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400849 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400850 return 0;
851 }
852
853 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
854
Yanbcc63ab2008-07-30 16:29:20 -0400855 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400856
857 /*
858 * insert an orphan item to track this unlinked/truncated file
859 */
860 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
861
862 return ret;
863}
864
865/*
866 * We have done the truncate/delete so we can go ahead and remove the orphan
867 * item for this particular inode.
868 */
869int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
870{
871 struct btrfs_root *root = BTRFS_I(inode)->root;
872 int ret = 0;
873
Yanbcc63ab2008-07-30 16:29:20 -0400874 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400875
876 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400877 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400878 return 0;
879 }
880
881 list_del_init(&BTRFS_I(inode)->i_orphan);
882 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400883 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400884 return 0;
885 }
886
Yanbcc63ab2008-07-30 16:29:20 -0400887 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400888
889 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
890
891 return ret;
892}
893
894/*
895 * this cleans up any orphans that may be left on the list from the last use
896 * of this root.
897 */
898void btrfs_orphan_cleanup(struct btrfs_root *root)
899{
900 struct btrfs_path *path;
901 struct extent_buffer *leaf;
902 struct btrfs_item *item;
903 struct btrfs_key key, found_key;
904 struct btrfs_trans_handle *trans;
905 struct inode *inode;
906 int ret = 0, nr_unlink = 0, nr_truncate = 0;
907
908 /* don't do orphan cleanup if the fs is readonly. */
909 if (root->inode->i_sb->s_flags & MS_RDONLY)
910 return;
911
912 path = btrfs_alloc_path();
913 if (!path)
914 return;
915 path->reada = -1;
916
917 key.objectid = BTRFS_ORPHAN_OBJECTID;
918 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
919 key.offset = (u64)-1;
920
921 trans = btrfs_start_transaction(root, 1);
922 btrfs_set_trans_block_group(trans, root->inode);
923
924 while (1) {
925 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
926 if (ret < 0) {
927 printk(KERN_ERR "Error searching slot for orphan: %d"
928 "\n", ret);
929 break;
930 }
931
932 /*
933 * if ret == 0 means we found what we were searching for, which
934 * is weird, but possible, so only screw with path if we didnt
935 * find the key and see if we have stuff that matches
936 */
937 if (ret > 0) {
938 if (path->slots[0] == 0)
939 break;
940 path->slots[0]--;
941 }
942
943 /* pull out the item */
944 leaf = path->nodes[0];
945 item = btrfs_item_nr(leaf, path->slots[0]);
946 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
947
948 /* make sure the item matches what we want */
949 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
950 break;
951 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
952 break;
953
954 /* release the path since we're done with it */
955 btrfs_release_path(root, path);
956
957 /*
958 * this is where we are basically btrfs_lookup, without the
959 * crossing root thing. we store the inode number in the
960 * offset of the orphan item.
961 */
962 inode = btrfs_iget_locked(root->inode->i_sb,
963 found_key.offset, root);
964 if (!inode)
965 break;
966
967 if (inode->i_state & I_NEW) {
968 BTRFS_I(inode)->root = root;
969
970 /* have to set the location manually */
971 BTRFS_I(inode)->location.objectid = inode->i_ino;
972 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
973 BTRFS_I(inode)->location.offset = 0;
974
975 btrfs_read_locked_inode(inode);
976 unlock_new_inode(inode);
977 }
978
979 /*
980 * add this inode to the orphan list so btrfs_orphan_del does
981 * the proper thing when we hit it
982 */
Yanbcc63ab2008-07-30 16:29:20 -0400983 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400984 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -0400985 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400986
987 /*
988 * if this is a bad inode, means we actually succeeded in
989 * removing the inode, but not the orphan record, which means
990 * we need to manually delete the orphan since iput will just
991 * do a destroy_inode
992 */
993 if (is_bad_inode(inode)) {
994 btrfs_orphan_del(trans, inode);
995 iput(inode);
996 continue;
997 }
998
999 /* if we have links, this was a truncate, lets do that */
1000 if (inode->i_nlink) {
1001 nr_truncate++;
1002 btrfs_truncate(inode);
1003 } else {
1004 nr_unlink++;
1005 }
1006
1007 /* this will do delete_inode and everything for us */
1008 iput(inode);
1009 }
1010
1011 if (nr_unlink)
1012 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1013 if (nr_truncate)
1014 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1015
1016 btrfs_free_path(path);
1017 btrfs_end_transaction(trans, root);
1018}
1019
Chris Mason39279cc2007-06-12 06:35:45 -04001020void btrfs_read_locked_inode(struct inode *inode)
1021{
1022 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001023 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001024 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001025 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001026 struct btrfs_root *root = BTRFS_I(inode)->root;
1027 struct btrfs_key location;
1028 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001029 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001030 int ret;
1031
1032 path = btrfs_alloc_path();
1033 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001034 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001035
Chris Mason39279cc2007-06-12 06:35:45 -04001036 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001037 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001038 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001039
Chris Mason5f39d392007-10-15 16:14:19 -04001040 leaf = path->nodes[0];
1041 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1042 struct btrfs_inode_item);
1043
1044 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1045 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1046 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1047 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001048 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001049
1050 tspec = btrfs_inode_atime(inode_item);
1051 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1052 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1053
1054 tspec = btrfs_inode_mtime(inode_item);
1055 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1056 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1057
1058 tspec = btrfs_inode_ctime(inode_item);
1059 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1060 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1061
1062 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1063 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -04001064 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001065 rdev = btrfs_inode_rdev(leaf, inode_item);
1066
Josef Bacikaec74772008-07-24 12:12:38 -04001067 BTRFS_I(inode)->index_cnt = (u64)-1;
1068
Chris Mason5f39d392007-10-15 16:14:19 -04001069 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001070 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1071 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001072 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001073 if (!BTRFS_I(inode)->block_group) {
1074 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001075 NULL, 0,
1076 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001077 }
Chris Mason39279cc2007-06-12 06:35:45 -04001078 btrfs_free_path(path);
1079 inode_item = NULL;
1080
Chris Mason39279cc2007-06-12 06:35:45 -04001081 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001082 case S_IFREG:
1083 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001084 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001085 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001086 inode->i_fop = &btrfs_file_operations;
1087 inode->i_op = &btrfs_file_inode_operations;
1088 break;
1089 case S_IFDIR:
1090 inode->i_fop = &btrfs_dir_file_operations;
1091 if (root == root->fs_info->tree_root)
1092 inode->i_op = &btrfs_dir_ro_inode_operations;
1093 else
1094 inode->i_op = &btrfs_dir_inode_operations;
1095 break;
1096 case S_IFLNK:
1097 inode->i_op = &btrfs_symlink_inode_operations;
1098 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001099 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001100 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001101 default:
1102 init_special_inode(inode, inode->i_mode, rdev);
1103 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001104 }
1105 return;
1106
1107make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001108 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001109 make_bad_inode(inode);
1110}
1111
Chris Mason5f39d392007-10-15 16:14:19 -04001112static void fill_inode_item(struct extent_buffer *leaf,
1113 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001114 struct inode *inode)
1115{
Chris Mason5f39d392007-10-15 16:14:19 -04001116 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1117 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001118 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001119 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1120 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1121
1122 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1123 inode->i_atime.tv_sec);
1124 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1125 inode->i_atime.tv_nsec);
1126
1127 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1128 inode->i_mtime.tv_sec);
1129 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1130 inode->i_mtime.tv_nsec);
1131
1132 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1133 inode->i_ctime.tv_sec);
1134 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1135 inode->i_ctime.tv_nsec);
1136
1137 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1138 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1139 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001140 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001141 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001142 BTRFS_I(inode)->block_group->key.objectid);
1143}
1144
Chris Masonba1da2f2008-07-17 12:54:15 -04001145int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001146 struct btrfs_root *root,
1147 struct inode *inode)
1148{
1149 struct btrfs_inode_item *inode_item;
1150 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001151 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001152 int ret;
1153
1154 path = btrfs_alloc_path();
1155 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001156 ret = btrfs_lookup_inode(trans, root, path,
1157 &BTRFS_I(inode)->location, 1);
1158 if (ret) {
1159 if (ret > 0)
1160 ret = -ENOENT;
1161 goto failed;
1162 }
1163
Chris Mason5f39d392007-10-15 16:14:19 -04001164 leaf = path->nodes[0];
1165 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001166 struct btrfs_inode_item);
1167
Chris Mason5f39d392007-10-15 16:14:19 -04001168 fill_inode_item(leaf, inode_item, inode);
1169 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001170 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001171 ret = 0;
1172failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001173 btrfs_free_path(path);
1174 return ret;
1175}
1176
1177
1178static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1179 struct btrfs_root *root,
1180 struct inode *dir,
1181 struct dentry *dentry)
1182{
1183 struct btrfs_path *path;
1184 const char *name = dentry->d_name.name;
1185 int name_len = dentry->d_name.len;
1186 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001187 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001188 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001189 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001190 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001191
1192 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001193 if (!path) {
1194 ret = -ENOMEM;
1195 goto err;
1196 }
1197
Chris Mason39279cc2007-06-12 06:35:45 -04001198 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1199 name, name_len, -1);
1200 if (IS_ERR(di)) {
1201 ret = PTR_ERR(di);
1202 goto err;
1203 }
1204 if (!di) {
1205 ret = -ENOENT;
1206 goto err;
1207 }
Chris Mason5f39d392007-10-15 16:14:19 -04001208 leaf = path->nodes[0];
1209 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001210 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001211 if (ret)
1212 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001213 btrfs_release_path(root, path);
1214
Josef Bacikaec74772008-07-24 12:12:38 -04001215 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1216 dentry->d_inode->i_ino,
1217 dentry->d_parent->d_inode->i_ino, &index);
1218 if (ret) {
1219 printk("failed to delete reference to %.*s, "
1220 "inode %lu parent %lu\n", name_len, name,
1221 dentry->d_inode->i_ino,
1222 dentry->d_parent->d_inode->i_ino);
1223 goto err;
1224 }
1225
Chris Mason39279cc2007-06-12 06:35:45 -04001226 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001227 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001228 if (IS_ERR(di)) {
1229 ret = PTR_ERR(di);
1230 goto err;
1231 }
1232 if (!di) {
1233 ret = -ENOENT;
1234 goto err;
1235 }
1236 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001237 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001238
1239 dentry->d_inode->i_ctime = dir->i_ctime;
1240err:
1241 btrfs_free_path(path);
1242 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001243 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001244 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001245 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001246#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1247 dentry->d_inode->i_nlink--;
1248#else
Chris Mason39279cc2007-06-12 06:35:45 -04001249 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001250#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001251 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001252 dir->i_sb->s_dirt = 1;
1253 }
1254 return ret;
1255}
1256
1257static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1258{
1259 struct btrfs_root *root;
1260 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001261 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001262 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001263 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001264
1265 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001266
1267 ret = btrfs_check_free_space(root, 1, 1);
1268 if (ret)
1269 goto fail;
1270
Chris Mason39279cc2007-06-12 06:35:45 -04001271 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001272
Chris Mason39279cc2007-06-12 06:35:45 -04001273 btrfs_set_trans_block_group(trans, dir);
1274 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Josef Bacik7b128762008-07-24 12:17:14 -04001275
1276 if (inode->i_nlink == 0)
1277 ret = btrfs_orphan_add(trans, inode);
1278
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001279 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001280
Chris Mason89ce8a62008-06-25 16:01:31 -04001281 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001282fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001283 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001284 return ret;
1285}
1286
1287static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1288{
1289 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001290 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001291 int ret;
1292 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001293 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001294 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001295
Chris Mason925baed2008-06-25 16:01:30 -04001296 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001297 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001298 }
Yan134d4512007-10-25 15:49:25 -04001299
Chris Mason1832a6d2007-12-21 16:27:21 -05001300 ret = btrfs_check_free_space(root, 1, 1);
1301 if (ret)
1302 goto fail;
1303
Chris Mason39279cc2007-06-12 06:35:45 -04001304 trans = btrfs_start_transaction(root, 1);
1305 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001306
Josef Bacik7b128762008-07-24 12:17:14 -04001307 err = btrfs_orphan_add(trans, inode);
1308 if (err)
1309 goto fail_trans;
1310
Chris Mason39279cc2007-06-12 06:35:45 -04001311 /* now the directory is empty */
1312 err = btrfs_unlink_trans(trans, root, dir, dentry);
1313 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001314 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001315 }
Chris Mason39544012007-12-12 14:38:19 -05001316
Josef Bacik7b128762008-07-24 12:17:14 -04001317fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001318 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001319 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001320fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001321 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001322
Chris Mason39279cc2007-06-12 06:35:45 -04001323 if (ret && !err)
1324 err = ret;
1325 return err;
1326}
1327
Chris Mason39279cc2007-06-12 06:35:45 -04001328/*
Chris Mason39279cc2007-06-12 06:35:45 -04001329 * this can truncate away extent items, csum items and directory items.
1330 * It starts at a high offset and removes keys until it can't find
1331 * any higher than i_size.
1332 *
1333 * csum items that cross the new i_size are truncated to the new size
1334 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001335 *
1336 * min_type is the minimum key type to truncate down to. If set to 0, this
1337 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001338 */
1339static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1340 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001341 struct inode *inode,
1342 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001343{
1344 int ret;
1345 struct btrfs_path *path;
1346 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001347 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001348 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001349 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001350 struct btrfs_file_extent_item *fi;
1351 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001352 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001353 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001354 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001355 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001356 int found_extent;
1357 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001358 int pending_del_nr = 0;
1359 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001360 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001361 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001362
Chris Mason3b951512008-04-17 11:29:12 -04001363 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001364 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001365 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001366 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001367
Chris Mason39279cc2007-06-12 06:35:45 -04001368 /* FIXME, add redo link to tree so we don't leak on crash */
1369 key.objectid = inode->i_ino;
1370 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001371 key.type = (u8)-1;
1372
Chris Mason85e21ba2008-01-29 15:11:36 -05001373 btrfs_init_path(path);
1374search_again:
1375 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1376 if (ret < 0) {
1377 goto error;
1378 }
1379 if (ret > 0) {
1380 BUG_ON(path->slots[0] == 0);
1381 path->slots[0]--;
1382 }
1383
Chris Mason39279cc2007-06-12 06:35:45 -04001384 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001385 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001386 leaf = path->nodes[0];
1387 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1388 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001389
Chris Mason5f39d392007-10-15 16:14:19 -04001390 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001391 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001392
Chris Mason85e21ba2008-01-29 15:11:36 -05001393 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001394 break;
1395
Chris Mason5f39d392007-10-15 16:14:19 -04001396 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001397 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001398 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001399 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001400 extent_type = btrfs_file_extent_type(leaf, fi);
1401 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001402 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001403 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001404 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1405 struct btrfs_item *item = btrfs_item_nr(leaf,
1406 path->slots[0]);
1407 item_end += btrfs_file_extent_inline_len(leaf,
1408 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001409 }
Yan008630c2007-11-07 13:31:09 -05001410 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001411 }
1412 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1413 ret = btrfs_csum_truncate(trans, root, path,
1414 inode->i_size);
1415 BUG_ON(ret);
1416 }
Yan008630c2007-11-07 13:31:09 -05001417 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001418 if (found_type == BTRFS_DIR_ITEM_KEY) {
1419 found_type = BTRFS_INODE_ITEM_KEY;
1420 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1421 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001422 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1423 found_type = BTRFS_XATTR_ITEM_KEY;
1424 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1425 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001426 } else if (found_type) {
1427 found_type--;
1428 } else {
1429 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001430 }
Yana61721d2007-09-17 11:08:38 -04001431 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001432 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001433 }
Chris Mason5f39d392007-10-15 16:14:19 -04001434 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001435 del_item = 1;
1436 else
1437 del_item = 0;
1438 found_extent = 0;
1439
1440 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001441 if (found_type != BTRFS_EXTENT_DATA_KEY)
1442 goto delete;
1443
1444 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001445 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001446 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001447 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001448 u64 orig_num_bytes =
1449 btrfs_file_extent_num_bytes(leaf, fi);
1450 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001451 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001452 extent_num_bytes = extent_num_bytes &
1453 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001454 btrfs_set_file_extent_num_bytes(leaf, fi,
1455 extent_num_bytes);
1456 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001457 extent_num_bytes);
1458 if (extent_start != 0)
1459 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001460 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001461 } else {
Chris Masondb945352007-10-15 16:15:53 -04001462 extent_num_bytes =
1463 btrfs_file_extent_disk_num_bytes(leaf,
1464 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001465 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001466 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001467 if (extent_start != 0) {
1468 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001469 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001470 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001471 root_gen = btrfs_header_generation(leaf);
1472 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001473 }
Chris Mason90692182008-02-08 13:49:28 -05001474 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1475 if (!del_item) {
1476 u32 newsize = inode->i_size - found_key.offset;
1477 dec_i_blocks(inode, item_end + 1 -
1478 found_key.offset - newsize);
1479 newsize =
1480 btrfs_file_extent_calc_inline_size(newsize);
1481 ret = btrfs_truncate_item(trans, root, path,
1482 newsize, 1);
1483 BUG_ON(ret);
1484 } else {
1485 dec_i_blocks(inode, item_end + 1 -
1486 found_key.offset);
1487 }
Chris Mason39279cc2007-06-12 06:35:45 -04001488 }
Chris Mason179e29e2007-11-01 11:28:41 -04001489delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001490 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001491 if (!pending_del_nr) {
1492 /* no pending yet, add ourselves */
1493 pending_del_slot = path->slots[0];
1494 pending_del_nr = 1;
1495 } else if (pending_del_nr &&
1496 path->slots[0] + 1 == pending_del_slot) {
1497 /* hop on the pending chunk */
1498 pending_del_nr++;
1499 pending_del_slot = path->slots[0];
1500 } else {
1501 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1502 }
Chris Mason39279cc2007-06-12 06:35:45 -04001503 } else {
1504 break;
1505 }
Chris Mason39279cc2007-06-12 06:35:45 -04001506 if (found_extent) {
1507 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001508 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001509 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001510 root_gen, inode->i_ino,
1511 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001512 BUG_ON(ret);
1513 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001514next:
1515 if (path->slots[0] == 0) {
1516 if (pending_del_nr)
1517 goto del_pending;
1518 btrfs_release_path(root, path);
1519 goto search_again;
1520 }
1521
1522 path->slots[0]--;
1523 if (pending_del_nr &&
1524 path->slots[0] + 1 != pending_del_slot) {
1525 struct btrfs_key debug;
1526del_pending:
1527 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1528 pending_del_slot);
1529 ret = btrfs_del_items(trans, root, path,
1530 pending_del_slot,
1531 pending_del_nr);
1532 BUG_ON(ret);
1533 pending_del_nr = 0;
1534 btrfs_release_path(root, path);
1535 goto search_again;
1536 }
Chris Mason39279cc2007-06-12 06:35:45 -04001537 }
1538 ret = 0;
1539error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001540 if (pending_del_nr) {
1541 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1542 pending_del_nr);
1543 }
Chris Mason39279cc2007-06-12 06:35:45 -04001544 btrfs_free_path(path);
1545 inode->i_sb->s_dirt = 1;
1546 return ret;
1547}
1548
Chris Masona52d9a82007-08-27 16:49:44 -04001549/*
1550 * taken from block_truncate_page, but does cow as it zeros out
1551 * any bytes left in the last page in the file.
1552 */
1553static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1554{
1555 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001556 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001557 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1558 struct btrfs_ordered_extent *ordered;
1559 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001560 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001561 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1562 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1563 struct page *page;
1564 int ret = 0;
1565 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001566 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001567
1568 if ((offset & (blocksize - 1)) == 0)
1569 goto out;
1570
1571 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001572again:
Chris Masona52d9a82007-08-27 16:49:44 -04001573 page = grab_cache_page(mapping, index);
1574 if (!page)
1575 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001576
1577 page_start = page_offset(page);
1578 page_end = page_start + PAGE_CACHE_SIZE - 1;
1579
Chris Masona52d9a82007-08-27 16:49:44 -04001580 if (!PageUptodate(page)) {
1581 ret = btrfs_readpage(NULL, page);
1582 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001583 if (page->mapping != mapping) {
1584 unlock_page(page);
1585 page_cache_release(page);
1586 goto again;
1587 }
Chris Masona52d9a82007-08-27 16:49:44 -04001588 if (!PageUptodate(page)) {
1589 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001590 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001591 }
1592 }
Chris Mason211c17f2008-05-15 09:13:45 -04001593 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001594
1595 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1596 set_page_extent_mapped(page);
1597
1598 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1599 if (ordered) {
1600 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1601 unlock_page(page);
1602 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001603 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001604 btrfs_put_ordered_extent(ordered);
1605 goto again;
1606 }
1607
Chris Masonea8c2812008-08-04 23:17:27 -04001608 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001609 ret = 0;
1610 if (offset != PAGE_CACHE_SIZE) {
1611 kaddr = kmap(page);
1612 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1613 flush_dcache_page(page);
1614 kunmap(page);
1615 }
Chris Mason247e7432008-07-17 12:53:51 -04001616 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001617 set_page_dirty(page);
1618 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001619
Chris Mason89642222008-07-24 09:41:53 -04001620out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001621 unlock_page(page);
1622 page_cache_release(page);
1623out:
1624 return ret;
1625}
1626
1627static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1628{
1629 struct inode *inode = dentry->d_inode;
1630 int err;
1631
1632 err = inode_change_ok(inode, attr);
1633 if (err)
1634 return err;
1635
1636 if (S_ISREG(inode->i_mode) &&
1637 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1638 struct btrfs_trans_handle *trans;
1639 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001640 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001641
Chris Mason5f39d392007-10-15 16:14:19 -04001642 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001643 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001644 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001645 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001646 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001647
Chris Mason1b0f7c22008-01-30 14:33:02 -05001648 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001649 goto out;
1650
Chris Mason1832a6d2007-12-21 16:27:21 -05001651 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001652 if (err)
1653 goto fail;
1654
Chris Mason39279cc2007-06-12 06:35:45 -04001655 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1656
Chris Mason5f564062008-01-22 16:47:59 -05001657 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001658 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1659 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001660
Chris Mason39279cc2007-06-12 06:35:45 -04001661 trans = btrfs_start_transaction(root, 1);
1662 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001663 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001664 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001665 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001666 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001667
Chris Mason179e29e2007-11-01 11:28:41 -04001668 if (alloc_hint != EXTENT_MAP_INLINE) {
1669 err = btrfs_insert_file_extent(trans, root,
1670 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001671 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001672 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001673 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001674 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001675 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001676 }
Chris Masonee6e6502008-07-17 12:54:40 -04001677 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001678 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001679 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001680 if (err)
1681 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001682 }
1683out:
1684 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001685
1686 if (!err && ((attr->ia_valid & ATTR_MODE)))
1687 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001688fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001689 return err;
1690}
Chris Mason61295eb2008-01-14 16:24:38 -05001691
Chris Mason39279cc2007-06-12 06:35:45 -04001692void btrfs_delete_inode(struct inode *inode)
1693{
1694 struct btrfs_trans_handle *trans;
1695 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001696 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001697 int ret;
1698
1699 truncate_inode_pages(&inode->i_data, 0);
1700 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001701 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001702 goto no_delete;
1703 }
Chris Mason4a096752008-07-21 10:29:44 -04001704 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001705
Chris Masondbe674a2008-07-17 12:54:05 -04001706 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001707 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001708
Chris Mason39279cc2007-06-12 06:35:45 -04001709 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001710 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001711 if (ret) {
1712 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001713 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001714 }
1715
1716 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001717
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001718 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001719 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001720
Chris Mason39279cc2007-06-12 06:35:45 -04001721 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001722 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001723 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001724
1725no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001726 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001727 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001728 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001729no_delete:
1730 clear_inode(inode);
1731}
1732
1733/*
1734 * this returns the key found in the dir entry in the location pointer.
1735 * If no dir entries were found, location->objectid is 0.
1736 */
1737static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1738 struct btrfs_key *location)
1739{
1740 const char *name = dentry->d_name.name;
1741 int namelen = dentry->d_name.len;
1742 struct btrfs_dir_item *di;
1743 struct btrfs_path *path;
1744 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001745 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001746
Chris Mason39544012007-12-12 14:38:19 -05001747 if (namelen == 1 && strcmp(name, ".") == 0) {
1748 location->objectid = dir->i_ino;
1749 location->type = BTRFS_INODE_ITEM_KEY;
1750 location->offset = 0;
1751 return 0;
1752 }
Chris Mason39279cc2007-06-12 06:35:45 -04001753 path = btrfs_alloc_path();
1754 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001755
Chris Mason7a720532007-12-13 09:06:59 -05001756 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001757 struct btrfs_key key;
1758 struct extent_buffer *leaf;
Chris Mason39544012007-12-12 14:38:19 -05001759 int slot;
1760
1761 key.objectid = dir->i_ino;
Yan445dceb2008-07-24 12:19:32 -04001762 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001763 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001764 if (ret < 0 || path->slots[0] == 0)
1765 goto out_err;
Chris Mason39544012007-12-12 14:38:19 -05001766 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1767 BUG_ON(ret == 0);
1768 ret = 0;
Chris Mason39544012007-12-12 14:38:19 -05001769 leaf = path->nodes[0];
Yan445dceb2008-07-24 12:19:32 -04001770 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001771
1772 btrfs_item_key_to_cpu(leaf, &key, slot);
1773 if (key.objectid != dir->i_ino ||
1774 key.type != BTRFS_INODE_REF_KEY) {
1775 goto out_err;
1776 }
1777 location->objectid = key.offset;
1778 location->type = BTRFS_INODE_ITEM_KEY;
1779 location->offset = 0;
1780 goto out;
1781 }
1782
Chris Mason39279cc2007-06-12 06:35:45 -04001783 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1784 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001785 if (IS_ERR(di))
1786 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001787 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001788 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001789 }
Chris Mason5f39d392007-10-15 16:14:19 -04001790 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001791out:
Chris Mason39279cc2007-06-12 06:35:45 -04001792 btrfs_free_path(path);
1793 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001794out_err:
1795 location->objectid = 0;
1796 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001797}
1798
1799/*
1800 * when we hit a tree root in a directory, the btrfs part of the inode
1801 * needs to be changed to reflect the root directory of the tree root. This
1802 * is kind of like crossing a mount point.
1803 */
1804static int fixup_tree_root_location(struct btrfs_root *root,
1805 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001806 struct btrfs_root **sub_root,
1807 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001808{
Chris Mason39279cc2007-06-12 06:35:45 -04001809 struct btrfs_root_item *ri;
1810
1811 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1812 return 0;
1813 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1814 return 0;
1815
Josef Bacik58176a92007-08-29 15:47:34 -04001816 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1817 dentry->d_name.name,
1818 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001819 if (IS_ERR(*sub_root))
1820 return PTR_ERR(*sub_root);
1821
1822 ri = &(*sub_root)->root_item;
1823 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001824 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1825 location->offset = 0;
1826
Chris Mason39279cc2007-06-12 06:35:45 -04001827 return 0;
1828}
1829
1830static int btrfs_init_locked_inode(struct inode *inode, void *p)
1831{
1832 struct btrfs_iget_args *args = p;
1833 inode->i_ino = args->ino;
1834 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001835 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04001836 inode->i_mapping->writeback_index = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001837 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001838 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001839 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1840 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001841 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001842 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1843 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001844 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04001845 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001846 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001847 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001848 return 0;
1849}
1850
1851static int btrfs_find_actor(struct inode *inode, void *opaque)
1852{
1853 struct btrfs_iget_args *args = opaque;
1854 return (args->ino == inode->i_ino &&
1855 args->root == BTRFS_I(inode)->root);
1856}
1857
Chris Masondc17ff82008-01-08 15:46:30 -05001858struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1859 u64 root_objectid)
1860{
1861 struct btrfs_iget_args args;
1862 args.ino = objectid;
1863 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1864
1865 if (!args.root)
1866 return NULL;
1867
1868 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1869}
1870
Chris Mason39279cc2007-06-12 06:35:45 -04001871struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1872 struct btrfs_root *root)
1873{
1874 struct inode *inode;
1875 struct btrfs_iget_args args;
1876 args.ino = objectid;
1877 args.root = root;
1878
1879 inode = iget5_locked(s, objectid, btrfs_find_actor,
1880 btrfs_init_locked_inode,
1881 (void *)&args);
1882 return inode;
1883}
1884
1885static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1886 struct nameidata *nd)
1887{
1888 struct inode * inode;
1889 struct btrfs_inode *bi = BTRFS_I(dir);
1890 struct btrfs_root *root = bi->root;
1891 struct btrfs_root *sub_root = root;
1892 struct btrfs_key location;
Josef Bacik7b128762008-07-24 12:17:14 -04001893 int ret, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001894
1895 if (dentry->d_name.len > BTRFS_NAME_LEN)
1896 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001897
Chris Mason39279cc2007-06-12 06:35:45 -04001898 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001899
Chris Mason39279cc2007-06-12 06:35:45 -04001900 if (ret < 0)
1901 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001902
Chris Mason39279cc2007-06-12 06:35:45 -04001903 inode = NULL;
1904 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001905 ret = fixup_tree_root_location(root, &location, &sub_root,
1906 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001907 if (ret < 0)
1908 return ERR_PTR(ret);
1909 if (ret > 0)
1910 return ERR_PTR(-ENOENT);
Josef Bacik7b128762008-07-24 12:17:14 -04001911
Chris Mason39279cc2007-06-12 06:35:45 -04001912 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1913 sub_root);
1914 if (!inode)
1915 return ERR_PTR(-EACCES);
1916 if (inode->i_state & I_NEW) {
1917 /* the inode and parent dir are two different roots */
1918 if (sub_root != root) {
1919 igrab(inode);
1920 sub_root->inode = inode;
Josef Bacik7b128762008-07-24 12:17:14 -04001921 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001922 }
1923 BTRFS_I(inode)->root = sub_root;
1924 memcpy(&BTRFS_I(inode)->location, &location,
1925 sizeof(location));
1926 btrfs_read_locked_inode(inode);
1927 unlock_new_inode(inode);
1928 }
1929 }
Josef Bacik7b128762008-07-24 12:17:14 -04001930
1931 if (unlikely(do_orphan))
1932 btrfs_orphan_cleanup(sub_root);
1933
Chris Mason39279cc2007-06-12 06:35:45 -04001934 return d_splice_alias(inode, dentry);
1935}
1936
Chris Mason39279cc2007-06-12 06:35:45 -04001937static unsigned char btrfs_filetype_table[] = {
1938 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1939};
1940
1941static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1942{
Chris Mason6da6aba2007-12-18 16:15:09 -05001943 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001944 struct btrfs_root *root = BTRFS_I(inode)->root;
1945 struct btrfs_item *item;
1946 struct btrfs_dir_item *di;
1947 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001948 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001949 struct btrfs_path *path;
1950 int ret;
1951 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001952 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001953 int slot;
1954 int advance;
1955 unsigned char d_type;
1956 int over = 0;
1957 u32 di_cur;
1958 u32 di_total;
1959 u32 di_len;
1960 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001961 char tmp_name[32];
1962 char *name_ptr;
1963 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001964
1965 /* FIXME, use a real flag for deciding about the key type */
1966 if (root->fs_info->tree_root == root)
1967 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001968
Chris Mason39544012007-12-12 14:38:19 -05001969 /* special case for "." */
1970 if (filp->f_pos == 0) {
1971 over = filldir(dirent, ".", 1,
1972 1, inode->i_ino,
1973 DT_DIR);
1974 if (over)
1975 return 0;
1976 filp->f_pos = 1;
1977 }
1978
Chris Mason39279cc2007-06-12 06:35:45 -04001979 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001980 path = btrfs_alloc_path();
1981 path->reada = 2;
1982
1983 /* special case for .., just use the back ref */
1984 if (filp->f_pos == 1) {
1985 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001986 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001987 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan445dceb2008-07-24 12:19:32 -04001988 if (ret < 0 || path->slots[0] == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001989 btrfs_release_path(root, path);
1990 goto read_dir_items;
1991 }
Yan445dceb2008-07-24 12:19:32 -04001992 BUG_ON(ret == 0);
1993 leaf = path->nodes[0];
1994 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001995 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1996 btrfs_release_path(root, path);
1997 if (found_key.objectid != key.objectid ||
1998 found_key.type != BTRFS_INODE_REF_KEY)
1999 goto read_dir_items;
2000 over = filldir(dirent, "..", 2,
2001 2, found_key.offset, DT_DIR);
2002 if (over)
2003 goto nopos;
2004 filp->f_pos = 2;
2005 }
2006
2007read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04002008 btrfs_set_key_type(&key, key_type);
2009 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04002010
Chris Mason39279cc2007-06-12 06:35:45 -04002011 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2012 if (ret < 0)
2013 goto err;
2014 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002015 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002016 leaf = path->nodes[0];
2017 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002018 slot = path->slots[0];
2019 if (advance || slot >= nritems) {
2020 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002021 ret = btrfs_next_leaf(root, path);
2022 if (ret)
2023 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002024 leaf = path->nodes[0];
2025 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002026 slot = path->slots[0];
2027 } else {
2028 slot++;
2029 path->slots[0]++;
2030 }
2031 }
2032 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002033 item = btrfs_item_nr(leaf, slot);
2034 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2035
2036 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04002037 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002038 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002039 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002040 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04002041 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002042
2043 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002044 advance = 1;
2045 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2046 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002047 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04002048 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04002049 struct btrfs_key location;
2050
2051 name_len = btrfs_dir_name_len(leaf, di);
2052 if (name_len < 32) {
2053 name_ptr = tmp_name;
2054 } else {
2055 name_ptr = kmalloc(name_len, GFP_NOFS);
2056 BUG_ON(!name_ptr);
2057 }
2058 read_extent_buffer(leaf, name_ptr,
2059 (unsigned long)(di + 1), name_len);
2060
2061 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2062 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002063 over = filldir(dirent, name_ptr, name_len,
2064 found_key.offset,
2065 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002066 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002067
2068 if (name_ptr != tmp_name)
2069 kfree(name_ptr);
2070
Chris Mason39279cc2007-06-12 06:35:45 -04002071 if (over)
2072 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05002073 di_len = btrfs_dir_name_len(leaf, di) +
2074 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002075 di_cur += di_len;
2076 di = (struct btrfs_dir_item *)((char *)di + di_len);
2077 }
2078 }
Yan Zheng5e591a02008-02-19 11:41:02 -05002079 if (key_type == BTRFS_DIR_INDEX_KEY)
2080 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2081 else
2082 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002083nopos:
2084 ret = 0;
2085err:
Chris Mason39279cc2007-06-12 06:35:45 -04002086 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002087 return ret;
2088}
2089
2090int btrfs_write_inode(struct inode *inode, int wait)
2091{
2092 struct btrfs_root *root = BTRFS_I(inode)->root;
2093 struct btrfs_trans_handle *trans;
2094 int ret = 0;
2095
Chris Mason4ca8b412008-08-05 13:30:48 -04002096 if (root->fs_info->closing > 1)
2097 return 0;
2098
Chris Mason39279cc2007-06-12 06:35:45 -04002099 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002100 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002101 btrfs_set_trans_block_group(trans, inode);
2102 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002103 }
2104 return ret;
2105}
2106
2107/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002108 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002109 * inode changes. But, it is most likely to find the inode in cache.
2110 * FIXME, needs more benchmarking...there are no reasons other than performance
2111 * to keep or drop this code.
2112 */
2113void btrfs_dirty_inode(struct inode *inode)
2114{
2115 struct btrfs_root *root = BTRFS_I(inode)->root;
2116 struct btrfs_trans_handle *trans;
2117
Chris Masonf9295742008-07-17 12:54:14 -04002118 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002119 btrfs_set_trans_block_group(trans, inode);
2120 btrfs_update_inode(trans, root, inode);
2121 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002122}
2123
Josef Bacikaec74772008-07-24 12:12:38 -04002124static int btrfs_set_inode_index_count(struct inode *inode)
2125{
2126 struct btrfs_root *root = BTRFS_I(inode)->root;
2127 struct btrfs_key key, found_key;
2128 struct btrfs_path *path;
2129 struct extent_buffer *leaf;
2130 int ret;
2131
2132 key.objectid = inode->i_ino;
2133 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2134 key.offset = (u64)-1;
2135
2136 path = btrfs_alloc_path();
2137 if (!path)
2138 return -ENOMEM;
2139
2140 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2141 if (ret < 0)
2142 goto out;
2143 /* FIXME: we should be able to handle this */
2144 if (ret == 0)
2145 goto out;
2146 ret = 0;
2147
2148 /*
2149 * MAGIC NUMBER EXPLANATION:
2150 * since we search a directory based on f_pos we have to start at 2
2151 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2152 * else has to start at 2
2153 */
2154 if (path->slots[0] == 0) {
2155 BTRFS_I(inode)->index_cnt = 2;
2156 goto out;
2157 }
2158
2159 path->slots[0]--;
2160
2161 leaf = path->nodes[0];
2162 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2163
2164 if (found_key.objectid != inode->i_ino ||
2165 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2166 BTRFS_I(inode)->index_cnt = 2;
2167 goto out;
2168 }
2169
2170 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2171out:
2172 btrfs_free_path(path);
2173 return ret;
2174}
2175
Chris Mason00e4e6b2008-08-05 11:18:09 -04002176static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2177 u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04002178{
2179 int ret = 0;
2180
2181 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2182 ret = btrfs_set_inode_index_count(dir);
2183 if (ret)
2184 return ret;
2185 }
2186
Chris Mason00e4e6b2008-08-05 11:18:09 -04002187 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04002188 BTRFS_I(dir)->index_cnt++;
2189
2190 return ret;
2191}
2192
Chris Mason39279cc2007-06-12 06:35:45 -04002193static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2194 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002195 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002196 const char *name, int name_len,
2197 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002198 u64 objectid,
2199 struct btrfs_block_group_cache *group,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002200 int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04002201{
2202 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002203 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002204 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002205 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002206 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002207 struct btrfs_inode_ref *ref;
2208 struct btrfs_key key[2];
2209 u32 sizes[2];
2210 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002211 int ret;
2212 int owner;
2213
Chris Mason5f39d392007-10-15 16:14:19 -04002214 path = btrfs_alloc_path();
2215 BUG_ON(!path);
2216
Chris Mason39279cc2007-06-12 06:35:45 -04002217 inode = new_inode(root->fs_info->sb);
2218 if (!inode)
2219 return ERR_PTR(-ENOMEM);
2220
Josef Bacikaec74772008-07-24 12:12:38 -04002221 if (dir) {
Chris Mason00e4e6b2008-08-05 11:18:09 -04002222 ret = btrfs_set_inode_index(dir, inode, index);
Josef Bacikaec74772008-07-24 12:12:38 -04002223 if (ret)
2224 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002225 }
2226 /*
2227 * index_cnt is ignored for everything but a dir,
2228 * btrfs_get_inode_index_count has an explanation for the magic
2229 * number
2230 */
2231 BTRFS_I(inode)->index_cnt = 2;
2232
Chris Masond1310b22008-01-24 16:13:08 -05002233 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2234 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002235 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002236 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2237 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002238 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonea8c2812008-08-04 23:17:27 -04002239 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04002240 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002241 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002242 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04002243 inode->i_mapping->writeback_index = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002244 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002245 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002246
Chris Mason39279cc2007-06-12 06:35:45 -04002247 if (mode & S_IFDIR)
2248 owner = 0;
2249 else
2250 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002251 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002252 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002253 if (!new_inode_group) {
2254 printk("find_block group failed\n");
2255 new_inode_group = group;
2256 }
2257 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002258 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002259
2260 key[0].objectid = objectid;
2261 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2262 key[0].offset = 0;
2263
2264 key[1].objectid = objectid;
2265 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2266 key[1].offset = ref_objectid;
2267
2268 sizes[0] = sizeof(struct btrfs_inode_item);
2269 sizes[1] = name_len + sizeof(*ref);
2270
2271 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2272 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002273 goto fail;
2274
Chris Mason9c583092008-01-29 15:15:18 -05002275 if (objectid > root->highest_inode)
2276 root->highest_inode = objectid;
2277
Chris Mason39279cc2007-06-12 06:35:45 -04002278 inode->i_uid = current->fsuid;
2279 inode->i_gid = current->fsgid;
2280 inode->i_mode = mode;
2281 inode->i_ino = objectid;
2282 inode->i_blocks = 0;
2283 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002284 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2285 struct btrfs_inode_item);
2286 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002287
2288 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2289 struct btrfs_inode_ref);
2290 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002291 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05002292 ptr = (unsigned long)(ref + 1);
2293 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2294
Chris Mason5f39d392007-10-15 16:14:19 -04002295 btrfs_mark_buffer_dirty(path->nodes[0]);
2296 btrfs_free_path(path);
2297
Chris Mason39279cc2007-06-12 06:35:45 -04002298 location = &BTRFS_I(inode)->location;
2299 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002300 location->offset = 0;
2301 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2302
Chris Mason39279cc2007-06-12 06:35:45 -04002303 insert_inode_hash(inode);
2304 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002305fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002306 if (dir)
2307 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002308 btrfs_free_path(path);
2309 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002310}
2311
2312static inline u8 btrfs_inode_type(struct inode *inode)
2313{
2314 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2315}
2316
2317static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002318 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002319 int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002320{
2321 int ret;
2322 struct btrfs_key key;
2323 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002324 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002325
Chris Mason39279cc2007-06-12 06:35:45 -04002326 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002327 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2328 key.offset = 0;
2329
2330 ret = btrfs_insert_dir_item(trans, root,
2331 dentry->d_name.name, dentry->d_name.len,
2332 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002333 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04002334 index);
Chris Mason39279cc2007-06-12 06:35:45 -04002335 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002336 if (add_backref) {
2337 ret = btrfs_insert_inode_ref(trans, root,
2338 dentry->d_name.name,
2339 dentry->d_name.len,
2340 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002341 parent_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002342 index);
Chris Mason9c583092008-01-29 15:15:18 -05002343 }
Chris Masondbe674a2008-07-17 12:54:05 -04002344 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2345 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002346 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002347 ret = btrfs_update_inode(trans, root,
2348 dentry->d_parent->d_inode);
2349 }
2350 return ret;
2351}
2352
2353static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002354 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002355 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002356{
Chris Mason00e4e6b2008-08-05 11:18:09 -04002357 int err = btrfs_add_link(trans, dentry, inode, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002358 if (!err) {
2359 d_instantiate(dentry, inode);
2360 return 0;
2361 }
2362 if (err > 0)
2363 err = -EEXIST;
2364 return err;
2365}
2366
Josef Bacik618e21d2007-07-11 10:18:17 -04002367static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2368 int mode, dev_t rdev)
2369{
2370 struct btrfs_trans_handle *trans;
2371 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002372 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002373 int err;
2374 int drop_inode = 0;
2375 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002376 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002377 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002378
2379 if (!new_valid_dev(rdev))
2380 return -EINVAL;
2381
Chris Mason1832a6d2007-12-21 16:27:21 -05002382 err = btrfs_check_free_space(root, 1, 0);
2383 if (err)
2384 goto fail;
2385
Josef Bacik618e21d2007-07-11 10:18:17 -04002386 trans = btrfs_start_transaction(root, 1);
2387 btrfs_set_trans_block_group(trans, dir);
2388
2389 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2390 if (err) {
2391 err = -ENOSPC;
2392 goto out_unlock;
2393 }
2394
Josef Bacikaec74772008-07-24 12:12:38 -04002395 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002396 dentry->d_name.len,
2397 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002398 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002399 err = PTR_ERR(inode);
2400 if (IS_ERR(inode))
2401 goto out_unlock;
2402
Josef Bacik33268ea2008-07-24 12:16:36 -04002403 err = btrfs_init_acl(inode, dir);
2404 if (err) {
2405 drop_inode = 1;
2406 goto out_unlock;
2407 }
2408
Josef Bacik618e21d2007-07-11 10:18:17 -04002409 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002410 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002411 if (err)
2412 drop_inode = 1;
2413 else {
2414 inode->i_op = &btrfs_special_inode_operations;
2415 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002416 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002417 }
2418 dir->i_sb->s_dirt = 1;
2419 btrfs_update_inode_block_group(trans, inode);
2420 btrfs_update_inode_block_group(trans, dir);
2421out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002422 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002423 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002424fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002425 if (drop_inode) {
2426 inode_dec_link_count(inode);
2427 iput(inode);
2428 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002429 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002430 return err;
2431}
2432
Chris Mason39279cc2007-06-12 06:35:45 -04002433static int btrfs_create(struct inode *dir, struct dentry *dentry,
2434 int mode, struct nameidata *nd)
2435{
2436 struct btrfs_trans_handle *trans;
2437 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002438 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002439 int err;
2440 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002441 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002442 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002443 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002444
Chris Mason1832a6d2007-12-21 16:27:21 -05002445 err = btrfs_check_free_space(root, 1, 0);
2446 if (err)
2447 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002448 trans = btrfs_start_transaction(root, 1);
2449 btrfs_set_trans_block_group(trans, dir);
2450
2451 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2452 if (err) {
2453 err = -ENOSPC;
2454 goto out_unlock;
2455 }
2456
Josef Bacikaec74772008-07-24 12:12:38 -04002457 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002458 dentry->d_name.len,
2459 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002460 objectid, BTRFS_I(dir)->block_group, mode,
2461 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002462 err = PTR_ERR(inode);
2463 if (IS_ERR(inode))
2464 goto out_unlock;
2465
Josef Bacik33268ea2008-07-24 12:16:36 -04002466 err = btrfs_init_acl(inode, dir);
2467 if (err) {
2468 drop_inode = 1;
2469 goto out_unlock;
2470 }
2471
Chris Mason39279cc2007-06-12 06:35:45 -04002472 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002473 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002474 if (err)
2475 drop_inode = 1;
2476 else {
2477 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002478 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002479 inode->i_fop = &btrfs_file_operations;
2480 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002481 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2482 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002483 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002484 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2485 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04002486 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04002487 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002488 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002489 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002490 BTRFS_I(inode)->disk_i_size = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04002491 inode->i_mapping->writeback_index = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002492 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002493 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002494 }
2495 dir->i_sb->s_dirt = 1;
2496 btrfs_update_inode_block_group(trans, inode);
2497 btrfs_update_inode_block_group(trans, dir);
2498out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002499 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002500 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002501fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002502 if (drop_inode) {
2503 inode_dec_link_count(inode);
2504 iput(inode);
2505 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002506 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002507 return err;
2508}
2509
2510static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2511 struct dentry *dentry)
2512{
2513 struct btrfs_trans_handle *trans;
2514 struct btrfs_root *root = BTRFS_I(dir)->root;
2515 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002516 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05002517 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002518 int err;
2519 int drop_inode = 0;
2520
2521 if (inode->i_nlink == 0)
2522 return -ENOENT;
2523
Chris Mason6da6aba2007-12-18 16:15:09 -05002524#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2525 inode->i_nlink++;
2526#else
Chris Mason39279cc2007-06-12 06:35:45 -04002527 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002528#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002529 err = btrfs_check_free_space(root, 1, 0);
2530 if (err)
2531 goto fail;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002532 err = btrfs_set_inode_index(dir, inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002533 if (err)
2534 goto fail;
2535
Chris Mason39279cc2007-06-12 06:35:45 -04002536 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002537
Chris Mason39279cc2007-06-12 06:35:45 -04002538 btrfs_set_trans_block_group(trans, dir);
2539 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002540
Chris Mason00e4e6b2008-08-05 11:18:09 -04002541 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04002542
Chris Mason39279cc2007-06-12 06:35:45 -04002543 if (err)
2544 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002545
Chris Mason39279cc2007-06-12 06:35:45 -04002546 dir->i_sb->s_dirt = 1;
2547 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002548 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002549
Chris Mason54aa1f42007-06-22 14:16:25 -04002550 if (err)
2551 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002552
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002553 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002554 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002555fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002556 if (drop_inode) {
2557 inode_dec_link_count(inode);
2558 iput(inode);
2559 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002560 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002561 return err;
2562}
2563
Chris Mason39279cc2007-06-12 06:35:45 -04002564static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2565{
Chris Masonb9d86662008-05-02 16:13:49 -04002566 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002567 struct btrfs_trans_handle *trans;
2568 struct btrfs_root *root = BTRFS_I(dir)->root;
2569 int err = 0;
2570 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002571 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002572 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002573 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002574
Chris Mason1832a6d2007-12-21 16:27:21 -05002575 err = btrfs_check_free_space(root, 1, 0);
2576 if (err)
2577 goto out_unlock;
2578
Chris Mason39279cc2007-06-12 06:35:45 -04002579 trans = btrfs_start_transaction(root, 1);
2580 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002581
Chris Mason39279cc2007-06-12 06:35:45 -04002582 if (IS_ERR(trans)) {
2583 err = PTR_ERR(trans);
2584 goto out_unlock;
2585 }
2586
2587 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2588 if (err) {
2589 err = -ENOSPC;
2590 goto out_unlock;
2591 }
2592
Josef Bacikaec74772008-07-24 12:12:38 -04002593 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002594 dentry->d_name.len,
2595 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002596 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2597 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002598 if (IS_ERR(inode)) {
2599 err = PTR_ERR(inode);
2600 goto out_fail;
2601 }
Chris Mason5f39d392007-10-15 16:14:19 -04002602
Chris Mason39279cc2007-06-12 06:35:45 -04002603 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002604
2605 err = btrfs_init_acl(inode, dir);
2606 if (err)
2607 goto out_fail;
2608
Chris Mason39279cc2007-06-12 06:35:45 -04002609 inode->i_op = &btrfs_dir_inode_operations;
2610 inode->i_fop = &btrfs_dir_file_operations;
2611 btrfs_set_trans_block_group(trans, inode);
2612
Chris Masondbe674a2008-07-17 12:54:05 -04002613 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002614 err = btrfs_update_inode(trans, root, inode);
2615 if (err)
2616 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002617
Chris Mason00e4e6b2008-08-05 11:18:09 -04002618 err = btrfs_add_link(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002619 if (err)
2620 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002621
Chris Mason39279cc2007-06-12 06:35:45 -04002622 d_instantiate(dentry, inode);
2623 drop_on_err = 0;
2624 dir->i_sb->s_dirt = 1;
2625 btrfs_update_inode_block_group(trans, inode);
2626 btrfs_update_inode_block_group(trans, dir);
2627
2628out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002629 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002630 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002631
Chris Mason39279cc2007-06-12 06:35:45 -04002632out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002633 if (drop_on_err)
2634 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002635 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002636 return err;
2637}
2638
Chris Mason3b951512008-04-17 11:29:12 -04002639static int merge_extent_mapping(struct extent_map_tree *em_tree,
2640 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002641 struct extent_map *em,
2642 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002643{
2644 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002645
Chris Masone6dcd2d2008-07-17 12:53:50 -04002646 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2647 start_diff = map_start - em->start;
2648 em->start = map_start;
2649 em->len = map_len;
2650 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2651 em->block_start += start_diff;
2652 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002653}
2654
Chris Masona52d9a82007-08-27 16:49:44 -04002655struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002656 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002657 int create)
2658{
2659 int ret;
2660 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002661 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002662 u64 extent_start = 0;
2663 u64 extent_end = 0;
2664 u64 objectid = inode->i_ino;
2665 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002666 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002667 struct btrfs_root *root = BTRFS_I(inode)->root;
2668 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002669 struct extent_buffer *leaf;
2670 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002671 struct extent_map *em = NULL;
2672 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002673 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002674 struct btrfs_trans_handle *trans = NULL;
2675
Chris Masona52d9a82007-08-27 16:49:44 -04002676again:
Chris Masond1310b22008-01-24 16:13:08 -05002677 spin_lock(&em_tree->lock);
2678 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002679 if (em)
2680 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002681 spin_unlock(&em_tree->lock);
2682
Chris Masona52d9a82007-08-27 16:49:44 -04002683 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002684 if (em->start > start || em->start + em->len <= start)
2685 free_extent_map(em);
2686 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002687 free_extent_map(em);
2688 else
2689 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002690 }
Chris Masond1310b22008-01-24 16:13:08 -05002691 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002692 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002693 err = -ENOMEM;
2694 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002695 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002696 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002697 em->start = EXTENT_MAP_HOLE;
2698 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002699
2700 if (!path) {
2701 path = btrfs_alloc_path();
2702 BUG_ON(!path);
2703 }
2704
Chris Mason179e29e2007-11-01 11:28:41 -04002705 ret = btrfs_lookup_file_extent(trans, root, path,
2706 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002707 if (ret < 0) {
2708 err = ret;
2709 goto out;
2710 }
2711
2712 if (ret != 0) {
2713 if (path->slots[0] == 0)
2714 goto not_found;
2715 path->slots[0]--;
2716 }
2717
Chris Mason5f39d392007-10-15 16:14:19 -04002718 leaf = path->nodes[0];
2719 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002720 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002721 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002722 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2723 found_type = btrfs_key_type(&found_key);
2724 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002725 found_type != BTRFS_EXTENT_DATA_KEY) {
2726 goto not_found;
2727 }
2728
Chris Mason5f39d392007-10-15 16:14:19 -04002729 found_type = btrfs_file_extent_type(leaf, item);
2730 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002731 if (found_type == BTRFS_FILE_EXTENT_REG) {
2732 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002733 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002734 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002735 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002736 em->start = start;
2737 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002738 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002739 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002740 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002741 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002742 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002743 }
2744 goto not_found_em;
2745 }
Chris Masondb945352007-10-15 16:15:53 -04002746 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2747 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002748 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002749 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002750 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002751 goto insert;
2752 }
Chris Masondb945352007-10-15 16:15:53 -04002753 bytenr += btrfs_file_extent_offset(leaf, item);
2754 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002755 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002756 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002757 goto insert;
2758 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002759 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002760 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002761 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002762 size_t size;
2763 size_t extent_offset;
2764 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002765
Chris Mason5f39d392007-10-15 16:14:19 -04002766 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2767 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002768 extent_end = (extent_start + size + root->sectorsize - 1) &
2769 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002770 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002771 em->start = start;
2772 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002773 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002774 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002775 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002776 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002777 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002778 }
2779 goto not_found_em;
2780 }
2781 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002782
2783 if (!page) {
2784 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002785 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002786 goto out;
2787 }
2788
Chris Mason70dec802008-01-29 09:59:12 -05002789 page_start = page_offset(page) + pg_offset;
2790 extent_offset = page_start - extent_start;
2791 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002792 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002793 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002794 em->len = (copy_size + root->sectorsize - 1) &
2795 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002796 map = kmap(page);
2797 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002798 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002799 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002800 copy_size);
2801 flush_dcache_page(page);
2802 } else if (create && PageUptodate(page)) {
2803 if (!trans) {
2804 kunmap(page);
2805 free_extent_map(em);
2806 em = NULL;
2807 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002808 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002809 goto again;
2810 }
Chris Mason70dec802008-01-29 09:59:12 -05002811 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002812 copy_size);
2813 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002814 }
Chris Masona52d9a82007-08-27 16:49:44 -04002815 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002816 set_extent_uptodate(io_tree, em->start,
2817 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002818 goto insert;
2819 } else {
2820 printk("unkknown found_type %d\n", found_type);
2821 WARN_ON(1);
2822 }
2823not_found:
2824 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002825 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002826not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002827 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002828insert:
2829 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002830 if (em->start > start || extent_map_end(em) <= start) {
2831 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002832 err = -EIO;
2833 goto out;
2834 }
Chris Masond1310b22008-01-24 16:13:08 -05002835
2836 err = 0;
2837 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002838 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002839 /* it is possible that someone inserted the extent into the tree
2840 * while we had the lock dropped. It is also possible that
2841 * an overlapping map exists in the tree
2842 */
Chris Masona52d9a82007-08-27 16:49:44 -04002843 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002844 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002845
2846 ret = 0;
2847
Chris Mason3b951512008-04-17 11:29:12 -04002848 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002849 if (existing && (existing->start > start ||
2850 existing->start + existing->len <= start)) {
2851 free_extent_map(existing);
2852 existing = NULL;
2853 }
Chris Mason3b951512008-04-17 11:29:12 -04002854 if (!existing) {
2855 existing = lookup_extent_mapping(em_tree, em->start,
2856 em->len);
2857 if (existing) {
2858 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002859 em, start,
2860 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002861 free_extent_map(existing);
2862 if (err) {
2863 free_extent_map(em);
2864 em = NULL;
2865 }
2866 } else {
2867 err = -EIO;
2868 printk("failing to insert %Lu %Lu\n",
2869 start, len);
2870 free_extent_map(em);
2871 em = NULL;
2872 }
2873 } else {
2874 free_extent_map(em);
2875 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002876 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002877 }
Chris Masona52d9a82007-08-27 16:49:44 -04002878 }
Chris Masond1310b22008-01-24 16:13:08 -05002879 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002880out:
Chris Masonf4219502008-07-22 11:18:09 -04002881 if (path)
2882 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002883 if (trans) {
2884 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002885 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002886 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002887 }
Chris Masona52d9a82007-08-27 16:49:44 -04002888 }
Chris Masona52d9a82007-08-27 16:49:44 -04002889 if (err) {
2890 free_extent_map(em);
2891 WARN_ON(1);
2892 return ERR_PTR(err);
2893 }
2894 return em;
2895}
2896
Chris Masone1c4b742008-04-22 13:26:46 -04002897#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002898static int btrfs_get_block(struct inode *inode, sector_t iblock,
2899 struct buffer_head *bh_result, int create)
2900{
2901 struct extent_map *em;
2902 u64 start = (u64)iblock << inode->i_blkbits;
2903 struct btrfs_multi_bio *multi = NULL;
2904 struct btrfs_root *root = BTRFS_I(inode)->root;
2905 u64 len;
2906 u64 logical;
2907 u64 map_length;
2908 int ret = 0;
2909
2910 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2911
2912 if (!em || IS_ERR(em))
2913 goto out;
2914
Chris Masone1c4b742008-04-22 13:26:46 -04002915 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002916 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002917 }
Chris Mason16432982008-04-10 10:23:21 -04002918
2919 if (em->block_start == EXTENT_MAP_INLINE) {
2920 ret = -EINVAL;
2921 goto out;
2922 }
2923
Chris Mason16432982008-04-10 10:23:21 -04002924 len = em->start + em->len - start;
2925 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2926
Chris Masone1c4b742008-04-22 13:26:46 -04002927 if (em->block_start == EXTENT_MAP_HOLE ||
2928 em->block_start == EXTENT_MAP_DELALLOC) {
2929 bh_result->b_size = len;
2930 goto out;
2931 }
2932
Chris Mason16432982008-04-10 10:23:21 -04002933 logical = start - em->start;
2934 logical = em->block_start + logical;
2935
2936 map_length = len;
2937 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2938 logical, &map_length, &multi, 0);
2939 BUG_ON(ret);
2940 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2941 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002942
Chris Mason16432982008-04-10 10:23:21 -04002943 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2944 set_buffer_mapped(bh_result);
2945 kfree(multi);
2946out:
2947 free_extent_map(em);
2948 return ret;
2949}
Chris Masone1c4b742008-04-22 13:26:46 -04002950#endif
Chris Mason16432982008-04-10 10:23:21 -04002951
2952static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2953 const struct iovec *iov, loff_t offset,
2954 unsigned long nr_segs)
2955{
Chris Masone1c4b742008-04-22 13:26:46 -04002956 return -EINVAL;
2957#if 0
Chris Mason16432982008-04-10 10:23:21 -04002958 struct file *file = iocb->ki_filp;
2959 struct inode *inode = file->f_mapping->host;
2960
2961 if (rw == WRITE)
2962 return -EINVAL;
2963
2964 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2965 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002966#endif
Chris Mason16432982008-04-10 10:23:21 -04002967}
2968
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002969static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002970{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002971 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002972}
2973
Chris Mason9ebefb182007-06-15 13:50:00 -04002974int btrfs_readpage(struct file *file, struct page *page)
2975{
Chris Masond1310b22008-01-24 16:13:08 -05002976 struct extent_io_tree *tree;
2977 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002978 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002979}
Chris Mason1832a6d2007-12-21 16:27:21 -05002980
Chris Mason39279cc2007-06-12 06:35:45 -04002981static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2982{
Chris Masond1310b22008-01-24 16:13:08 -05002983 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002984
2985
2986 if (current->flags & PF_MEMALLOC) {
2987 redirty_page_for_writepage(wbc, page);
2988 unlock_page(page);
2989 return 0;
2990 }
Chris Masond1310b22008-01-24 16:13:08 -05002991 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002992 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2993}
Chris Mason39279cc2007-06-12 06:35:45 -04002994
Chris Masonf4219502008-07-22 11:18:09 -04002995int btrfs_writepages(struct address_space *mapping,
2996 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04002997{
Chris Masond1310b22008-01-24 16:13:08 -05002998 struct extent_io_tree *tree;
2999 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04003000 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3001}
3002
Chris Mason3ab2fb52007-11-08 10:59:22 -05003003static int
3004btrfs_readpages(struct file *file, struct address_space *mapping,
3005 struct list_head *pages, unsigned nr_pages)
3006{
Chris Masond1310b22008-01-24 16:13:08 -05003007 struct extent_io_tree *tree;
3008 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05003009 return extent_readpages(tree, mapping, pages, nr_pages,
3010 btrfs_get_extent);
3011}
Chris Masone6dcd2d2008-07-17 12:53:50 -04003012static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04003013{
Chris Masond1310b22008-01-24 16:13:08 -05003014 struct extent_io_tree *tree;
3015 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04003016 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003017
Chris Masond1310b22008-01-24 16:13:08 -05003018 tree = &BTRFS_I(page->mapping->host)->io_tree;
3019 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05003020 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003021 if (ret == 1) {
3022 ClearPagePrivate(page);
3023 set_page_private(page, 0);
3024 page_cache_release(page);
3025 }
3026 return ret;
3027}
Chris Mason39279cc2007-06-12 06:35:45 -04003028
Chris Masone6dcd2d2008-07-17 12:53:50 -04003029static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3030{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003031 return __btrfs_releasepage(page, gfp_flags);
3032}
3033
Chris Masona52d9a82007-08-27 16:49:44 -04003034static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3035{
Chris Masond1310b22008-01-24 16:13:08 -05003036 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003037 struct btrfs_ordered_extent *ordered;
3038 u64 page_start = page_offset(page);
3039 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003040
Chris Masone6dcd2d2008-07-17 12:53:50 -04003041 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003042 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003043 if (offset) {
3044 btrfs_releasepage(page, GFP_NOFS);
3045 return;
3046 }
3047
3048 lock_extent(tree, page_start, page_end, GFP_NOFS);
3049 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3050 page_offset(page));
3051 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003052 /*
3053 * IO on this page will never be started, so we need
3054 * to account for any ordered extents now
3055 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003056 clear_extent_bit(tree, page_start, page_end,
3057 EXTENT_DIRTY | EXTENT_DELALLOC |
3058 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003059 btrfs_finish_ordered_io(page->mapping->host,
3060 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003061 btrfs_put_ordered_extent(ordered);
3062 lock_extent(tree, page_start, page_end, GFP_NOFS);
3063 }
3064 clear_extent_bit(tree, page_start, page_end,
3065 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3066 EXTENT_ORDERED,
3067 1, 1, GFP_NOFS);
3068 __btrfs_releasepage(page, GFP_NOFS);
3069
Chris Mason4a096752008-07-21 10:29:44 -04003070 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003071 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003072 ClearPagePrivate(page);
3073 set_page_private(page, 0);
3074 page_cache_release(page);
3075 }
Chris Mason39279cc2007-06-12 06:35:45 -04003076}
3077
Chris Mason9ebefb182007-06-15 13:50:00 -04003078/*
3079 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3080 * called from a page fault handler when a page is first dirtied. Hence we must
3081 * be careful to check for EOF conditions here. We set the page up correctly
3082 * for a written page which means we get ENOSPC checking when writing into
3083 * holes and correct delalloc and unwritten extent mapping on filesystems that
3084 * support these features.
3085 *
3086 * We are not allowed to take the i_mutex here so we have to play games to
3087 * protect against truncate races as the page could now be beyond EOF. Because
3088 * vmtruncate() writes the inode size before removing pages, once we have the
3089 * page lock we can determine safely if the page is beyond EOF. If it is not
3090 * beyond EOF, then the page is guaranteed safe against truncation until we
3091 * unlock the page.
3092 */
3093int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3094{
Chris Mason6da6aba2007-12-18 16:15:09 -05003095 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003096 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003097 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3098 struct btrfs_ordered_extent *ordered;
3099 char *kaddr;
3100 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003101 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003102 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003103 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003104 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003105
Chris Mason1832a6d2007-12-21 16:27:21 -05003106 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003107 if (ret)
3108 goto out;
3109
3110 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003111again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003112 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003113 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003114 page_start = page_offset(page);
3115 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003116
Chris Mason9ebefb182007-06-15 13:50:00 -04003117 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003118 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003119 /* page got truncated out from underneath us */
3120 goto out_unlock;
3121 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003122 wait_on_page_writeback(page);
3123
3124 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3125 set_page_extent_mapped(page);
3126
Chris Masoneb84ae02008-07-17 13:53:27 -04003127 /*
3128 * we can't set the delalloc bits if there are pending ordered
3129 * extents. Drop our locks and wait for them to finish
3130 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003131 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3132 if (ordered) {
3133 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3134 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003135 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003136 btrfs_put_ordered_extent(ordered);
3137 goto again;
3138 }
3139
Chris Masonea8c2812008-08-04 23:17:27 -04003140 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003141 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003142
3143 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003144 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003145 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003146 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003147 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003148
Chris Masone6dcd2d2008-07-17 12:53:50 -04003149 if (zero_start != PAGE_CACHE_SIZE) {
3150 kaddr = kmap(page);
3151 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3152 flush_dcache_page(page);
3153 kunmap(page);
3154 }
Chris Mason247e7432008-07-17 12:53:51 -04003155 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003156 set_page_dirty(page);
3157 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003158
3159out_unlock:
3160 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003161out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003162 return ret;
3163}
3164
Chris Mason39279cc2007-06-12 06:35:45 -04003165static void btrfs_truncate(struct inode *inode)
3166{
3167 struct btrfs_root *root = BTRFS_I(inode)->root;
3168 int ret;
3169 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003170 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003171 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003172
3173 if (!S_ISREG(inode->i_mode))
3174 return;
3175 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3176 return;
3177
3178 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003179 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003180
Chris Mason39279cc2007-06-12 06:35:45 -04003181 trans = btrfs_start_transaction(root, 1);
3182 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003183 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003184
Josef Bacik7b128762008-07-24 12:17:14 -04003185 ret = btrfs_orphan_add(trans, inode);
3186 if (ret)
3187 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003188 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05003189 ret = btrfs_truncate_in_trans(trans, root, inode,
3190 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003191 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003192
Josef Bacik7b128762008-07-24 12:17:14 -04003193 ret = btrfs_orphan_del(trans, inode);
3194 BUG_ON(ret);
3195
3196out:
3197 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003198 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003199 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003200 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003201}
3202
Sven Wegener3b963622008-06-09 21:57:42 -04003203/*
3204 * Invalidate a single dcache entry at the root of the filesystem.
3205 * Needed after creation of snapshot or subvolume.
3206 */
3207void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3208 int namelen)
3209{
3210 struct dentry *alias, *entry;
3211 struct qstr qstr;
3212
3213 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3214 if (alias) {
3215 qstr.name = name;
3216 qstr.len = namelen;
3217 /* change me if btrfs ever gets a d_hash operation */
3218 qstr.hash = full_name_hash(qstr.name, qstr.len);
3219 entry = d_lookup(alias, &qstr);
3220 dput(alias);
3221 if (entry) {
3222 d_invalidate(entry);
3223 dput(entry);
3224 }
3225 }
3226}
3227
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003228int btrfs_create_subvol_root(struct btrfs_root *new_root,
3229 struct btrfs_trans_handle *trans, u64 new_dirid,
3230 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003231{
Chris Mason39279cc2007-06-12 06:35:45 -04003232 struct inode *inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003233 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003234
Josef Bacikaec74772008-07-24 12:12:38 -04003235 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003236 new_dirid, block_group, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04003237 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003238 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003239 inode->i_op = &btrfs_dir_inode_operations;
3240 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003241 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003242
Chris Mason39279cc2007-06-12 06:35:45 -04003243 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003244 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003245
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003246 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003247}
3248
Chris Masonedbd8d42007-12-21 16:27:24 -05003249unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003250 struct file_ra_state *ra, struct file *file,
3251 pgoff_t offset, pgoff_t last_index)
3252{
Chris Mason8e7bf942008-04-28 09:02:36 -04003253 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003254
3255#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003256 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3257 return offset;
3258#else
Chris Mason86479a02007-09-10 19:58:16 -04003259 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3260 return offset + req_size;
3261#endif
3262}
3263
Chris Mason39279cc2007-06-12 06:35:45 -04003264struct inode *btrfs_alloc_inode(struct super_block *sb)
3265{
3266 struct btrfs_inode *ei;
3267
3268 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3269 if (!ei)
3270 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003271 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003272 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003273 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3274 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003275 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003276 return &ei->vfs_inode;
3277}
3278
3279void btrfs_destroy_inode(struct inode *inode)
3280{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003281 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003282 WARN_ON(!list_empty(&inode->i_dentry));
3283 WARN_ON(inode->i_data.nrpages);
3284
Josef Bacik33268ea2008-07-24 12:16:36 -04003285 if (BTRFS_I(inode)->i_acl &&
3286 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3287 posix_acl_release(BTRFS_I(inode)->i_acl);
3288 if (BTRFS_I(inode)->i_default_acl &&
3289 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3290 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3291
Yanbcc63ab2008-07-30 16:29:20 -04003292 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003293 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3294 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3295 " list\n", inode->i_ino);
3296 dump_stack();
3297 }
Yanbcc63ab2008-07-30 16:29:20 -04003298 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003299
Chris Masone6dcd2d2008-07-17 12:53:50 -04003300 while(1) {
3301 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3302 if (!ordered)
3303 break;
3304 else {
3305 printk("found ordered extent %Lu %Lu\n",
3306 ordered->file_offset, ordered->len);
3307 btrfs_remove_ordered_extent(inode, ordered);
3308 btrfs_put_ordered_extent(ordered);
3309 btrfs_put_ordered_extent(ordered);
3310 }
3311 }
Chris Mason8c416c92008-01-14 15:10:26 -05003312 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003313 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3314}
3315
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003316#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3317static void init_once(void *foo)
3318#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003319static void init_once(struct kmem_cache * cachep, void *foo)
3320#else
Chris Mason39279cc2007-06-12 06:35:45 -04003321static void init_once(void * foo, struct kmem_cache * cachep,
3322 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003323#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003324{
3325 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3326
3327 inode_init_once(&ei->vfs_inode);
3328}
3329
3330void btrfs_destroy_cachep(void)
3331{
3332 if (btrfs_inode_cachep)
3333 kmem_cache_destroy(btrfs_inode_cachep);
3334 if (btrfs_trans_handle_cachep)
3335 kmem_cache_destroy(btrfs_trans_handle_cachep);
3336 if (btrfs_transaction_cachep)
3337 kmem_cache_destroy(btrfs_transaction_cachep);
3338 if (btrfs_bit_radix_cachep)
3339 kmem_cache_destroy(btrfs_bit_radix_cachep);
3340 if (btrfs_path_cachep)
3341 kmem_cache_destroy(btrfs_path_cachep);
3342}
3343
Chris Mason86479a02007-09-10 19:58:16 -04003344struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003345 unsigned long extra_flags,
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003346#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3347 void (*ctor)(void *)
3348#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003349 void (*ctor)(struct kmem_cache *, void *)
3350#else
Chris Mason92fee662007-07-25 12:31:35 -04003351 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003352 unsigned long)
3353#endif
3354 )
Chris Mason92fee662007-07-25 12:31:35 -04003355{
3356 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3357 SLAB_MEM_SPREAD | extra_flags), ctor
3358#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3359 ,NULL
3360#endif
3361 );
3362}
3363
Chris Mason39279cc2007-06-12 06:35:45 -04003364int btrfs_init_cachep(void)
3365{
Chris Mason86479a02007-09-10 19:58:16 -04003366 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003367 sizeof(struct btrfs_inode),
3368 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003369 if (!btrfs_inode_cachep)
3370 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003371 btrfs_trans_handle_cachep =
3372 btrfs_cache_create("btrfs_trans_handle_cache",
3373 sizeof(struct btrfs_trans_handle),
3374 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003375 if (!btrfs_trans_handle_cachep)
3376 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003377 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003378 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003379 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003380 if (!btrfs_transaction_cachep)
3381 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003382 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003383 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003384 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003385 if (!btrfs_path_cachep)
3386 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003387 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003388 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003389 if (!btrfs_bit_radix_cachep)
3390 goto fail;
3391 return 0;
3392fail:
3393 btrfs_destroy_cachep();
3394 return -ENOMEM;
3395}
3396
3397static int btrfs_getattr(struct vfsmount *mnt,
3398 struct dentry *dentry, struct kstat *stat)
3399{
3400 struct inode *inode = dentry->d_inode;
3401 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003402 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003403 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003404 return 0;
3405}
3406
3407static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3408 struct inode * new_dir,struct dentry *new_dentry)
3409{
3410 struct btrfs_trans_handle *trans;
3411 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3412 struct inode *new_inode = new_dentry->d_inode;
3413 struct inode *old_inode = old_dentry->d_inode;
3414 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003415 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003416 int ret;
3417
3418 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3419 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3420 return -ENOTEMPTY;
3421 }
Chris Mason5f39d392007-10-15 16:14:19 -04003422
Chris Mason1832a6d2007-12-21 16:27:21 -05003423 ret = btrfs_check_free_space(root, 1, 0);
3424 if (ret)
3425 goto out_unlock;
3426
Chris Mason39279cc2007-06-12 06:35:45 -04003427 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003428
Chris Mason39279cc2007-06-12 06:35:45 -04003429 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003430
3431 old_dentry->d_inode->i_nlink++;
3432 old_dir->i_ctime = old_dir->i_mtime = ctime;
3433 new_dir->i_ctime = new_dir->i_mtime = ctime;
3434 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003435
Chris Mason39279cc2007-06-12 06:35:45 -04003436 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3437 if (ret)
3438 goto out_fail;
3439
3440 if (new_inode) {
3441 new_inode->i_ctime = CURRENT_TIME;
3442 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3443 if (ret)
3444 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003445 if (new_inode->i_nlink == 0) {
3446 ret = btrfs_orphan_add(trans, new_inode);
3447 if (ret)
3448 goto out_fail;
3449 }
Chris Mason39279cc2007-06-12 06:35:45 -04003450 }
Chris Mason00e4e6b2008-08-05 11:18:09 -04003451 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003452 if (ret)
3453 goto out_fail;
3454
Chris Mason00e4e6b2008-08-05 11:18:09 -04003455 ret = btrfs_add_link(trans, new_dentry, old_inode, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003456 if (ret)
3457 goto out_fail;
3458
3459out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003460 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003461out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003462 return ret;
3463}
3464
Chris Masonea8c2812008-08-04 23:17:27 -04003465int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3466{
3467 struct list_head *head = &root->fs_info->delalloc_inodes;
3468 struct btrfs_inode *binode;
3469 unsigned long flags;
3470
3471 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3472 while(!list_empty(head)) {
3473 binode = list_entry(head->next, struct btrfs_inode,
3474 delalloc_inodes);
3475 atomic_inc(&binode->vfs_inode.i_count);
3476 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3477 filemap_write_and_wait(binode->vfs_inode.i_mapping);
3478 iput(&binode->vfs_inode);
3479 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3480 }
3481 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3482 return 0;
3483}
3484
Chris Mason39279cc2007-06-12 06:35:45 -04003485static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3486 const char *symname)
3487{
3488 struct btrfs_trans_handle *trans;
3489 struct btrfs_root *root = BTRFS_I(dir)->root;
3490 struct btrfs_path *path;
3491 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003492 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003493 int err;
3494 int drop_inode = 0;
3495 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003496 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04003497 int name_len;
3498 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003499 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003500 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003501 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003502 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003503
3504 name_len = strlen(symname) + 1;
3505 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3506 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003507
Chris Mason1832a6d2007-12-21 16:27:21 -05003508 err = btrfs_check_free_space(root, 1, 0);
3509 if (err)
3510 goto out_fail;
3511
Chris Mason39279cc2007-06-12 06:35:45 -04003512 trans = btrfs_start_transaction(root, 1);
3513 btrfs_set_trans_block_group(trans, dir);
3514
3515 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3516 if (err) {
3517 err = -ENOSPC;
3518 goto out_unlock;
3519 }
3520
Josef Bacikaec74772008-07-24 12:12:38 -04003521 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003522 dentry->d_name.len,
3523 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003524 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3525 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003526 err = PTR_ERR(inode);
3527 if (IS_ERR(inode))
3528 goto out_unlock;
3529
Josef Bacik33268ea2008-07-24 12:16:36 -04003530 err = btrfs_init_acl(inode, dir);
3531 if (err) {
3532 drop_inode = 1;
3533 goto out_unlock;
3534 }
3535
Chris Mason39279cc2007-06-12 06:35:45 -04003536 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003537 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003538 if (err)
3539 drop_inode = 1;
3540 else {
3541 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003542 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003543 inode->i_fop = &btrfs_file_operations;
3544 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003545 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3546 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003547 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003548 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3549 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003550 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04003551 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003552 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003553 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003554 BTRFS_I(inode)->disk_i_size = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04003555 inode->i_mapping->writeback_index = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003556 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003557 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003558 }
3559 dir->i_sb->s_dirt = 1;
3560 btrfs_update_inode_block_group(trans, inode);
3561 btrfs_update_inode_block_group(trans, dir);
3562 if (drop_inode)
3563 goto out_unlock;
3564
3565 path = btrfs_alloc_path();
3566 BUG_ON(!path);
3567 key.objectid = inode->i_ino;
3568 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003569 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3570 datasize = btrfs_file_extent_calc_inline_size(name_len);
3571 err = btrfs_insert_empty_item(trans, root, path, &key,
3572 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003573 if (err) {
3574 drop_inode = 1;
3575 goto out_unlock;
3576 }
Chris Mason5f39d392007-10-15 16:14:19 -04003577 leaf = path->nodes[0];
3578 ei = btrfs_item_ptr(leaf, path->slots[0],
3579 struct btrfs_file_extent_item);
3580 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3581 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003582 BTRFS_FILE_EXTENT_INLINE);
3583 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003584 write_extent_buffer(leaf, symname, ptr, name_len);
3585 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003586 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003587
Chris Mason39279cc2007-06-12 06:35:45 -04003588 inode->i_op = &btrfs_symlink_inode_operations;
3589 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003590 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003591 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003592 err = btrfs_update_inode(trans, root, inode);
3593 if (err)
3594 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003595
3596out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003597 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003598 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003599out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003600 if (drop_inode) {
3601 inode_dec_link_count(inode);
3602 iput(inode);
3603 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003604 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003605 return err;
3606}
Chris Mason16432982008-04-10 10:23:21 -04003607
Chris Masone6dcd2d2008-07-17 12:53:50 -04003608static int btrfs_set_page_dirty(struct page *page)
3609{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003610 return __set_page_dirty_nobuffers(page);
3611}
3612
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003613#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3614static int btrfs_permission(struct inode *inode, int mask)
3615#else
Yanfdebe2b2008-01-14 13:26:08 -05003616static int btrfs_permission(struct inode *inode, int mask,
3617 struct nameidata *nd)
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003618#endif
Yanfdebe2b2008-01-14 13:26:08 -05003619{
3620 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3621 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003622 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003623}
Chris Mason39279cc2007-06-12 06:35:45 -04003624
3625static struct inode_operations btrfs_dir_inode_operations = {
3626 .lookup = btrfs_lookup,
3627 .create = btrfs_create,
3628 .unlink = btrfs_unlink,
3629 .link = btrfs_link,
3630 .mkdir = btrfs_mkdir,
3631 .rmdir = btrfs_rmdir,
3632 .rename = btrfs_rename,
3633 .symlink = btrfs_symlink,
3634 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003635 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003636 .setxattr = generic_setxattr,
3637 .getxattr = generic_getxattr,
3638 .listxattr = btrfs_listxattr,
3639 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003640 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003641};
Chris Mason39279cc2007-06-12 06:35:45 -04003642static struct inode_operations btrfs_dir_ro_inode_operations = {
3643 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003644 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003645};
Chris Mason39279cc2007-06-12 06:35:45 -04003646static struct file_operations btrfs_dir_file_operations = {
3647 .llseek = generic_file_llseek,
3648 .read = generic_read_dir,
3649 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003650 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003651#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003652 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003653#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003654 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003655};
3656
Chris Masond1310b22008-01-24 16:13:08 -05003657static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003658 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003659 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003660 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason3de9d6b2008-08-04 23:17:27 -04003661 .readpage_io_hook = btrfs_readpage_io_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003662 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003663 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003664 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003665 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003666 .set_bit_hook = btrfs_set_bit_hook,
3667 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003668};
3669
Chris Mason39279cc2007-06-12 06:35:45 -04003670static struct address_space_operations btrfs_aops = {
3671 .readpage = btrfs_readpage,
3672 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003673 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003674 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003675 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003676 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003677 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003678 .invalidatepage = btrfs_invalidatepage,
3679 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003680 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003681};
3682
3683static struct address_space_operations btrfs_symlink_aops = {
3684 .readpage = btrfs_readpage,
3685 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003686 .invalidatepage = btrfs_invalidatepage,
3687 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003688};
3689
3690static struct inode_operations btrfs_file_inode_operations = {
3691 .truncate = btrfs_truncate,
3692 .getattr = btrfs_getattr,
3693 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003694 .setxattr = generic_setxattr,
3695 .getxattr = generic_getxattr,
3696 .listxattr = btrfs_listxattr,
3697 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003698 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003699};
Josef Bacik618e21d2007-07-11 10:18:17 -04003700static struct inode_operations btrfs_special_inode_operations = {
3701 .getattr = btrfs_getattr,
3702 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003703 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -04003704 .setxattr = generic_setxattr,
3705 .getxattr = generic_getxattr,
3706 .listxattr = btrfs_listxattr,
3707 .removexattr = generic_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003708};
Chris Mason39279cc2007-06-12 06:35:45 -04003709static struct inode_operations btrfs_symlink_inode_operations = {
3710 .readlink = generic_readlink,
3711 .follow_link = page_follow_link_light,
3712 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003713 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003714};