blob: 43d3f2649ca3877262464507d416a813f5306ea8 [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"
Christoph Hellwig95819c02008-08-28 06:21:17 -040048#include "xattr.h"
Chris Mason39279cc2007-06-12 06:35:45 -040049
50struct btrfs_iget_args {
51 u64 ino;
52 struct btrfs_root *root;
53};
54
55static struct inode_operations btrfs_dir_inode_operations;
56static struct inode_operations btrfs_symlink_inode_operations;
57static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040058static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040059static struct inode_operations btrfs_file_inode_operations;
60static struct address_space_operations btrfs_aops;
61static struct address_space_operations btrfs_symlink_aops;
62static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050063static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040064
65static struct kmem_cache *btrfs_inode_cachep;
66struct kmem_cache *btrfs_trans_handle_cachep;
67struct kmem_cache *btrfs_transaction_cachep;
68struct kmem_cache *btrfs_bit_radix_cachep;
69struct kmem_cache *btrfs_path_cachep;
70
71#define S_SHIFT 12
72static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
73 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
74 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
75 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
76 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
77 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
78 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
79 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
80};
81
Josef Bacik7b128762008-07-24 12:17:14 -040082static void btrfs_truncate(struct inode *inode);
83
Chris Mason1832a6d2007-12-21 16:27:21 -050084int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
85 int for_del)
86{
Chris Masona2135012008-06-25 16:01:30 -040087 u64 total;
88 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050089 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040090 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050091 int ret = 0;
92
Chris Masona2135012008-06-25 16:01:30 -040093 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
94 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
95 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050096 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050097 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050098 else
Chris Masonf9ef6602008-01-03 09:22:38 -050099 thresh = total * 85;
100
101 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500102
Chris Mason1832a6d2007-12-21 16:27:21 -0500103 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
104 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400105 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500106 return ret;
107}
108
Chris Masonbe20aa92007-12-17 20:14:01 -0500109static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400110{
111 struct btrfs_root *root = BTRFS_I(inode)->root;
112 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400113 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400114 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500115 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400116 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500117 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500118 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400119 struct extent_map *em;
120 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
121 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400122
Chris Masonf9295742008-07-17 12:54:14 -0400123 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400124 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500125 btrfs_set_trans_block_group(trans, inode);
126
Chris Masondb945352007-10-15 16:15:53 -0400127 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500128 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500129 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400130
Chris Mason179e29e2007-11-01 11:28:41 -0400131 if (alloc_hint == EXTENT_MAP_INLINE)
132 goto out;
133
Chris Mason3b951512008-04-17 11:29:12 -0400134 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone5a22172008-07-18 20:42:20 -0400135 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400136 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Masone5a22172008-07-18 20:42:20 -0400137 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400138
Chris Masonc59f8952007-12-17 20:14:04 -0500139 while(num_bytes > 0) {
140 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400141 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
142 root->sectorsize, 0, 0,
143 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500144 if (ret) {
145 WARN_ON(1);
146 goto out;
147 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400148 em = alloc_extent_map(GFP_NOFS);
149 em->start = start;
150 em->len = ins.offset;
151 em->block_start = ins.objectid;
152 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masone5a22172008-07-18 20:42:20 -0400153 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400154 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400155 while(1) {
156 spin_lock(&em_tree->lock);
157 ret = add_extent_mapping(em_tree, em);
158 spin_unlock(&em_tree->lock);
159 if (ret != -EEXIST) {
160 free_extent_map(em);
161 break;
162 }
163 btrfs_drop_extent_cache(inode, start,
164 start + ins.offset - 1);
165 }
Chris Masone5a22172008-07-18 20:42:20 -0400166 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400167
Chris Mason98d20f62008-04-14 09:46:10 -0400168 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400169 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Yan Zheng7ea394f2008-08-05 13:05:02 -0400170 ins.offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400171 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400172 if (num_bytes < cur_alloc_size) {
173 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
174 cur_alloc_size);
175 break;
176 }
Chris Masonc59f8952007-12-17 20:14:04 -0500177 num_bytes -= cur_alloc_size;
178 alloc_hint = ins.objectid + ins.offset;
179 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400180 }
Chris Masonb888db22007-08-27 16:49:44 -0400181out:
182 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500183 return ret;
184}
185
186static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
187{
188 u64 extent_start;
189 u64 extent_end;
190 u64 bytenr;
Chris Mason1832a6d2007-12-21 16:27:21 -0500191 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500192 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500193 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400194 struct btrfs_block_group_cache *block_group;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400195 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500196 struct extent_buffer *leaf;
197 int found_type;
198 struct btrfs_path *path;
199 struct btrfs_file_extent_item *item;
200 int ret;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400201 int err = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500202 struct btrfs_key found_key;
203
Chris Masonc31f8832008-01-08 15:46:31 -0500204 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500205 path = btrfs_alloc_path();
206 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400207 trans = btrfs_join_transaction(root, 1);
208 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500209again:
210 ret = btrfs_lookup_file_extent(NULL, root, path,
211 inode->i_ino, start, 0);
212 if (ret < 0) {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400213 err = ret;
214 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500215 }
216
Chris Masonbe20aa92007-12-17 20:14:01 -0500217 if (ret != 0) {
218 if (path->slots[0] == 0)
219 goto not_found;
220 path->slots[0]--;
221 }
222
223 leaf = path->nodes[0];
224 item = btrfs_item_ptr(leaf, path->slots[0],
225 struct btrfs_file_extent_item);
226
227 /* are we inside the extent that was found? */
228 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
229 found_type = btrfs_key_type(&found_key);
230 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400231 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500232 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500233
234 found_type = btrfs_file_extent_type(leaf, item);
235 extent_start = found_key.offset;
236 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500237 u64 extent_num_bytes;
238
239 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
240 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500241 err = 0;
242
Chris Mason1832a6d2007-12-21 16:27:21 -0500243 if (loops && start != extent_start)
244 goto not_found;
245
Chris Masonbe20aa92007-12-17 20:14:01 -0500246 if (start < extent_start || start >= extent_end)
247 goto not_found;
248
Chris Masonbe20aa92007-12-17 20:14:01 -0500249 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
250 if (bytenr == 0)
251 goto not_found;
252
Yan Zheng7ea394f2008-08-05 13:05:02 -0400253 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
Chris Masona68d5932008-05-08 14:11:56 -0400254 goto not_found;
Chris Masonc31f8832008-01-08 15:46:31 -0500255 /*
256 * we may be called by the resizer, make sure we're inside
257 * the limits of the FS
258 */
Chris Masona68d5932008-05-08 14:11:56 -0400259 block_group = btrfs_lookup_block_group(root->fs_info,
260 bytenr);
261 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500262 goto not_found;
263
Yan Zheng7ea394f2008-08-05 13:05:02 -0400264 bytenr += btrfs_file_extent_offset(leaf, item);
265 extent_num_bytes = min(end + 1, extent_end) - start;
266 ret = btrfs_add_ordered_extent(inode, start, bytenr,
267 extent_num_bytes, 1);
268 if (ret) {
269 err = ret;
270 goto out;
271 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500272
Yan Zheng7ea394f2008-08-05 13:05:02 -0400273 btrfs_release_path(root, path);
274 start = extent_end;
275 if (start <= end) {
276 loops++;
277 goto again;
278 }
279 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500280not_found:
Yan Zheng7ea394f2008-08-05 13:05:02 -0400281 btrfs_end_transaction(trans, root);
282 btrfs_free_path(path);
283 return cow_file_range(inode, start, end);
284 }
285out:
286 WARN_ON(err);
287 btrfs_end_transaction(trans, root);
288 btrfs_free_path(path);
289 return err;
Chris Masonbe20aa92007-12-17 20:14:01 -0500290}
291
292static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
293{
294 struct btrfs_root *root = BTRFS_I(inode)->root;
295 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400296
Yanb98b6762008-01-08 15:54:37 -0500297 if (btrfs_test_opt(root, NODATACOW) ||
298 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500299 ret = run_delalloc_nocow(inode, start, end);
300 else
301 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500302
Chris Masonb888db22007-08-27 16:49:44 -0400303 return ret;
304}
305
Chris Mason291d6732008-01-29 15:55:23 -0500306int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500307 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500308{
Chris Masonbcbfce82008-04-22 13:26:47 -0400309 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500310 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500311 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400312 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500313 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500314 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -0400315 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
316 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
317 &root->fs_info->delalloc_inodes);
318 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400319 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500320 }
321 return 0;
322}
323
324int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500325 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500326{
Chris Masonb0c68f82008-01-31 11:05:37 -0500327 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500328 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400329 unsigned long flags;
330
331 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500332 if (end - start + 1 > root->fs_info->delalloc_bytes) {
333 printk("warning: delalloc account %Lu %Lu\n",
334 end - start + 1, root->fs_info->delalloc_bytes);
335 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500336 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500337 } else {
338 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500339 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500340 }
Chris Masonea8c2812008-08-04 23:17:27 -0400341 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
342 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
343 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
344 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400345 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500346 }
347 return 0;
348}
349
Chris Mason239b14b2008-03-24 15:02:07 -0400350int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
351 size_t size, struct bio *bio)
352{
353 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
354 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400355 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400356 u64 length = 0;
357 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400358 int ret;
359
Chris Masonf2d8d742008-04-21 10:03:05 -0400360 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400361 map_tree = &root->fs_info->mapping_tree;
362 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400363 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400364 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400365
Chris Mason239b14b2008-03-24 15:02:07 -0400366 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400367 return 1;
368 }
369 return 0;
370}
371
Chris Mason44b8bd72008-04-16 11:14:51 -0400372int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400373 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500374{
Chris Mason065631f2008-02-20 12:07:25 -0500375 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500376 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400377
Chris Mason3edf7d32008-07-18 06:17:13 -0400378 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400379 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400380
Chris Mason8b712842008-06-11 16:50:36 -0400381 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400382}
383
384int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
385 int mirror_num)
386{
387 struct btrfs_root *root = BTRFS_I(inode)->root;
388 int ret = 0;
389
Chris Masone6dcd2d2008-07-17 12:53:50 -0400390 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
391 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500392
Yan Zheng7ea394f2008-08-05 13:05:02 -0400393 if (btrfs_test_opt(root, NODATASUM) ||
394 btrfs_test_flag(inode, NODATASUM)) {
395 goto mapit;
396 }
397
Chris Mason4d1b5fb2008-08-20 09:44:52 -0400398 if (!(rw & (1 << BIO_RW))) {
399 btrfs_lookup_bio_sums(root, inode, bio);
400 goto mapit;
401 }
Chris Mason44b8bd72008-04-16 11:14:51 -0400402 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
403 inode, rw, bio, mirror_num,
404 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400405mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400406 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500407}
Chris Mason6885f302008-02-20 16:11:05 -0500408
Chris Masonba1da2f2008-07-17 12:54:15 -0400409static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400410 struct inode *inode, u64 file_offset,
411 struct list_head *list)
412{
413 struct list_head *cur;
414 struct btrfs_ordered_sum *sum;
415
416 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400417 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400418 sum = list_entry(cur, struct btrfs_ordered_sum, list);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400419 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
420 inode, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400421 }
422 return 0;
423}
424
Chris Masonea8c2812008-08-04 23:17:27 -0400425int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
426{
427 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
428 GFP_NOFS);
429}
430
Chris Mason247e7432008-07-17 12:53:51 -0400431struct btrfs_writepage_fixup {
432 struct page *page;
433 struct btrfs_work work;
434};
435
436/* see btrfs_writepage_start_hook for details on why this is required */
437void btrfs_writepage_fixup_worker(struct btrfs_work *work)
438{
439 struct btrfs_writepage_fixup *fixup;
440 struct btrfs_ordered_extent *ordered;
441 struct page *page;
442 struct inode *inode;
443 u64 page_start;
444 u64 page_end;
445
446 fixup = container_of(work, struct btrfs_writepage_fixup, work);
447 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400448again:
Chris Mason247e7432008-07-17 12:53:51 -0400449 lock_page(page);
450 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
451 ClearPageChecked(page);
452 goto out_page;
453 }
454
455 inode = page->mapping->host;
456 page_start = page_offset(page);
457 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
458
459 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400460
461 /* already ordered? We're done */
462 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
463 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400464 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400465 }
466
467 ordered = btrfs_lookup_ordered_extent(inode, page_start);
468 if (ordered) {
469 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
470 page_end, GFP_NOFS);
471 unlock_page(page);
472 btrfs_start_ordered_extent(inode, ordered, 1);
473 goto again;
474 }
Chris Mason247e7432008-07-17 12:53:51 -0400475
Chris Masonea8c2812008-08-04 23:17:27 -0400476 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -0400477 ClearPageChecked(page);
478out:
479 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
480out_page:
481 unlock_page(page);
482 page_cache_release(page);
483}
484
485/*
486 * There are a few paths in the higher layers of the kernel that directly
487 * set the page dirty bit without asking the filesystem if it is a
488 * good idea. This causes problems because we want to make sure COW
489 * properly happens and the data=ordered rules are followed.
490 *
491 * In our case any range that doesn't have the EXTENT_ORDERED bit set
492 * hasn't been properly setup for IO. We kick off an async process
493 * to fix it up. The async helper will wait for ordered extents, set
494 * the delalloc bit and make it safe to write the page.
495 */
496int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
497{
498 struct inode *inode = page->mapping->host;
499 struct btrfs_writepage_fixup *fixup;
500 struct btrfs_root *root = BTRFS_I(inode)->root;
501 int ret;
502
503 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
504 EXTENT_ORDERED, 0);
505 if (ret)
506 return 0;
507
508 if (PageChecked(page))
509 return -EAGAIN;
510
511 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
512 if (!fixup)
513 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400514
Chris Mason247e7432008-07-17 12:53:51 -0400515 SetPageChecked(page);
516 page_cache_get(page);
517 fixup->work.func = btrfs_writepage_fixup_worker;
518 fixup->page = page;
519 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
520 return -EAGAIN;
521}
522
Chris Mason211f90e2008-07-18 11:56:15 -0400523static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400524{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400525 struct btrfs_root *root = BTRFS_I(inode)->root;
526 struct btrfs_trans_handle *trans;
527 struct btrfs_ordered_extent *ordered_extent;
528 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
529 u64 alloc_hint = 0;
530 struct list_head list;
531 struct btrfs_key ins;
532 int ret;
533
534 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400535 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400536 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400537
Chris Masonf9295742008-07-17 12:54:14 -0400538 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400539
540 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
541 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400542 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
543 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400544
545 lock_extent(io_tree, ordered_extent->file_offset,
546 ordered_extent->file_offset + ordered_extent->len - 1,
547 GFP_NOFS);
548
549 INIT_LIST_HEAD(&list);
550
551 ins.objectid = ordered_extent->start;
552 ins.offset = ordered_extent->len;
553 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400554
Chris Masone6dcd2d2008-07-17 12:53:50 -0400555 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
556 trans->transid, inode->i_ino,
557 ordered_extent->file_offset, &ins);
558 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400559
560 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400561
Chris Masone6dcd2d2008-07-17 12:53:50 -0400562 ret = btrfs_drop_extents(trans, root, inode,
563 ordered_extent->file_offset,
564 ordered_extent->file_offset +
565 ordered_extent->len,
566 ordered_extent->file_offset, &alloc_hint);
567 BUG_ON(ret);
568 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
569 ordered_extent->file_offset,
570 ordered_extent->start,
571 ordered_extent->len,
572 ordered_extent->len, 0);
573 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400574
Chris Masone6dcd2d2008-07-17 12:53:50 -0400575 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
576 ordered_extent->file_offset +
577 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400578 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
579
Chris Masone6dcd2d2008-07-17 12:53:50 -0400580 inode->i_blocks += ordered_extent->len >> 9;
581 unlock_extent(io_tree, ordered_extent->file_offset,
582 ordered_extent->file_offset + ordered_extent->len - 1,
583 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400584nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -0400585 add_pending_csums(trans, inode, ordered_extent->file_offset,
586 &ordered_extent->list);
587
Chris Masondbe674a2008-07-17 12:54:05 -0400588 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400589 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400590
Chris Masone6dcd2d2008-07-17 12:53:50 -0400591 /* once for us */
592 btrfs_put_ordered_extent(ordered_extent);
593 /* once for the tree */
594 btrfs_put_ordered_extent(ordered_extent);
595
596 btrfs_update_inode(trans, root, inode);
597 btrfs_end_transaction(trans, root);
598 return 0;
599}
600
Chris Mason211f90e2008-07-18 11:56:15 -0400601int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
602 struct extent_state *state, int uptodate)
603{
604 return btrfs_finish_ordered_io(page->mapping->host, start, end);
605}
606
Chris Mason7e383262008-04-09 16:28:12 -0400607struct io_failure_record {
608 struct page *page;
609 u64 start;
610 u64 len;
611 u64 logical;
612 int last_mirror;
613};
614
Chris Mason1259ab72008-05-12 13:39:03 -0400615int btrfs_io_failed_hook(struct bio *failed_bio,
616 struct page *page, u64 start, u64 end,
617 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400618{
619 struct io_failure_record *failrec = NULL;
620 u64 private;
621 struct extent_map *em;
622 struct inode *inode = page->mapping->host;
623 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400624 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400625 struct bio *bio;
626 int num_copies;
627 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400628 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400629 u64 logical;
630
631 ret = get_state_private(failure_tree, start, &private);
632 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400633 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
634 if (!failrec)
635 return -ENOMEM;
636 failrec->start = start;
637 failrec->len = end - start + 1;
638 failrec->last_mirror = 0;
639
Chris Mason3b951512008-04-17 11:29:12 -0400640 spin_lock(&em_tree->lock);
641 em = lookup_extent_mapping(em_tree, start, failrec->len);
642 if (em->start > start || em->start + em->len < start) {
643 free_extent_map(em);
644 em = NULL;
645 }
646 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400647
648 if (!em || IS_ERR(em)) {
649 kfree(failrec);
650 return -EIO;
651 }
652 logical = start - em->start;
653 logical = em->block_start + logical;
654 failrec->logical = logical;
655 free_extent_map(em);
656 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
657 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400658 set_state_private(failure_tree, start,
659 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400660 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400661 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400662 }
663 num_copies = btrfs_num_copies(
664 &BTRFS_I(inode)->root->fs_info->mapping_tree,
665 failrec->logical, failrec->len);
666 failrec->last_mirror++;
667 if (!state) {
668 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
669 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
670 failrec->start,
671 EXTENT_LOCKED);
672 if (state && state->start != failrec->start)
673 state = NULL;
674 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
675 }
676 if (!state || failrec->last_mirror > num_copies) {
677 set_state_private(failure_tree, failrec->start, 0);
678 clear_extent_bits(failure_tree, failrec->start,
679 failrec->start + failrec->len - 1,
680 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
681 kfree(failrec);
682 return -EIO;
683 }
684 bio = bio_alloc(GFP_NOFS, 1);
685 bio->bi_private = state;
686 bio->bi_end_io = failed_bio->bi_end_io;
687 bio->bi_sector = failrec->logical >> 9;
688 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400689 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400690 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400691 if (failed_bio->bi_rw & (1 << BIO_RW))
692 rw = WRITE;
693 else
694 rw = READ;
695
696 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
697 failrec->last_mirror);
698 return 0;
699}
700
701int btrfs_clean_io_failures(struct inode *inode, u64 start)
702{
703 u64 private;
704 u64 private_failure;
705 struct io_failure_record *failure;
706 int ret;
707
708 private = 0;
709 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
710 (u64)-1, 1, EXTENT_DIRTY)) {
711 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
712 start, &private_failure);
713 if (ret == 0) {
714 failure = (struct io_failure_record *)(unsigned long)
715 private_failure;
716 set_state_private(&BTRFS_I(inode)->io_failure_tree,
717 failure->start, 0);
718 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
719 failure->start,
720 failure->start + failure->len - 1,
721 EXTENT_DIRTY | EXTENT_LOCKED,
722 GFP_NOFS);
723 kfree(failure);
724 }
725 }
Chris Mason7e383262008-04-09 16:28:12 -0400726 return 0;
727}
728
Chris Mason70dec802008-01-29 09:59:12 -0500729int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
730 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400731{
Chris Mason35ebb932007-10-30 16:56:53 -0400732 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400733 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500734 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400735 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500736 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400737 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400738 struct btrfs_root *root = BTRFS_I(inode)->root;
739 u32 csum = ~(u32)0;
Jens Axboebbf0d0062007-10-19 09:23:07 -0400740 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500741
Yanb98b6762008-01-08 15:54:37 -0500742 if (btrfs_test_opt(root, NODATASUM) ||
743 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500744 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500745 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500746 private = state->private;
747 ret = 0;
748 } else {
749 ret = get_state_private(io_tree, start, &private);
750 }
Jens Axboebbf0d0062007-10-19 09:23:07 -0400751 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400752 kaddr = kmap_atomic(page, KM_IRQ0);
753 if (ret) {
754 goto zeroit;
755 }
Chris Masonff79f812007-10-15 16:22:25 -0400756 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
757 btrfs_csum_final(csum, (char *)&csum);
758 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400759 goto zeroit;
760 }
761 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -0400762 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400763
764 /* if the io failure tree for this inode is non-empty,
765 * check to see if we've recovered from a failed IO
766 */
Chris Mason1259ab72008-05-12 13:39:03 -0400767 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400768 return 0;
769
770zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500771 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
772 page->mapping->host->i_ino, (unsigned long long)start, csum,
773 private);
Chris Masondb945352007-10-15 16:15:53 -0400774 memset(kaddr + offset, 1, end - start + 1);
775 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400776 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d0062007-10-19 09:23:07 -0400777 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400778 if (private == 0)
779 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400780 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400781}
Chris Masonb888db22007-08-27 16:49:44 -0400782
Josef Bacik7b128762008-07-24 12:17:14 -0400783/*
784 * This creates an orphan entry for the given inode in case something goes
785 * wrong in the middle of an unlink/truncate.
786 */
787int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
788{
789 struct btrfs_root *root = BTRFS_I(inode)->root;
790 int ret = 0;
791
Yanbcc63ab2008-07-30 16:29:20 -0400792 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400793
794 /* already on the orphan list, we're good */
795 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400796 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400797 return 0;
798 }
799
800 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
801
Yanbcc63ab2008-07-30 16:29:20 -0400802 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400803
804 /*
805 * insert an orphan item to track this unlinked/truncated file
806 */
807 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
808
809 return ret;
810}
811
812/*
813 * We have done the truncate/delete so we can go ahead and remove the orphan
814 * item for this particular inode.
815 */
816int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
817{
818 struct btrfs_root *root = BTRFS_I(inode)->root;
819 int ret = 0;
820
Yanbcc63ab2008-07-30 16:29:20 -0400821 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400822
823 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400824 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400825 return 0;
826 }
827
828 list_del_init(&BTRFS_I(inode)->i_orphan);
829 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400830 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400831 return 0;
832 }
833
Yanbcc63ab2008-07-30 16:29:20 -0400834 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400835
836 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
837
838 return ret;
839}
840
841/*
842 * this cleans up any orphans that may be left on the list from the last use
843 * of this root.
844 */
845void btrfs_orphan_cleanup(struct btrfs_root *root)
846{
847 struct btrfs_path *path;
848 struct extent_buffer *leaf;
849 struct btrfs_item *item;
850 struct btrfs_key key, found_key;
851 struct btrfs_trans_handle *trans;
852 struct inode *inode;
853 int ret = 0, nr_unlink = 0, nr_truncate = 0;
854
855 /* don't do orphan cleanup if the fs is readonly. */
856 if (root->inode->i_sb->s_flags & MS_RDONLY)
857 return;
858
859 path = btrfs_alloc_path();
860 if (!path)
861 return;
862 path->reada = -1;
863
864 key.objectid = BTRFS_ORPHAN_OBJECTID;
865 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
866 key.offset = (u64)-1;
867
868 trans = btrfs_start_transaction(root, 1);
869 btrfs_set_trans_block_group(trans, root->inode);
870
871 while (1) {
872 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
873 if (ret < 0) {
874 printk(KERN_ERR "Error searching slot for orphan: %d"
875 "\n", ret);
876 break;
877 }
878
879 /*
880 * if ret == 0 means we found what we were searching for, which
881 * is weird, but possible, so only screw with path if we didnt
882 * find the key and see if we have stuff that matches
883 */
884 if (ret > 0) {
885 if (path->slots[0] == 0)
886 break;
887 path->slots[0]--;
888 }
889
890 /* pull out the item */
891 leaf = path->nodes[0];
892 item = btrfs_item_nr(leaf, path->slots[0]);
893 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
894
895 /* make sure the item matches what we want */
896 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
897 break;
898 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
899 break;
900
901 /* release the path since we're done with it */
902 btrfs_release_path(root, path);
903
904 /*
905 * this is where we are basically btrfs_lookup, without the
906 * crossing root thing. we store the inode number in the
907 * offset of the orphan item.
908 */
909 inode = btrfs_iget_locked(root->inode->i_sb,
910 found_key.offset, root);
911 if (!inode)
912 break;
913
914 if (inode->i_state & I_NEW) {
915 BTRFS_I(inode)->root = root;
916
917 /* have to set the location manually */
918 BTRFS_I(inode)->location.objectid = inode->i_ino;
919 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
920 BTRFS_I(inode)->location.offset = 0;
921
922 btrfs_read_locked_inode(inode);
923 unlock_new_inode(inode);
924 }
925
926 /*
927 * add this inode to the orphan list so btrfs_orphan_del does
928 * the proper thing when we hit it
929 */
Yanbcc63ab2008-07-30 16:29:20 -0400930 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400931 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -0400932 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400933
934 /*
935 * if this is a bad inode, means we actually succeeded in
936 * removing the inode, but not the orphan record, which means
937 * we need to manually delete the orphan since iput will just
938 * do a destroy_inode
939 */
940 if (is_bad_inode(inode)) {
941 btrfs_orphan_del(trans, inode);
942 iput(inode);
943 continue;
944 }
945
946 /* if we have links, this was a truncate, lets do that */
947 if (inode->i_nlink) {
948 nr_truncate++;
949 btrfs_truncate(inode);
950 } else {
951 nr_unlink++;
952 }
953
954 /* this will do delete_inode and everything for us */
955 iput(inode);
956 }
957
958 if (nr_unlink)
959 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
960 if (nr_truncate)
961 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
962
963 btrfs_free_path(path);
964 btrfs_end_transaction(trans, root);
965}
966
Chris Mason39279cc2007-06-12 06:35:45 -0400967void btrfs_read_locked_inode(struct inode *inode)
968{
969 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400970 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400971 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400972 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400973 struct btrfs_root *root = BTRFS_I(inode)->root;
974 struct btrfs_key location;
975 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400976 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400977 int ret;
978
979 path = btrfs_alloc_path();
980 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400981 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500982
Chris Mason39279cc2007-06-12 06:35:45 -0400983 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400984 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400985 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400986
Chris Mason5f39d392007-10-15 16:14:19 -0400987 leaf = path->nodes[0];
988 inode_item = btrfs_item_ptr(leaf, path->slots[0],
989 struct btrfs_inode_item);
990
991 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
992 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
993 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
994 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -0400995 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400996
997 tspec = btrfs_inode_atime(inode_item);
998 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
999 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1000
1001 tspec = btrfs_inode_mtime(inode_item);
1002 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1003 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1004
1005 tspec = btrfs_inode_ctime(inode_item);
1006 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1007 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1008
1009 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1010 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -04001011 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001012 rdev = btrfs_inode_rdev(leaf, inode_item);
1013
Josef Bacikaec74772008-07-24 12:12:38 -04001014 BTRFS_I(inode)->index_cnt = (u64)-1;
1015
Chris Mason5f39d392007-10-15 16:14:19 -04001016 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001017 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1018 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001019 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001020 if (!BTRFS_I(inode)->block_group) {
1021 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001022 NULL, 0,
1023 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001024 }
Chris Mason39279cc2007-06-12 06:35:45 -04001025 btrfs_free_path(path);
1026 inode_item = NULL;
1027
Chris Mason39279cc2007-06-12 06:35:45 -04001028 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001029 case S_IFREG:
1030 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001031 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001032 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001033 inode->i_fop = &btrfs_file_operations;
1034 inode->i_op = &btrfs_file_inode_operations;
1035 break;
1036 case S_IFDIR:
1037 inode->i_fop = &btrfs_dir_file_operations;
1038 if (root == root->fs_info->tree_root)
1039 inode->i_op = &btrfs_dir_ro_inode_operations;
1040 else
1041 inode->i_op = &btrfs_dir_inode_operations;
1042 break;
1043 case S_IFLNK:
1044 inode->i_op = &btrfs_symlink_inode_operations;
1045 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001046 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001047 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001048 default:
1049 init_special_inode(inode, inode->i_mode, rdev);
1050 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001051 }
1052 return;
1053
1054make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001055 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001056 make_bad_inode(inode);
1057}
1058
Chris Mason5f39d392007-10-15 16:14:19 -04001059static void fill_inode_item(struct extent_buffer *leaf,
1060 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001061 struct inode *inode)
1062{
Chris Mason5f39d392007-10-15 16:14:19 -04001063 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1064 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001065 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001066 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1067 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1068
1069 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1070 inode->i_atime.tv_sec);
1071 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1072 inode->i_atime.tv_nsec);
1073
1074 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1075 inode->i_mtime.tv_sec);
1076 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1077 inode->i_mtime.tv_nsec);
1078
1079 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1080 inode->i_ctime.tv_sec);
1081 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1082 inode->i_ctime.tv_nsec);
1083
1084 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1085 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1086 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001087 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001088 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001089 BTRFS_I(inode)->block_group->key.objectid);
1090}
1091
Chris Masonba1da2f2008-07-17 12:54:15 -04001092int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001093 struct btrfs_root *root,
1094 struct inode *inode)
1095{
1096 struct btrfs_inode_item *inode_item;
1097 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001098 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001099 int ret;
1100
1101 path = btrfs_alloc_path();
1102 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001103 ret = btrfs_lookup_inode(trans, root, path,
1104 &BTRFS_I(inode)->location, 1);
1105 if (ret) {
1106 if (ret > 0)
1107 ret = -ENOENT;
1108 goto failed;
1109 }
1110
Chris Mason5f39d392007-10-15 16:14:19 -04001111 leaf = path->nodes[0];
1112 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001113 struct btrfs_inode_item);
1114
Chris Mason5f39d392007-10-15 16:14:19 -04001115 fill_inode_item(leaf, inode_item, inode);
1116 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001117 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001118 ret = 0;
1119failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001120 btrfs_free_path(path);
1121 return ret;
1122}
1123
1124
1125static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1126 struct btrfs_root *root,
1127 struct inode *dir,
1128 struct dentry *dentry)
1129{
1130 struct btrfs_path *path;
1131 const char *name = dentry->d_name.name;
1132 int name_len = dentry->d_name.len;
1133 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001134 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001135 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001136 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001137 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001138
1139 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001140 if (!path) {
1141 ret = -ENOMEM;
1142 goto err;
1143 }
1144
Chris Mason39279cc2007-06-12 06:35:45 -04001145 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1146 name, name_len, -1);
1147 if (IS_ERR(di)) {
1148 ret = PTR_ERR(di);
1149 goto err;
1150 }
1151 if (!di) {
1152 ret = -ENOENT;
1153 goto err;
1154 }
Chris Mason5f39d392007-10-15 16:14:19 -04001155 leaf = path->nodes[0];
1156 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001157 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001158 if (ret)
1159 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001160 btrfs_release_path(root, path);
1161
Josef Bacikaec74772008-07-24 12:12:38 -04001162 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1163 dentry->d_inode->i_ino,
1164 dentry->d_parent->d_inode->i_ino, &index);
1165 if (ret) {
1166 printk("failed to delete reference to %.*s, "
1167 "inode %lu parent %lu\n", name_len, name,
1168 dentry->d_inode->i_ino,
1169 dentry->d_parent->d_inode->i_ino);
1170 goto err;
1171 }
1172
Chris Mason39279cc2007-06-12 06:35:45 -04001173 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001174 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001175 if (IS_ERR(di)) {
1176 ret = PTR_ERR(di);
1177 goto err;
1178 }
1179 if (!di) {
1180 ret = -ENOENT;
1181 goto err;
1182 }
1183 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001184 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001185
1186 dentry->d_inode->i_ctime = dir->i_ctime;
1187err:
1188 btrfs_free_path(path);
1189 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001190 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001191 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001192 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001193#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1194 dentry->d_inode->i_nlink--;
1195#else
Chris Mason39279cc2007-06-12 06:35:45 -04001196 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001197#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001198 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001199 dir->i_sb->s_dirt = 1;
1200 }
1201 return ret;
1202}
1203
1204static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1205{
1206 struct btrfs_root *root;
1207 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001208 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001209 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001210 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001211
1212 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001213
1214 ret = btrfs_check_free_space(root, 1, 1);
1215 if (ret)
1216 goto fail;
1217
Chris Mason39279cc2007-06-12 06:35:45 -04001218 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001219
Chris Mason39279cc2007-06-12 06:35:45 -04001220 btrfs_set_trans_block_group(trans, dir);
1221 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Josef Bacik7b128762008-07-24 12:17:14 -04001222
1223 if (inode->i_nlink == 0)
1224 ret = btrfs_orphan_add(trans, inode);
1225
Chris Masond3c2fdc2007-09-17 10:58:06 -04001226 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001227
Chris Mason89ce8a62008-06-25 16:01:31 -04001228 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001229fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001230 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001231 return ret;
1232}
1233
1234static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1235{
1236 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001237 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001238 int ret;
1239 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001240 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001241 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001242
Chris Mason925baed2008-06-25 16:01:30 -04001243 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001244 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001245 }
Yan134d4512007-10-25 15:49:25 -04001246
Chris Mason1832a6d2007-12-21 16:27:21 -05001247 ret = btrfs_check_free_space(root, 1, 1);
1248 if (ret)
1249 goto fail;
1250
Chris Mason39279cc2007-06-12 06:35:45 -04001251 trans = btrfs_start_transaction(root, 1);
1252 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001253
Josef Bacik7b128762008-07-24 12:17:14 -04001254 err = btrfs_orphan_add(trans, inode);
1255 if (err)
1256 goto fail_trans;
1257
Chris Mason39279cc2007-06-12 06:35:45 -04001258 /* now the directory is empty */
1259 err = btrfs_unlink_trans(trans, root, dir, dentry);
1260 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001261 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001262 }
Chris Mason39544012007-12-12 14:38:19 -05001263
Josef Bacik7b128762008-07-24 12:17:14 -04001264fail_trans:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001265 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001266 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001267fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001268 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001269
Chris Mason39279cc2007-06-12 06:35:45 -04001270 if (ret && !err)
1271 err = ret;
1272 return err;
1273}
1274
Chris Mason39279cc2007-06-12 06:35:45 -04001275/*
Chris Mason39279cc2007-06-12 06:35:45 -04001276 * this can truncate away extent items, csum items and directory items.
1277 * It starts at a high offset and removes keys until it can't find
1278 * any higher than i_size.
1279 *
1280 * csum items that cross the new i_size are truncated to the new size
1281 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001282 *
1283 * min_type is the minimum key type to truncate down to. If set to 0, this
1284 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001285 */
1286static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1287 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001288 struct inode *inode,
1289 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001290{
1291 int ret;
1292 struct btrfs_path *path;
1293 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001294 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001295 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001296 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001297 struct btrfs_file_extent_item *fi;
1298 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001299 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001300 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001301 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001302 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001303 int found_extent;
1304 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001305 int pending_del_nr = 0;
1306 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001307 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001308 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001309
Chris Mason3b951512008-04-17 11:29:12 -04001310 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001311 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001312 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001313 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001314
Chris Mason39279cc2007-06-12 06:35:45 -04001315 /* FIXME, add redo link to tree so we don't leak on crash */
1316 key.objectid = inode->i_ino;
1317 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001318 key.type = (u8)-1;
1319
Chris Mason85e21ba2008-01-29 15:11:36 -05001320 btrfs_init_path(path);
1321search_again:
1322 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1323 if (ret < 0) {
1324 goto error;
1325 }
1326 if (ret > 0) {
1327 BUG_ON(path->slots[0] == 0);
1328 path->slots[0]--;
1329 }
1330
Chris Mason39279cc2007-06-12 06:35:45 -04001331 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001332 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001333 leaf = path->nodes[0];
1334 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1335 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001336
Chris Mason5f39d392007-10-15 16:14:19 -04001337 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001338 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001339
Chris Mason85e21ba2008-01-29 15:11:36 -05001340 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001341 break;
1342
Chris Mason5f39d392007-10-15 16:14:19 -04001343 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001344 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001345 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001346 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001347 extent_type = btrfs_file_extent_type(leaf, fi);
1348 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001349 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001350 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001351 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1352 struct btrfs_item *item = btrfs_item_nr(leaf,
1353 path->slots[0]);
1354 item_end += btrfs_file_extent_inline_len(leaf,
1355 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001356 }
Yan008630c2007-11-07 13:31:09 -05001357 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001358 }
1359 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1360 ret = btrfs_csum_truncate(trans, root, path,
1361 inode->i_size);
1362 BUG_ON(ret);
1363 }
Yan008630c2007-11-07 13:31:09 -05001364 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001365 if (found_type == BTRFS_DIR_ITEM_KEY) {
1366 found_type = BTRFS_INODE_ITEM_KEY;
1367 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1368 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001369 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1370 found_type = BTRFS_XATTR_ITEM_KEY;
1371 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1372 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001373 } else if (found_type) {
1374 found_type--;
1375 } else {
1376 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001377 }
Yana61721d2007-09-17 11:08:38 -04001378 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001379 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001380 }
Chris Mason5f39d392007-10-15 16:14:19 -04001381 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001382 del_item = 1;
1383 else
1384 del_item = 0;
1385 found_extent = 0;
1386
1387 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001388 if (found_type != BTRFS_EXTENT_DATA_KEY)
1389 goto delete;
1390
1391 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001392 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001393 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001394 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001395 u64 orig_num_bytes =
1396 btrfs_file_extent_num_bytes(leaf, fi);
1397 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001398 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001399 extent_num_bytes = extent_num_bytes &
1400 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001401 btrfs_set_file_extent_num_bytes(leaf, fi,
1402 extent_num_bytes);
1403 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001404 extent_num_bytes);
1405 if (extent_start != 0)
1406 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001407 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001408 } else {
Chris Masondb945352007-10-15 16:15:53 -04001409 extent_num_bytes =
1410 btrfs_file_extent_disk_num_bytes(leaf,
1411 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001412 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001413 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001414 if (extent_start != 0) {
1415 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001416 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001417 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001418 root_gen = btrfs_header_generation(leaf);
1419 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001420 }
Chris Mason90692182008-02-08 13:49:28 -05001421 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1422 if (!del_item) {
1423 u32 newsize = inode->i_size - found_key.offset;
1424 dec_i_blocks(inode, item_end + 1 -
1425 found_key.offset - newsize);
1426 newsize =
1427 btrfs_file_extent_calc_inline_size(newsize);
1428 ret = btrfs_truncate_item(trans, root, path,
1429 newsize, 1);
1430 BUG_ON(ret);
1431 } else {
1432 dec_i_blocks(inode, item_end + 1 -
1433 found_key.offset);
1434 }
Chris Mason39279cc2007-06-12 06:35:45 -04001435 }
Chris Mason179e29e2007-11-01 11:28:41 -04001436delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001437 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001438 if (!pending_del_nr) {
1439 /* no pending yet, add ourselves */
1440 pending_del_slot = path->slots[0];
1441 pending_del_nr = 1;
1442 } else if (pending_del_nr &&
1443 path->slots[0] + 1 == pending_del_slot) {
1444 /* hop on the pending chunk */
1445 pending_del_nr++;
1446 pending_del_slot = path->slots[0];
1447 } else {
1448 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1449 }
Chris Mason39279cc2007-06-12 06:35:45 -04001450 } else {
1451 break;
1452 }
Chris Mason39279cc2007-06-12 06:35:45 -04001453 if (found_extent) {
1454 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001455 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001456 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001457 root_gen, inode->i_ino,
1458 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001459 BUG_ON(ret);
1460 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001461next:
1462 if (path->slots[0] == 0) {
1463 if (pending_del_nr)
1464 goto del_pending;
1465 btrfs_release_path(root, path);
1466 goto search_again;
1467 }
1468
1469 path->slots[0]--;
1470 if (pending_del_nr &&
1471 path->slots[0] + 1 != pending_del_slot) {
1472 struct btrfs_key debug;
1473del_pending:
1474 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1475 pending_del_slot);
1476 ret = btrfs_del_items(trans, root, path,
1477 pending_del_slot,
1478 pending_del_nr);
1479 BUG_ON(ret);
1480 pending_del_nr = 0;
1481 btrfs_release_path(root, path);
1482 goto search_again;
1483 }
Chris Mason39279cc2007-06-12 06:35:45 -04001484 }
1485 ret = 0;
1486error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001487 if (pending_del_nr) {
1488 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1489 pending_del_nr);
1490 }
Chris Mason39279cc2007-06-12 06:35:45 -04001491 btrfs_free_path(path);
1492 inode->i_sb->s_dirt = 1;
1493 return ret;
1494}
1495
Chris Masona52d9a82007-08-27 16:49:44 -04001496/*
1497 * taken from block_truncate_page, but does cow as it zeros out
1498 * any bytes left in the last page in the file.
1499 */
1500static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1501{
1502 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001503 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001504 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1505 struct btrfs_ordered_extent *ordered;
1506 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001507 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001508 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1509 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1510 struct page *page;
1511 int ret = 0;
1512 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001513 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001514
1515 if ((offset & (blocksize - 1)) == 0)
1516 goto out;
1517
1518 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001519again:
Chris Masona52d9a82007-08-27 16:49:44 -04001520 page = grab_cache_page(mapping, index);
1521 if (!page)
1522 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001523
1524 page_start = page_offset(page);
1525 page_end = page_start + PAGE_CACHE_SIZE - 1;
1526
Chris Masona52d9a82007-08-27 16:49:44 -04001527 if (!PageUptodate(page)) {
1528 ret = btrfs_readpage(NULL, page);
1529 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001530 if (page->mapping != mapping) {
1531 unlock_page(page);
1532 page_cache_release(page);
1533 goto again;
1534 }
Chris Masona52d9a82007-08-27 16:49:44 -04001535 if (!PageUptodate(page)) {
1536 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001537 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001538 }
1539 }
Chris Mason211c17f2008-05-15 09:13:45 -04001540 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001541
1542 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1543 set_page_extent_mapped(page);
1544
1545 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1546 if (ordered) {
1547 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1548 unlock_page(page);
1549 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001550 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001551 btrfs_put_ordered_extent(ordered);
1552 goto again;
1553 }
1554
Chris Masonea8c2812008-08-04 23:17:27 -04001555 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001556 ret = 0;
1557 if (offset != PAGE_CACHE_SIZE) {
1558 kaddr = kmap(page);
1559 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1560 flush_dcache_page(page);
1561 kunmap(page);
1562 }
Chris Mason247e7432008-07-17 12:53:51 -04001563 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001564 set_page_dirty(page);
1565 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001566
Chris Mason89642222008-07-24 09:41:53 -04001567out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001568 unlock_page(page);
1569 page_cache_release(page);
1570out:
1571 return ret;
1572}
1573
1574static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1575{
1576 struct inode *inode = dentry->d_inode;
1577 int err;
1578
1579 err = inode_change_ok(inode, attr);
1580 if (err)
1581 return err;
1582
1583 if (S_ISREG(inode->i_mode) &&
1584 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1585 struct btrfs_trans_handle *trans;
1586 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001587 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001588
Chris Mason5f39d392007-10-15 16:14:19 -04001589 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001590 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001591 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001592 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001593 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001594
Chris Mason1b0f7c22008-01-30 14:33:02 -05001595 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001596 goto out;
1597
Chris Mason1832a6d2007-12-21 16:27:21 -05001598 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001599 if (err)
1600 goto fail;
1601
Chris Mason39279cc2007-06-12 06:35:45 -04001602 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1603
Chris Mason5f564062008-01-22 16:47:59 -05001604 hole_size = block_end - hole_start;
Chris Mason7c2fe322008-08-20 08:51:50 -04001605 while(1) {
1606 struct btrfs_ordered_extent *ordered;
1607 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1608
1609 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1610 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
1611 if (ordered) {
1612 unlock_extent(io_tree, hole_start,
1613 block_end - 1, GFP_NOFS);
1614 btrfs_put_ordered_extent(ordered);
1615 } else {
1616 break;
1617 }
1618 }
Chris Mason39279cc2007-06-12 06:35:45 -04001619
Chris Mason39279cc2007-06-12 06:35:45 -04001620 trans = btrfs_start_transaction(root, 1);
1621 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001622 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001623 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001624 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001625 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001626
Chris Mason179e29e2007-11-01 11:28:41 -04001627 if (alloc_hint != EXTENT_MAP_INLINE) {
1628 err = btrfs_insert_file_extent(trans, root,
1629 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001630 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001631 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001632 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001633 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001634 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001635 }
Chris Masonee6e6502008-07-17 12:54:40 -04001636 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001637 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001638 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001639 if (err)
1640 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001641 }
1642out:
1643 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001644
1645 if (!err && ((attr->ia_valid & ATTR_MODE)))
1646 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001647fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001648 return err;
1649}
Chris Mason61295eb2008-01-14 16:24:38 -05001650
Chris Mason39279cc2007-06-12 06:35:45 -04001651void btrfs_delete_inode(struct inode *inode)
1652{
1653 struct btrfs_trans_handle *trans;
1654 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001655 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001656 int ret;
1657
1658 truncate_inode_pages(&inode->i_data, 0);
1659 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001660 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001661 goto no_delete;
1662 }
Chris Mason4a096752008-07-21 10:29:44 -04001663 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001664
Chris Masondbe674a2008-07-17 12:54:05 -04001665 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001666 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001667
Chris Mason39279cc2007-06-12 06:35:45 -04001668 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001669 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001670 if (ret) {
1671 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001672 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001673 }
1674
1675 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001676
Chris Masond3c2fdc2007-09-17 10:58:06 -04001677 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001678 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001679
Chris Mason39279cc2007-06-12 06:35:45 -04001680 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001681 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001682 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001683
1684no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001685 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001686 btrfs_end_transaction(trans, root);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001687 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001688no_delete:
1689 clear_inode(inode);
1690}
1691
1692/*
1693 * this returns the key found in the dir entry in the location pointer.
1694 * If no dir entries were found, location->objectid is 0.
1695 */
1696static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1697 struct btrfs_key *location)
1698{
1699 const char *name = dentry->d_name.name;
1700 int namelen = dentry->d_name.len;
1701 struct btrfs_dir_item *di;
1702 struct btrfs_path *path;
1703 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001704 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001705
1706 path = btrfs_alloc_path();
1707 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001708
Chris Mason39279cc2007-06-12 06:35:45 -04001709 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1710 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001711 if (IS_ERR(di))
1712 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001713 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001714 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001715 }
Chris Mason5f39d392007-10-15 16:14:19 -04001716 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001717out:
Chris Mason39279cc2007-06-12 06:35:45 -04001718 btrfs_free_path(path);
1719 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001720out_err:
1721 location->objectid = 0;
1722 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001723}
1724
1725/*
1726 * when we hit a tree root in a directory, the btrfs part of the inode
1727 * needs to be changed to reflect the root directory of the tree root. This
1728 * is kind of like crossing a mount point.
1729 */
1730static int fixup_tree_root_location(struct btrfs_root *root,
1731 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001732 struct btrfs_root **sub_root,
1733 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001734{
Chris Mason39279cc2007-06-12 06:35:45 -04001735 struct btrfs_root_item *ri;
1736
1737 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1738 return 0;
1739 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1740 return 0;
1741
Josef Bacik58176a92007-08-29 15:47:34 -04001742 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1743 dentry->d_name.name,
1744 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001745 if (IS_ERR(*sub_root))
1746 return PTR_ERR(*sub_root);
1747
1748 ri = &(*sub_root)->root_item;
1749 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001750 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1751 location->offset = 0;
1752
Chris Mason39279cc2007-06-12 06:35:45 -04001753 return 0;
1754}
1755
1756static int btrfs_init_locked_inode(struct inode *inode, void *p)
1757{
1758 struct btrfs_iget_args *args = p;
1759 inode->i_ino = args->ino;
1760 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001761 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04001762 inode->i_mapping->writeback_index = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001763 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001764 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001765 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1766 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001767 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001768 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1769 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001770 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04001771 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001772 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001773 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001774 return 0;
1775}
1776
1777static int btrfs_find_actor(struct inode *inode, void *opaque)
1778{
1779 struct btrfs_iget_args *args = opaque;
1780 return (args->ino == inode->i_ino &&
1781 args->root == BTRFS_I(inode)->root);
1782}
1783
Chris Masondc17ff82008-01-08 15:46:30 -05001784struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1785 u64 root_objectid)
1786{
1787 struct btrfs_iget_args args;
1788 args.ino = objectid;
1789 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1790
1791 if (!args.root)
1792 return NULL;
1793
1794 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1795}
1796
Chris Mason39279cc2007-06-12 06:35:45 -04001797struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1798 struct btrfs_root *root)
1799{
1800 struct inode *inode;
1801 struct btrfs_iget_args args;
1802 args.ino = objectid;
1803 args.root = root;
1804
1805 inode = iget5_locked(s, objectid, btrfs_find_actor,
1806 btrfs_init_locked_inode,
1807 (void *)&args);
1808 return inode;
1809}
1810
Balaji Rao1a54ef82008-07-21 02:01:04 +05301811/* Get an inode object given its location and corresponding root.
1812 * Returns in *is_new if the inode was read from disk
1813 */
1814struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
1815 struct btrfs_root *root, int *is_new)
1816{
1817 struct inode *inode;
1818
1819 inode = btrfs_iget_locked(s, location->objectid, root);
1820 if (!inode)
1821 return ERR_PTR(-EACCES);
1822
1823 if (inode->i_state & I_NEW) {
1824 BTRFS_I(inode)->root = root;
1825 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
1826 btrfs_read_locked_inode(inode);
1827 unlock_new_inode(inode);
1828 if (is_new)
1829 *is_new = 1;
1830 } else {
1831 if (is_new)
1832 *is_new = 0;
1833 }
1834
1835 return inode;
1836}
1837
Chris Mason39279cc2007-06-12 06:35:45 -04001838static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1839 struct nameidata *nd)
1840{
1841 struct inode * inode;
1842 struct btrfs_inode *bi = BTRFS_I(dir);
1843 struct btrfs_root *root = bi->root;
1844 struct btrfs_root *sub_root = root;
1845 struct btrfs_key location;
Balaji Rao1a54ef82008-07-21 02:01:04 +05301846 int ret, new, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001847
1848 if (dentry->d_name.len > BTRFS_NAME_LEN)
1849 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001850
Chris Mason39279cc2007-06-12 06:35:45 -04001851 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001852
Chris Mason39279cc2007-06-12 06:35:45 -04001853 if (ret < 0)
1854 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001855
Chris Mason39279cc2007-06-12 06:35:45 -04001856 inode = NULL;
1857 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001858 ret = fixup_tree_root_location(root, &location, &sub_root,
1859 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001860 if (ret < 0)
1861 return ERR_PTR(ret);
1862 if (ret > 0)
1863 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05301864 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
1865 if (IS_ERR(inode))
1866 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04001867
Balaji Rao1a54ef82008-07-21 02:01:04 +05301868 /* the inode and parent dir are two different roots */
1869 if (new && root != sub_root) {
1870 igrab(inode);
1871 sub_root->inode = inode;
1872 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001873 }
1874 }
Josef Bacik7b128762008-07-24 12:17:14 -04001875
1876 if (unlikely(do_orphan))
1877 btrfs_orphan_cleanup(sub_root);
1878
Chris Mason39279cc2007-06-12 06:35:45 -04001879 return d_splice_alias(inode, dentry);
1880}
1881
Chris Mason39279cc2007-06-12 06:35:45 -04001882static unsigned char btrfs_filetype_table[] = {
1883 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1884};
1885
David Woodhousecbdf5a22008-08-06 19:42:33 +01001886static int btrfs_real_readdir(struct file *filp, void *dirent,
1887 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04001888{
Chris Mason6da6aba2007-12-18 16:15:09 -05001889 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001890 struct btrfs_root *root = BTRFS_I(inode)->root;
1891 struct btrfs_item *item;
1892 struct btrfs_dir_item *di;
1893 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001894 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001895 struct btrfs_path *path;
1896 int ret;
1897 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001898 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001899 int slot;
1900 int advance;
1901 unsigned char d_type;
1902 int over = 0;
1903 u32 di_cur;
1904 u32 di_total;
1905 u32 di_len;
1906 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001907 char tmp_name[32];
1908 char *name_ptr;
1909 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001910
1911 /* FIXME, use a real flag for deciding about the key type */
1912 if (root->fs_info->tree_root == root)
1913 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001914
Chris Mason39544012007-12-12 14:38:19 -05001915 /* special case for "." */
1916 if (filp->f_pos == 0) {
1917 over = filldir(dirent, ".", 1,
1918 1, inode->i_ino,
1919 DT_DIR);
1920 if (over)
1921 return 0;
1922 filp->f_pos = 1;
1923 }
Chris Mason39544012007-12-12 14:38:19 -05001924 /* special case for .., just use the back ref */
1925 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01001926 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05001927 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01001928 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05001929 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01001930 return 0;
Chris Mason39544012007-12-12 14:38:19 -05001931 filp->f_pos = 2;
1932 }
1933
David Woodhouse49593bf2008-08-17 17:08:36 +01001934 path = btrfs_alloc_path();
1935 path->reada = 2;
1936
Chris Mason39279cc2007-06-12 06:35:45 -04001937 btrfs_set_key_type(&key, key_type);
1938 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01001939 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04001940
Chris Mason39279cc2007-06-12 06:35:45 -04001941 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1942 if (ret < 0)
1943 goto err;
1944 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01001945
1946 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001947 leaf = path->nodes[0];
1948 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001949 slot = path->slots[0];
1950 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01001951 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001952 ret = btrfs_next_leaf(root, path);
1953 if (ret)
1954 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001955 leaf = path->nodes[0];
1956 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001957 slot = path->slots[0];
1958 } else {
1959 slot++;
1960 path->slots[0]++;
1961 }
1962 }
1963 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001964 item = btrfs_item_nr(leaf, slot);
1965 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1966
1967 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001968 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001969 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001970 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001971 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001972 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001973
1974 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01001975
Chris Mason39279cc2007-06-12 06:35:45 -04001976 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1977 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001978 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01001979
1980 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001981 struct btrfs_key location;
1982
1983 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01001984 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04001985 name_ptr = tmp_name;
1986 } else {
1987 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01001988 if (!name_ptr) {
1989 ret = -ENOMEM;
1990 goto err;
1991 }
Chris Mason5f39d392007-10-15 16:14:19 -04001992 }
1993 read_extent_buffer(leaf, name_ptr,
1994 (unsigned long)(di + 1), name_len);
1995
1996 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1997 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001998 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01001999 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002000 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002001
2002 if (name_ptr != tmp_name)
2003 kfree(name_ptr);
2004
Chris Mason39279cc2007-06-12 06:35:45 -04002005 if (over)
2006 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002007
Josef Bacik5103e942007-11-16 11:45:54 -05002008 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01002009 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002010 di_cur += di_len;
2011 di = (struct btrfs_dir_item *)((char *)di + di_len);
2012 }
2013 }
David Woodhouse49593bf2008-08-17 17:08:36 +01002014
2015 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05002016 if (key_type == BTRFS_DIR_INDEX_KEY)
2017 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2018 else
2019 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002020nopos:
2021 ret = 0;
2022err:
Chris Mason39279cc2007-06-12 06:35:45 -04002023 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002024 return ret;
2025}
2026
David Woodhousecbdf5a22008-08-06 19:42:33 +01002027/* Kernels earlier than 2.6.28 still have the NFS deadlock where nfsd
2028 will call the file system's ->lookup() method from within its
2029 filldir callback, which in turn was called from the file system's
2030 ->readdir() method. And will deadlock for many file systems. */
2031#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
2032
2033struct nfshack_dirent {
2034 u64 ino;
2035 loff_t offset;
2036 int namlen;
2037 unsigned int d_type;
2038 char name[];
2039};
2040
2041struct nfshack_readdir {
2042 char *dirent;
2043 size_t used;
David Woodhousef2322b12008-08-17 17:12:56 +01002044 int full;
David Woodhousecbdf5a22008-08-06 19:42:33 +01002045};
2046
2047
2048
2049static int btrfs_nfshack_filldir(void *__buf, const char *name, int namlen,
2050 loff_t offset, u64 ino, unsigned int d_type)
2051{
2052 struct nfshack_readdir *buf = __buf;
2053 struct nfshack_dirent *de = (void *)(buf->dirent + buf->used);
2054 unsigned int reclen;
2055
2056 reclen = ALIGN(sizeof(struct nfshack_dirent) + namlen, sizeof(u64));
David Woodhousef2322b12008-08-17 17:12:56 +01002057 if (buf->used + reclen > PAGE_SIZE) {
2058 buf->full = 1;
David Woodhousecbdf5a22008-08-06 19:42:33 +01002059 return -EINVAL;
David Woodhousef2322b12008-08-17 17:12:56 +01002060 }
David Woodhousecbdf5a22008-08-06 19:42:33 +01002061
2062 de->namlen = namlen;
2063 de->offset = offset;
2064 de->ino = ino;
2065 de->d_type = d_type;
2066 memcpy(de->name, name, namlen);
2067 buf->used += reclen;
2068
2069 return 0;
2070}
2071
2072static int btrfs_nfshack_readdir(struct file *file, void *dirent,
2073 filldir_t filldir)
2074{
2075 struct nfshack_readdir buf;
2076 struct nfshack_dirent *de;
2077 int err;
2078 int size;
2079 loff_t offset;
2080
2081 buf.dirent = (void *)__get_free_page(GFP_KERNEL);
2082 if (!buf.dirent)
2083 return -ENOMEM;
2084
2085 offset = file->f_pos;
2086
David Woodhousef2322b12008-08-17 17:12:56 +01002087 do {
David Woodhousecbdf5a22008-08-06 19:42:33 +01002088 unsigned int reclen;
2089
2090 buf.used = 0;
David Woodhousef2322b12008-08-17 17:12:56 +01002091 buf.full = 0;
David Woodhousecbdf5a22008-08-06 19:42:33 +01002092 err = btrfs_real_readdir(file, &buf, btrfs_nfshack_filldir);
2093 if (err)
2094 break;
2095
2096 size = buf.used;
2097
2098 if (!size)
2099 break;
2100
2101 de = (struct nfshack_dirent *)buf.dirent;
2102 while (size > 0) {
2103 offset = de->offset;
2104
2105 if (filldir(dirent, de->name, de->namlen, de->offset,
2106 de->ino, de->d_type))
2107 goto done;
2108 offset = file->f_pos;
2109
2110 reclen = ALIGN(sizeof(*de) + de->namlen,
2111 sizeof(u64));
2112 size -= reclen;
2113 de = (struct nfshack_dirent *)((char *)de + reclen);
2114 }
David Woodhousef2322b12008-08-17 17:12:56 +01002115 } while (buf.full);
David Woodhousecbdf5a22008-08-06 19:42:33 +01002116
2117 done:
2118 free_page((unsigned long)buf.dirent);
2119 file->f_pos = offset;
2120
2121 return err;
2122}
2123#endif
2124
Chris Mason39279cc2007-06-12 06:35:45 -04002125int btrfs_write_inode(struct inode *inode, int wait)
2126{
2127 struct btrfs_root *root = BTRFS_I(inode)->root;
2128 struct btrfs_trans_handle *trans;
2129 int ret = 0;
2130
Chris Mason4ca8b412008-08-05 13:30:48 -04002131 if (root->fs_info->closing > 1)
2132 return 0;
2133
Chris Mason39279cc2007-06-12 06:35:45 -04002134 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002135 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002136 btrfs_set_trans_block_group(trans, inode);
2137 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002138 }
2139 return ret;
2140}
2141
2142/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002143 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002144 * inode changes. But, it is most likely to find the inode in cache.
2145 * FIXME, needs more benchmarking...there are no reasons other than performance
2146 * to keep or drop this code.
2147 */
2148void btrfs_dirty_inode(struct inode *inode)
2149{
2150 struct btrfs_root *root = BTRFS_I(inode)->root;
2151 struct btrfs_trans_handle *trans;
2152
Chris Masonf9295742008-07-17 12:54:14 -04002153 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002154 btrfs_set_trans_block_group(trans, inode);
2155 btrfs_update_inode(trans, root, inode);
2156 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002157}
2158
Josef Bacikaec74772008-07-24 12:12:38 -04002159static int btrfs_set_inode_index_count(struct inode *inode)
2160{
2161 struct btrfs_root *root = BTRFS_I(inode)->root;
2162 struct btrfs_key key, found_key;
2163 struct btrfs_path *path;
2164 struct extent_buffer *leaf;
2165 int ret;
2166
2167 key.objectid = inode->i_ino;
2168 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2169 key.offset = (u64)-1;
2170
2171 path = btrfs_alloc_path();
2172 if (!path)
2173 return -ENOMEM;
2174
2175 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2176 if (ret < 0)
2177 goto out;
2178 /* FIXME: we should be able to handle this */
2179 if (ret == 0)
2180 goto out;
2181 ret = 0;
2182
2183 /*
2184 * MAGIC NUMBER EXPLANATION:
2185 * since we search a directory based on f_pos we have to start at 2
2186 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2187 * else has to start at 2
2188 */
2189 if (path->slots[0] == 0) {
2190 BTRFS_I(inode)->index_cnt = 2;
2191 goto out;
2192 }
2193
2194 path->slots[0]--;
2195
2196 leaf = path->nodes[0];
2197 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2198
2199 if (found_key.objectid != inode->i_ino ||
2200 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2201 BTRFS_I(inode)->index_cnt = 2;
2202 goto out;
2203 }
2204
2205 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2206out:
2207 btrfs_free_path(path);
2208 return ret;
2209}
2210
Chris Mason00e4e6b2008-08-05 11:18:09 -04002211static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2212 u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04002213{
2214 int ret = 0;
2215
2216 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2217 ret = btrfs_set_inode_index_count(dir);
2218 if (ret)
2219 return ret;
2220 }
2221
Chris Mason00e4e6b2008-08-05 11:18:09 -04002222 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04002223 BTRFS_I(dir)->index_cnt++;
2224
2225 return ret;
2226}
2227
Chris Mason39279cc2007-06-12 06:35:45 -04002228static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2229 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002230 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002231 const char *name, int name_len,
2232 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002233 u64 objectid,
2234 struct btrfs_block_group_cache *group,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002235 int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04002236{
2237 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002238 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002239 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002240 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002241 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002242 struct btrfs_inode_ref *ref;
2243 struct btrfs_key key[2];
2244 u32 sizes[2];
2245 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002246 int ret;
2247 int owner;
2248
Chris Mason5f39d392007-10-15 16:14:19 -04002249 path = btrfs_alloc_path();
2250 BUG_ON(!path);
2251
Chris Mason39279cc2007-06-12 06:35:45 -04002252 inode = new_inode(root->fs_info->sb);
2253 if (!inode)
2254 return ERR_PTR(-ENOMEM);
2255
Josef Bacikaec74772008-07-24 12:12:38 -04002256 if (dir) {
Chris Mason00e4e6b2008-08-05 11:18:09 -04002257 ret = btrfs_set_inode_index(dir, inode, index);
Josef Bacikaec74772008-07-24 12:12:38 -04002258 if (ret)
2259 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002260 }
2261 /*
2262 * index_cnt is ignored for everything but a dir,
2263 * btrfs_get_inode_index_count has an explanation for the magic
2264 * number
2265 */
2266 BTRFS_I(inode)->index_cnt = 2;
2267
Chris Masond1310b22008-01-24 16:13:08 -05002268 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2269 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002270 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002271 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2272 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002273 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonea8c2812008-08-04 23:17:27 -04002274 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04002275 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002276 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002277 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04002278 inode->i_mapping->writeback_index = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002279 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002280 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002281
Chris Mason39279cc2007-06-12 06:35:45 -04002282 if (mode & S_IFDIR)
2283 owner = 0;
2284 else
2285 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002286 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002287 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002288 if (!new_inode_group) {
2289 printk("find_block group failed\n");
2290 new_inode_group = group;
2291 }
2292 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002293 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002294
2295 key[0].objectid = objectid;
2296 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2297 key[0].offset = 0;
2298
2299 key[1].objectid = objectid;
2300 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2301 key[1].offset = ref_objectid;
2302
2303 sizes[0] = sizeof(struct btrfs_inode_item);
2304 sizes[1] = name_len + sizeof(*ref);
2305
2306 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2307 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002308 goto fail;
2309
Chris Mason9c583092008-01-29 15:15:18 -05002310 if (objectid > root->highest_inode)
2311 root->highest_inode = objectid;
2312
Chris Mason39279cc2007-06-12 06:35:45 -04002313 inode->i_uid = current->fsuid;
2314 inode->i_gid = current->fsgid;
2315 inode->i_mode = mode;
2316 inode->i_ino = objectid;
2317 inode->i_blocks = 0;
2318 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002319 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2320 struct btrfs_inode_item);
2321 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002322
2323 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2324 struct btrfs_inode_ref);
2325 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002326 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05002327 ptr = (unsigned long)(ref + 1);
2328 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2329
Chris Mason5f39d392007-10-15 16:14:19 -04002330 btrfs_mark_buffer_dirty(path->nodes[0]);
2331 btrfs_free_path(path);
2332
Chris Mason39279cc2007-06-12 06:35:45 -04002333 location = &BTRFS_I(inode)->location;
2334 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002335 location->offset = 0;
2336 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2337
Chris Mason39279cc2007-06-12 06:35:45 -04002338 insert_inode_hash(inode);
2339 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002340fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002341 if (dir)
2342 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002343 btrfs_free_path(path);
2344 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002345}
2346
2347static inline u8 btrfs_inode_type(struct inode *inode)
2348{
2349 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2350}
2351
2352static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002353 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002354 int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002355{
2356 int ret;
2357 struct btrfs_key key;
2358 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002359 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002360
Chris Mason39279cc2007-06-12 06:35:45 -04002361 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002362 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2363 key.offset = 0;
2364
2365 ret = btrfs_insert_dir_item(trans, root,
2366 dentry->d_name.name, dentry->d_name.len,
2367 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002368 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04002369 index);
Chris Mason39279cc2007-06-12 06:35:45 -04002370 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002371 if (add_backref) {
2372 ret = btrfs_insert_inode_ref(trans, root,
2373 dentry->d_name.name,
2374 dentry->d_name.len,
2375 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002376 parent_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002377 index);
Chris Mason9c583092008-01-29 15:15:18 -05002378 }
Chris Masondbe674a2008-07-17 12:54:05 -04002379 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2380 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002381 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002382 ret = btrfs_update_inode(trans, root,
2383 dentry->d_parent->d_inode);
2384 }
2385 return ret;
2386}
2387
2388static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002389 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002390 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002391{
Chris Mason00e4e6b2008-08-05 11:18:09 -04002392 int err = btrfs_add_link(trans, dentry, inode, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002393 if (!err) {
2394 d_instantiate(dentry, inode);
2395 return 0;
2396 }
2397 if (err > 0)
2398 err = -EEXIST;
2399 return err;
2400}
2401
Josef Bacik618e21d2007-07-11 10:18:17 -04002402static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2403 int mode, dev_t rdev)
2404{
2405 struct btrfs_trans_handle *trans;
2406 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002407 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002408 int err;
2409 int drop_inode = 0;
2410 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002411 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002412 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002413
2414 if (!new_valid_dev(rdev))
2415 return -EINVAL;
2416
Chris Mason1832a6d2007-12-21 16:27:21 -05002417 err = btrfs_check_free_space(root, 1, 0);
2418 if (err)
2419 goto fail;
2420
Josef Bacik618e21d2007-07-11 10:18:17 -04002421 trans = btrfs_start_transaction(root, 1);
2422 btrfs_set_trans_block_group(trans, dir);
2423
2424 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2425 if (err) {
2426 err = -ENOSPC;
2427 goto out_unlock;
2428 }
2429
Josef Bacikaec74772008-07-24 12:12:38 -04002430 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002431 dentry->d_name.len,
2432 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002433 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002434 err = PTR_ERR(inode);
2435 if (IS_ERR(inode))
2436 goto out_unlock;
2437
Josef Bacik33268ea2008-07-24 12:16:36 -04002438 err = btrfs_init_acl(inode, dir);
2439 if (err) {
2440 drop_inode = 1;
2441 goto out_unlock;
2442 }
2443
Josef Bacik618e21d2007-07-11 10:18:17 -04002444 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002445 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002446 if (err)
2447 drop_inode = 1;
2448 else {
2449 inode->i_op = &btrfs_special_inode_operations;
2450 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002451 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002452 }
2453 dir->i_sb->s_dirt = 1;
2454 btrfs_update_inode_block_group(trans, inode);
2455 btrfs_update_inode_block_group(trans, dir);
2456out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002457 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002458 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002459fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002460 if (drop_inode) {
2461 inode_dec_link_count(inode);
2462 iput(inode);
2463 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002464 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002465 return err;
2466}
2467
Chris Mason39279cc2007-06-12 06:35:45 -04002468static int btrfs_create(struct inode *dir, struct dentry *dentry,
2469 int mode, struct nameidata *nd)
2470{
2471 struct btrfs_trans_handle *trans;
2472 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002473 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002474 int err;
2475 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002476 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002477 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002478 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002479
Chris Mason1832a6d2007-12-21 16:27:21 -05002480 err = btrfs_check_free_space(root, 1, 0);
2481 if (err)
2482 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002483 trans = btrfs_start_transaction(root, 1);
2484 btrfs_set_trans_block_group(trans, dir);
2485
2486 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2487 if (err) {
2488 err = -ENOSPC;
2489 goto out_unlock;
2490 }
2491
Josef Bacikaec74772008-07-24 12:12:38 -04002492 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002493 dentry->d_name.len,
2494 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002495 objectid, BTRFS_I(dir)->block_group, mode,
2496 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002497 err = PTR_ERR(inode);
2498 if (IS_ERR(inode))
2499 goto out_unlock;
2500
Josef Bacik33268ea2008-07-24 12:16:36 -04002501 err = btrfs_init_acl(inode, dir);
2502 if (err) {
2503 drop_inode = 1;
2504 goto out_unlock;
2505 }
2506
Chris Mason39279cc2007-06-12 06:35:45 -04002507 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002508 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002509 if (err)
2510 drop_inode = 1;
2511 else {
2512 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002513 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002514 inode->i_fop = &btrfs_file_operations;
2515 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002516 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2517 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002518 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002519 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2520 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04002521 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04002522 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002523 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002524 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002525 BTRFS_I(inode)->disk_i_size = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04002526 inode->i_mapping->writeback_index = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002527 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002528 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002529 }
2530 dir->i_sb->s_dirt = 1;
2531 btrfs_update_inode_block_group(trans, inode);
2532 btrfs_update_inode_block_group(trans, dir);
2533out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002534 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002535 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002536fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002537 if (drop_inode) {
2538 inode_dec_link_count(inode);
2539 iput(inode);
2540 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002541 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002542 return err;
2543}
2544
2545static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2546 struct dentry *dentry)
2547{
2548 struct btrfs_trans_handle *trans;
2549 struct btrfs_root *root = BTRFS_I(dir)->root;
2550 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002551 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05002552 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002553 int err;
2554 int drop_inode = 0;
2555
2556 if (inode->i_nlink == 0)
2557 return -ENOENT;
2558
Chris Mason6da6aba2007-12-18 16:15:09 -05002559#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2560 inode->i_nlink++;
2561#else
Chris Mason39279cc2007-06-12 06:35:45 -04002562 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002563#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002564 err = btrfs_check_free_space(root, 1, 0);
2565 if (err)
2566 goto fail;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002567 err = btrfs_set_inode_index(dir, inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002568 if (err)
2569 goto fail;
2570
Chris Mason39279cc2007-06-12 06:35:45 -04002571 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002572
Chris Mason39279cc2007-06-12 06:35:45 -04002573 btrfs_set_trans_block_group(trans, dir);
2574 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002575
Chris Mason00e4e6b2008-08-05 11:18:09 -04002576 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04002577
Chris Mason39279cc2007-06-12 06:35:45 -04002578 if (err)
2579 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002580
Chris Mason39279cc2007-06-12 06:35:45 -04002581 dir->i_sb->s_dirt = 1;
2582 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002583 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002584
Chris Mason54aa1f42007-06-22 14:16:25 -04002585 if (err)
2586 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002587
Chris Masond3c2fdc2007-09-17 10:58:06 -04002588 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002589 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002590fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002591 if (drop_inode) {
2592 inode_dec_link_count(inode);
2593 iput(inode);
2594 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002595 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002596 return err;
2597}
2598
Chris Mason39279cc2007-06-12 06:35:45 -04002599static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2600{
Chris Masonb9d86662008-05-02 16:13:49 -04002601 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002602 struct btrfs_trans_handle *trans;
2603 struct btrfs_root *root = BTRFS_I(dir)->root;
2604 int err = 0;
2605 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002606 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002607 u64 index = 0;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002608 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002609
Chris Mason1832a6d2007-12-21 16:27:21 -05002610 err = btrfs_check_free_space(root, 1, 0);
2611 if (err)
2612 goto out_unlock;
2613
Chris Mason39279cc2007-06-12 06:35:45 -04002614 trans = btrfs_start_transaction(root, 1);
2615 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002616
Chris Mason39279cc2007-06-12 06:35:45 -04002617 if (IS_ERR(trans)) {
2618 err = PTR_ERR(trans);
2619 goto out_unlock;
2620 }
2621
2622 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2623 if (err) {
2624 err = -ENOSPC;
2625 goto out_unlock;
2626 }
2627
Josef Bacikaec74772008-07-24 12:12:38 -04002628 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002629 dentry->d_name.len,
2630 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002631 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2632 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002633 if (IS_ERR(inode)) {
2634 err = PTR_ERR(inode);
2635 goto out_fail;
2636 }
Chris Mason5f39d392007-10-15 16:14:19 -04002637
Chris Mason39279cc2007-06-12 06:35:45 -04002638 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002639
2640 err = btrfs_init_acl(inode, dir);
2641 if (err)
2642 goto out_fail;
2643
Chris Mason39279cc2007-06-12 06:35:45 -04002644 inode->i_op = &btrfs_dir_inode_operations;
2645 inode->i_fop = &btrfs_dir_file_operations;
2646 btrfs_set_trans_block_group(trans, inode);
2647
Chris Masondbe674a2008-07-17 12:54:05 -04002648 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002649 err = btrfs_update_inode(trans, root, inode);
2650 if (err)
2651 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002652
Chris Mason00e4e6b2008-08-05 11:18:09 -04002653 err = btrfs_add_link(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002654 if (err)
2655 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002656
Chris Mason39279cc2007-06-12 06:35:45 -04002657 d_instantiate(dentry, inode);
2658 drop_on_err = 0;
2659 dir->i_sb->s_dirt = 1;
2660 btrfs_update_inode_block_group(trans, inode);
2661 btrfs_update_inode_block_group(trans, dir);
2662
2663out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002664 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002665 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002666
Chris Mason39279cc2007-06-12 06:35:45 -04002667out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002668 if (drop_on_err)
2669 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002670 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002671 return err;
2672}
2673
Chris Mason3b951512008-04-17 11:29:12 -04002674static int merge_extent_mapping(struct extent_map_tree *em_tree,
2675 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002676 struct extent_map *em,
2677 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002678{
2679 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002680
Chris Masone6dcd2d2008-07-17 12:53:50 -04002681 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2682 start_diff = map_start - em->start;
2683 em->start = map_start;
2684 em->len = map_len;
2685 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2686 em->block_start += start_diff;
2687 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002688}
2689
Chris Masona52d9a82007-08-27 16:49:44 -04002690struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002691 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002692 int create)
2693{
2694 int ret;
2695 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002696 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002697 u64 extent_start = 0;
2698 u64 extent_end = 0;
2699 u64 objectid = inode->i_ino;
2700 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002701 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002702 struct btrfs_root *root = BTRFS_I(inode)->root;
2703 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002704 struct extent_buffer *leaf;
2705 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002706 struct extent_map *em = NULL;
2707 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002708 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002709 struct btrfs_trans_handle *trans = NULL;
2710
Chris Masona52d9a82007-08-27 16:49:44 -04002711again:
Chris Masond1310b22008-01-24 16:13:08 -05002712 spin_lock(&em_tree->lock);
2713 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002714 if (em)
2715 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002716 spin_unlock(&em_tree->lock);
2717
Chris Masona52d9a82007-08-27 16:49:44 -04002718 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002719 if (em->start > start || em->start + em->len <= start)
2720 free_extent_map(em);
2721 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002722 free_extent_map(em);
2723 else
2724 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002725 }
Chris Masond1310b22008-01-24 16:13:08 -05002726 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002727 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002728 err = -ENOMEM;
2729 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002730 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002731 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002732 em->start = EXTENT_MAP_HOLE;
2733 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002734
2735 if (!path) {
2736 path = btrfs_alloc_path();
2737 BUG_ON(!path);
2738 }
2739
Chris Mason179e29e2007-11-01 11:28:41 -04002740 ret = btrfs_lookup_file_extent(trans, root, path,
2741 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002742 if (ret < 0) {
2743 err = ret;
2744 goto out;
2745 }
2746
2747 if (ret != 0) {
2748 if (path->slots[0] == 0)
2749 goto not_found;
2750 path->slots[0]--;
2751 }
2752
Chris Mason5f39d392007-10-15 16:14:19 -04002753 leaf = path->nodes[0];
2754 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002755 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002756 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002757 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2758 found_type = btrfs_key_type(&found_key);
2759 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002760 found_type != BTRFS_EXTENT_DATA_KEY) {
2761 goto not_found;
2762 }
2763
Chris Mason5f39d392007-10-15 16:14:19 -04002764 found_type = btrfs_file_extent_type(leaf, item);
2765 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002766 if (found_type == BTRFS_FILE_EXTENT_REG) {
2767 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002768 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002769 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002770 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002771 em->start = start;
2772 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002773 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002774 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002775 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002776 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002777 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002778 }
2779 goto not_found_em;
2780 }
Chris Masondb945352007-10-15 16:15:53 -04002781 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2782 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002783 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002784 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002785 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002786 goto insert;
2787 }
Chris Masondb945352007-10-15 16:15:53 -04002788 bytenr += btrfs_file_extent_offset(leaf, item);
2789 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002790 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002791 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002792 goto insert;
2793 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002794 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002795 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002796 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002797 size_t size;
2798 size_t extent_offset;
2799 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002800
Chris Mason5f39d392007-10-15 16:14:19 -04002801 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2802 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002803 extent_end = (extent_start + size + root->sectorsize - 1) &
2804 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002805 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002806 em->start = start;
2807 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002808 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002809 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002810 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002811 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002812 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002813 }
2814 goto not_found_em;
2815 }
2816 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002817
2818 if (!page) {
2819 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002820 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002821 goto out;
2822 }
2823
Chris Mason70dec802008-01-29 09:59:12 -05002824 page_start = page_offset(page) + pg_offset;
2825 extent_offset = page_start - extent_start;
2826 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002827 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002828 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002829 em->len = (copy_size + root->sectorsize - 1) &
2830 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002831 map = kmap(page);
2832 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002833 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002834 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002835 copy_size);
2836 flush_dcache_page(page);
2837 } else if (create && PageUptodate(page)) {
2838 if (!trans) {
2839 kunmap(page);
2840 free_extent_map(em);
2841 em = NULL;
2842 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002843 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002844 goto again;
2845 }
Chris Mason70dec802008-01-29 09:59:12 -05002846 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002847 copy_size);
2848 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002849 }
Chris Masona52d9a82007-08-27 16:49:44 -04002850 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002851 set_extent_uptodate(io_tree, em->start,
2852 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002853 goto insert;
2854 } else {
2855 printk("unkknown found_type %d\n", found_type);
2856 WARN_ON(1);
2857 }
2858not_found:
2859 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002860 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002861not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002862 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002863insert:
2864 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002865 if (em->start > start || extent_map_end(em) <= start) {
2866 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002867 err = -EIO;
2868 goto out;
2869 }
Chris Masond1310b22008-01-24 16:13:08 -05002870
2871 err = 0;
2872 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002873 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002874 /* it is possible that someone inserted the extent into the tree
2875 * while we had the lock dropped. It is also possible that
2876 * an overlapping map exists in the tree
2877 */
Chris Masona52d9a82007-08-27 16:49:44 -04002878 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002879 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002880
2881 ret = 0;
2882
Chris Mason3b951512008-04-17 11:29:12 -04002883 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002884 if (existing && (existing->start > start ||
2885 existing->start + existing->len <= start)) {
2886 free_extent_map(existing);
2887 existing = NULL;
2888 }
Chris Mason3b951512008-04-17 11:29:12 -04002889 if (!existing) {
2890 existing = lookup_extent_mapping(em_tree, em->start,
2891 em->len);
2892 if (existing) {
2893 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002894 em, start,
2895 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002896 free_extent_map(existing);
2897 if (err) {
2898 free_extent_map(em);
2899 em = NULL;
2900 }
2901 } else {
2902 err = -EIO;
2903 printk("failing to insert %Lu %Lu\n",
2904 start, len);
2905 free_extent_map(em);
2906 em = NULL;
2907 }
2908 } else {
2909 free_extent_map(em);
2910 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002911 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002912 }
Chris Masona52d9a82007-08-27 16:49:44 -04002913 }
Chris Masond1310b22008-01-24 16:13:08 -05002914 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002915out:
Chris Masonf4219502008-07-22 11:18:09 -04002916 if (path)
2917 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002918 if (trans) {
2919 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002920 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002921 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002922 }
Chris Masona52d9a82007-08-27 16:49:44 -04002923 }
Chris Masona52d9a82007-08-27 16:49:44 -04002924 if (err) {
2925 free_extent_map(em);
2926 WARN_ON(1);
2927 return ERR_PTR(err);
2928 }
2929 return em;
2930}
2931
Chris Masone1c4b742008-04-22 13:26:46 -04002932#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002933static int btrfs_get_block(struct inode *inode, sector_t iblock,
2934 struct buffer_head *bh_result, int create)
2935{
2936 struct extent_map *em;
2937 u64 start = (u64)iblock << inode->i_blkbits;
2938 struct btrfs_multi_bio *multi = NULL;
2939 struct btrfs_root *root = BTRFS_I(inode)->root;
2940 u64 len;
2941 u64 logical;
2942 u64 map_length;
2943 int ret = 0;
2944
2945 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2946
2947 if (!em || IS_ERR(em))
2948 goto out;
2949
Chris Masone1c4b742008-04-22 13:26:46 -04002950 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002951 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002952 }
Chris Mason16432982008-04-10 10:23:21 -04002953
2954 if (em->block_start == EXTENT_MAP_INLINE) {
2955 ret = -EINVAL;
2956 goto out;
2957 }
2958
Chris Mason16432982008-04-10 10:23:21 -04002959 len = em->start + em->len - start;
2960 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2961
Chris Masone1c4b742008-04-22 13:26:46 -04002962 if (em->block_start == EXTENT_MAP_HOLE ||
2963 em->block_start == EXTENT_MAP_DELALLOC) {
2964 bh_result->b_size = len;
2965 goto out;
2966 }
2967
Chris Mason16432982008-04-10 10:23:21 -04002968 logical = start - em->start;
2969 logical = em->block_start + logical;
2970
2971 map_length = len;
2972 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2973 logical, &map_length, &multi, 0);
2974 BUG_ON(ret);
2975 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2976 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002977
Chris Mason16432982008-04-10 10:23:21 -04002978 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2979 set_buffer_mapped(bh_result);
2980 kfree(multi);
2981out:
2982 free_extent_map(em);
2983 return ret;
2984}
Chris Masone1c4b742008-04-22 13:26:46 -04002985#endif
Chris Mason16432982008-04-10 10:23:21 -04002986
2987static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2988 const struct iovec *iov, loff_t offset,
2989 unsigned long nr_segs)
2990{
Chris Masone1c4b742008-04-22 13:26:46 -04002991 return -EINVAL;
2992#if 0
Chris Mason16432982008-04-10 10:23:21 -04002993 struct file *file = iocb->ki_filp;
2994 struct inode *inode = file->f_mapping->host;
2995
2996 if (rw == WRITE)
2997 return -EINVAL;
2998
2999 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3000 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04003001#endif
Chris Mason16432982008-04-10 10:23:21 -04003002}
3003
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04003004static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04003005{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04003006 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04003007}
3008
Chris Mason9ebefb182007-06-15 13:50:00 -04003009int btrfs_readpage(struct file *file, struct page *page)
3010{
Chris Masond1310b22008-01-24 16:13:08 -05003011 struct extent_io_tree *tree;
3012 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003013 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04003014}
Chris Mason1832a6d2007-12-21 16:27:21 -05003015
Chris Mason39279cc2007-06-12 06:35:45 -04003016static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3017{
Chris Masond1310b22008-01-24 16:13:08 -05003018 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04003019
3020
3021 if (current->flags & PF_MEMALLOC) {
3022 redirty_page_for_writepage(wbc, page);
3023 unlock_page(page);
3024 return 0;
3025 }
Chris Masond1310b22008-01-24 16:13:08 -05003026 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003027 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3028}
Chris Mason39279cc2007-06-12 06:35:45 -04003029
Chris Masonf4219502008-07-22 11:18:09 -04003030int btrfs_writepages(struct address_space *mapping,
3031 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04003032{
Chris Masond1310b22008-01-24 16:13:08 -05003033 struct extent_io_tree *tree;
3034 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04003035 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3036}
3037
Chris Mason3ab2fb52007-11-08 10:59:22 -05003038static int
3039btrfs_readpages(struct file *file, struct address_space *mapping,
3040 struct list_head *pages, unsigned nr_pages)
3041{
Chris Masond1310b22008-01-24 16:13:08 -05003042 struct extent_io_tree *tree;
3043 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05003044 return extent_readpages(tree, mapping, pages, nr_pages,
3045 btrfs_get_extent);
3046}
Chris Masone6dcd2d2008-07-17 12:53:50 -04003047static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04003048{
Chris Masond1310b22008-01-24 16:13:08 -05003049 struct extent_io_tree *tree;
3050 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04003051 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003052
Chris Masond1310b22008-01-24 16:13:08 -05003053 tree = &BTRFS_I(page->mapping->host)->io_tree;
3054 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05003055 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003056 if (ret == 1) {
3057 ClearPagePrivate(page);
3058 set_page_private(page, 0);
3059 page_cache_release(page);
3060 }
3061 return ret;
3062}
Chris Mason39279cc2007-06-12 06:35:45 -04003063
Chris Masone6dcd2d2008-07-17 12:53:50 -04003064static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3065{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003066 return __btrfs_releasepage(page, gfp_flags);
3067}
3068
Chris Masona52d9a82007-08-27 16:49:44 -04003069static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3070{
Chris Masond1310b22008-01-24 16:13:08 -05003071 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003072 struct btrfs_ordered_extent *ordered;
3073 u64 page_start = page_offset(page);
3074 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003075
Chris Masone6dcd2d2008-07-17 12:53:50 -04003076 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003077 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003078 if (offset) {
3079 btrfs_releasepage(page, GFP_NOFS);
3080 return;
3081 }
3082
3083 lock_extent(tree, page_start, page_end, GFP_NOFS);
3084 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3085 page_offset(page));
3086 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003087 /*
3088 * IO on this page will never be started, so we need
3089 * to account for any ordered extents now
3090 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003091 clear_extent_bit(tree, page_start, page_end,
3092 EXTENT_DIRTY | EXTENT_DELALLOC |
3093 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003094 btrfs_finish_ordered_io(page->mapping->host,
3095 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003096 btrfs_put_ordered_extent(ordered);
3097 lock_extent(tree, page_start, page_end, GFP_NOFS);
3098 }
3099 clear_extent_bit(tree, page_start, page_end,
3100 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3101 EXTENT_ORDERED,
3102 1, 1, GFP_NOFS);
3103 __btrfs_releasepage(page, GFP_NOFS);
3104
Chris Mason4a096752008-07-21 10:29:44 -04003105 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003106 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003107 ClearPagePrivate(page);
3108 set_page_private(page, 0);
3109 page_cache_release(page);
3110 }
Chris Mason39279cc2007-06-12 06:35:45 -04003111}
3112
Chris Mason9ebefb182007-06-15 13:50:00 -04003113/*
3114 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3115 * called from a page fault handler when a page is first dirtied. Hence we must
3116 * be careful to check for EOF conditions here. We set the page up correctly
3117 * for a written page which means we get ENOSPC checking when writing into
3118 * holes and correct delalloc and unwritten extent mapping on filesystems that
3119 * support these features.
3120 *
3121 * We are not allowed to take the i_mutex here so we have to play games to
3122 * protect against truncate races as the page could now be beyond EOF. Because
3123 * vmtruncate() writes the inode size before removing pages, once we have the
3124 * page lock we can determine safely if the page is beyond EOF. If it is not
3125 * beyond EOF, then the page is guaranteed safe against truncation until we
3126 * unlock the page.
3127 */
3128int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3129{
Chris Mason6da6aba2007-12-18 16:15:09 -05003130 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003131 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003132 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3133 struct btrfs_ordered_extent *ordered;
3134 char *kaddr;
3135 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003136 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003137 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003138 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003139 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003140
Chris Mason1832a6d2007-12-21 16:27:21 -05003141 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003142 if (ret)
3143 goto out;
3144
3145 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003146again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003147 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003148 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003149 page_start = page_offset(page);
3150 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003151
Chris Mason9ebefb182007-06-15 13:50:00 -04003152 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003153 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003154 /* page got truncated out from underneath us */
3155 goto out_unlock;
3156 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003157 wait_on_page_writeback(page);
3158
3159 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3160 set_page_extent_mapped(page);
3161
Chris Masoneb84ae02008-07-17 13:53:27 -04003162 /*
3163 * we can't set the delalloc bits if there are pending ordered
3164 * extents. Drop our locks and wait for them to finish
3165 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003166 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3167 if (ordered) {
3168 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3169 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003170 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003171 btrfs_put_ordered_extent(ordered);
3172 goto again;
3173 }
3174
Chris Masonea8c2812008-08-04 23:17:27 -04003175 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003176 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003177
3178 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003179 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003180 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003181 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003182 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003183
Chris Masone6dcd2d2008-07-17 12:53:50 -04003184 if (zero_start != PAGE_CACHE_SIZE) {
3185 kaddr = kmap(page);
3186 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3187 flush_dcache_page(page);
3188 kunmap(page);
3189 }
Chris Mason247e7432008-07-17 12:53:51 -04003190 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003191 set_page_dirty(page);
3192 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003193
3194out_unlock:
3195 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003196out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003197 return ret;
3198}
3199
Chris Mason39279cc2007-06-12 06:35:45 -04003200static void btrfs_truncate(struct inode *inode)
3201{
3202 struct btrfs_root *root = BTRFS_I(inode)->root;
3203 int ret;
3204 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04003205 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003206 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003207
3208 if (!S_ISREG(inode->i_mode))
3209 return;
3210 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3211 return;
3212
3213 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003214 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003215
Chris Mason39279cc2007-06-12 06:35:45 -04003216 trans = btrfs_start_transaction(root, 1);
3217 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003218 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003219
Josef Bacik7b128762008-07-24 12:17:14 -04003220 ret = btrfs_orphan_add(trans, inode);
3221 if (ret)
3222 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003223 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05003224 ret = btrfs_truncate_in_trans(trans, root, inode,
3225 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003226 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003227
Josef Bacik7b128762008-07-24 12:17:14 -04003228 ret = btrfs_orphan_del(trans, inode);
3229 BUG_ON(ret);
3230
3231out:
3232 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003233 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003234 BUG_ON(ret);
Chris Masond3c2fdc2007-09-17 10:58:06 -04003235 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003236}
3237
Sven Wegener3b963622008-06-09 21:57:42 -04003238/*
3239 * Invalidate a single dcache entry at the root of the filesystem.
3240 * Needed after creation of snapshot or subvolume.
3241 */
3242void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3243 int namelen)
3244{
3245 struct dentry *alias, *entry;
3246 struct qstr qstr;
3247
3248 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3249 if (alias) {
3250 qstr.name = name;
3251 qstr.len = namelen;
3252 /* change me if btrfs ever gets a d_hash operation */
3253 qstr.hash = full_name_hash(qstr.name, qstr.len);
3254 entry = d_lookup(alias, &qstr);
3255 dput(alias);
3256 if (entry) {
3257 d_invalidate(entry);
3258 dput(entry);
3259 }
3260 }
3261}
3262
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003263int btrfs_create_subvol_root(struct btrfs_root *new_root,
3264 struct btrfs_trans_handle *trans, u64 new_dirid,
3265 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003266{
Chris Mason39279cc2007-06-12 06:35:45 -04003267 struct inode *inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003268 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003269
Josef Bacikaec74772008-07-24 12:12:38 -04003270 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003271 new_dirid, block_group, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04003272 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003273 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003274 inode->i_op = &btrfs_dir_inode_operations;
3275 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003276 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003277
Chris Mason39279cc2007-06-12 06:35:45 -04003278 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003279 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003280
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003281 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003282}
3283
Chris Masonedbd8d42007-12-21 16:27:24 -05003284unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003285 struct file_ra_state *ra, struct file *file,
3286 pgoff_t offset, pgoff_t last_index)
3287{
Chris Mason8e7bf942008-04-28 09:02:36 -04003288 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003289
3290#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003291 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3292 return offset;
3293#else
Chris Mason86479a02007-09-10 19:58:16 -04003294 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3295 return offset + req_size;
3296#endif
3297}
3298
Chris Mason39279cc2007-06-12 06:35:45 -04003299struct inode *btrfs_alloc_inode(struct super_block *sb)
3300{
3301 struct btrfs_inode *ei;
3302
3303 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3304 if (!ei)
3305 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003306 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003307 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003308 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3309 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003310 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003311 return &ei->vfs_inode;
3312}
3313
3314void btrfs_destroy_inode(struct inode *inode)
3315{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003316 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003317 WARN_ON(!list_empty(&inode->i_dentry));
3318 WARN_ON(inode->i_data.nrpages);
3319
Josef Bacik33268ea2008-07-24 12:16:36 -04003320 if (BTRFS_I(inode)->i_acl &&
3321 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3322 posix_acl_release(BTRFS_I(inode)->i_acl);
3323 if (BTRFS_I(inode)->i_default_acl &&
3324 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3325 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3326
Yanbcc63ab2008-07-30 16:29:20 -04003327 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003328 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3329 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3330 " list\n", inode->i_ino);
3331 dump_stack();
3332 }
Yanbcc63ab2008-07-30 16:29:20 -04003333 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003334
Chris Masone6dcd2d2008-07-17 12:53:50 -04003335 while(1) {
3336 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3337 if (!ordered)
3338 break;
3339 else {
3340 printk("found ordered extent %Lu %Lu\n",
3341 ordered->file_offset, ordered->len);
3342 btrfs_remove_ordered_extent(inode, ordered);
3343 btrfs_put_ordered_extent(ordered);
3344 btrfs_put_ordered_extent(ordered);
3345 }
3346 }
Chris Mason8c416c92008-01-14 15:10:26 -05003347 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003348 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3349}
3350
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003351#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3352static void init_once(void *foo)
3353#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003354static void init_once(struct kmem_cache * cachep, void *foo)
3355#else
Chris Mason39279cc2007-06-12 06:35:45 -04003356static void init_once(void * foo, struct kmem_cache * cachep,
3357 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003358#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003359{
3360 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3361
3362 inode_init_once(&ei->vfs_inode);
3363}
3364
3365void btrfs_destroy_cachep(void)
3366{
3367 if (btrfs_inode_cachep)
3368 kmem_cache_destroy(btrfs_inode_cachep);
3369 if (btrfs_trans_handle_cachep)
3370 kmem_cache_destroy(btrfs_trans_handle_cachep);
3371 if (btrfs_transaction_cachep)
3372 kmem_cache_destroy(btrfs_transaction_cachep);
3373 if (btrfs_bit_radix_cachep)
3374 kmem_cache_destroy(btrfs_bit_radix_cachep);
3375 if (btrfs_path_cachep)
3376 kmem_cache_destroy(btrfs_path_cachep);
3377}
3378
Chris Mason86479a02007-09-10 19:58:16 -04003379struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003380 unsigned long extra_flags,
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003381#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3382 void (*ctor)(void *)
3383#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003384 void (*ctor)(struct kmem_cache *, void *)
3385#else
Chris Mason92fee662007-07-25 12:31:35 -04003386 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003387 unsigned long)
3388#endif
3389 )
Chris Mason92fee662007-07-25 12:31:35 -04003390{
3391 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3392 SLAB_MEM_SPREAD | extra_flags), ctor
3393#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3394 ,NULL
3395#endif
3396 );
3397}
3398
Chris Mason39279cc2007-06-12 06:35:45 -04003399int btrfs_init_cachep(void)
3400{
Chris Mason86479a02007-09-10 19:58:16 -04003401 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003402 sizeof(struct btrfs_inode),
3403 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003404 if (!btrfs_inode_cachep)
3405 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003406 btrfs_trans_handle_cachep =
3407 btrfs_cache_create("btrfs_trans_handle_cache",
3408 sizeof(struct btrfs_trans_handle),
3409 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003410 if (!btrfs_trans_handle_cachep)
3411 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003412 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003413 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003414 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003415 if (!btrfs_transaction_cachep)
3416 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003417 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003418 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003419 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003420 if (!btrfs_path_cachep)
3421 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003422 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003423 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003424 if (!btrfs_bit_radix_cachep)
3425 goto fail;
3426 return 0;
3427fail:
3428 btrfs_destroy_cachep();
3429 return -ENOMEM;
3430}
3431
3432static int btrfs_getattr(struct vfsmount *mnt,
3433 struct dentry *dentry, struct kstat *stat)
3434{
3435 struct inode *inode = dentry->d_inode;
3436 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003437 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003438 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003439 return 0;
3440}
3441
3442static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3443 struct inode * new_dir,struct dentry *new_dentry)
3444{
3445 struct btrfs_trans_handle *trans;
3446 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3447 struct inode *new_inode = new_dentry->d_inode;
3448 struct inode *old_inode = old_dentry->d_inode;
3449 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003450 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003451 int ret;
3452
3453 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3454 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3455 return -ENOTEMPTY;
3456 }
Chris Mason5f39d392007-10-15 16:14:19 -04003457
Chris Mason1832a6d2007-12-21 16:27:21 -05003458 ret = btrfs_check_free_space(root, 1, 0);
3459 if (ret)
3460 goto out_unlock;
3461
Chris Mason39279cc2007-06-12 06:35:45 -04003462 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003463
Chris Mason39279cc2007-06-12 06:35:45 -04003464 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003465
3466 old_dentry->d_inode->i_nlink++;
3467 old_dir->i_ctime = old_dir->i_mtime = ctime;
3468 new_dir->i_ctime = new_dir->i_mtime = ctime;
3469 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003470
Chris Mason39279cc2007-06-12 06:35:45 -04003471 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3472 if (ret)
3473 goto out_fail;
3474
3475 if (new_inode) {
3476 new_inode->i_ctime = CURRENT_TIME;
3477 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3478 if (ret)
3479 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003480 if (new_inode->i_nlink == 0) {
3481 ret = btrfs_orphan_add(trans, new_inode);
3482 if (ret)
3483 goto out_fail;
3484 }
Chris Mason39279cc2007-06-12 06:35:45 -04003485 }
Chris Mason00e4e6b2008-08-05 11:18:09 -04003486 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003487 if (ret)
3488 goto out_fail;
3489
Chris Mason00e4e6b2008-08-05 11:18:09 -04003490 ret = btrfs_add_link(trans, new_dentry, old_inode, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003491 if (ret)
3492 goto out_fail;
3493
3494out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003495 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003496out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003497 return ret;
3498}
3499
Chris Masonea8c2812008-08-04 23:17:27 -04003500int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3501{
3502 struct list_head *head = &root->fs_info->delalloc_inodes;
3503 struct btrfs_inode *binode;
3504 unsigned long flags;
3505
3506 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3507 while(!list_empty(head)) {
3508 binode = list_entry(head->next, struct btrfs_inode,
3509 delalloc_inodes);
3510 atomic_inc(&binode->vfs_inode.i_count);
3511 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3512 filemap_write_and_wait(binode->vfs_inode.i_mapping);
3513 iput(&binode->vfs_inode);
3514 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3515 }
3516 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3517 return 0;
3518}
3519
Chris Mason39279cc2007-06-12 06:35:45 -04003520static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3521 const char *symname)
3522{
3523 struct btrfs_trans_handle *trans;
3524 struct btrfs_root *root = BTRFS_I(dir)->root;
3525 struct btrfs_path *path;
3526 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003527 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003528 int err;
3529 int drop_inode = 0;
3530 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003531 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04003532 int name_len;
3533 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003534 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003535 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003536 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003537 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003538
3539 name_len = strlen(symname) + 1;
3540 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3541 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003542
Chris Mason1832a6d2007-12-21 16:27:21 -05003543 err = btrfs_check_free_space(root, 1, 0);
3544 if (err)
3545 goto out_fail;
3546
Chris Mason39279cc2007-06-12 06:35:45 -04003547 trans = btrfs_start_transaction(root, 1);
3548 btrfs_set_trans_block_group(trans, dir);
3549
3550 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3551 if (err) {
3552 err = -ENOSPC;
3553 goto out_unlock;
3554 }
3555
Josef Bacikaec74772008-07-24 12:12:38 -04003556 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003557 dentry->d_name.len,
3558 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003559 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3560 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003561 err = PTR_ERR(inode);
3562 if (IS_ERR(inode))
3563 goto out_unlock;
3564
Josef Bacik33268ea2008-07-24 12:16:36 -04003565 err = btrfs_init_acl(inode, dir);
3566 if (err) {
3567 drop_inode = 1;
3568 goto out_unlock;
3569 }
3570
Chris Mason39279cc2007-06-12 06:35:45 -04003571 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003572 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003573 if (err)
3574 drop_inode = 1;
3575 else {
3576 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003577 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003578 inode->i_fop = &btrfs_file_operations;
3579 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003580 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3581 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003582 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003583 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3584 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003585 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04003586 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003587 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003588 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003589 BTRFS_I(inode)->disk_i_size = 0;
Chris Masondb69e0e2008-08-15 15:34:14 -04003590 inode->i_mapping->writeback_index = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003591 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003592 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003593 }
3594 dir->i_sb->s_dirt = 1;
3595 btrfs_update_inode_block_group(trans, inode);
3596 btrfs_update_inode_block_group(trans, dir);
3597 if (drop_inode)
3598 goto out_unlock;
3599
3600 path = btrfs_alloc_path();
3601 BUG_ON(!path);
3602 key.objectid = inode->i_ino;
3603 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003604 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3605 datasize = btrfs_file_extent_calc_inline_size(name_len);
3606 err = btrfs_insert_empty_item(trans, root, path, &key,
3607 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003608 if (err) {
3609 drop_inode = 1;
3610 goto out_unlock;
3611 }
Chris Mason5f39d392007-10-15 16:14:19 -04003612 leaf = path->nodes[0];
3613 ei = btrfs_item_ptr(leaf, path->slots[0],
3614 struct btrfs_file_extent_item);
3615 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3616 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003617 BTRFS_FILE_EXTENT_INLINE);
3618 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003619 write_extent_buffer(leaf, symname, ptr, name_len);
3620 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003621 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003622
Chris Mason39279cc2007-06-12 06:35:45 -04003623 inode->i_op = &btrfs_symlink_inode_operations;
3624 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003625 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003626 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003627 err = btrfs_update_inode(trans, root, inode);
3628 if (err)
3629 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003630
3631out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04003632 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003633 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003634out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003635 if (drop_inode) {
3636 inode_dec_link_count(inode);
3637 iput(inode);
3638 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04003639 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003640 return err;
3641}
Chris Mason16432982008-04-10 10:23:21 -04003642
Chris Masone6dcd2d2008-07-17 12:53:50 -04003643static int btrfs_set_page_dirty(struct page *page)
3644{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003645 return __set_page_dirty_nobuffers(page);
3646}
3647
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003648#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3649static int btrfs_permission(struct inode *inode, int mask)
3650#else
Yanfdebe2b2008-01-14 13:26:08 -05003651static int btrfs_permission(struct inode *inode, int mask,
3652 struct nameidata *nd)
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003653#endif
Yanfdebe2b2008-01-14 13:26:08 -05003654{
3655 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3656 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003657 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003658}
Chris Mason39279cc2007-06-12 06:35:45 -04003659
3660static struct inode_operations btrfs_dir_inode_operations = {
3661 .lookup = btrfs_lookup,
3662 .create = btrfs_create,
3663 .unlink = btrfs_unlink,
3664 .link = btrfs_link,
3665 .mkdir = btrfs_mkdir,
3666 .rmdir = btrfs_rmdir,
3667 .rename = btrfs_rename,
3668 .symlink = btrfs_symlink,
3669 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003670 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003671 .setxattr = btrfs_setxattr,
3672 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003673 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003674 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003675 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003676};
Chris Mason39279cc2007-06-12 06:35:45 -04003677static struct inode_operations btrfs_dir_ro_inode_operations = {
3678 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003679 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003680};
Chris Mason39279cc2007-06-12 06:35:45 -04003681static struct file_operations btrfs_dir_file_operations = {
3682 .llseek = generic_file_llseek,
3683 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01003684#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
3685 .readdir = btrfs_nfshack_readdir,
3686#else /* NFSd readdir/lookup deadlock is fixed */
3687 .readdir = btrfs_real_readdir,
3688#endif
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003689 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003690#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003691 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003692#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003693 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003694};
3695
Chris Masond1310b22008-01-24 16:13:08 -05003696static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003697 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003698 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003699 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003700 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003701 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003702 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003703 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003704 .set_bit_hook = btrfs_set_bit_hook,
3705 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003706};
3707
Chris Mason39279cc2007-06-12 06:35:45 -04003708static struct address_space_operations btrfs_aops = {
3709 .readpage = btrfs_readpage,
3710 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003711 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003712 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003713 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003714 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003715 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003716 .invalidatepage = btrfs_invalidatepage,
3717 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003718 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003719};
3720
3721static struct address_space_operations btrfs_symlink_aops = {
3722 .readpage = btrfs_readpage,
3723 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003724 .invalidatepage = btrfs_invalidatepage,
3725 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003726};
3727
3728static struct inode_operations btrfs_file_inode_operations = {
3729 .truncate = btrfs_truncate,
3730 .getattr = btrfs_getattr,
3731 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003732 .setxattr = btrfs_setxattr,
3733 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003734 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003735 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003736 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003737};
Josef Bacik618e21d2007-07-11 10:18:17 -04003738static struct inode_operations btrfs_special_inode_operations = {
3739 .getattr = btrfs_getattr,
3740 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003741 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003742 .setxattr = btrfs_setxattr,
3743 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04003744 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003745 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003746};
Chris Mason39279cc2007-06-12 06:35:45 -04003747static struct inode_operations btrfs_symlink_inode_operations = {
3748 .readlink = generic_readlink,
3749 .follow_link = page_follow_link_light,
3750 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003751 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003752};