blob: 65107894a5bbd6181fb7c00ba335850d84d28516 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040037#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050038#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040039#include <linux/posix_acl.h>
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040046#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040047#include "ordered-data.h"
Chris Mason39279cc2007-06-12 06:35:45 -040048
49struct btrfs_iget_args {
50 u64 ino;
51 struct btrfs_root *root;
52};
53
54static struct inode_operations btrfs_dir_inode_operations;
55static struct inode_operations btrfs_symlink_inode_operations;
56static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040057static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040058static struct inode_operations btrfs_file_inode_operations;
59static struct address_space_operations btrfs_aops;
60static struct address_space_operations btrfs_symlink_aops;
61static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050062static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040063
64static struct kmem_cache *btrfs_inode_cachep;
65struct kmem_cache *btrfs_trans_handle_cachep;
66struct kmem_cache *btrfs_transaction_cachep;
67struct kmem_cache *btrfs_bit_radix_cachep;
68struct kmem_cache *btrfs_path_cachep;
69
70#define S_SHIFT 12
71static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
72 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
73 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
74 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
75 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
76 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
77 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
78 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
79};
80
Josef Bacik7b128762008-07-24 12:17:14 -040081static void btrfs_truncate(struct inode *inode);
82
Chris Mason1832a6d2007-12-21 16:27:21 -050083int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
84 int for_del)
85{
Chris Masona2135012008-06-25 16:01:30 -040086 u64 total;
87 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050088 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040089 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050090 int ret = 0;
91
Chris Masona2135012008-06-25 16:01:30 -040092 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
93 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
94 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050095 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050096 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050097 else
Chris Masonf9ef6602008-01-03 09:22:38 -050098 thresh = total * 85;
99
100 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500101
Chris Mason1832a6d2007-12-21 16:27:21 -0500102 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
103 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400104 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500105 return ret;
106}
107
Chris Masonbe20aa92007-12-17 20:14:01 -0500108static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400109{
110 struct btrfs_root *root = BTRFS_I(inode)->root;
111 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400112 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400113 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500114 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400115 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500116 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500117 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400118 struct extent_map *em;
119 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
120 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400121
Chris Masonf9295742008-07-17 12:54:14 -0400122 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400123 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500124 btrfs_set_trans_block_group(trans, inode);
125
Chris Masondb945352007-10-15 16:15:53 -0400126 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500127 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500128 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400129
Chris Mason179e29e2007-11-01 11:28:41 -0400130 if (alloc_hint == EXTENT_MAP_INLINE)
131 goto out;
132
Chris Mason3b951512008-04-17 11:29:12 -0400133 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone5a22172008-07-18 20:42:20 -0400134 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400135 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Masone5a22172008-07-18 20:42:20 -0400136 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400137
Chris Masonc59f8952007-12-17 20:14:04 -0500138 while(num_bytes > 0) {
139 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400140 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
141 root->sectorsize, 0, 0,
142 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500143 if (ret) {
144 WARN_ON(1);
145 goto out;
146 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400147 em = alloc_extent_map(GFP_NOFS);
148 em->start = start;
149 em->len = ins.offset;
150 em->block_start = ins.objectid;
151 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masone5a22172008-07-18 20:42:20 -0400152 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400153 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400154 while(1) {
155 spin_lock(&em_tree->lock);
156 ret = add_extent_mapping(em_tree, em);
157 spin_unlock(&em_tree->lock);
158 if (ret != -EEXIST) {
159 free_extent_map(em);
160 break;
161 }
162 btrfs_drop_extent_cache(inode, start,
163 start + ins.offset - 1);
164 }
Chris Masone5a22172008-07-18 20:42:20 -0400165 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400166
Chris Mason98d20f62008-04-14 09:46:10 -0400167 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400168 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Yan Zheng7ea394f2008-08-05 13:05:02 -0400169 ins.offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400170 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400171 if (num_bytes < cur_alloc_size) {
172 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
173 cur_alloc_size);
174 break;
175 }
Chris Masonc59f8952007-12-17 20:14:04 -0500176 num_bytes -= cur_alloc_size;
177 alloc_hint = ins.objectid + ins.offset;
178 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400179 }
Chris Masonb888db22007-08-27 16:49:44 -0400180out:
181 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500182 return ret;
183}
184
185static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
186{
187 u64 extent_start;
188 u64 extent_end;
189 u64 bytenr;
Chris Mason1832a6d2007-12-21 16:27:21 -0500190 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500191 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500192 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400193 struct btrfs_block_group_cache *block_group;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400194 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500195 struct extent_buffer *leaf;
196 int found_type;
197 struct btrfs_path *path;
198 struct btrfs_file_extent_item *item;
199 int ret;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400200 int err = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500201 struct btrfs_key found_key;
202
Chris Masonc31f8832008-01-08 15:46:31 -0500203 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500204 path = btrfs_alloc_path();
205 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400206 trans = btrfs_join_transaction(root, 1);
207 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500208again:
209 ret = btrfs_lookup_file_extent(NULL, root, path,
210 inode->i_ino, start, 0);
211 if (ret < 0) {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400212 err = ret;
213 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500214 }
215
Chris Masonbe20aa92007-12-17 20:14:01 -0500216 if (ret != 0) {
217 if (path->slots[0] == 0)
218 goto not_found;
219 path->slots[0]--;
220 }
221
222 leaf = path->nodes[0];
223 item = btrfs_item_ptr(leaf, path->slots[0],
224 struct btrfs_file_extent_item);
225
226 /* are we inside the extent that was found? */
227 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
228 found_type = btrfs_key_type(&found_key);
229 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400230 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500231 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500232
233 found_type = btrfs_file_extent_type(leaf, item);
234 extent_start = found_key.offset;
235 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500236 u64 extent_num_bytes;
237
238 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
239 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500240 err = 0;
241
Chris Mason1832a6d2007-12-21 16:27:21 -0500242 if (loops && start != extent_start)
243 goto not_found;
244
Chris Masonbe20aa92007-12-17 20:14:01 -0500245 if (start < extent_start || start >= extent_end)
246 goto not_found;
247
Chris Masonbe20aa92007-12-17 20:14:01 -0500248 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
249 if (bytenr == 0)
250 goto not_found;
251
Yan Zheng7ea394f2008-08-05 13:05:02 -0400252 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
Chris Masona68d5932008-05-08 14:11:56 -0400253 goto not_found;
Chris Masonc31f8832008-01-08 15:46:31 -0500254 /*
255 * we may be called by the resizer, make sure we're inside
256 * the limits of the FS
257 */
Chris Masona68d5932008-05-08 14:11:56 -0400258 block_group = btrfs_lookup_block_group(root->fs_info,
259 bytenr);
260 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500261 goto not_found;
262
Yan Zheng7ea394f2008-08-05 13:05:02 -0400263 bytenr += btrfs_file_extent_offset(leaf, item);
264 extent_num_bytes = min(end + 1, extent_end) - start;
265 ret = btrfs_add_ordered_extent(inode, start, bytenr,
266 extent_num_bytes, 1);
267 if (ret) {
268 err = ret;
269 goto out;
270 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500271
Yan Zheng7ea394f2008-08-05 13:05:02 -0400272 btrfs_release_path(root, path);
273 start = extent_end;
274 if (start <= end) {
275 loops++;
276 goto again;
277 }
278 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500279not_found:
Yan Zheng7ea394f2008-08-05 13:05:02 -0400280 btrfs_end_transaction(trans, root);
281 btrfs_free_path(path);
282 return cow_file_range(inode, start, end);
283 }
284out:
285 WARN_ON(err);
286 btrfs_end_transaction(trans, root);
287 btrfs_free_path(path);
288 return err;
Chris Masonbe20aa92007-12-17 20:14:01 -0500289}
290
291static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
292{
293 struct btrfs_root *root = BTRFS_I(inode)->root;
294 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400295
Yanb98b6762008-01-08 15:54:37 -0500296 if (btrfs_test_opt(root, NODATACOW) ||
297 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500298 ret = run_delalloc_nocow(inode, start, end);
299 else
300 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500301
Chris Masonb888db22007-08-27 16:49:44 -0400302 return ret;
303}
304
Chris Mason291d6732008-01-29 15:55:23 -0500305int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500306 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500307{
Chris Masonbcbfce82008-04-22 13:26:47 -0400308 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500309 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500310 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400311 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500312 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500313 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -0400314 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
315 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
316 &root->fs_info->delalloc_inodes);
317 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400318 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500319 }
320 return 0;
321}
322
323int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500324 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500325{
Chris Masonb0c68f82008-01-31 11:05:37 -0500326 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500327 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400328 unsigned long flags;
329
330 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500331 if (end - start + 1 > root->fs_info->delalloc_bytes) {
332 printk("warning: delalloc account %Lu %Lu\n",
333 end - start + 1, root->fs_info->delalloc_bytes);
334 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500335 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500336 } else {
337 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500338 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500339 }
Chris Masonea8c2812008-08-04 23:17:27 -0400340 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
341 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
342 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
343 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400344 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500345 }
346 return 0;
347}
348
Chris Mason239b14b2008-03-24 15:02:07 -0400349int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
350 size_t size, struct bio *bio)
351{
352 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
353 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400354 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400355 u64 length = 0;
356 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400357 int ret;
358
Chris Masonf2d8d742008-04-21 10:03:05 -0400359 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400360 map_tree = &root->fs_info->mapping_tree;
361 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400362 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400363 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400364
Chris Mason239b14b2008-03-24 15:02:07 -0400365 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400366 return 1;
367 }
368 return 0;
369}
370
Chris Mason44b8bd72008-04-16 11:14:51 -0400371int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400372 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500373{
Chris Mason065631f2008-02-20 12:07:25 -0500374 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500375 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400376
Chris Mason3edf7d32008-07-18 06:17:13 -0400377 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400378 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400379
Chris Mason8b712842008-06-11 16:50:36 -0400380 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400381}
382
383int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
384 int mirror_num)
385{
386 struct btrfs_root *root = BTRFS_I(inode)->root;
387 int ret = 0;
388
Chris Masone6dcd2d2008-07-17 12:53:50 -0400389 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
390 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500391
Chris Masone6dcd2d2008-07-17 12:53:50 -0400392 if (!(rw & (1 << BIO_RW))) {
Chris Mason0b86a832008-03-24 15:01:56 -0400393 goto mapit;
394 }
Chris Mason065631f2008-02-20 12:07:25 -0500395
Yan Zheng7ea394f2008-08-05 13:05:02 -0400396 if (btrfs_test_opt(root, NODATASUM) ||
397 btrfs_test_flag(inode, NODATASUM)) {
398 goto mapit;
399 }
400
Chris Mason44b8bd72008-04-16 11:14:51 -0400401 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
402 inode, rw, bio, mirror_num,
403 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400404mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400405 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500406}
Chris Mason6885f302008-02-20 16:11:05 -0500407
Chris Masonba1da2f2008-07-17 12:54:15 -0400408static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400409 struct inode *inode, u64 file_offset,
410 struct list_head *list)
411{
412 struct list_head *cur;
413 struct btrfs_ordered_sum *sum;
414
415 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400416 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400417 sum = list_entry(cur, struct btrfs_ordered_sum, list);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400418 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
419 inode, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400420 }
421 return 0;
422}
423
Chris Masonea8c2812008-08-04 23:17:27 -0400424int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
425{
426 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
427 GFP_NOFS);
428}
429
Chris Mason247e7432008-07-17 12:53:51 -0400430struct btrfs_writepage_fixup {
431 struct page *page;
432 struct btrfs_work work;
433};
434
435/* see btrfs_writepage_start_hook for details on why this is required */
436void btrfs_writepage_fixup_worker(struct btrfs_work *work)
437{
438 struct btrfs_writepage_fixup *fixup;
439 struct btrfs_ordered_extent *ordered;
440 struct page *page;
441 struct inode *inode;
442 u64 page_start;
443 u64 page_end;
444
445 fixup = container_of(work, struct btrfs_writepage_fixup, work);
446 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400447again:
Chris Mason247e7432008-07-17 12:53:51 -0400448 lock_page(page);
449 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
450 ClearPageChecked(page);
451 goto out_page;
452 }
453
454 inode = page->mapping->host;
455 page_start = page_offset(page);
456 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
457
458 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400459
460 /* already ordered? We're done */
461 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
462 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400463 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400464 }
465
466 ordered = btrfs_lookup_ordered_extent(inode, page_start);
467 if (ordered) {
468 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
469 page_end, GFP_NOFS);
470 unlock_page(page);
471 btrfs_start_ordered_extent(inode, ordered, 1);
472 goto again;
473 }
Chris Mason247e7432008-07-17 12:53:51 -0400474
Chris Masonea8c2812008-08-04 23:17:27 -0400475 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -0400476 ClearPageChecked(page);
477out:
478 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
479out_page:
480 unlock_page(page);
481 page_cache_release(page);
482}
483
484/*
485 * There are a few paths in the higher layers of the kernel that directly
486 * set the page dirty bit without asking the filesystem if it is a
487 * good idea. This causes problems because we want to make sure COW
488 * properly happens and the data=ordered rules are followed.
489 *
490 * In our case any range that doesn't have the EXTENT_ORDERED bit set
491 * hasn't been properly setup for IO. We kick off an async process
492 * to fix it up. The async helper will wait for ordered extents, set
493 * the delalloc bit and make it safe to write the page.
494 */
495int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
496{
497 struct inode *inode = page->mapping->host;
498 struct btrfs_writepage_fixup *fixup;
499 struct btrfs_root *root = BTRFS_I(inode)->root;
500 int ret;
501
502 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
503 EXTENT_ORDERED, 0);
504 if (ret)
505 return 0;
506
507 if (PageChecked(page))
508 return -EAGAIN;
509
510 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
511 if (!fixup)
512 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400513
Chris Mason247e7432008-07-17 12:53:51 -0400514 SetPageChecked(page);
515 page_cache_get(page);
516 fixup->work.func = btrfs_writepage_fixup_worker;
517 fixup->page = page;
518 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
519 return -EAGAIN;
520}
521
Chris Mason211f90e2008-07-18 11:56:15 -0400522static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400523{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400524 struct btrfs_root *root = BTRFS_I(inode)->root;
525 struct btrfs_trans_handle *trans;
526 struct btrfs_ordered_extent *ordered_extent;
527 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
528 u64 alloc_hint = 0;
529 struct list_head list;
530 struct btrfs_key ins;
531 int ret;
532
533 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400534 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400535 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400536
Chris Masonf9295742008-07-17 12:54:14 -0400537 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400538
539 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
540 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400541 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
542 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400543
544 lock_extent(io_tree, ordered_extent->file_offset,
545 ordered_extent->file_offset + ordered_extent->len - 1,
546 GFP_NOFS);
547
548 INIT_LIST_HEAD(&list);
549
550 ins.objectid = ordered_extent->start;
551 ins.offset = ordered_extent->len;
552 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400553
Chris Masone6dcd2d2008-07-17 12:53:50 -0400554 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
555 trans->transid, inode->i_ino,
556 ordered_extent->file_offset, &ins);
557 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400558
559 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400560
Chris Masone6dcd2d2008-07-17 12:53:50 -0400561 ret = btrfs_drop_extents(trans, root, inode,
562 ordered_extent->file_offset,
563 ordered_extent->file_offset +
564 ordered_extent->len,
565 ordered_extent->file_offset, &alloc_hint);
566 BUG_ON(ret);
567 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
568 ordered_extent->file_offset,
569 ordered_extent->start,
570 ordered_extent->len,
571 ordered_extent->len, 0);
572 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400573
Chris Masone6dcd2d2008-07-17 12:53:50 -0400574 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
575 ordered_extent->file_offset +
576 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400577 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
578
Chris Masone6dcd2d2008-07-17 12:53:50 -0400579 inode->i_blocks += ordered_extent->len >> 9;
580 unlock_extent(io_tree, ordered_extent->file_offset,
581 ordered_extent->file_offset + ordered_extent->len - 1,
582 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400583nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -0400584 add_pending_csums(trans, inode, ordered_extent->file_offset,
585 &ordered_extent->list);
586
Chris Masondbe674a2008-07-17 12:54:05 -0400587 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400588 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400589
Chris Masone6dcd2d2008-07-17 12:53:50 -0400590 /* once for us */
591 btrfs_put_ordered_extent(ordered_extent);
592 /* once for the tree */
593 btrfs_put_ordered_extent(ordered_extent);
594
595 btrfs_update_inode(trans, root, inode);
596 btrfs_end_transaction(trans, root);
597 return 0;
598}
599
Chris Mason211f90e2008-07-18 11:56:15 -0400600int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
601 struct extent_state *state, int uptodate)
602{
603 return btrfs_finish_ordered_io(page->mapping->host, start, end);
604}
605
Chris Mason3de9d6b2008-08-04 23:17:27 -0400606int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
607{
608 int ret = 0;
609 struct inode *inode = page->mapping->host;
610 struct btrfs_root *root = BTRFS_I(inode)->root;
611 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
612 struct btrfs_csum_item *item;
613 struct btrfs_path *path = NULL;
614 u32 csum;
615
616 if (btrfs_test_opt(root, NODATASUM) ||
617 btrfs_test_flag(inode, NODATASUM))
618 return 0;
619
620 /*
621 * It is possible there is an ordered extent that has
622 * not yet finished for this range in the file. If so,
623 * that extent will have a csum cached, and it will insert
624 * the sum after all the blocks in the extent are fully
625 * on disk. So, look for an ordered extent and use the
626 * sum if found. We have to do this before looking in the
627 * btree because csum items are pre-inserted based on
628 * the file size. btrfs_lookup_csum might find an item
629 * that still hasn't been fully filled.
630 */
631 ret = btrfs_find_ordered_sum(inode, start, &csum);
632 if (ret == 0)
633 goto found;
634
635 ret = 0;
636 path = btrfs_alloc_path();
637 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
638 if (IS_ERR(item)) {
639 ret = PTR_ERR(item);
640 /* a csum that isn't present is a preallocated region. */
641 if (ret == -ENOENT || ret == -EFBIG)
642 ret = 0;
643 csum = 0;
644 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
645 start);
646 goto out;
647 }
648 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
649 BTRFS_CRC32_SIZE);
650found:
651 set_state_private(io_tree, start, csum);
652out:
653 if (path)
654 btrfs_free_path(path);
655 return ret;
656}
657
Chris Mason7e383262008-04-09 16:28:12 -0400658struct io_failure_record {
659 struct page *page;
660 u64 start;
661 u64 len;
662 u64 logical;
663 int last_mirror;
664};
665
Chris Mason1259ab72008-05-12 13:39:03 -0400666int btrfs_io_failed_hook(struct bio *failed_bio,
667 struct page *page, u64 start, u64 end,
668 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400669{
670 struct io_failure_record *failrec = NULL;
671 u64 private;
672 struct extent_map *em;
673 struct inode *inode = page->mapping->host;
674 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400675 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400676 struct bio *bio;
677 int num_copies;
678 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400679 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400680 u64 logical;
681
682 ret = get_state_private(failure_tree, start, &private);
683 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400684 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
685 if (!failrec)
686 return -ENOMEM;
687 failrec->start = start;
688 failrec->len = end - start + 1;
689 failrec->last_mirror = 0;
690
Chris Mason3b951512008-04-17 11:29:12 -0400691 spin_lock(&em_tree->lock);
692 em = lookup_extent_mapping(em_tree, start, failrec->len);
693 if (em->start > start || em->start + em->len < start) {
694 free_extent_map(em);
695 em = NULL;
696 }
697 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400698
699 if (!em || IS_ERR(em)) {
700 kfree(failrec);
701 return -EIO;
702 }
703 logical = start - em->start;
704 logical = em->block_start + logical;
705 failrec->logical = logical;
706 free_extent_map(em);
707 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
708 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400709 set_state_private(failure_tree, start,
710 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400711 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400712 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400713 }
714 num_copies = btrfs_num_copies(
715 &BTRFS_I(inode)->root->fs_info->mapping_tree,
716 failrec->logical, failrec->len);
717 failrec->last_mirror++;
718 if (!state) {
719 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
720 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
721 failrec->start,
722 EXTENT_LOCKED);
723 if (state && state->start != failrec->start)
724 state = NULL;
725 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
726 }
727 if (!state || failrec->last_mirror > num_copies) {
728 set_state_private(failure_tree, failrec->start, 0);
729 clear_extent_bits(failure_tree, failrec->start,
730 failrec->start + failrec->len - 1,
731 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
732 kfree(failrec);
733 return -EIO;
734 }
735 bio = bio_alloc(GFP_NOFS, 1);
736 bio->bi_private = state;
737 bio->bi_end_io = failed_bio->bi_end_io;
738 bio->bi_sector = failrec->logical >> 9;
739 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400740 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400741 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400742 if (failed_bio->bi_rw & (1 << BIO_RW))
743 rw = WRITE;
744 else
745 rw = READ;
746
747 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
748 failrec->last_mirror);
749 return 0;
750}
751
752int btrfs_clean_io_failures(struct inode *inode, u64 start)
753{
754 u64 private;
755 u64 private_failure;
756 struct io_failure_record *failure;
757 int ret;
758
759 private = 0;
760 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
761 (u64)-1, 1, EXTENT_DIRTY)) {
762 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
763 start, &private_failure);
764 if (ret == 0) {
765 failure = (struct io_failure_record *)(unsigned long)
766 private_failure;
767 set_state_private(&BTRFS_I(inode)->io_failure_tree,
768 failure->start, 0);
769 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
770 failure->start,
771 failure->start + failure->len - 1,
772 EXTENT_DIRTY | EXTENT_LOCKED,
773 GFP_NOFS);
774 kfree(failure);
775 }
776 }
Chris Mason7e383262008-04-09 16:28:12 -0400777 return 0;
778}
779
Chris Mason70dec802008-01-29 09:59:12 -0500780int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
781 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400782{
Chris Mason35ebb932007-10-30 16:56:53 -0400783 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400784 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500785 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400786 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500787 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400788 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400789 struct btrfs_root *root = BTRFS_I(inode)->root;
790 u32 csum = ~(u32)0;
Jens Axboebbf0d0062007-10-19 09:23:07 -0400791 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500792
Yanb98b6762008-01-08 15:54:37 -0500793 if (btrfs_test_opt(root, NODATASUM) ||
794 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500795 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500796 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500797 private = state->private;
798 ret = 0;
799 } else {
800 ret = get_state_private(io_tree, start, &private);
801 }
Jens Axboebbf0d0062007-10-19 09:23:07 -0400802 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400803 kaddr = kmap_atomic(page, KM_IRQ0);
804 if (ret) {
805 goto zeroit;
806 }
Chris Masonff79f812007-10-15 16:22:25 -0400807 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
808 btrfs_csum_final(csum, (char *)&csum);
809 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400810 goto zeroit;
811 }
812 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -0400813 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400814
815 /* if the io failure tree for this inode is non-empty,
816 * check to see if we've recovered from a failed IO
817 */
Chris Mason1259ab72008-05-12 13:39:03 -0400818 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400819 return 0;
820
821zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500822 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
823 page->mapping->host->i_ino, (unsigned long long)start, csum,
824 private);
Chris Masondb945352007-10-15 16:15:53 -0400825 memset(kaddr + offset, 1, end - start + 1);
826 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400827 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -0400828 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400829 if (private == 0)
830 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400831 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400832}
Chris Masonb888db22007-08-27 16:49:44 -0400833
Josef Bacik7b128762008-07-24 12:17:14 -0400834/*
835 * This creates an orphan entry for the given inode in case something goes
836 * wrong in the middle of an unlink/truncate.
837 */
838int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
839{
840 struct btrfs_root *root = BTRFS_I(inode)->root;
841 int ret = 0;
842
Yanbcc63ab2008-07-30 16:29:20 -0400843 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400844
845 /* already on the orphan list, we're good */
846 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400847 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400848 return 0;
849 }
850
851 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
852
Yanbcc63ab2008-07-30 16:29:20 -0400853 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400854
855 /*
856 * insert an orphan item to track this unlinked/truncated file
857 */
858 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
859
860 return ret;
861}
862
863/*
864 * We have done the truncate/delete so we can go ahead and remove the orphan
865 * item for this particular inode.
866 */
867int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
868{
869 struct btrfs_root *root = BTRFS_I(inode)->root;
870 int ret = 0;
871
Yanbcc63ab2008-07-30 16:29:20 -0400872 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400873
874 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400875 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400876 return 0;
877 }
878
879 list_del_init(&BTRFS_I(inode)->i_orphan);
880 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400881 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400882 return 0;
883 }
884
Yanbcc63ab2008-07-30 16:29:20 -0400885 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400886
887 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
888
889 return ret;
890}
891
892/*
893 * this cleans up any orphans that may be left on the list from the last use
894 * of this root.
895 */
896void btrfs_orphan_cleanup(struct btrfs_root *root)
897{
898 struct btrfs_path *path;
899 struct extent_buffer *leaf;
900 struct btrfs_item *item;
901 struct btrfs_key key, found_key;
902 struct btrfs_trans_handle *trans;
903 struct inode *inode;
904 int ret = 0, nr_unlink = 0, nr_truncate = 0;
905
906 /* don't do orphan cleanup if the fs is readonly. */
907 if (root->inode->i_sb->s_flags & MS_RDONLY)
908 return;
909
910 path = btrfs_alloc_path();
911 if (!path)
912 return;
913 path->reada = -1;
914
915 key.objectid = BTRFS_ORPHAN_OBJECTID;
916 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
917 key.offset = (u64)-1;
918
919 trans = btrfs_start_transaction(root, 1);
920 btrfs_set_trans_block_group(trans, root->inode);
921
922 while (1) {
923 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
924 if (ret < 0) {
925 printk(KERN_ERR "Error searching slot for orphan: %d"
926 "\n", ret);
927 break;
928 }
929
930 /*
931 * if ret == 0 means we found what we were searching for, which
932 * is weird, but possible, so only screw with path if we didnt
933 * find the key and see if we have stuff that matches
934 */
935 if (ret > 0) {
936 if (path->slots[0] == 0)
937 break;
938 path->slots[0]--;
939 }
940
941 /* pull out the item */
942 leaf = path->nodes[0];
943 item = btrfs_item_nr(leaf, path->slots[0]);
944 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
945
946 /* make sure the item matches what we want */
947 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
948 break;
949 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
950 break;
951
952 /* release the path since we're done with it */
953 btrfs_release_path(root, path);
954
955 /*
956 * this is where we are basically btrfs_lookup, without the
957 * crossing root thing. we store the inode number in the
958 * offset of the orphan item.
959 */
960 inode = btrfs_iget_locked(root->inode->i_sb,
961 found_key.offset, root);
962 if (!inode)
963 break;
964
965 if (inode->i_state & I_NEW) {
966 BTRFS_I(inode)->root = root;
967
968 /* have to set the location manually */
969 BTRFS_I(inode)->location.objectid = inode->i_ino;
970 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
971 BTRFS_I(inode)->location.offset = 0;
972
973 btrfs_read_locked_inode(inode);
974 unlock_new_inode(inode);
975 }
976
977 /*
978 * add this inode to the orphan list so btrfs_orphan_del does
979 * the proper thing when we hit it
980 */
Yanbcc63ab2008-07-30 16:29:20 -0400981 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400982 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -0400983 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400984
985 /*
986 * if this is a bad inode, means we actually succeeded in
987 * removing the inode, but not the orphan record, which means
988 * we need to manually delete the orphan since iput will just
989 * do a destroy_inode
990 */
991 if (is_bad_inode(inode)) {
992 btrfs_orphan_del(trans, inode);
993 iput(inode);
994 continue;
995 }
996
997 /* if we have links, this was a truncate, lets do that */
998 if (inode->i_nlink) {
999 nr_truncate++;
1000 btrfs_truncate(inode);
1001 } else {
1002 nr_unlink++;
1003 }
1004
1005 /* this will do delete_inode and everything for us */
1006 iput(inode);
1007 }
1008
1009 if (nr_unlink)
1010 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1011 if (nr_truncate)
1012 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1013
1014 btrfs_free_path(path);
1015 btrfs_end_transaction(trans, root);
1016}
1017
Chris Mason39279cc2007-06-12 06:35:45 -04001018void btrfs_read_locked_inode(struct inode *inode)
1019{
1020 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001021 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001022 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001023 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001024 struct btrfs_root *root = BTRFS_I(inode)->root;
1025 struct btrfs_key location;
1026 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001027 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001028 int ret;
1029
1030 path = btrfs_alloc_path();
1031 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001032 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001033
Chris Mason39279cc2007-06-12 06:35:45 -04001034 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001035 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001036 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001037
Chris Mason5f39d392007-10-15 16:14:19 -04001038 leaf = path->nodes[0];
1039 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1040 struct btrfs_inode_item);
1041
1042 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1043 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1044 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1045 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001046 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001047
1048 tspec = btrfs_inode_atime(inode_item);
1049 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1050 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1051
1052 tspec = btrfs_inode_mtime(inode_item);
1053 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1054 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1055
1056 tspec = btrfs_inode_ctime(inode_item);
1057 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1058 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1059
1060 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1061 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -04001062 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001063 rdev = btrfs_inode_rdev(leaf, inode_item);
1064
Josef Bacikaec74772008-07-24 12:12:38 -04001065 BTRFS_I(inode)->index_cnt = (u64)-1;
1066
Chris Mason5f39d392007-10-15 16:14:19 -04001067 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001068 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1069 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001070 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001071 if (!BTRFS_I(inode)->block_group) {
1072 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001073 NULL, 0,
1074 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001075 }
Chris Mason39279cc2007-06-12 06:35:45 -04001076 btrfs_free_path(path);
1077 inode_item = NULL;
1078
Chris Mason39279cc2007-06-12 06:35:45 -04001079 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001080 case S_IFREG:
1081 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001082 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001083 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001084 inode->i_fop = &btrfs_file_operations;
1085 inode->i_op = &btrfs_file_inode_operations;
1086 break;
1087 case S_IFDIR:
1088 inode->i_fop = &btrfs_dir_file_operations;
1089 if (root == root->fs_info->tree_root)
1090 inode->i_op = &btrfs_dir_ro_inode_operations;
1091 else
1092 inode->i_op = &btrfs_dir_inode_operations;
1093 break;
1094 case S_IFLNK:
1095 inode->i_op = &btrfs_symlink_inode_operations;
1096 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001097 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001098 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001099 default:
1100 init_special_inode(inode, inode->i_mode, rdev);
1101 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001102 }
1103 return;
1104
1105make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001106 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001107 make_bad_inode(inode);
1108}
1109
Chris Mason5f39d392007-10-15 16:14:19 -04001110static void fill_inode_item(struct extent_buffer *leaf,
1111 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001112 struct inode *inode)
1113{
Chris Mason5f39d392007-10-15 16:14:19 -04001114 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1115 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001116 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001117 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1118 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1119
1120 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1121 inode->i_atime.tv_sec);
1122 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1123 inode->i_atime.tv_nsec);
1124
1125 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1126 inode->i_mtime.tv_sec);
1127 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1128 inode->i_mtime.tv_nsec);
1129
1130 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1131 inode->i_ctime.tv_sec);
1132 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1133 inode->i_ctime.tv_nsec);
1134
1135 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1136 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1137 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001138 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001139 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001140 BTRFS_I(inode)->block_group->key.objectid);
1141}
1142
Chris Masonba1da2f2008-07-17 12:54:15 -04001143int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001144 struct btrfs_root *root,
1145 struct inode *inode)
1146{
1147 struct btrfs_inode_item *inode_item;
1148 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001149 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001150 int ret;
1151
1152 path = btrfs_alloc_path();
1153 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001154 ret = btrfs_lookup_inode(trans, root, path,
1155 &BTRFS_I(inode)->location, 1);
1156 if (ret) {
1157 if (ret > 0)
1158 ret = -ENOENT;
1159 goto failed;
1160 }
1161
Chris Mason5f39d392007-10-15 16:14:19 -04001162 leaf = path->nodes[0];
1163 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001164 struct btrfs_inode_item);
1165
Chris Mason5f39d392007-10-15 16:14:19 -04001166 fill_inode_item(leaf, inode_item, inode);
1167 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001168 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001169 ret = 0;
1170failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001171 btrfs_free_path(path);
1172 return ret;
1173}
1174
1175
1176static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1177 struct btrfs_root *root,
1178 struct inode *dir,
1179 struct dentry *dentry)
1180{
1181 struct btrfs_path *path;
1182 const char *name = dentry->d_name.name;
1183 int name_len = dentry->d_name.len;
1184 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001185 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001186 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001187 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001188 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001189
1190 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001191 if (!path) {
1192 ret = -ENOMEM;
1193 goto err;
1194 }
1195
Chris Mason39279cc2007-06-12 06:35:45 -04001196 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1197 name, name_len, -1);
1198 if (IS_ERR(di)) {
1199 ret = PTR_ERR(di);
1200 goto err;
1201 }
1202 if (!di) {
1203 ret = -ENOENT;
1204 goto err;
1205 }
Chris Mason5f39d392007-10-15 16:14:19 -04001206 leaf = path->nodes[0];
1207 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001208 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001209 if (ret)
1210 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001211 btrfs_release_path(root, path);
1212
Josef Bacikaec74772008-07-24 12:12:38 -04001213 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1214 dentry->d_inode->i_ino,
1215 dentry->d_parent->d_inode->i_ino, &index);
1216 if (ret) {
1217 printk("failed to delete reference to %.*s, "
1218 "inode %lu parent %lu\n", name_len, name,
1219 dentry->d_inode->i_ino,
1220 dentry->d_parent->d_inode->i_ino);
1221 goto err;
1222 }
1223
Chris Mason39279cc2007-06-12 06:35:45 -04001224 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001225 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001226 if (IS_ERR(di)) {
1227 ret = PTR_ERR(di);
1228 goto err;
1229 }
1230 if (!di) {
1231 ret = -ENOENT;
1232 goto err;
1233 }
1234 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001235 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001236
1237 dentry->d_inode->i_ctime = dir->i_ctime;
1238err:
1239 btrfs_free_path(path);
1240 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001241 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001242 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001243 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001244#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1245 dentry->d_inode->i_nlink--;
1246#else
Chris Mason39279cc2007-06-12 06:35:45 -04001247 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001248#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001249 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001250 dir->i_sb->s_dirt = 1;
1251 }
1252 return ret;
1253}
1254
1255static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1256{
1257 struct btrfs_root *root;
1258 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001259 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001260 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001261 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001262
1263 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001264
1265 ret = btrfs_check_free_space(root, 1, 1);
1266 if (ret)
1267 goto fail;
1268
Chris Mason39279cc2007-06-12 06:35:45 -04001269 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001270
Chris Mason39279cc2007-06-12 06:35:45 -04001271 btrfs_set_trans_block_group(trans, dir);
1272 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Josef Bacik7b128762008-07-24 12:17:14 -04001273
1274 if (inode->i_nlink == 0)
1275 ret = btrfs_orphan_add(trans, inode);
1276
Chris Masond3c2fdc2007-09-17 10:58:06 -04001277 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001278
Chris Mason89ce8a62008-06-25 16:01:31 -04001279 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001280fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001281 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001282 return ret;
1283}
1284
1285static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1286{
1287 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001288 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001289 int ret;
1290 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001291 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001292 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001293
Chris Mason925baed2008-06-25 16:01:30 -04001294 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001295 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001296 }
Yan134d4512007-10-25 15:49:25 -04001297
Chris Mason1832a6d2007-12-21 16:27:21 -05001298 ret = btrfs_check_free_space(root, 1, 1);
1299 if (ret)
1300 goto fail;
1301
Chris Mason39279cc2007-06-12 06:35:45 -04001302 trans = btrfs_start_transaction(root, 1);
1303 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001304
Josef Bacik7b128762008-07-24 12:17:14 -04001305 err = btrfs_orphan_add(trans, inode);
1306 if (err)
1307 goto fail_trans;
1308
Chris Mason39279cc2007-06-12 06:35:45 -04001309 /* now the directory is empty */
1310 err = btrfs_unlink_trans(trans, root, dir, dentry);
1311 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001312 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001313 }
Chris Mason39544012007-12-12 14:38:19 -05001314
Josef Bacik7b128762008-07-24 12:17:14 -04001315fail_trans:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001316 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001317 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001318fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001319 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001320
Chris Mason39279cc2007-06-12 06:35:45 -04001321 if (ret && !err)
1322 err = ret;
1323 return err;
1324}
1325
Chris Mason39279cc2007-06-12 06:35:45 -04001326/*
Chris Mason39279cc2007-06-12 06:35:45 -04001327 * this can truncate away extent items, csum items and directory items.
1328 * It starts at a high offset and removes keys until it can't find
1329 * any higher than i_size.
1330 *
1331 * csum items that cross the new i_size are truncated to the new size
1332 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001333 *
1334 * min_type is the minimum key type to truncate down to. If set to 0, this
1335 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001336 */
1337static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1338 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001339 struct inode *inode,
1340 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001341{
1342 int ret;
1343 struct btrfs_path *path;
1344 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001345 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001346 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001347 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001348 struct btrfs_file_extent_item *fi;
1349 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001350 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001351 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001352 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001353 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001354 int found_extent;
1355 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001356 int pending_del_nr = 0;
1357 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001358 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001359 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001360
Chris Mason3b951512008-04-17 11:29:12 -04001361 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001362 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001363 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001364 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001365
Chris Mason39279cc2007-06-12 06:35:45 -04001366 /* FIXME, add redo link to tree so we don't leak on crash */
1367 key.objectid = inode->i_ino;
1368 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001369 key.type = (u8)-1;
1370
Chris Mason85e21ba2008-01-29 15:11:36 -05001371 btrfs_init_path(path);
1372search_again:
1373 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1374 if (ret < 0) {
1375 goto error;
1376 }
1377 if (ret > 0) {
1378 BUG_ON(path->slots[0] == 0);
1379 path->slots[0]--;
1380 }
1381
Chris Mason39279cc2007-06-12 06:35:45 -04001382 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001383 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001384 leaf = path->nodes[0];
1385 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1386 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001387
Chris Mason5f39d392007-10-15 16:14:19 -04001388 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001389 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001390
Chris Mason85e21ba2008-01-29 15:11:36 -05001391 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001392 break;
1393
Chris Mason5f39d392007-10-15 16:14:19 -04001394 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001395 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001396 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001397 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001398 extent_type = btrfs_file_extent_type(leaf, fi);
1399 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001400 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001401 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001402 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1403 struct btrfs_item *item = btrfs_item_nr(leaf,
1404 path->slots[0]);
1405 item_end += btrfs_file_extent_inline_len(leaf,
1406 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001407 }
Yan008630c2007-11-07 13:31:09 -05001408 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001409 }
1410 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1411 ret = btrfs_csum_truncate(trans, root, path,
1412 inode->i_size);
1413 BUG_ON(ret);
1414 }
Yan008630c2007-11-07 13:31:09 -05001415 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001416 if (found_type == BTRFS_DIR_ITEM_KEY) {
1417 found_type = BTRFS_INODE_ITEM_KEY;
1418 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1419 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001420 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1421 found_type = BTRFS_XATTR_ITEM_KEY;
1422 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1423 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001424 } else if (found_type) {
1425 found_type--;
1426 } else {
1427 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001428 }
Yana61721d2007-09-17 11:08:38 -04001429 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001430 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001431 }
Chris Mason5f39d392007-10-15 16:14:19 -04001432 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001433 del_item = 1;
1434 else
1435 del_item = 0;
1436 found_extent = 0;
1437
1438 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001439 if (found_type != BTRFS_EXTENT_DATA_KEY)
1440 goto delete;
1441
1442 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001443 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001444 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001445 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001446 u64 orig_num_bytes =
1447 btrfs_file_extent_num_bytes(leaf, fi);
1448 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001449 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001450 extent_num_bytes = extent_num_bytes &
1451 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001452 btrfs_set_file_extent_num_bytes(leaf, fi,
1453 extent_num_bytes);
1454 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001455 extent_num_bytes);
1456 if (extent_start != 0)
1457 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001458 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001459 } else {
Chris Masondb945352007-10-15 16:15:53 -04001460 extent_num_bytes =
1461 btrfs_file_extent_disk_num_bytes(leaf,
1462 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001463 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001464 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001465 if (extent_start != 0) {
1466 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001467 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001468 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001469 root_gen = btrfs_header_generation(leaf);
1470 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001471 }
Chris Mason90692182008-02-08 13:49:28 -05001472 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1473 if (!del_item) {
1474 u32 newsize = inode->i_size - found_key.offset;
1475 dec_i_blocks(inode, item_end + 1 -
1476 found_key.offset - newsize);
1477 newsize =
1478 btrfs_file_extent_calc_inline_size(newsize);
1479 ret = btrfs_truncate_item(trans, root, path,
1480 newsize, 1);
1481 BUG_ON(ret);
1482 } else {
1483 dec_i_blocks(inode, item_end + 1 -
1484 found_key.offset);
1485 }
Chris Mason39279cc2007-06-12 06:35:45 -04001486 }
Chris Mason179e29e2007-11-01 11:28:41 -04001487delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001488 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001489 if (!pending_del_nr) {
1490 /* no pending yet, add ourselves */
1491 pending_del_slot = path->slots[0];
1492 pending_del_nr = 1;
1493 } else if (pending_del_nr &&
1494 path->slots[0] + 1 == pending_del_slot) {
1495 /* hop on the pending chunk */
1496 pending_del_nr++;
1497 pending_del_slot = path->slots[0];
1498 } else {
1499 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1500 }
Chris Mason39279cc2007-06-12 06:35:45 -04001501 } else {
1502 break;
1503 }
Chris Mason39279cc2007-06-12 06:35:45 -04001504 if (found_extent) {
1505 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001506 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001507 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001508 root_gen, inode->i_ino,
1509 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001510 BUG_ON(ret);
1511 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001512next:
1513 if (path->slots[0] == 0) {
1514 if (pending_del_nr)
1515 goto del_pending;
1516 btrfs_release_path(root, path);
1517 goto search_again;
1518 }
1519
1520 path->slots[0]--;
1521 if (pending_del_nr &&
1522 path->slots[0] + 1 != pending_del_slot) {
1523 struct btrfs_key debug;
1524del_pending:
1525 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1526 pending_del_slot);
1527 ret = btrfs_del_items(trans, root, path,
1528 pending_del_slot,
1529 pending_del_nr);
1530 BUG_ON(ret);
1531 pending_del_nr = 0;
1532 btrfs_release_path(root, path);
1533 goto search_again;
1534 }
Chris Mason39279cc2007-06-12 06:35:45 -04001535 }
1536 ret = 0;
1537error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001538 if (pending_del_nr) {
1539 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1540 pending_del_nr);
1541 }
Chris Mason39279cc2007-06-12 06:35:45 -04001542 btrfs_free_path(path);
1543 inode->i_sb->s_dirt = 1;
1544 return ret;
1545}
1546
Chris Masona52d9a82007-08-27 16:49:44 -04001547/*
1548 * taken from block_truncate_page, but does cow as it zeros out
1549 * any bytes left in the last page in the file.
1550 */
1551static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1552{
1553 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001554 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001555 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1556 struct btrfs_ordered_extent *ordered;
1557 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001558 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001559 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1560 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1561 struct page *page;
1562 int ret = 0;
1563 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001564 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001565
1566 if ((offset & (blocksize - 1)) == 0)
1567 goto out;
1568
1569 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001570again:
Chris Masona52d9a82007-08-27 16:49:44 -04001571 page = grab_cache_page(mapping, index);
1572 if (!page)
1573 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001574
1575 page_start = page_offset(page);
1576 page_end = page_start + PAGE_CACHE_SIZE - 1;
1577
Chris Masona52d9a82007-08-27 16:49:44 -04001578 if (!PageUptodate(page)) {
1579 ret = btrfs_readpage(NULL, page);
1580 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001581 if (page->mapping != mapping) {
1582 unlock_page(page);
1583 page_cache_release(page);
1584 goto again;
1585 }
Chris Masona52d9a82007-08-27 16:49:44 -04001586 if (!PageUptodate(page)) {
1587 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001588 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001589 }
1590 }
Chris Mason211c17f2008-05-15 09:13:45 -04001591 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001592
1593 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1594 set_page_extent_mapped(page);
1595
1596 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1597 if (ordered) {
1598 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1599 unlock_page(page);
1600 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001601 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001602 btrfs_put_ordered_extent(ordered);
1603 goto again;
1604 }
1605
Chris Masonea8c2812008-08-04 23:17:27 -04001606 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001607 ret = 0;
1608 if (offset != PAGE_CACHE_SIZE) {
1609 kaddr = kmap(page);
1610 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1611 flush_dcache_page(page);
1612 kunmap(page);
1613 }
Chris Mason247e7432008-07-17 12:53:51 -04001614 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001615 set_page_dirty(page);
1616 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001617
Chris Mason89642222008-07-24 09:41:53 -04001618out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001619 unlock_page(page);
1620 page_cache_release(page);
1621out:
1622 return ret;
1623}
1624
1625static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1626{
1627 struct inode *inode = dentry->d_inode;
1628 int err;
1629
1630 err = inode_change_ok(inode, attr);
1631 if (err)
1632 return err;
1633
1634 if (S_ISREG(inode->i_mode) &&
1635 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1636 struct btrfs_trans_handle *trans;
1637 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001638 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001639
Chris Mason5f39d392007-10-15 16:14:19 -04001640 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001641 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001642 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001643 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001644 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001645
Chris Mason1b0f7c22008-01-30 14:33:02 -05001646 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001647 goto out;
1648
Chris Mason1832a6d2007-12-21 16:27:21 -05001649 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001650 if (err)
1651 goto fail;
1652
Chris Mason39279cc2007-06-12 06:35:45 -04001653 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1654
Chris Mason5f564062008-01-22 16:47:59 -05001655 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001656 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1657 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001658
Chris Mason39279cc2007-06-12 06:35:45 -04001659 trans = btrfs_start_transaction(root, 1);
1660 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001661 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001662 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001663 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001664 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001665
Chris Mason179e29e2007-11-01 11:28:41 -04001666 if (alloc_hint != EXTENT_MAP_INLINE) {
1667 err = btrfs_insert_file_extent(trans, root,
1668 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001669 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001670 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001671 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001672 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001673 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001674 }
Chris Masonee6e6502008-07-17 12:54:40 -04001675 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001676 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001677 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001678 if (err)
1679 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001680 }
1681out:
1682 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001683
1684 if (!err && ((attr->ia_valid & ATTR_MODE)))
1685 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001686fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001687 return err;
1688}
Chris Mason61295eb2008-01-14 16:24:38 -05001689
Chris Mason39279cc2007-06-12 06:35:45 -04001690void btrfs_delete_inode(struct inode *inode)
1691{
1692 struct btrfs_trans_handle *trans;
1693 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001694 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001695 int ret;
1696
1697 truncate_inode_pages(&inode->i_data, 0);
1698 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001699 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001700 goto no_delete;
1701 }
Chris Mason4a096752008-07-21 10:29:44 -04001702 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001703
Chris Masondbe674a2008-07-17 12:54:05 -04001704 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001705 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001706
Chris Mason39279cc2007-06-12 06:35:45 -04001707 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001708 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001709 if (ret) {
1710 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001711 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001712 }
1713
1714 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001715
Chris Masond3c2fdc2007-09-17 10:58:06 -04001716 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001717 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001718
Chris Mason39279cc2007-06-12 06:35:45 -04001719 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001720 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001721 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001722
1723no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001724 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001725 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001726 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001727no_delete:
1728 clear_inode(inode);
1729}
1730
1731/*
1732 * this returns the key found in the dir entry in the location pointer.
1733 * If no dir entries were found, location->objectid is 0.
1734 */
1735static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1736 struct btrfs_key *location)
1737{
1738 const char *name = dentry->d_name.name;
1739 int namelen = dentry->d_name.len;
1740 struct btrfs_dir_item *di;
1741 struct btrfs_path *path;
1742 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001743 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001744
Chris Mason39544012007-12-12 14:38:19 -05001745 if (namelen == 1 && strcmp(name, ".") == 0) {
1746 location->objectid = dir->i_ino;
1747 location->type = BTRFS_INODE_ITEM_KEY;
1748 location->offset = 0;
1749 return 0;
1750 }
Chris Mason39279cc2007-06-12 06:35:45 -04001751 path = btrfs_alloc_path();
1752 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001753
Chris Mason7a720532007-12-13 09:06:59 -05001754 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001755 struct btrfs_key key;
1756 struct extent_buffer *leaf;
Chris Mason39544012007-12-12 14:38:19 -05001757 int slot;
1758
1759 key.objectid = dir->i_ino;
Yan445dceb2008-07-24 12:19:32 -04001760 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001761 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001762 if (ret < 0 || path->slots[0] == 0)
1763 goto out_err;
Chris Mason39544012007-12-12 14:38:19 -05001764 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1765 BUG_ON(ret == 0);
1766 ret = 0;
Chris Mason39544012007-12-12 14:38:19 -05001767 leaf = path->nodes[0];
Yan445dceb2008-07-24 12:19:32 -04001768 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001769
1770 btrfs_item_key_to_cpu(leaf, &key, slot);
1771 if (key.objectid != dir->i_ino ||
1772 key.type != BTRFS_INODE_REF_KEY) {
1773 goto out_err;
1774 }
1775 location->objectid = key.offset;
1776 location->type = BTRFS_INODE_ITEM_KEY;
1777 location->offset = 0;
1778 goto out;
1779 }
1780
Chris Mason39279cc2007-06-12 06:35:45 -04001781 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1782 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001783 if (IS_ERR(di))
1784 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001785 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001786 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001787 }
Chris Mason5f39d392007-10-15 16:14:19 -04001788 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001789out:
Chris Mason39279cc2007-06-12 06:35:45 -04001790 btrfs_free_path(path);
1791 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001792out_err:
1793 location->objectid = 0;
1794 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001795}
1796
1797/*
1798 * when we hit a tree root in a directory, the btrfs part of the inode
1799 * needs to be changed to reflect the root directory of the tree root. This
1800 * is kind of like crossing a mount point.
1801 */
1802static int fixup_tree_root_location(struct btrfs_root *root,
1803 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001804 struct btrfs_root **sub_root,
1805 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001806{
Chris Mason39279cc2007-06-12 06:35:45 -04001807 struct btrfs_root_item *ri;
1808
1809 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1810 return 0;
1811 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1812 return 0;
1813
Josef Bacik58176a92007-08-29 15:47:34 -04001814 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1815 dentry->d_name.name,
1816 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001817 if (IS_ERR(*sub_root))
1818 return PTR_ERR(*sub_root);
1819
1820 ri = &(*sub_root)->root_item;
1821 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001822 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1823 location->offset = 0;
1824
Chris Mason39279cc2007-06-12 06:35:45 -04001825 return 0;
1826}
1827
1828static int btrfs_init_locked_inode(struct inode *inode, void *p)
1829{
1830 struct btrfs_iget_args *args = p;
1831 inode->i_ino = args->ino;
1832 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001833 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04001834 inode->i_mapping->writeback_index = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001835 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001836 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001837 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1838 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001839 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001840 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1841 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001842 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04001843 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001844 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001845 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001846 return 0;
1847}
1848
1849static int btrfs_find_actor(struct inode *inode, void *opaque)
1850{
1851 struct btrfs_iget_args *args = opaque;
1852 return (args->ino == inode->i_ino &&
1853 args->root == BTRFS_I(inode)->root);
1854}
1855
Chris Masondc17ff82008-01-08 15:46:30 -05001856struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1857 u64 root_objectid)
1858{
1859 struct btrfs_iget_args args;
1860 args.ino = objectid;
1861 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1862
1863 if (!args.root)
1864 return NULL;
1865
1866 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1867}
1868
Chris Mason39279cc2007-06-12 06:35:45 -04001869struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1870 struct btrfs_root *root)
1871{
1872 struct inode *inode;
1873 struct btrfs_iget_args args;
1874 args.ino = objectid;
1875 args.root = root;
1876
1877 inode = iget5_locked(s, objectid, btrfs_find_actor,
1878 btrfs_init_locked_inode,
1879 (void *)&args);
1880 return inode;
1881}
1882
1883static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1884 struct nameidata *nd)
1885{
1886 struct inode * inode;
1887 struct btrfs_inode *bi = BTRFS_I(dir);
1888 struct btrfs_root *root = bi->root;
1889 struct btrfs_root *sub_root = root;
1890 struct btrfs_key location;
Josef Bacik7b128762008-07-24 12:17:14 -04001891 int ret, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001892
1893 if (dentry->d_name.len > BTRFS_NAME_LEN)
1894 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001895
Chris Mason39279cc2007-06-12 06:35:45 -04001896 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001897
Chris Mason39279cc2007-06-12 06:35:45 -04001898 if (ret < 0)
1899 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001900
Chris Mason39279cc2007-06-12 06:35:45 -04001901 inode = NULL;
1902 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001903 ret = fixup_tree_root_location(root, &location, &sub_root,
1904 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001905 if (ret < 0)
1906 return ERR_PTR(ret);
1907 if (ret > 0)
1908 return ERR_PTR(-ENOENT);
Josef Bacik7b128762008-07-24 12:17:14 -04001909
Chris Mason39279cc2007-06-12 06:35:45 -04001910 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1911 sub_root);
1912 if (!inode)
1913 return ERR_PTR(-EACCES);
1914 if (inode->i_state & I_NEW) {
1915 /* the inode and parent dir are two different roots */
1916 if (sub_root != root) {
1917 igrab(inode);
1918 sub_root->inode = inode;
Josef Bacik7b128762008-07-24 12:17:14 -04001919 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001920 }
1921 BTRFS_I(inode)->root = sub_root;
1922 memcpy(&BTRFS_I(inode)->location, &location,
1923 sizeof(location));
1924 btrfs_read_locked_inode(inode);
1925 unlock_new_inode(inode);
1926 }
1927 }
Josef Bacik7b128762008-07-24 12:17:14 -04001928
1929 if (unlikely(do_orphan))
1930 btrfs_orphan_cleanup(sub_root);
1931
Chris Mason39279cc2007-06-12 06:35:45 -04001932 return d_splice_alias(inode, dentry);
1933}
1934
Chris Mason39279cc2007-06-12 06:35:45 -04001935static unsigned char btrfs_filetype_table[] = {
1936 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1937};
1938
1939static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1940{
Chris Mason6da6aba2007-12-18 16:15:09 -05001941 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001942 struct btrfs_root *root = BTRFS_I(inode)->root;
1943 struct btrfs_item *item;
1944 struct btrfs_dir_item *di;
1945 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001946 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001947 struct btrfs_path *path;
1948 int ret;
1949 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001950 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001951 int slot;
1952 int advance;
1953 unsigned char d_type;
1954 int over = 0;
1955 u32 di_cur;
1956 u32 di_total;
1957 u32 di_len;
1958 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001959 char tmp_name[32];
1960 char *name_ptr;
1961 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001962
1963 /* FIXME, use a real flag for deciding about the key type */
1964 if (root->fs_info->tree_root == root)
1965 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001966
Chris Mason39544012007-12-12 14:38:19 -05001967 /* special case for "." */
1968 if (filp->f_pos == 0) {
1969 over = filldir(dirent, ".", 1,
1970 1, inode->i_ino,
1971 DT_DIR);
1972 if (over)
1973 return 0;
1974 filp->f_pos = 1;
1975 }
1976
Chris Mason39279cc2007-06-12 06:35:45 -04001977 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001978 path = btrfs_alloc_path();
1979 path->reada = 2;
1980
1981 /* special case for .., just use the back ref */
1982 if (filp->f_pos == 1) {
1983 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001984 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001985 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan445dceb2008-07-24 12:19:32 -04001986 if (ret < 0 || path->slots[0] == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001987 btrfs_release_path(root, path);
1988 goto read_dir_items;
1989 }
Yan445dceb2008-07-24 12:19:32 -04001990 BUG_ON(ret == 0);
1991 leaf = path->nodes[0];
1992 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001993 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1994 btrfs_release_path(root, path);
1995 if (found_key.objectid != key.objectid ||
1996 found_key.type != BTRFS_INODE_REF_KEY)
1997 goto read_dir_items;
1998 over = filldir(dirent, "..", 2,
1999 2, found_key.offset, DT_DIR);
2000 if (over)
2001 goto nopos;
2002 filp->f_pos = 2;
2003 }
2004
2005read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04002006 btrfs_set_key_type(&key, key_type);
2007 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04002008
Chris Mason39279cc2007-06-12 06:35:45 -04002009 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2010 if (ret < 0)
2011 goto err;
2012 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002013 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002014 leaf = path->nodes[0];
2015 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002016 slot = path->slots[0];
2017 if (advance || slot >= nritems) {
2018 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002019 ret = btrfs_next_leaf(root, path);
2020 if (ret)
2021 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002022 leaf = path->nodes[0];
2023 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002024 slot = path->slots[0];
2025 } else {
2026 slot++;
2027 path->slots[0]++;
2028 }
2029 }
2030 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002031 item = btrfs_item_nr(leaf, slot);
2032 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2033
2034 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04002035 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002036 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002037 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002038 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04002039 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002040
2041 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002042 advance = 1;
2043 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2044 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002045 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04002046 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04002047 struct btrfs_key location;
2048
2049 name_len = btrfs_dir_name_len(leaf, di);
2050 if (name_len < 32) {
2051 name_ptr = tmp_name;
2052 } else {
2053 name_ptr = kmalloc(name_len, GFP_NOFS);
2054 BUG_ON(!name_ptr);
2055 }
2056 read_extent_buffer(leaf, name_ptr,
2057 (unsigned long)(di + 1), name_len);
2058
2059 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2060 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002061 over = filldir(dirent, name_ptr, name_len,
2062 found_key.offset,
2063 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002064 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002065
2066 if (name_ptr != tmp_name)
2067 kfree(name_ptr);
2068
Chris Mason39279cc2007-06-12 06:35:45 -04002069 if (over)
2070 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05002071 di_len = btrfs_dir_name_len(leaf, di) +
2072 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002073 di_cur += di_len;
2074 di = (struct btrfs_dir_item *)((char *)di + di_len);
2075 }
2076 }
Yan Zheng5e591a02008-02-19 11:41:02 -05002077 if (key_type == BTRFS_DIR_INDEX_KEY)
2078 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2079 else
2080 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002081nopos:
2082 ret = 0;
2083err:
Chris Mason39279cc2007-06-12 06:35:45 -04002084 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002085 return ret;
2086}
2087
2088int btrfs_write_inode(struct inode *inode, int wait)
2089{
2090 struct btrfs_root *root = BTRFS_I(inode)->root;
2091 struct btrfs_trans_handle *trans;
2092 int ret = 0;
2093
Chris Mason4ca8b412008-08-05 13:30:48 -04002094 if (root->fs_info->closing > 1)
2095 return 0;
2096
Chris Mason39279cc2007-06-12 06:35:45 -04002097 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002098 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002099 btrfs_set_trans_block_group(trans, inode);
2100 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002101 }
2102 return ret;
2103}
2104
2105/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002106 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002107 * inode changes. But, it is most likely to find the inode in cache.
2108 * FIXME, needs more benchmarking...there are no reasons other than performance
2109 * to keep or drop this code.
2110 */
2111void btrfs_dirty_inode(struct inode *inode)
2112{
2113 struct btrfs_root *root = BTRFS_I(inode)->root;
2114 struct btrfs_trans_handle *trans;
2115
Chris Masonf9295742008-07-17 12:54:14 -04002116 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002117 btrfs_set_trans_block_group(trans, inode);
2118 btrfs_update_inode(trans, root, inode);
2119 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002120}
2121
Josef Bacikaec74772008-07-24 12:12:38 -04002122static int btrfs_set_inode_index_count(struct inode *inode)
2123{
2124 struct btrfs_root *root = BTRFS_I(inode)->root;
2125 struct btrfs_key key, found_key;
2126 struct btrfs_path *path;
2127 struct extent_buffer *leaf;
2128 int ret;
2129
2130 key.objectid = inode->i_ino;
2131 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2132 key.offset = (u64)-1;
2133
2134 path = btrfs_alloc_path();
2135 if (!path)
2136 return -ENOMEM;
2137
2138 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2139 if (ret < 0)
2140 goto out;
2141 /* FIXME: we should be able to handle this */
2142 if (ret == 0)
2143 goto out;
2144 ret = 0;
2145
2146 /*
2147 * MAGIC NUMBER EXPLANATION:
2148 * since we search a directory based on f_pos we have to start at 2
2149 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2150 * else has to start at 2
2151 */
2152 if (path->slots[0] == 0) {
2153 BTRFS_I(inode)->index_cnt = 2;
2154 goto out;
2155 }
2156
2157 path->slots[0]--;
2158
2159 leaf = path->nodes[0];
2160 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2161
2162 if (found_key.objectid != inode->i_ino ||
2163 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2164 BTRFS_I(inode)->index_cnt = 2;
2165 goto out;
2166 }
2167
2168 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2169out:
2170 btrfs_free_path(path);
2171 return ret;
2172}
2173
Chris Mason00e4e6b2008-08-05 11:18:09 -04002174static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2175 u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04002176{
2177 int ret = 0;
2178
2179 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2180 ret = btrfs_set_inode_index_count(dir);
2181 if (ret)
2182 return ret;
2183 }
2184
Chris Mason00e4e6b2008-08-05 11:18:09 -04002185 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04002186 BTRFS_I(dir)->index_cnt++;
2187
2188 return ret;
2189}
2190
Chris Mason39279cc2007-06-12 06:35:45 -04002191static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2192 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002193 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002194 const char *name, int name_len,
2195 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002196 u64 objectid,
2197 struct btrfs_block_group_cache *group,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002198 int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04002199{
2200 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002201 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002202 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002203 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002204 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002205 struct btrfs_inode_ref *ref;
2206 struct btrfs_key key[2];
2207 u32 sizes[2];
2208 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002209 int ret;
2210 int owner;
2211
Chris Mason5f39d392007-10-15 16:14:19 -04002212 path = btrfs_alloc_path();
2213 BUG_ON(!path);
2214
Chris Mason39279cc2007-06-12 06:35:45 -04002215 inode = new_inode(root->fs_info->sb);
2216 if (!inode)
2217 return ERR_PTR(-ENOMEM);
2218
Josef Bacikaec74772008-07-24 12:12:38 -04002219 if (dir) {
Chris Mason00e4e6b2008-08-05 11:18:09 -04002220 ret = btrfs_set_inode_index(dir, inode, index);
Josef Bacikaec74772008-07-24 12:12:38 -04002221 if (ret)
2222 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002223 }
2224 /*
2225 * index_cnt is ignored for everything but a dir,
2226 * btrfs_get_inode_index_count has an explanation for the magic
2227 * number
2228 */
2229 BTRFS_I(inode)->index_cnt = 2;
2230
Chris Masond1310b22008-01-24 16:13:08 -05002231 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2232 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002233 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002234 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2235 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002236 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonea8c2812008-08-04 23:17:27 -04002237 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04002238 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002239 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002240 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04002241 inode->i_mapping->writeback_index = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002242 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002243 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002244
Chris Mason39279cc2007-06-12 06:35:45 -04002245 if (mode & S_IFDIR)
2246 owner = 0;
2247 else
2248 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002249 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002250 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002251 if (!new_inode_group) {
2252 printk("find_block group failed\n");
2253 new_inode_group = group;
2254 }
2255 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002256 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002257
2258 key[0].objectid = objectid;
2259 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2260 key[0].offset = 0;
2261
2262 key[1].objectid = objectid;
2263 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2264 key[1].offset = ref_objectid;
2265
2266 sizes[0] = sizeof(struct btrfs_inode_item);
2267 sizes[1] = name_len + sizeof(*ref);
2268
2269 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2270 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002271 goto fail;
2272
Chris Mason9c583092008-01-29 15:15:18 -05002273 if (objectid > root->highest_inode)
2274 root->highest_inode = objectid;
2275
Chris Mason39279cc2007-06-12 06:35:45 -04002276 inode->i_uid = current->fsuid;
2277 inode->i_gid = current->fsgid;
2278 inode->i_mode = mode;
2279 inode->i_ino = objectid;
2280 inode->i_blocks = 0;
2281 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002282 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2283 struct btrfs_inode_item);
2284 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002285
2286 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2287 struct btrfs_inode_ref);
2288 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002289 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05002290 ptr = (unsigned long)(ref + 1);
2291 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2292
Chris Mason5f39d392007-10-15 16:14:19 -04002293 btrfs_mark_buffer_dirty(path->nodes[0]);
2294 btrfs_free_path(path);
2295
Chris Mason39279cc2007-06-12 06:35:45 -04002296 location = &BTRFS_I(inode)->location;
2297 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002298 location->offset = 0;
2299 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2300
Chris Mason39279cc2007-06-12 06:35:45 -04002301 insert_inode_hash(inode);
2302 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002303fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002304 if (dir)
2305 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002306 btrfs_free_path(path);
2307 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002308}
2309
2310static inline u8 btrfs_inode_type(struct inode *inode)
2311{
2312 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2313}
2314
2315static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002316 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002317 int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002318{
2319 int ret;
2320 struct btrfs_key key;
2321 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002322 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002323
Chris Mason39279cc2007-06-12 06:35:45 -04002324 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002325 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2326 key.offset = 0;
2327
2328 ret = btrfs_insert_dir_item(trans, root,
2329 dentry->d_name.name, dentry->d_name.len,
2330 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002331 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04002332 index);
Chris Mason39279cc2007-06-12 06:35:45 -04002333 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002334 if (add_backref) {
2335 ret = btrfs_insert_inode_ref(trans, root,
2336 dentry->d_name.name,
2337 dentry->d_name.len,
2338 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002339 parent_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002340 index);
Chris Mason9c583092008-01-29 15:15:18 -05002341 }
Chris Masondbe674a2008-07-17 12:54:05 -04002342 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2343 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002344 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002345 ret = btrfs_update_inode(trans, root,
2346 dentry->d_parent->d_inode);
2347 }
2348 return ret;
2349}
2350
2351static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002352 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002353 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002354{
Chris Mason00e4e6b2008-08-05 11:18:09 -04002355 int err = btrfs_add_link(trans, dentry, inode, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002356 if (!err) {
2357 d_instantiate(dentry, inode);
2358 return 0;
2359 }
2360 if (err > 0)
2361 err = -EEXIST;
2362 return err;
2363}
2364
Josef Bacik618e21d2007-07-11 10:18:17 -04002365static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2366 int mode, dev_t rdev)
2367{
2368 struct btrfs_trans_handle *trans;
2369 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002370 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002371 int err;
2372 int drop_inode = 0;
2373 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002374 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002375 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002376
2377 if (!new_valid_dev(rdev))
2378 return -EINVAL;
2379
Chris Mason1832a6d2007-12-21 16:27:21 -05002380 err = btrfs_check_free_space(root, 1, 0);
2381 if (err)
2382 goto fail;
2383
Josef Bacik618e21d2007-07-11 10:18:17 -04002384 trans = btrfs_start_transaction(root, 1);
2385 btrfs_set_trans_block_group(trans, dir);
2386
2387 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2388 if (err) {
2389 err = -ENOSPC;
2390 goto out_unlock;
2391 }
2392
Josef Bacikaec74772008-07-24 12:12:38 -04002393 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002394 dentry->d_name.len,
2395 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002396 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002397 err = PTR_ERR(inode);
2398 if (IS_ERR(inode))
2399 goto out_unlock;
2400
Josef Bacik33268ea2008-07-24 12:16:36 -04002401 err = btrfs_init_acl(inode, dir);
2402 if (err) {
2403 drop_inode = 1;
2404 goto out_unlock;
2405 }
2406
Josef Bacik618e21d2007-07-11 10:18:17 -04002407 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002408 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002409 if (err)
2410 drop_inode = 1;
2411 else {
2412 inode->i_op = &btrfs_special_inode_operations;
2413 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002414 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002415 }
2416 dir->i_sb->s_dirt = 1;
2417 btrfs_update_inode_block_group(trans, inode);
2418 btrfs_update_inode_block_group(trans, dir);
2419out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002420 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002421 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002422fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002423 if (drop_inode) {
2424 inode_dec_link_count(inode);
2425 iput(inode);
2426 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002427 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002428 return err;
2429}
2430
Chris Mason39279cc2007-06-12 06:35:45 -04002431static int btrfs_create(struct inode *dir, struct dentry *dentry,
2432 int mode, struct nameidata *nd)
2433{
2434 struct btrfs_trans_handle *trans;
2435 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002436 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002437 int err;
2438 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002439 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002440 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002441 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002442
Chris Mason1832a6d2007-12-21 16:27:21 -05002443 err = btrfs_check_free_space(root, 1, 0);
2444 if (err)
2445 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002446 trans = btrfs_start_transaction(root, 1);
2447 btrfs_set_trans_block_group(trans, dir);
2448
2449 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2450 if (err) {
2451 err = -ENOSPC;
2452 goto out_unlock;
2453 }
2454
Josef Bacikaec74772008-07-24 12:12:38 -04002455 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002456 dentry->d_name.len,
2457 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002458 objectid, BTRFS_I(dir)->block_group, mode,
2459 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002460 err = PTR_ERR(inode);
2461 if (IS_ERR(inode))
2462 goto out_unlock;
2463
Josef Bacik33268ea2008-07-24 12:16:36 -04002464 err = btrfs_init_acl(inode, dir);
2465 if (err) {
2466 drop_inode = 1;
2467 goto out_unlock;
2468 }
2469
Chris Mason39279cc2007-06-12 06:35:45 -04002470 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002471 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002472 if (err)
2473 drop_inode = 1;
2474 else {
2475 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002476 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002477 inode->i_fop = &btrfs_file_operations;
2478 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002479 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2480 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002481 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002482 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2483 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04002484 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04002485 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002486 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002487 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002488 BTRFS_I(inode)->disk_i_size = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04002489 inode->i_mapping->writeback_index = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002490 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002491 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002492 }
2493 dir->i_sb->s_dirt = 1;
2494 btrfs_update_inode_block_group(trans, inode);
2495 btrfs_update_inode_block_group(trans, dir);
2496out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002497 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002498 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002499fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002500 if (drop_inode) {
2501 inode_dec_link_count(inode);
2502 iput(inode);
2503 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002504 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002505 return err;
2506}
2507
2508static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2509 struct dentry *dentry)
2510{
2511 struct btrfs_trans_handle *trans;
2512 struct btrfs_root *root = BTRFS_I(dir)->root;
2513 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002514 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05002515 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002516 int err;
2517 int drop_inode = 0;
2518
2519 if (inode->i_nlink == 0)
2520 return -ENOENT;
2521
Chris Mason6da6aba2007-12-18 16:15:09 -05002522#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2523 inode->i_nlink++;
2524#else
Chris Mason39279cc2007-06-12 06:35:45 -04002525 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002526#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002527 err = btrfs_check_free_space(root, 1, 0);
2528 if (err)
2529 goto fail;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002530 err = btrfs_set_inode_index(dir, inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002531 if (err)
2532 goto fail;
2533
Chris Mason39279cc2007-06-12 06:35:45 -04002534 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002535
Chris Mason39279cc2007-06-12 06:35:45 -04002536 btrfs_set_trans_block_group(trans, dir);
2537 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002538
Chris Mason00e4e6b2008-08-05 11:18:09 -04002539 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04002540
Chris Mason39279cc2007-06-12 06:35:45 -04002541 if (err)
2542 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002543
Chris Mason39279cc2007-06-12 06:35:45 -04002544 dir->i_sb->s_dirt = 1;
2545 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002546 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002547
Chris Mason54aa1f42007-06-22 14:16:25 -04002548 if (err)
2549 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002550
Chris Masond3c2fdc2007-09-17 10:58:06 -04002551 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002552 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002553fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002554 if (drop_inode) {
2555 inode_dec_link_count(inode);
2556 iput(inode);
2557 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002558 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002559 return err;
2560}
2561
Chris Mason39279cc2007-06-12 06:35:45 -04002562static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2563{
Chris Masonb9d86662008-05-02 16:13:49 -04002564 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002565 struct btrfs_trans_handle *trans;
2566 struct btrfs_root *root = BTRFS_I(dir)->root;
2567 int err = 0;
2568 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002569 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002570 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002571 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002572
Chris Mason1832a6d2007-12-21 16:27:21 -05002573 err = btrfs_check_free_space(root, 1, 0);
2574 if (err)
2575 goto out_unlock;
2576
Chris Mason39279cc2007-06-12 06:35:45 -04002577 trans = btrfs_start_transaction(root, 1);
2578 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002579
Chris Mason39279cc2007-06-12 06:35:45 -04002580 if (IS_ERR(trans)) {
2581 err = PTR_ERR(trans);
2582 goto out_unlock;
2583 }
2584
2585 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2586 if (err) {
2587 err = -ENOSPC;
2588 goto out_unlock;
2589 }
2590
Josef Bacikaec74772008-07-24 12:12:38 -04002591 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002592 dentry->d_name.len,
2593 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002594 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2595 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002596 if (IS_ERR(inode)) {
2597 err = PTR_ERR(inode);
2598 goto out_fail;
2599 }
Chris Mason5f39d392007-10-15 16:14:19 -04002600
Chris Mason39279cc2007-06-12 06:35:45 -04002601 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002602
2603 err = btrfs_init_acl(inode, dir);
2604 if (err)
2605 goto out_fail;
2606
Chris Mason39279cc2007-06-12 06:35:45 -04002607 inode->i_op = &btrfs_dir_inode_operations;
2608 inode->i_fop = &btrfs_dir_file_operations;
2609 btrfs_set_trans_block_group(trans, inode);
2610
Chris Masondbe674a2008-07-17 12:54:05 -04002611 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002612 err = btrfs_update_inode(trans, root, inode);
2613 if (err)
2614 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002615
Chris Mason00e4e6b2008-08-05 11:18:09 -04002616 err = btrfs_add_link(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002617 if (err)
2618 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002619
Chris Mason39279cc2007-06-12 06:35:45 -04002620 d_instantiate(dentry, inode);
2621 drop_on_err = 0;
2622 dir->i_sb->s_dirt = 1;
2623 btrfs_update_inode_block_group(trans, inode);
2624 btrfs_update_inode_block_group(trans, dir);
2625
2626out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002627 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002628 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002629
Chris Mason39279cc2007-06-12 06:35:45 -04002630out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002631 if (drop_on_err)
2632 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002633 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002634 return err;
2635}
2636
Chris Mason3b951512008-04-17 11:29:12 -04002637static int merge_extent_mapping(struct extent_map_tree *em_tree,
2638 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002639 struct extent_map *em,
2640 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002641{
2642 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002643
Chris Masone6dcd2d2008-07-17 12:53:50 -04002644 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2645 start_diff = map_start - em->start;
2646 em->start = map_start;
2647 em->len = map_len;
2648 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2649 em->block_start += start_diff;
2650 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002651}
2652
Chris Masona52d9a82007-08-27 16:49:44 -04002653struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002654 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002655 int create)
2656{
2657 int ret;
2658 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002659 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002660 u64 extent_start = 0;
2661 u64 extent_end = 0;
2662 u64 objectid = inode->i_ino;
2663 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002664 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002665 struct btrfs_root *root = BTRFS_I(inode)->root;
2666 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002667 struct extent_buffer *leaf;
2668 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002669 struct extent_map *em = NULL;
2670 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002671 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002672 struct btrfs_trans_handle *trans = NULL;
2673
Chris Masona52d9a82007-08-27 16:49:44 -04002674again:
Chris Masond1310b22008-01-24 16:13:08 -05002675 spin_lock(&em_tree->lock);
2676 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002677 if (em)
2678 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002679 spin_unlock(&em_tree->lock);
2680
Chris Masona52d9a82007-08-27 16:49:44 -04002681 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002682 if (em->start > start || em->start + em->len <= start)
2683 free_extent_map(em);
2684 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002685 free_extent_map(em);
2686 else
2687 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002688 }
Chris Masond1310b22008-01-24 16:13:08 -05002689 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002690 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002691 err = -ENOMEM;
2692 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002693 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002694 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002695 em->start = EXTENT_MAP_HOLE;
2696 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002697
2698 if (!path) {
2699 path = btrfs_alloc_path();
2700 BUG_ON(!path);
2701 }
2702
Chris Mason179e29e2007-11-01 11:28:41 -04002703 ret = btrfs_lookup_file_extent(trans, root, path,
2704 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002705 if (ret < 0) {
2706 err = ret;
2707 goto out;
2708 }
2709
2710 if (ret != 0) {
2711 if (path->slots[0] == 0)
2712 goto not_found;
2713 path->slots[0]--;
2714 }
2715
Chris Mason5f39d392007-10-15 16:14:19 -04002716 leaf = path->nodes[0];
2717 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002718 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002719 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002720 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2721 found_type = btrfs_key_type(&found_key);
2722 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002723 found_type != BTRFS_EXTENT_DATA_KEY) {
2724 goto not_found;
2725 }
2726
Chris Mason5f39d392007-10-15 16:14:19 -04002727 found_type = btrfs_file_extent_type(leaf, item);
2728 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002729 if (found_type == BTRFS_FILE_EXTENT_REG) {
2730 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002731 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002732 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002733 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002734 em->start = start;
2735 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002736 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002737 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002738 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002739 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002740 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002741 }
2742 goto not_found_em;
2743 }
Chris Masondb945352007-10-15 16:15:53 -04002744 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2745 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002746 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002747 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002748 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002749 goto insert;
2750 }
Chris Masondb945352007-10-15 16:15:53 -04002751 bytenr += btrfs_file_extent_offset(leaf, item);
2752 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002753 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002754 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002755 goto insert;
2756 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002757 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002758 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002759 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002760 size_t size;
2761 size_t extent_offset;
2762 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002763
Chris Mason5f39d392007-10-15 16:14:19 -04002764 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2765 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002766 extent_end = (extent_start + size + root->sectorsize - 1) &
2767 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002768 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002769 em->start = start;
2770 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002771 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002772 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002773 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002774 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002775 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002776 }
2777 goto not_found_em;
2778 }
2779 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002780
2781 if (!page) {
2782 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002783 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002784 goto out;
2785 }
2786
Chris Mason70dec802008-01-29 09:59:12 -05002787 page_start = page_offset(page) + pg_offset;
2788 extent_offset = page_start - extent_start;
2789 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002790 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002791 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002792 em->len = (copy_size + root->sectorsize - 1) &
2793 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002794 map = kmap(page);
2795 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002796 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002797 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002798 copy_size);
2799 flush_dcache_page(page);
2800 } else if (create && PageUptodate(page)) {
2801 if (!trans) {
2802 kunmap(page);
2803 free_extent_map(em);
2804 em = NULL;
2805 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002806 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002807 goto again;
2808 }
Chris Mason70dec802008-01-29 09:59:12 -05002809 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002810 copy_size);
2811 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002812 }
Chris Masona52d9a82007-08-27 16:49:44 -04002813 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002814 set_extent_uptodate(io_tree, em->start,
2815 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002816 goto insert;
2817 } else {
2818 printk("unkknown found_type %d\n", found_type);
2819 WARN_ON(1);
2820 }
2821not_found:
2822 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002823 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002824not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002825 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002826insert:
2827 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002828 if (em->start > start || extent_map_end(em) <= start) {
2829 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002830 err = -EIO;
2831 goto out;
2832 }
Chris Masond1310b22008-01-24 16:13:08 -05002833
2834 err = 0;
2835 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002836 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002837 /* it is possible that someone inserted the extent into the tree
2838 * while we had the lock dropped. It is also possible that
2839 * an overlapping map exists in the tree
2840 */
Chris Masona52d9a82007-08-27 16:49:44 -04002841 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002842 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002843
2844 ret = 0;
2845
Chris Mason3b951512008-04-17 11:29:12 -04002846 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002847 if (existing && (existing->start > start ||
2848 existing->start + existing->len <= start)) {
2849 free_extent_map(existing);
2850 existing = NULL;
2851 }
Chris Mason3b951512008-04-17 11:29:12 -04002852 if (!existing) {
2853 existing = lookup_extent_mapping(em_tree, em->start,
2854 em->len);
2855 if (existing) {
2856 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002857 em, start,
2858 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002859 free_extent_map(existing);
2860 if (err) {
2861 free_extent_map(em);
2862 em = NULL;
2863 }
2864 } else {
2865 err = -EIO;
2866 printk("failing to insert %Lu %Lu\n",
2867 start, len);
2868 free_extent_map(em);
2869 em = NULL;
2870 }
2871 } else {
2872 free_extent_map(em);
2873 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002874 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002875 }
Chris Masona52d9a82007-08-27 16:49:44 -04002876 }
Chris Masond1310b22008-01-24 16:13:08 -05002877 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002878out:
Chris Masonf4219502008-07-22 11:18:09 -04002879 if (path)
2880 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002881 if (trans) {
2882 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002883 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002884 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002885 }
Chris Masona52d9a82007-08-27 16:49:44 -04002886 }
Chris Masona52d9a82007-08-27 16:49:44 -04002887 if (err) {
2888 free_extent_map(em);
2889 WARN_ON(1);
2890 return ERR_PTR(err);
2891 }
2892 return em;
2893}
2894
Chris Masone1c4b742008-04-22 13:26:46 -04002895#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002896static int btrfs_get_block(struct inode *inode, sector_t iblock,
2897 struct buffer_head *bh_result, int create)
2898{
2899 struct extent_map *em;
2900 u64 start = (u64)iblock << inode->i_blkbits;
2901 struct btrfs_multi_bio *multi = NULL;
2902 struct btrfs_root *root = BTRFS_I(inode)->root;
2903 u64 len;
2904 u64 logical;
2905 u64 map_length;
2906 int ret = 0;
2907
2908 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2909
2910 if (!em || IS_ERR(em))
2911 goto out;
2912
Chris Masone1c4b742008-04-22 13:26:46 -04002913 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002914 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002915 }
Chris Mason16432982008-04-10 10:23:21 -04002916
2917 if (em->block_start == EXTENT_MAP_INLINE) {
2918 ret = -EINVAL;
2919 goto out;
2920 }
2921
Chris Mason16432982008-04-10 10:23:21 -04002922 len = em->start + em->len - start;
2923 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2924
Chris Masone1c4b742008-04-22 13:26:46 -04002925 if (em->block_start == EXTENT_MAP_HOLE ||
2926 em->block_start == EXTENT_MAP_DELALLOC) {
2927 bh_result->b_size = len;
2928 goto out;
2929 }
2930
Chris Mason16432982008-04-10 10:23:21 -04002931 logical = start - em->start;
2932 logical = em->block_start + logical;
2933
2934 map_length = len;
2935 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2936 logical, &map_length, &multi, 0);
2937 BUG_ON(ret);
2938 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2939 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002940
Chris Mason16432982008-04-10 10:23:21 -04002941 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2942 set_buffer_mapped(bh_result);
2943 kfree(multi);
2944out:
2945 free_extent_map(em);
2946 return ret;
2947}
Chris Masone1c4b742008-04-22 13:26:46 -04002948#endif
Chris Mason16432982008-04-10 10:23:21 -04002949
2950static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2951 const struct iovec *iov, loff_t offset,
2952 unsigned long nr_segs)
2953{
Chris Masone1c4b742008-04-22 13:26:46 -04002954 return -EINVAL;
2955#if 0
Chris Mason16432982008-04-10 10:23:21 -04002956 struct file *file = iocb->ki_filp;
2957 struct inode *inode = file->f_mapping->host;
2958
2959 if (rw == WRITE)
2960 return -EINVAL;
2961
2962 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2963 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002964#endif
Chris Mason16432982008-04-10 10:23:21 -04002965}
2966
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002967static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002968{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002969 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002970}
2971
Chris Mason9ebefb182007-06-15 13:50:00 -04002972int btrfs_readpage(struct file *file, struct page *page)
2973{
Chris Masond1310b22008-01-24 16:13:08 -05002974 struct extent_io_tree *tree;
2975 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002976 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002977}
Chris Mason1832a6d2007-12-21 16:27:21 -05002978
Chris Mason39279cc2007-06-12 06:35:45 -04002979static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2980{
Chris Masond1310b22008-01-24 16:13:08 -05002981 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002982
2983
2984 if (current->flags & PF_MEMALLOC) {
2985 redirty_page_for_writepage(wbc, page);
2986 unlock_page(page);
2987 return 0;
2988 }
Chris Masond1310b22008-01-24 16:13:08 -05002989 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002990 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2991}
Chris Mason39279cc2007-06-12 06:35:45 -04002992
Chris Masonf4219502008-07-22 11:18:09 -04002993int btrfs_writepages(struct address_space *mapping,
2994 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04002995{
Chris Masond1310b22008-01-24 16:13:08 -05002996 struct extent_io_tree *tree;
2997 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002998 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2999}
3000
Chris Mason3ab2fb52007-11-08 10:59:22 -05003001static int
3002btrfs_readpages(struct file *file, struct address_space *mapping,
3003 struct list_head *pages, unsigned nr_pages)
3004{
Chris Masond1310b22008-01-24 16:13:08 -05003005 struct extent_io_tree *tree;
3006 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05003007 return extent_readpages(tree, mapping, pages, nr_pages,
3008 btrfs_get_extent);
3009}
Chris Masone6dcd2d2008-07-17 12:53:50 -04003010static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04003011{
Chris Masond1310b22008-01-24 16:13:08 -05003012 struct extent_io_tree *tree;
3013 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04003014 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003015
Chris Masond1310b22008-01-24 16:13:08 -05003016 tree = &BTRFS_I(page->mapping->host)->io_tree;
3017 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05003018 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003019 if (ret == 1) {
3020 ClearPagePrivate(page);
3021 set_page_private(page, 0);
3022 page_cache_release(page);
3023 }
3024 return ret;
3025}
Chris Mason39279cc2007-06-12 06:35:45 -04003026
Chris Masone6dcd2d2008-07-17 12:53:50 -04003027static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3028{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003029 return __btrfs_releasepage(page, gfp_flags);
3030}
3031
Chris Masona52d9a82007-08-27 16:49:44 -04003032static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3033{
Chris Masond1310b22008-01-24 16:13:08 -05003034 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003035 struct btrfs_ordered_extent *ordered;
3036 u64 page_start = page_offset(page);
3037 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003038
Chris Masone6dcd2d2008-07-17 12:53:50 -04003039 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003040 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003041 if (offset) {
3042 btrfs_releasepage(page, GFP_NOFS);
3043 return;
3044 }
3045
3046 lock_extent(tree, page_start, page_end, GFP_NOFS);
3047 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3048 page_offset(page));
3049 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003050 /*
3051 * IO on this page will never be started, so we need
3052 * to account for any ordered extents now
3053 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003054 clear_extent_bit(tree, page_start, page_end,
3055 EXTENT_DIRTY | EXTENT_DELALLOC |
3056 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003057 btrfs_finish_ordered_io(page->mapping->host,
3058 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003059 btrfs_put_ordered_extent(ordered);
3060 lock_extent(tree, page_start, page_end, GFP_NOFS);
3061 }
3062 clear_extent_bit(tree, page_start, page_end,
3063 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3064 EXTENT_ORDERED,
3065 1, 1, GFP_NOFS);
3066 __btrfs_releasepage(page, GFP_NOFS);
3067
Chris Mason4a096752008-07-21 10:29:44 -04003068 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003069 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003070 ClearPagePrivate(page);
3071 set_page_private(page, 0);
3072 page_cache_release(page);
3073 }
Chris Mason39279cc2007-06-12 06:35:45 -04003074}
3075
Chris Mason9ebefb182007-06-15 13:50:00 -04003076/*
3077 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3078 * called from a page fault handler when a page is first dirtied. Hence we must
3079 * be careful to check for EOF conditions here. We set the page up correctly
3080 * for a written page which means we get ENOSPC checking when writing into
3081 * holes and correct delalloc and unwritten extent mapping on filesystems that
3082 * support these features.
3083 *
3084 * We are not allowed to take the i_mutex here so we have to play games to
3085 * protect against truncate races as the page could now be beyond EOF. Because
3086 * vmtruncate() writes the inode size before removing pages, once we have the
3087 * page lock we can determine safely if the page is beyond EOF. If it is not
3088 * beyond EOF, then the page is guaranteed safe against truncation until we
3089 * unlock the page.
3090 */
3091int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3092{
Chris Mason6da6aba2007-12-18 16:15:09 -05003093 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003094 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003095 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3096 struct btrfs_ordered_extent *ordered;
3097 char *kaddr;
3098 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003099 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003100 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003101 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003102 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003103
Chris Mason1832a6d2007-12-21 16:27:21 -05003104 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003105 if (ret)
3106 goto out;
3107
3108 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003109again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003110 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003111 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003112 page_start = page_offset(page);
3113 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003114
Chris Mason9ebefb182007-06-15 13:50:00 -04003115 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003116 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003117 /* page got truncated out from underneath us */
3118 goto out_unlock;
3119 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003120 wait_on_page_writeback(page);
3121
3122 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3123 set_page_extent_mapped(page);
3124
Chris Masoneb84ae02008-07-17 13:53:27 -04003125 /*
3126 * we can't set the delalloc bits if there are pending ordered
3127 * extents. Drop our locks and wait for them to finish
3128 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003129 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3130 if (ordered) {
3131 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3132 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003133 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003134 btrfs_put_ordered_extent(ordered);
3135 goto again;
3136 }
3137
Chris Masonea8c2812008-08-04 23:17:27 -04003138 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003139 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003140
3141 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003142 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003143 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003144 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003145 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003146
Chris Masone6dcd2d2008-07-17 12:53:50 -04003147 if (zero_start != PAGE_CACHE_SIZE) {
3148 kaddr = kmap(page);
3149 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3150 flush_dcache_page(page);
3151 kunmap(page);
3152 }
Chris Mason247e7432008-07-17 12:53:51 -04003153 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003154 set_page_dirty(page);
3155 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003156
3157out_unlock:
3158 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003159out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003160 return ret;
3161}
3162
Chris Mason39279cc2007-06-12 06:35:45 -04003163static void btrfs_truncate(struct inode *inode)
3164{
3165 struct btrfs_root *root = BTRFS_I(inode)->root;
3166 int ret;
3167 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04003168 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003169 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003170
3171 if (!S_ISREG(inode->i_mode))
3172 return;
3173 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3174 return;
3175
3176 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003177 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003178
Chris Mason39279cc2007-06-12 06:35:45 -04003179 trans = btrfs_start_transaction(root, 1);
3180 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003181 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003182
Josef Bacik7b128762008-07-24 12:17:14 -04003183 ret = btrfs_orphan_add(trans, inode);
3184 if (ret)
3185 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003186 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05003187 ret = btrfs_truncate_in_trans(trans, root, inode,
3188 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003189 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003190
Josef Bacik7b128762008-07-24 12:17:14 -04003191 ret = btrfs_orphan_del(trans, inode);
3192 BUG_ON(ret);
3193
3194out:
3195 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003196 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003197 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003198 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003199}
3200
Sven Wegener3b963622008-06-09 21:57:42 -04003201/*
3202 * Invalidate a single dcache entry at the root of the filesystem.
3203 * Needed after creation of snapshot or subvolume.
3204 */
3205void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3206 int namelen)
3207{
3208 struct dentry *alias, *entry;
3209 struct qstr qstr;
3210
3211 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3212 if (alias) {
3213 qstr.name = name;
3214 qstr.len = namelen;
3215 /* change me if btrfs ever gets a d_hash operation */
3216 qstr.hash = full_name_hash(qstr.name, qstr.len);
3217 entry = d_lookup(alias, &qstr);
3218 dput(alias);
3219 if (entry) {
3220 d_invalidate(entry);
3221 dput(entry);
3222 }
3223 }
3224}
3225
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003226int btrfs_create_subvol_root(struct btrfs_root *new_root,
3227 struct btrfs_trans_handle *trans, u64 new_dirid,
3228 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003229{
Chris Mason39279cc2007-06-12 06:35:45 -04003230 struct inode *inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003231 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003232
Josef Bacikaec74772008-07-24 12:12:38 -04003233 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003234 new_dirid, block_group, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04003235 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003236 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003237 inode->i_op = &btrfs_dir_inode_operations;
3238 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003239 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003240
Chris Mason39279cc2007-06-12 06:35:45 -04003241 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003242 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003243
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003244 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003245}
3246
Chris Masonedbd8d42007-12-21 16:27:24 -05003247unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003248 struct file_ra_state *ra, struct file *file,
3249 pgoff_t offset, pgoff_t last_index)
3250{
Chris Mason8e7bf942008-04-28 09:02:36 -04003251 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003252
3253#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003254 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3255 return offset;
3256#else
Chris Mason86479a02007-09-10 19:58:16 -04003257 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3258 return offset + req_size;
3259#endif
3260}
3261
Chris Mason39279cc2007-06-12 06:35:45 -04003262struct inode *btrfs_alloc_inode(struct super_block *sb)
3263{
3264 struct btrfs_inode *ei;
3265
3266 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3267 if (!ei)
3268 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003269 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003270 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003271 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3272 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003273 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003274 return &ei->vfs_inode;
3275}
3276
3277void btrfs_destroy_inode(struct inode *inode)
3278{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003279 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003280 WARN_ON(!list_empty(&inode->i_dentry));
3281 WARN_ON(inode->i_data.nrpages);
3282
Josef Bacik33268ea2008-07-24 12:16:36 -04003283 if (BTRFS_I(inode)->i_acl &&
3284 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3285 posix_acl_release(BTRFS_I(inode)->i_acl);
3286 if (BTRFS_I(inode)->i_default_acl &&
3287 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3288 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3289
Yanbcc63ab2008-07-30 16:29:20 -04003290 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003291 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3292 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3293 " list\n", inode->i_ino);
3294 dump_stack();
3295 }
Yanbcc63ab2008-07-30 16:29:20 -04003296 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003297
Chris Masone6dcd2d2008-07-17 12:53:50 -04003298 while(1) {
3299 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3300 if (!ordered)
3301 break;
3302 else {
3303 printk("found ordered extent %Lu %Lu\n",
3304 ordered->file_offset, ordered->len);
3305 btrfs_remove_ordered_extent(inode, ordered);
3306 btrfs_put_ordered_extent(ordered);
3307 btrfs_put_ordered_extent(ordered);
3308 }
3309 }
Chris Mason8c416c92008-01-14 15:10:26 -05003310 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003311 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3312}
3313
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003314#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3315static void init_once(void *foo)
3316#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003317static void init_once(struct kmem_cache * cachep, void *foo)
3318#else
Chris Mason39279cc2007-06-12 06:35:45 -04003319static void init_once(void * foo, struct kmem_cache * cachep,
3320 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003321#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003322{
3323 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3324
3325 inode_init_once(&ei->vfs_inode);
3326}
3327
3328void btrfs_destroy_cachep(void)
3329{
3330 if (btrfs_inode_cachep)
3331 kmem_cache_destroy(btrfs_inode_cachep);
3332 if (btrfs_trans_handle_cachep)
3333 kmem_cache_destroy(btrfs_trans_handle_cachep);
3334 if (btrfs_transaction_cachep)
3335 kmem_cache_destroy(btrfs_transaction_cachep);
3336 if (btrfs_bit_radix_cachep)
3337 kmem_cache_destroy(btrfs_bit_radix_cachep);
3338 if (btrfs_path_cachep)
3339 kmem_cache_destroy(btrfs_path_cachep);
3340}
3341
Chris Mason86479a02007-09-10 19:58:16 -04003342struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003343 unsigned long extra_flags,
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003344#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3345 void (*ctor)(void *)
3346#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003347 void (*ctor)(struct kmem_cache *, void *)
3348#else
Chris Mason92fee662007-07-25 12:31:35 -04003349 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003350 unsigned long)
3351#endif
3352 )
Chris Mason92fee662007-07-25 12:31:35 -04003353{
3354 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3355 SLAB_MEM_SPREAD | extra_flags), ctor
3356#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3357 ,NULL
3358#endif
3359 );
3360}
3361
Chris Mason39279cc2007-06-12 06:35:45 -04003362int btrfs_init_cachep(void)
3363{
Chris Mason86479a02007-09-10 19:58:16 -04003364 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003365 sizeof(struct btrfs_inode),
3366 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003367 if (!btrfs_inode_cachep)
3368 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003369 btrfs_trans_handle_cachep =
3370 btrfs_cache_create("btrfs_trans_handle_cache",
3371 sizeof(struct btrfs_trans_handle),
3372 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003373 if (!btrfs_trans_handle_cachep)
3374 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003375 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003376 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003377 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003378 if (!btrfs_transaction_cachep)
3379 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003380 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003381 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003382 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003383 if (!btrfs_path_cachep)
3384 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003385 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003386 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003387 if (!btrfs_bit_radix_cachep)
3388 goto fail;
3389 return 0;
3390fail:
3391 btrfs_destroy_cachep();
3392 return -ENOMEM;
3393}
3394
3395static int btrfs_getattr(struct vfsmount *mnt,
3396 struct dentry *dentry, struct kstat *stat)
3397{
3398 struct inode *inode = dentry->d_inode;
3399 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003400 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003401 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003402 return 0;
3403}
3404
3405static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3406 struct inode * new_dir,struct dentry *new_dentry)
3407{
3408 struct btrfs_trans_handle *trans;
3409 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3410 struct inode *new_inode = new_dentry->d_inode;
3411 struct inode *old_inode = old_dentry->d_inode;
3412 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003413 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003414 int ret;
3415
3416 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3417 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3418 return -ENOTEMPTY;
3419 }
Chris Mason5f39d392007-10-15 16:14:19 -04003420
Chris Mason1832a6d2007-12-21 16:27:21 -05003421 ret = btrfs_check_free_space(root, 1, 0);
3422 if (ret)
3423 goto out_unlock;
3424
Chris Mason39279cc2007-06-12 06:35:45 -04003425 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003426
Chris Mason39279cc2007-06-12 06:35:45 -04003427 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003428
3429 old_dentry->d_inode->i_nlink++;
3430 old_dir->i_ctime = old_dir->i_mtime = ctime;
3431 new_dir->i_ctime = new_dir->i_mtime = ctime;
3432 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003433
Chris Mason39279cc2007-06-12 06:35:45 -04003434 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3435 if (ret)
3436 goto out_fail;
3437
3438 if (new_inode) {
3439 new_inode->i_ctime = CURRENT_TIME;
3440 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3441 if (ret)
3442 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003443 if (new_inode->i_nlink == 0) {
3444 ret = btrfs_orphan_add(trans, new_inode);
3445 if (ret)
3446 goto out_fail;
3447 }
Chris Mason39279cc2007-06-12 06:35:45 -04003448 }
Chris Mason00e4e6b2008-08-05 11:18:09 -04003449 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003450 if (ret)
3451 goto out_fail;
3452
Chris Mason00e4e6b2008-08-05 11:18:09 -04003453 ret = btrfs_add_link(trans, new_dentry, old_inode, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003454 if (ret)
3455 goto out_fail;
3456
3457out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003458 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003459out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003460 return ret;
3461}
3462
Chris Masonea8c2812008-08-04 23:17:27 -04003463int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3464{
3465 struct list_head *head = &root->fs_info->delalloc_inodes;
3466 struct btrfs_inode *binode;
3467 unsigned long flags;
3468
3469 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3470 while(!list_empty(head)) {
3471 binode = list_entry(head->next, struct btrfs_inode,
3472 delalloc_inodes);
3473 atomic_inc(&binode->vfs_inode.i_count);
3474 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3475 filemap_write_and_wait(binode->vfs_inode.i_mapping);
3476 iput(&binode->vfs_inode);
3477 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3478 }
3479 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3480 return 0;
3481}
3482
Chris Mason39279cc2007-06-12 06:35:45 -04003483static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3484 const char *symname)
3485{
3486 struct btrfs_trans_handle *trans;
3487 struct btrfs_root *root = BTRFS_I(dir)->root;
3488 struct btrfs_path *path;
3489 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003490 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003491 int err;
3492 int drop_inode = 0;
3493 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003494 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04003495 int name_len;
3496 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003497 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003498 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003499 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003500 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003501
3502 name_len = strlen(symname) + 1;
3503 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3504 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003505
Chris Mason1832a6d2007-12-21 16:27:21 -05003506 err = btrfs_check_free_space(root, 1, 0);
3507 if (err)
3508 goto out_fail;
3509
Chris Mason39279cc2007-06-12 06:35:45 -04003510 trans = btrfs_start_transaction(root, 1);
3511 btrfs_set_trans_block_group(trans, dir);
3512
3513 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3514 if (err) {
3515 err = -ENOSPC;
3516 goto out_unlock;
3517 }
3518
Josef Bacikaec74772008-07-24 12:12:38 -04003519 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003520 dentry->d_name.len,
3521 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003522 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3523 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003524 err = PTR_ERR(inode);
3525 if (IS_ERR(inode))
3526 goto out_unlock;
3527
Josef Bacik33268ea2008-07-24 12:16:36 -04003528 err = btrfs_init_acl(inode, dir);
3529 if (err) {
3530 drop_inode = 1;
3531 goto out_unlock;
3532 }
3533
Chris Mason39279cc2007-06-12 06:35:45 -04003534 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003535 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003536 if (err)
3537 drop_inode = 1;
3538 else {
3539 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003540 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003541 inode->i_fop = &btrfs_file_operations;
3542 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003543 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3544 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003545 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003546 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3547 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003548 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04003549 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003550 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003551 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003552 BTRFS_I(inode)->disk_i_size = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04003553 inode->i_mapping->writeback_index = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003554 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003555 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003556 }
3557 dir->i_sb->s_dirt = 1;
3558 btrfs_update_inode_block_group(trans, inode);
3559 btrfs_update_inode_block_group(trans, dir);
3560 if (drop_inode)
3561 goto out_unlock;
3562
3563 path = btrfs_alloc_path();
3564 BUG_ON(!path);
3565 key.objectid = inode->i_ino;
3566 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003567 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3568 datasize = btrfs_file_extent_calc_inline_size(name_len);
3569 err = btrfs_insert_empty_item(trans, root, path, &key,
3570 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003571 if (err) {
3572 drop_inode = 1;
3573 goto out_unlock;
3574 }
Chris Mason5f39d392007-10-15 16:14:19 -04003575 leaf = path->nodes[0];
3576 ei = btrfs_item_ptr(leaf, path->slots[0],
3577 struct btrfs_file_extent_item);
3578 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3579 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003580 BTRFS_FILE_EXTENT_INLINE);
3581 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003582 write_extent_buffer(leaf, symname, ptr, name_len);
3583 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003584 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003585
Chris Mason39279cc2007-06-12 06:35:45 -04003586 inode->i_op = &btrfs_symlink_inode_operations;
3587 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003588 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003589 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003590 err = btrfs_update_inode(trans, root, inode);
3591 if (err)
3592 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003593
3594out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003595 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003596 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003597out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003598 if (drop_inode) {
3599 inode_dec_link_count(inode);
3600 iput(inode);
3601 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003602 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003603 return err;
3604}
Chris Mason16432982008-04-10 10:23:21 -04003605
Chris Masone6dcd2d2008-07-17 12:53:50 -04003606static int btrfs_set_page_dirty(struct page *page)
3607{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003608 return __set_page_dirty_nobuffers(page);
3609}
3610
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003611#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3612static int btrfs_permission(struct inode *inode, int mask)
3613#else
Yanfdebe2b2008-01-14 13:26:08 -05003614static int btrfs_permission(struct inode *inode, int mask,
3615 struct nameidata *nd)
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003616#endif
Yanfdebe2b2008-01-14 13:26:08 -05003617{
3618 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3619 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003620 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003621}
Chris Mason39279cc2007-06-12 06:35:45 -04003622
3623static struct inode_operations btrfs_dir_inode_operations = {
3624 .lookup = btrfs_lookup,
3625 .create = btrfs_create,
3626 .unlink = btrfs_unlink,
3627 .link = btrfs_link,
3628 .mkdir = btrfs_mkdir,
3629 .rmdir = btrfs_rmdir,
3630 .rename = btrfs_rename,
3631 .symlink = btrfs_symlink,
3632 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003633 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003634 .setxattr = generic_setxattr,
3635 .getxattr = generic_getxattr,
3636 .listxattr = btrfs_listxattr,
3637 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003638 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003639};
Chris Mason39279cc2007-06-12 06:35:45 -04003640static struct inode_operations btrfs_dir_ro_inode_operations = {
3641 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003642 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003643};
Chris Mason39279cc2007-06-12 06:35:45 -04003644static struct file_operations btrfs_dir_file_operations = {
3645 .llseek = generic_file_llseek,
3646 .read = generic_read_dir,
3647 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003648 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003649#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003650 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003651#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003652 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003653};
3654
Chris Masond1310b22008-01-24 16:13:08 -05003655static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003656 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003657 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003658 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason3de9d6b2008-08-04 23:17:27 -04003659 .readpage_io_hook = btrfs_readpage_io_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003660 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003661 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003662 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003663 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003664 .set_bit_hook = btrfs_set_bit_hook,
3665 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003666};
3667
Chris Mason39279cc2007-06-12 06:35:45 -04003668static struct address_space_operations btrfs_aops = {
3669 .readpage = btrfs_readpage,
3670 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003671 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003672 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003673 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003674 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003675 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003676 .invalidatepage = btrfs_invalidatepage,
3677 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003678 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003679};
3680
3681static struct address_space_operations btrfs_symlink_aops = {
3682 .readpage = btrfs_readpage,
3683 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003684 .invalidatepage = btrfs_invalidatepage,
3685 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003686};
3687
3688static struct inode_operations btrfs_file_inode_operations = {
3689 .truncate = btrfs_truncate,
3690 .getattr = btrfs_getattr,
3691 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003692 .setxattr = generic_setxattr,
3693 .getxattr = generic_getxattr,
3694 .listxattr = btrfs_listxattr,
3695 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003696 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003697};
Josef Bacik618e21d2007-07-11 10:18:17 -04003698static struct inode_operations btrfs_special_inode_operations = {
3699 .getattr = btrfs_getattr,
3700 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003701 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -04003702 .setxattr = generic_setxattr,
3703 .getxattr = generic_getxattr,
3704 .listxattr = btrfs_listxattr,
3705 .removexattr = generic_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003706};
Chris Mason39279cc2007-06-12 06:35:45 -04003707static struct inode_operations btrfs_symlink_inode_operations = {
3708 .readlink = generic_readlink,
3709 .follow_link = page_follow_link_light,
3710 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003711 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003712};