blob: f3abecc2d14c941da0ae39d643c08fb78225fbc3 [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 Masone02119d2008-09-05 16:13:11 -040049#include "compat.h"
50#include "tree-log.h"
Chris Mason39279cc2007-06-12 06:35:45 -040051
52struct btrfs_iget_args {
53 u64 ino;
54 struct btrfs_root *root;
55};
56
57static struct inode_operations btrfs_dir_inode_operations;
58static struct inode_operations btrfs_symlink_inode_operations;
59static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040060static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040061static struct inode_operations btrfs_file_inode_operations;
62static struct address_space_operations btrfs_aops;
63static struct address_space_operations btrfs_symlink_aops;
64static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050065static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040066
67static struct kmem_cache *btrfs_inode_cachep;
68struct kmem_cache *btrfs_trans_handle_cachep;
69struct kmem_cache *btrfs_transaction_cachep;
70struct kmem_cache *btrfs_bit_radix_cachep;
71struct kmem_cache *btrfs_path_cachep;
72
73#define S_SHIFT 12
74static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
75 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
76 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
77 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
78 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
79 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
80 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
81 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
82};
83
Josef Bacik7b128762008-07-24 12:17:14 -040084static void btrfs_truncate(struct inode *inode);
85
Chris Masond352ac62008-09-29 15:18:18 -040086/*
87 * a very lame attempt at stopping writes when the FS is 85% full. There
88 * are countless ways this is incorrect, but it is better than nothing.
89 */
Chris Mason1832a6d2007-12-21 16:27:21 -050090int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
91 int for_del)
92{
Chris Masona2135012008-06-25 16:01:30 -040093 u64 total;
94 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050095 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040096 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050097 int ret = 0;
98
Chris Masona2135012008-06-25 16:01:30 -040099 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
100 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
101 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -0500102 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -0500103 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -0500104 else
Chris Masonf9ef6602008-01-03 09:22:38 -0500105 thresh = total * 85;
106
107 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500108
Chris Mason1832a6d2007-12-21 16:27:21 -0500109 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
110 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400111 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500112 return ret;
113}
114
Chris Masond352ac62008-09-29 15:18:18 -0400115/*
116 * when extent_io.c finds a delayed allocation range in the file,
117 * the call backs end up in this code. The basic idea is to
118 * allocate extents on disk for the range, and create ordered data structs
119 * in ram to track those extents.
120 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500121static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400122{
123 struct btrfs_root *root = BTRFS_I(inode)->root;
124 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400125 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400126 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500127 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400128 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500129 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500130 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400131 struct extent_map *em;
132 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
133 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400134
Chris Masonf9295742008-07-17 12:54:14 -0400135 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400136 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500137 btrfs_set_trans_block_group(trans, inode);
138
Chris Masondb945352007-10-15 16:15:53 -0400139 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500140 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500141 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400142
Chris Mason179e29e2007-11-01 11:28:41 -0400143 if (alloc_hint == EXTENT_MAP_INLINE)
144 goto out;
145
Chris Mason3b951512008-04-17 11:29:12 -0400146 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone5a22172008-07-18 20:42:20 -0400147 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400148 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Masone5a22172008-07-18 20:42:20 -0400149 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400150
Chris Masonc59f8952007-12-17 20:14:04 -0500151 while(num_bytes > 0) {
152 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400153 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400154 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400155 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500156 if (ret) {
157 WARN_ON(1);
158 goto out;
159 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400160 em = alloc_extent_map(GFP_NOFS);
161 em->start = start;
162 em->len = ins.offset;
163 em->block_start = ins.objectid;
164 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masone5a22172008-07-18 20:42:20 -0400165 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400166 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400167 while(1) {
168 spin_lock(&em_tree->lock);
169 ret = add_extent_mapping(em_tree, em);
170 spin_unlock(&em_tree->lock);
171 if (ret != -EEXIST) {
172 free_extent_map(em);
173 break;
174 }
175 btrfs_drop_extent_cache(inode, start,
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400176 start + ins.offset - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400177 }
Chris Masone5a22172008-07-18 20:42:20 -0400178 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400179
Chris Mason98d20f62008-04-14 09:46:10 -0400180 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400181 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Yan Zheng7ea394f2008-08-05 13:05:02 -0400182 ins.offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400183 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400184 if (num_bytes < cur_alloc_size) {
185 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
186 cur_alloc_size);
187 break;
188 }
Chris Masonc59f8952007-12-17 20:14:04 -0500189 num_bytes -= cur_alloc_size;
190 alloc_hint = ins.objectid + ins.offset;
191 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400192 }
Chris Masonb888db22007-08-27 16:49:44 -0400193out:
194 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500195 return ret;
196}
197
Chris Masond352ac62008-09-29 15:18:18 -0400198/*
199 * when nowcow writeback call back. This checks for snapshots or COW copies
200 * of the extents that exist in the file, and COWs the file as required.
201 *
202 * If no cow copies or snapshots exist, we write directly to the existing
203 * blocks on disk
204 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500205static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
206{
207 u64 extent_start;
208 u64 extent_end;
209 u64 bytenr;
Chris Mason1832a6d2007-12-21 16:27:21 -0500210 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500211 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500212 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400213 struct btrfs_block_group_cache *block_group;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400214 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500215 struct extent_buffer *leaf;
216 int found_type;
217 struct btrfs_path *path;
218 struct btrfs_file_extent_item *item;
219 int ret;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400220 int err = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500221 struct btrfs_key found_key;
222
Chris Masonc31f8832008-01-08 15:46:31 -0500223 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500224 path = btrfs_alloc_path();
225 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400226 trans = btrfs_join_transaction(root, 1);
227 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500228again:
229 ret = btrfs_lookup_file_extent(NULL, root, path,
230 inode->i_ino, start, 0);
231 if (ret < 0) {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400232 err = ret;
233 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500234 }
235
Chris Masonbe20aa92007-12-17 20:14:01 -0500236 if (ret != 0) {
237 if (path->slots[0] == 0)
238 goto not_found;
239 path->slots[0]--;
240 }
241
242 leaf = path->nodes[0];
243 item = btrfs_item_ptr(leaf, path->slots[0],
244 struct btrfs_file_extent_item);
245
246 /* are we inside the extent that was found? */
247 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
248 found_type = btrfs_key_type(&found_key);
249 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400250 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500251 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500252
253 found_type = btrfs_file_extent_type(leaf, item);
254 extent_start = found_key.offset;
255 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500256 u64 extent_num_bytes;
257
258 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
259 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500260 err = 0;
261
Chris Mason1832a6d2007-12-21 16:27:21 -0500262 if (loops && start != extent_start)
263 goto not_found;
264
Chris Masonbe20aa92007-12-17 20:14:01 -0500265 if (start < extent_start || start >= extent_end)
266 goto not_found;
267
Chris Masonbe20aa92007-12-17 20:14:01 -0500268 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
269 if (bytenr == 0)
270 goto not_found;
271
Yan Zheng7ea394f2008-08-05 13:05:02 -0400272 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
Chris Masona68d5932008-05-08 14:11:56 -0400273 goto not_found;
Chris Masonc31f8832008-01-08 15:46:31 -0500274 /*
275 * we may be called by the resizer, make sure we're inside
276 * the limits of the FS
277 */
Chris Masona68d5932008-05-08 14:11:56 -0400278 block_group = btrfs_lookup_block_group(root->fs_info,
279 bytenr);
280 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500281 goto not_found;
282
Yan Zheng7ea394f2008-08-05 13:05:02 -0400283 bytenr += btrfs_file_extent_offset(leaf, item);
284 extent_num_bytes = min(end + 1, extent_end) - start;
285 ret = btrfs_add_ordered_extent(inode, start, bytenr,
286 extent_num_bytes, 1);
287 if (ret) {
288 err = ret;
289 goto out;
290 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500291
Yan Zheng7ea394f2008-08-05 13:05:02 -0400292 btrfs_release_path(root, path);
293 start = extent_end;
294 if (start <= end) {
295 loops++;
296 goto again;
297 }
298 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500299not_found:
Yan Zheng7ea394f2008-08-05 13:05:02 -0400300 btrfs_end_transaction(trans, root);
301 btrfs_free_path(path);
302 return cow_file_range(inode, start, end);
303 }
304out:
305 WARN_ON(err);
306 btrfs_end_transaction(trans, root);
307 btrfs_free_path(path);
308 return err;
Chris Masonbe20aa92007-12-17 20:14:01 -0500309}
310
Chris Masond352ac62008-09-29 15:18:18 -0400311/*
312 * extent_io.c call back to do delayed allocation processing
313 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500314static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
315{
316 struct btrfs_root *root = BTRFS_I(inode)->root;
317 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400318
Yanb98b6762008-01-08 15:54:37 -0500319 if (btrfs_test_opt(root, NODATACOW) ||
320 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500321 ret = run_delalloc_nocow(inode, start, end);
322 else
323 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500324
Chris Masonb888db22007-08-27 16:49:44 -0400325 return ret;
326}
327
Chris Masond352ac62008-09-29 15:18:18 -0400328/*
329 * extent_io.c set_bit_hook, used to track delayed allocation
330 * bytes in this file, and to maintain the list of inodes that
331 * have pending delalloc work to be done.
332 */
Chris Mason291d6732008-01-29 15:55:23 -0500333int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500334 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500335{
Chris Masonbcbfce82008-04-22 13:26:47 -0400336 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500337 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500338 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400339 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500340 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500341 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -0400342 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
343 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
344 &root->fs_info->delalloc_inodes);
345 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400346 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500347 }
348 return 0;
349}
350
Chris Masond352ac62008-09-29 15:18:18 -0400351/*
352 * extent_io.c clear_bit_hook, see set_bit_hook for why
353 */
Chris Mason291d6732008-01-29 15:55:23 -0500354int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500355 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500356{
Chris Masonb0c68f82008-01-31 11:05:37 -0500357 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500358 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400359 unsigned long flags;
360
361 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500362 if (end - start + 1 > root->fs_info->delalloc_bytes) {
363 printk("warning: delalloc account %Lu %Lu\n",
364 end - start + 1, root->fs_info->delalloc_bytes);
365 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500366 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500367 } else {
368 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500369 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500370 }
Chris Masonea8c2812008-08-04 23:17:27 -0400371 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
372 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
373 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
374 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400375 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500376 }
377 return 0;
378}
379
Chris Masond352ac62008-09-29 15:18:18 -0400380/*
381 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
382 * we don't create bios that span stripes or chunks
383 */
Chris Mason239b14b2008-03-24 15:02:07 -0400384int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
385 size_t size, struct bio *bio)
386{
387 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
388 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400389 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400390 u64 length = 0;
391 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400392 int ret;
393
Chris Masonf2d8d742008-04-21 10:03:05 -0400394 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400395 map_tree = &root->fs_info->mapping_tree;
396 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400397 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400398 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400399
Chris Mason239b14b2008-03-24 15:02:07 -0400400 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400401 return 1;
402 }
403 return 0;
404}
405
Chris Masond352ac62008-09-29 15:18:18 -0400406/*
407 * in order to insert checksums into the metadata in large chunks,
408 * we wait until bio submission time. All the pages in the bio are
409 * checksummed and sums are attached onto the ordered extent record.
410 *
411 * At IO completion time the cums attached on the ordered extent record
412 * are inserted into the btree
413 */
Chris Mason44b8bd72008-04-16 11:14:51 -0400414int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400415 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500416{
Chris Mason065631f2008-02-20 12:07:25 -0500417 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500418 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400419
Chris Mason3edf7d32008-07-18 06:17:13 -0400420 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400421 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400422
Chris Mason8b712842008-06-11 16:50:36 -0400423 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400424}
425
Chris Masond352ac62008-09-29 15:18:18 -0400426/*
427 * extent_io.c submission hook. This does the right thing for csum calculation on write,
428 * or reading the csums from the tree before a read
429 */
Chris Mason44b8bd72008-04-16 11:14:51 -0400430int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
431 int mirror_num)
432{
433 struct btrfs_root *root = BTRFS_I(inode)->root;
434 int ret = 0;
435
Chris Masone6dcd2d2008-07-17 12:53:50 -0400436 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
437 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500438
Yan Zheng7ea394f2008-08-05 13:05:02 -0400439 if (btrfs_test_opt(root, NODATASUM) ||
440 btrfs_test_flag(inode, NODATASUM)) {
441 goto mapit;
442 }
443
Chris Mason4d1b5fb2008-08-20 09:44:52 -0400444 if (!(rw & (1 << BIO_RW))) {
445 btrfs_lookup_bio_sums(root, inode, bio);
446 goto mapit;
447 }
Chris Mason44b8bd72008-04-16 11:14:51 -0400448 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
449 inode, rw, bio, mirror_num,
450 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400451mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400452 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500453}
Chris Mason6885f302008-02-20 16:11:05 -0500454
Chris Masond352ac62008-09-29 15:18:18 -0400455/*
456 * given a list of ordered sums record them in the inode. This happens
457 * at IO completion time based on sums calculated at bio submission time.
458 */
Chris Masonba1da2f2008-07-17 12:54:15 -0400459static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400460 struct inode *inode, u64 file_offset,
461 struct list_head *list)
462{
463 struct list_head *cur;
464 struct btrfs_ordered_sum *sum;
465
466 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400467 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400468 sum = list_entry(cur, struct btrfs_ordered_sum, list);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400469 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
470 inode, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400471 }
472 return 0;
473}
474
Chris Masonea8c2812008-08-04 23:17:27 -0400475int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
476{
477 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
478 GFP_NOFS);
479}
480
Chris Masond352ac62008-09-29 15:18:18 -0400481/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -0400482struct btrfs_writepage_fixup {
483 struct page *page;
484 struct btrfs_work work;
485};
486
Chris Mason247e7432008-07-17 12:53:51 -0400487void btrfs_writepage_fixup_worker(struct btrfs_work *work)
488{
489 struct btrfs_writepage_fixup *fixup;
490 struct btrfs_ordered_extent *ordered;
491 struct page *page;
492 struct inode *inode;
493 u64 page_start;
494 u64 page_end;
495
496 fixup = container_of(work, struct btrfs_writepage_fixup, work);
497 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400498again:
Chris Mason247e7432008-07-17 12:53:51 -0400499 lock_page(page);
500 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
501 ClearPageChecked(page);
502 goto out_page;
503 }
504
505 inode = page->mapping->host;
506 page_start = page_offset(page);
507 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
508
509 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400510
511 /* already ordered? We're done */
512 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
513 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400514 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400515 }
516
517 ordered = btrfs_lookup_ordered_extent(inode, page_start);
518 if (ordered) {
519 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
520 page_end, GFP_NOFS);
521 unlock_page(page);
522 btrfs_start_ordered_extent(inode, ordered, 1);
523 goto again;
524 }
Chris Mason247e7432008-07-17 12:53:51 -0400525
Chris Masonea8c2812008-08-04 23:17:27 -0400526 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -0400527 ClearPageChecked(page);
528out:
529 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
530out_page:
531 unlock_page(page);
532 page_cache_release(page);
533}
534
535/*
536 * There are a few paths in the higher layers of the kernel that directly
537 * set the page dirty bit without asking the filesystem if it is a
538 * good idea. This causes problems because we want to make sure COW
539 * properly happens and the data=ordered rules are followed.
540 *
541 * In our case any range that doesn't have the EXTENT_ORDERED bit set
542 * hasn't been properly setup for IO. We kick off an async process
543 * to fix it up. The async helper will wait for ordered extents, set
544 * the delalloc bit and make it safe to write the page.
545 */
546int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
547{
548 struct inode *inode = page->mapping->host;
549 struct btrfs_writepage_fixup *fixup;
550 struct btrfs_root *root = BTRFS_I(inode)->root;
551 int ret;
552
553 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
554 EXTENT_ORDERED, 0);
555 if (ret)
556 return 0;
557
558 if (PageChecked(page))
559 return -EAGAIN;
560
561 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
562 if (!fixup)
563 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400564
Chris Mason247e7432008-07-17 12:53:51 -0400565 SetPageChecked(page);
566 page_cache_get(page);
567 fixup->work.func = btrfs_writepage_fixup_worker;
568 fixup->page = page;
569 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
570 return -EAGAIN;
571}
572
Chris Masond352ac62008-09-29 15:18:18 -0400573/* as ordered data IO finishes, this gets called so we can finish
574 * an ordered extent if the range of bytes in the file it covers are
575 * fully written.
576 */
Chris Mason211f90e2008-07-18 11:56:15 -0400577static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400578{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400579 struct btrfs_root *root = BTRFS_I(inode)->root;
580 struct btrfs_trans_handle *trans;
581 struct btrfs_ordered_extent *ordered_extent;
582 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Zheng Yan31840ae2008-09-23 13:14:14 -0400583 struct btrfs_file_extent_item *extent_item;
584 struct btrfs_path *path = NULL;
585 struct extent_buffer *leaf;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400586 u64 alloc_hint = 0;
587 struct list_head list;
588 struct btrfs_key ins;
589 int ret;
590
591 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400592 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400593 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400594
Chris Masonf9295742008-07-17 12:54:14 -0400595 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400596
597 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
598 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400599 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
600 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400601
Zheng Yan31840ae2008-09-23 13:14:14 -0400602 path = btrfs_alloc_path();
603 BUG_ON(!path);
604
Chris Masone6dcd2d2008-07-17 12:53:50 -0400605 lock_extent(io_tree, ordered_extent->file_offset,
606 ordered_extent->file_offset + ordered_extent->len - 1,
607 GFP_NOFS);
608
609 INIT_LIST_HEAD(&list);
610
Chris Masonee6e6502008-07-17 12:54:40 -0400611 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400612
Chris Masone6dcd2d2008-07-17 12:53:50 -0400613 ret = btrfs_drop_extents(trans, root, inode,
614 ordered_extent->file_offset,
615 ordered_extent->file_offset +
616 ordered_extent->len,
617 ordered_extent->file_offset, &alloc_hint);
618 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -0400619
620 ins.objectid = inode->i_ino;
621 ins.offset = ordered_extent->file_offset;
622 ins.type = BTRFS_EXTENT_DATA_KEY;
623 ret = btrfs_insert_empty_item(trans, root, path, &ins,
624 sizeof(*extent_item));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400625 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -0400626 leaf = path->nodes[0];
627 extent_item = btrfs_item_ptr(leaf, path->slots[0],
628 struct btrfs_file_extent_item);
629 btrfs_set_file_extent_generation(leaf, extent_item, trans->transid);
630 btrfs_set_file_extent_type(leaf, extent_item, BTRFS_FILE_EXTENT_REG);
631 btrfs_set_file_extent_disk_bytenr(leaf, extent_item,
632 ordered_extent->start);
633 btrfs_set_file_extent_disk_num_bytes(leaf, extent_item,
634 ordered_extent->len);
635 btrfs_set_file_extent_offset(leaf, extent_item, 0);
636 btrfs_set_file_extent_num_bytes(leaf, extent_item,
637 ordered_extent->len);
638 btrfs_mark_buffer_dirty(leaf);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400639
Chris Masone6dcd2d2008-07-17 12:53:50 -0400640 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
641 ordered_extent->file_offset +
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400642 ordered_extent->len - 1, 0);
Chris Masonee6e6502008-07-17 12:54:40 -0400643 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
644
Zheng Yan31840ae2008-09-23 13:14:14 -0400645 ins.objectid = ordered_extent->start;
646 ins.offset = ordered_extent->len;
647 ins.type = BTRFS_EXTENT_ITEM_KEY;
648 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
649 root->root_key.objectid,
650 trans->transid, inode->i_ino,
651 ordered_extent->file_offset, &ins);
652 BUG_ON(ret);
653 btrfs_release_path(root, path);
654
Chris Masone6dcd2d2008-07-17 12:53:50 -0400655 inode->i_blocks += ordered_extent->len >> 9;
656 unlock_extent(io_tree, ordered_extent->file_offset,
657 ordered_extent->file_offset + ordered_extent->len - 1,
658 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400659nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -0400660 add_pending_csums(trans, inode, ordered_extent->file_offset,
661 &ordered_extent->list);
662
Chris Mason34353022008-09-23 20:19:49 -0400663 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -0400664 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -0400665 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400666 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -0400667 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400668
Chris Masone6dcd2d2008-07-17 12:53:50 -0400669 /* once for us */
670 btrfs_put_ordered_extent(ordered_extent);
671 /* once for the tree */
672 btrfs_put_ordered_extent(ordered_extent);
673
Chris Masone6dcd2d2008-07-17 12:53:50 -0400674 btrfs_end_transaction(trans, root);
Zheng Yan31840ae2008-09-23 13:14:14 -0400675 if (path)
676 btrfs_free_path(path);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400677 return 0;
678}
679
Chris Mason211f90e2008-07-18 11:56:15 -0400680int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
681 struct extent_state *state, int uptodate)
682{
683 return btrfs_finish_ordered_io(page->mapping->host, start, end);
684}
685
Chris Masond352ac62008-09-29 15:18:18 -0400686/*
687 * When IO fails, either with EIO or csum verification fails, we
688 * try other mirrors that might have a good copy of the data. This
689 * io_failure_record is used to record state as we go through all the
690 * mirrors. If another mirror has good data, the page is set up to date
691 * and things continue. If a good mirror can't be found, the original
692 * bio end_io callback is called to indicate things have failed.
693 */
Chris Mason7e383262008-04-09 16:28:12 -0400694struct io_failure_record {
695 struct page *page;
696 u64 start;
697 u64 len;
698 u64 logical;
699 int last_mirror;
700};
701
Chris Mason1259ab72008-05-12 13:39:03 -0400702int btrfs_io_failed_hook(struct bio *failed_bio,
703 struct page *page, u64 start, u64 end,
704 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400705{
706 struct io_failure_record *failrec = NULL;
707 u64 private;
708 struct extent_map *em;
709 struct inode *inode = page->mapping->host;
710 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400711 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400712 struct bio *bio;
713 int num_copies;
714 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400715 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400716 u64 logical;
717
718 ret = get_state_private(failure_tree, start, &private);
719 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400720 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
721 if (!failrec)
722 return -ENOMEM;
723 failrec->start = start;
724 failrec->len = end - start + 1;
725 failrec->last_mirror = 0;
726
Chris Mason3b951512008-04-17 11:29:12 -0400727 spin_lock(&em_tree->lock);
728 em = lookup_extent_mapping(em_tree, start, failrec->len);
729 if (em->start > start || em->start + em->len < start) {
730 free_extent_map(em);
731 em = NULL;
732 }
733 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400734
735 if (!em || IS_ERR(em)) {
736 kfree(failrec);
737 return -EIO;
738 }
739 logical = start - em->start;
740 logical = em->block_start + logical;
741 failrec->logical = logical;
742 free_extent_map(em);
743 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
744 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400745 set_state_private(failure_tree, start,
746 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400747 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400748 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400749 }
750 num_copies = btrfs_num_copies(
751 &BTRFS_I(inode)->root->fs_info->mapping_tree,
752 failrec->logical, failrec->len);
753 failrec->last_mirror++;
754 if (!state) {
755 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
756 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
757 failrec->start,
758 EXTENT_LOCKED);
759 if (state && state->start != failrec->start)
760 state = NULL;
761 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
762 }
763 if (!state || failrec->last_mirror > num_copies) {
764 set_state_private(failure_tree, failrec->start, 0);
765 clear_extent_bits(failure_tree, failrec->start,
766 failrec->start + failrec->len - 1,
767 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
768 kfree(failrec);
769 return -EIO;
770 }
771 bio = bio_alloc(GFP_NOFS, 1);
772 bio->bi_private = state;
773 bio->bi_end_io = failed_bio->bi_end_io;
774 bio->bi_sector = failrec->logical >> 9;
775 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400776 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400777 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400778 if (failed_bio->bi_rw & (1 << BIO_RW))
779 rw = WRITE;
780 else
781 rw = READ;
782
783 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
784 failrec->last_mirror);
785 return 0;
786}
787
Chris Masond352ac62008-09-29 15:18:18 -0400788/*
789 * each time an IO finishes, we do a fast check in the IO failure tree
790 * to see if we need to process or clean up an io_failure_record
791 */
Chris Mason1259ab72008-05-12 13:39:03 -0400792int btrfs_clean_io_failures(struct inode *inode, u64 start)
793{
794 u64 private;
795 u64 private_failure;
796 struct io_failure_record *failure;
797 int ret;
798
799 private = 0;
800 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
801 (u64)-1, 1, EXTENT_DIRTY)) {
802 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
803 start, &private_failure);
804 if (ret == 0) {
805 failure = (struct io_failure_record *)(unsigned long)
806 private_failure;
807 set_state_private(&BTRFS_I(inode)->io_failure_tree,
808 failure->start, 0);
809 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
810 failure->start,
811 failure->start + failure->len - 1,
812 EXTENT_DIRTY | EXTENT_LOCKED,
813 GFP_NOFS);
814 kfree(failure);
815 }
816 }
Chris Mason7e383262008-04-09 16:28:12 -0400817 return 0;
818}
819
Chris Masond352ac62008-09-29 15:18:18 -0400820/*
821 * when reads are done, we need to check csums to verify the data is correct
822 * if there's a match, we allow the bio to finish. If not, we go through
823 * the io_failure_record routines to find good copies
824 */
Chris Mason70dec802008-01-29 09:59:12 -0500825int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
826 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400827{
Chris Mason35ebb932007-10-30 16:56:53 -0400828 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400829 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500830 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400831 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500832 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400833 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400834 struct btrfs_root *root = BTRFS_I(inode)->root;
835 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400836 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500837
Yanb98b6762008-01-08 15:54:37 -0500838 if (btrfs_test_opt(root, NODATASUM) ||
839 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500840 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500841 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500842 private = state->private;
843 ret = 0;
844 } else {
845 ret = get_state_private(io_tree, start, &private);
846 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400847 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400848 kaddr = kmap_atomic(page, KM_IRQ0);
849 if (ret) {
850 goto zeroit;
851 }
Chris Masonff79f812007-10-15 16:22:25 -0400852 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
853 btrfs_csum_final(csum, (char *)&csum);
854 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400855 goto zeroit;
856 }
857 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400858 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400859
860 /* if the io failure tree for this inode is non-empty,
861 * check to see if we've recovered from a failed IO
862 */
Chris Mason1259ab72008-05-12 13:39:03 -0400863 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400864 return 0;
865
866zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500867 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
868 page->mapping->host->i_ino, (unsigned long long)start, csum,
869 private);
Chris Masondb945352007-10-15 16:15:53 -0400870 memset(kaddr + offset, 1, end - start + 1);
871 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400872 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400873 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400874 if (private == 0)
875 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400876 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400877}
Chris Masonb888db22007-08-27 16:49:44 -0400878
Josef Bacik7b128762008-07-24 12:17:14 -0400879/*
880 * This creates an orphan entry for the given inode in case something goes
881 * wrong in the middle of an unlink/truncate.
882 */
883int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
884{
885 struct btrfs_root *root = BTRFS_I(inode)->root;
886 int ret = 0;
887
Yanbcc63ab2008-07-30 16:29:20 -0400888 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400889
890 /* already on the orphan list, we're good */
891 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400892 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400893 return 0;
894 }
895
896 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
897
Yanbcc63ab2008-07-30 16:29:20 -0400898 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400899
900 /*
901 * insert an orphan item to track this unlinked/truncated file
902 */
903 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
904
905 return ret;
906}
907
908/*
909 * We have done the truncate/delete so we can go ahead and remove the orphan
910 * item for this particular inode.
911 */
912int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
913{
914 struct btrfs_root *root = BTRFS_I(inode)->root;
915 int ret = 0;
916
Yanbcc63ab2008-07-30 16:29:20 -0400917 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400918
919 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400920 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400921 return 0;
922 }
923
924 list_del_init(&BTRFS_I(inode)->i_orphan);
925 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400926 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400927 return 0;
928 }
929
Yanbcc63ab2008-07-30 16:29:20 -0400930 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400931
932 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
933
934 return ret;
935}
936
937/*
938 * this cleans up any orphans that may be left on the list from the last use
939 * of this root.
940 */
941void btrfs_orphan_cleanup(struct btrfs_root *root)
942{
943 struct btrfs_path *path;
944 struct extent_buffer *leaf;
945 struct btrfs_item *item;
946 struct btrfs_key key, found_key;
947 struct btrfs_trans_handle *trans;
948 struct inode *inode;
949 int ret = 0, nr_unlink = 0, nr_truncate = 0;
950
951 /* don't do orphan cleanup if the fs is readonly. */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400952 if (root->fs_info->sb->s_flags & MS_RDONLY)
Josef Bacik7b128762008-07-24 12:17:14 -0400953 return;
954
955 path = btrfs_alloc_path();
956 if (!path)
957 return;
958 path->reada = -1;
959
960 key.objectid = BTRFS_ORPHAN_OBJECTID;
961 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
962 key.offset = (u64)-1;
963
Josef Bacik7b128762008-07-24 12:17:14 -0400964
965 while (1) {
966 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
967 if (ret < 0) {
968 printk(KERN_ERR "Error searching slot for orphan: %d"
969 "\n", ret);
970 break;
971 }
972
973 /*
974 * if ret == 0 means we found what we were searching for, which
975 * is weird, but possible, so only screw with path if we didnt
976 * find the key and see if we have stuff that matches
977 */
978 if (ret > 0) {
979 if (path->slots[0] == 0)
980 break;
981 path->slots[0]--;
982 }
983
984 /* pull out the item */
985 leaf = path->nodes[0];
986 item = btrfs_item_nr(leaf, path->slots[0]);
987 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
988
989 /* make sure the item matches what we want */
990 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
991 break;
992 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
993 break;
994
995 /* release the path since we're done with it */
996 btrfs_release_path(root, path);
997
998 /*
999 * this is where we are basically btrfs_lookup, without the
1000 * crossing root thing. we store the inode number in the
1001 * offset of the orphan item.
1002 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001003 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001004 found_key.offset, root);
1005 if (!inode)
1006 break;
1007
1008 if (inode->i_state & I_NEW) {
1009 BTRFS_I(inode)->root = root;
1010
1011 /* have to set the location manually */
1012 BTRFS_I(inode)->location.objectid = inode->i_ino;
1013 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1014 BTRFS_I(inode)->location.offset = 0;
1015
1016 btrfs_read_locked_inode(inode);
1017 unlock_new_inode(inode);
1018 }
1019
1020 /*
1021 * add this inode to the orphan list so btrfs_orphan_del does
1022 * the proper thing when we hit it
1023 */
Yanbcc63ab2008-07-30 16:29:20 -04001024 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001025 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001026 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001027
1028 /*
1029 * if this is a bad inode, means we actually succeeded in
1030 * removing the inode, but not the orphan record, which means
1031 * we need to manually delete the orphan since iput will just
1032 * do a destroy_inode
1033 */
1034 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001035 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001036 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001037 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001038 iput(inode);
1039 continue;
1040 }
1041
1042 /* if we have links, this was a truncate, lets do that */
1043 if (inode->i_nlink) {
1044 nr_truncate++;
1045 btrfs_truncate(inode);
1046 } else {
1047 nr_unlink++;
1048 }
1049
1050 /* this will do delete_inode and everything for us */
1051 iput(inode);
1052 }
1053
1054 if (nr_unlink)
1055 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1056 if (nr_truncate)
1057 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1058
1059 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04001060}
1061
Chris Masond352ac62008-09-29 15:18:18 -04001062/*
1063 * read an inode from the btree into the in-memory inode
1064 */
Chris Mason39279cc2007-06-12 06:35:45 -04001065void btrfs_read_locked_inode(struct inode *inode)
1066{
1067 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001068 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001069 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001070 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001071 struct btrfs_root *root = BTRFS_I(inode)->root;
1072 struct btrfs_key location;
1073 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001074 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001075 int ret;
1076
1077 path = btrfs_alloc_path();
1078 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001079 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001080
Chris Mason39279cc2007-06-12 06:35:45 -04001081 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001082 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001083 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001084
Chris Mason5f39d392007-10-15 16:14:19 -04001085 leaf = path->nodes[0];
1086 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1087 struct btrfs_inode_item);
1088
1089 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1090 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1091 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1092 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001093 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001094
1095 tspec = btrfs_inode_atime(inode_item);
1096 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1097 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1098
1099 tspec = btrfs_inode_mtime(inode_item);
1100 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1101 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1102
1103 tspec = btrfs_inode_ctime(inode_item);
1104 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1105 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1106
1107 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04001108 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
1109 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04001110 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001111 rdev = btrfs_inode_rdev(leaf, inode_item);
1112
Josef Bacikaec74772008-07-24 12:12:38 -04001113 BTRFS_I(inode)->index_cnt = (u64)-1;
1114
Chris Mason5f39d392007-10-15 16:14:19 -04001115 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001116 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1117 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001118 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001119 if (!BTRFS_I(inode)->block_group) {
1120 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001121 NULL, 0,
1122 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001123 }
Chris Mason39279cc2007-06-12 06:35:45 -04001124 btrfs_free_path(path);
1125 inode_item = NULL;
1126
Chris Mason39279cc2007-06-12 06:35:45 -04001127 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001128 case S_IFREG:
1129 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001130 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001131 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001132 inode->i_fop = &btrfs_file_operations;
1133 inode->i_op = &btrfs_file_inode_operations;
1134 break;
1135 case S_IFDIR:
1136 inode->i_fop = &btrfs_dir_file_operations;
1137 if (root == root->fs_info->tree_root)
1138 inode->i_op = &btrfs_dir_ro_inode_operations;
1139 else
1140 inode->i_op = &btrfs_dir_inode_operations;
1141 break;
1142 case S_IFLNK:
1143 inode->i_op = &btrfs_symlink_inode_operations;
1144 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001145 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001146 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001147 default:
1148 init_special_inode(inode, inode->i_mode, rdev);
1149 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001150 }
1151 return;
1152
1153make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001154 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001155 make_bad_inode(inode);
1156}
1157
Chris Masond352ac62008-09-29 15:18:18 -04001158/*
1159 * given a leaf and an inode, copy the inode fields into the leaf
1160 */
Chris Masone02119d2008-09-05 16:13:11 -04001161static void fill_inode_item(struct btrfs_trans_handle *trans,
1162 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04001163 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001164 struct inode *inode)
1165{
Chris Mason5f39d392007-10-15 16:14:19 -04001166 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1167 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001168 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001169 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1170 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1171
1172 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1173 inode->i_atime.tv_sec);
1174 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1175 inode->i_atime.tv_nsec);
1176
1177 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1178 inode->i_mtime.tv_sec);
1179 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1180 inode->i_mtime.tv_nsec);
1181
1182 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1183 inode->i_ctime.tv_sec);
1184 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1185 inode->i_ctime.tv_nsec);
1186
1187 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
Chris Masone02119d2008-09-05 16:13:11 -04001188 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
1189 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001190 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001191 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001192 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001193 BTRFS_I(inode)->block_group->key.objectid);
1194}
1195
Chris Masond352ac62008-09-29 15:18:18 -04001196/*
1197 * copy everything in the in-memory inode into the btree.
1198 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001199int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001200 struct btrfs_root *root,
1201 struct inode *inode)
1202{
1203 struct btrfs_inode_item *inode_item;
1204 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001205 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001206 int ret;
1207
1208 path = btrfs_alloc_path();
1209 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001210 ret = btrfs_lookup_inode(trans, root, path,
1211 &BTRFS_I(inode)->location, 1);
1212 if (ret) {
1213 if (ret > 0)
1214 ret = -ENOENT;
1215 goto failed;
1216 }
1217
Chris Mason5f39d392007-10-15 16:14:19 -04001218 leaf = path->nodes[0];
1219 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001220 struct btrfs_inode_item);
1221
Chris Masone02119d2008-09-05 16:13:11 -04001222 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001223 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001224 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001225 ret = 0;
1226failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001227 btrfs_free_path(path);
1228 return ret;
1229}
1230
1231
Chris Masond352ac62008-09-29 15:18:18 -04001232/*
1233 * unlink helper that gets used here in inode.c and in the tree logging
1234 * recovery code. It remove a link in a directory with a given name, and
1235 * also drops the back refs in the inode to the directory
1236 */
Chris Masone02119d2008-09-05 16:13:11 -04001237int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
1238 struct btrfs_root *root,
1239 struct inode *dir, struct inode *inode,
1240 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04001241{
1242 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001243 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001244 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001245 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001246 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001247 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001248
1249 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001250 if (!path) {
1251 ret = -ENOMEM;
1252 goto err;
1253 }
1254
Chris Mason39279cc2007-06-12 06:35:45 -04001255 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1256 name, name_len, -1);
1257 if (IS_ERR(di)) {
1258 ret = PTR_ERR(di);
1259 goto err;
1260 }
1261 if (!di) {
1262 ret = -ENOENT;
1263 goto err;
1264 }
Chris Mason5f39d392007-10-15 16:14:19 -04001265 leaf = path->nodes[0];
1266 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001267 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001268 if (ret)
1269 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001270 btrfs_release_path(root, path);
1271
Josef Bacikaec74772008-07-24 12:12:38 -04001272 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001273 inode->i_ino,
1274 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04001275 if (ret) {
1276 printk("failed to delete reference to %.*s, "
1277 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04001278 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04001279 goto err;
1280 }
1281
Chris Mason39279cc2007-06-12 06:35:45 -04001282 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001283 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001284 if (IS_ERR(di)) {
1285 ret = PTR_ERR(di);
1286 goto err;
1287 }
1288 if (!di) {
1289 ret = -ENOENT;
1290 goto err;
1291 }
1292 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001293 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001294
Chris Masone02119d2008-09-05 16:13:11 -04001295 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
1296 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04001297 BUG_ON(ret != 0 && ret != -ENOENT);
1298 if (ret != -ENOENT)
1299 BTRFS_I(dir)->log_dirty_trans = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04001300
1301 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
1302 dir, index);
1303 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001304err:
1305 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001306 if (ret)
1307 goto out;
1308
1309 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1310 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1311 btrfs_update_inode(trans, root, dir);
1312 btrfs_drop_nlink(inode);
1313 ret = btrfs_update_inode(trans, root, inode);
1314 dir->i_sb->s_dirt = 1;
1315out:
Chris Mason39279cc2007-06-12 06:35:45 -04001316 return ret;
1317}
1318
1319static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1320{
1321 struct btrfs_root *root;
1322 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001323 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001324 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001325 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001326
1327 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001328
1329 ret = btrfs_check_free_space(root, 1, 1);
1330 if (ret)
1331 goto fail;
1332
Chris Mason39279cc2007-06-12 06:35:45 -04001333 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001334
Chris Mason39279cc2007-06-12 06:35:45 -04001335 btrfs_set_trans_block_group(trans, dir);
Chris Masone02119d2008-09-05 16:13:11 -04001336 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1337 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04001338
1339 if (inode->i_nlink == 0)
1340 ret = btrfs_orphan_add(trans, inode);
1341
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001342 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001343
Chris Mason89ce8a62008-06-25 16:01:31 -04001344 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001345fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001346 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001347 return ret;
1348}
1349
1350static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1351{
1352 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001353 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001354 int ret;
1355 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001356 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001357 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001358
Chris Mason925baed2008-06-25 16:01:30 -04001359 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001360 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001361 }
Yan134d4512007-10-25 15:49:25 -04001362
Chris Mason1832a6d2007-12-21 16:27:21 -05001363 ret = btrfs_check_free_space(root, 1, 1);
1364 if (ret)
1365 goto fail;
1366
Chris Mason39279cc2007-06-12 06:35:45 -04001367 trans = btrfs_start_transaction(root, 1);
1368 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001369
Josef Bacik7b128762008-07-24 12:17:14 -04001370 err = btrfs_orphan_add(trans, inode);
1371 if (err)
1372 goto fail_trans;
1373
Chris Mason39279cc2007-06-12 06:35:45 -04001374 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04001375 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1376 dentry->d_name.name, dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001377 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001378 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001379 }
Chris Mason39544012007-12-12 14:38:19 -05001380
Josef Bacik7b128762008-07-24 12:17:14 -04001381fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001382 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001383 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001384fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001385 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001386
Chris Mason39279cc2007-06-12 06:35:45 -04001387 if (ret && !err)
1388 err = ret;
1389 return err;
1390}
1391
Chris Mason39279cc2007-06-12 06:35:45 -04001392/*
Chris Mason39279cc2007-06-12 06:35:45 -04001393 * this can truncate away extent items, csum items and directory items.
1394 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04001395 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04001396 *
1397 * csum items that cross the new i_size are truncated to the new size
1398 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001399 *
1400 * min_type is the minimum key type to truncate down to. If set to 0, this
1401 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001402 */
Chris Masone02119d2008-09-05 16:13:11 -04001403noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
1404 struct btrfs_root *root,
1405 struct inode *inode,
1406 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001407{
1408 int ret;
1409 struct btrfs_path *path;
1410 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001411 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001412 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001413 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001414 struct btrfs_file_extent_item *fi;
1415 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001416 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001417 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001418 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001419 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001420 int found_extent;
1421 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001422 int pending_del_nr = 0;
1423 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001424 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001425 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001426
Chris Masone02119d2008-09-05 16:13:11 -04001427 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001428 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001429 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001430 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001431 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001432
Chris Mason39279cc2007-06-12 06:35:45 -04001433 /* FIXME, add redo link to tree so we don't leak on crash */
1434 key.objectid = inode->i_ino;
1435 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001436 key.type = (u8)-1;
1437
Chris Mason85e21ba2008-01-29 15:11:36 -05001438 btrfs_init_path(path);
1439search_again:
1440 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1441 if (ret < 0) {
1442 goto error;
1443 }
1444 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001445 /* there are no items in the tree for us to truncate, we're
1446 * done
1447 */
1448 if (path->slots[0] == 0) {
1449 ret = 0;
1450 goto error;
1451 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001452 path->slots[0]--;
1453 }
1454
Chris Mason39279cc2007-06-12 06:35:45 -04001455 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001456 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001457 leaf = path->nodes[0];
1458 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1459 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001460
Chris Mason5f39d392007-10-15 16:14:19 -04001461 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001462 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001463
Chris Mason85e21ba2008-01-29 15:11:36 -05001464 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001465 break;
1466
Chris Mason5f39d392007-10-15 16:14:19 -04001467 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001468 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001469 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001470 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001471 extent_type = btrfs_file_extent_type(leaf, fi);
1472 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001473 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001474 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001475 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1476 struct btrfs_item *item = btrfs_item_nr(leaf,
1477 path->slots[0]);
1478 item_end += btrfs_file_extent_inline_len(leaf,
1479 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001480 }
Yan008630c2007-11-07 13:31:09 -05001481 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001482 }
1483 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1484 ret = btrfs_csum_truncate(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04001485 new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04001486 BUG_ON(ret);
1487 }
Chris Masone02119d2008-09-05 16:13:11 -04001488 if (item_end < new_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001489 if (found_type == BTRFS_DIR_ITEM_KEY) {
1490 found_type = BTRFS_INODE_ITEM_KEY;
1491 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1492 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001493 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1494 found_type = BTRFS_XATTR_ITEM_KEY;
1495 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1496 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001497 } else if (found_type) {
1498 found_type--;
1499 } else {
1500 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001501 }
Yana61721d2007-09-17 11:08:38 -04001502 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001503 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001504 }
Chris Masone02119d2008-09-05 16:13:11 -04001505 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001506 del_item = 1;
1507 else
1508 del_item = 0;
1509 found_extent = 0;
1510
1511 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001512 if (found_type != BTRFS_EXTENT_DATA_KEY)
1513 goto delete;
1514
1515 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001516 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001517 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001518 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001519 u64 orig_num_bytes =
1520 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04001521 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001522 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001523 extent_num_bytes = extent_num_bytes &
1524 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001525 btrfs_set_file_extent_num_bytes(leaf, fi,
1526 extent_num_bytes);
1527 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001528 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04001529 if (root->ref_cows && extent_start != 0)
Chris Mason90692182008-02-08 13:49:28 -05001530 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001531 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001532 } else {
Chris Masondb945352007-10-15 16:15:53 -04001533 extent_num_bytes =
1534 btrfs_file_extent_disk_num_bytes(leaf,
1535 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001536 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001537 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001538 if (extent_start != 0) {
1539 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04001540 if (root->ref_cows)
1541 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001542 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001543 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001544 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001545 }
Chris Mason90692182008-02-08 13:49:28 -05001546 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1547 if (!del_item) {
Chris Masone02119d2008-09-05 16:13:11 -04001548 u32 size = new_size - found_key.offset;
1549
1550 if (root->ref_cows) {
1551 dec_i_blocks(inode, item_end + 1 -
1552 found_key.offset - size);
1553 }
1554 size =
1555 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05001556 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04001557 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05001558 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001559 } else if (root->ref_cows) {
Chris Mason90692182008-02-08 13:49:28 -05001560 dec_i_blocks(inode, item_end + 1 -
1561 found_key.offset);
1562 }
Chris Mason39279cc2007-06-12 06:35:45 -04001563 }
Chris Mason179e29e2007-11-01 11:28:41 -04001564delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001565 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001566 if (!pending_del_nr) {
1567 /* no pending yet, add ourselves */
1568 pending_del_slot = path->slots[0];
1569 pending_del_nr = 1;
1570 } else if (pending_del_nr &&
1571 path->slots[0] + 1 == pending_del_slot) {
1572 /* hop on the pending chunk */
1573 pending_del_nr++;
1574 pending_del_slot = path->slots[0];
1575 } else {
1576 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1577 }
Chris Mason39279cc2007-06-12 06:35:45 -04001578 } else {
1579 break;
1580 }
Chris Mason39279cc2007-06-12 06:35:45 -04001581 if (found_extent) {
1582 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001583 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04001584 leaf->start, root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001585 root_gen, inode->i_ino,
1586 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001587 BUG_ON(ret);
1588 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001589next:
1590 if (path->slots[0] == 0) {
1591 if (pending_del_nr)
1592 goto del_pending;
1593 btrfs_release_path(root, path);
1594 goto search_again;
1595 }
1596
1597 path->slots[0]--;
1598 if (pending_del_nr &&
1599 path->slots[0] + 1 != pending_del_slot) {
1600 struct btrfs_key debug;
1601del_pending:
1602 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1603 pending_del_slot);
1604 ret = btrfs_del_items(trans, root, path,
1605 pending_del_slot,
1606 pending_del_nr);
1607 BUG_ON(ret);
1608 pending_del_nr = 0;
1609 btrfs_release_path(root, path);
1610 goto search_again;
1611 }
Chris Mason39279cc2007-06-12 06:35:45 -04001612 }
1613 ret = 0;
1614error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001615 if (pending_del_nr) {
1616 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1617 pending_del_nr);
1618 }
Chris Mason39279cc2007-06-12 06:35:45 -04001619 btrfs_free_path(path);
1620 inode->i_sb->s_dirt = 1;
1621 return ret;
1622}
1623
Chris Masona52d9a82007-08-27 16:49:44 -04001624/*
1625 * taken from block_truncate_page, but does cow as it zeros out
1626 * any bytes left in the last page in the file.
1627 */
1628static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1629{
1630 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001631 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001632 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1633 struct btrfs_ordered_extent *ordered;
1634 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001635 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001636 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1637 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1638 struct page *page;
1639 int ret = 0;
1640 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001641 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001642
1643 if ((offset & (blocksize - 1)) == 0)
1644 goto out;
1645
1646 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001647again:
Chris Masona52d9a82007-08-27 16:49:44 -04001648 page = grab_cache_page(mapping, index);
1649 if (!page)
1650 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001651
1652 page_start = page_offset(page);
1653 page_end = page_start + PAGE_CACHE_SIZE - 1;
1654
Chris Masona52d9a82007-08-27 16:49:44 -04001655 if (!PageUptodate(page)) {
1656 ret = btrfs_readpage(NULL, page);
1657 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001658 if (page->mapping != mapping) {
1659 unlock_page(page);
1660 page_cache_release(page);
1661 goto again;
1662 }
Chris Masona52d9a82007-08-27 16:49:44 -04001663 if (!PageUptodate(page)) {
1664 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001665 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001666 }
1667 }
Chris Mason211c17f2008-05-15 09:13:45 -04001668 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001669
1670 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1671 set_page_extent_mapped(page);
1672
1673 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1674 if (ordered) {
1675 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1676 unlock_page(page);
1677 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001678 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001679 btrfs_put_ordered_extent(ordered);
1680 goto again;
1681 }
1682
Chris Masonea8c2812008-08-04 23:17:27 -04001683 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001684 ret = 0;
1685 if (offset != PAGE_CACHE_SIZE) {
1686 kaddr = kmap(page);
1687 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1688 flush_dcache_page(page);
1689 kunmap(page);
1690 }
Chris Mason247e7432008-07-17 12:53:51 -04001691 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001692 set_page_dirty(page);
1693 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001694
Chris Mason89642222008-07-24 09:41:53 -04001695out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001696 unlock_page(page);
1697 page_cache_release(page);
1698out:
1699 return ret;
1700}
1701
1702static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1703{
1704 struct inode *inode = dentry->d_inode;
1705 int err;
1706
1707 err = inode_change_ok(inode, attr);
1708 if (err)
1709 return err;
1710
1711 if (S_ISREG(inode->i_mode) &&
1712 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1713 struct btrfs_trans_handle *trans;
1714 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001715 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001716
Chris Mason5f39d392007-10-15 16:14:19 -04001717 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001718 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001719 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001720 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001721 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001722
Chris Mason1b0f7c22008-01-30 14:33:02 -05001723 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001724 goto out;
1725
Chris Mason1832a6d2007-12-21 16:27:21 -05001726 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001727 if (err)
1728 goto fail;
1729
Chris Mason39279cc2007-06-12 06:35:45 -04001730 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1731
Chris Mason5f564062008-01-22 16:47:59 -05001732 hole_size = block_end - hole_start;
Chris Mason7c2fe322008-08-20 08:51:50 -04001733 while(1) {
1734 struct btrfs_ordered_extent *ordered;
1735 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1736
1737 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1738 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
1739 if (ordered) {
1740 unlock_extent(io_tree, hole_start,
1741 block_end - 1, GFP_NOFS);
1742 btrfs_put_ordered_extent(ordered);
1743 } else {
1744 break;
1745 }
1746 }
Chris Mason39279cc2007-06-12 06:35:45 -04001747
Chris Mason39279cc2007-06-12 06:35:45 -04001748 trans = btrfs_start_transaction(root, 1);
1749 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001750 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001751 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001752 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001753 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001754
Chris Mason179e29e2007-11-01 11:28:41 -04001755 if (alloc_hint != EXTENT_MAP_INLINE) {
1756 err = btrfs_insert_file_extent(trans, root,
1757 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001758 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001759 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001760 btrfs_drop_extent_cache(inode, hole_start,
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001761 (u64)-1, 0);
Chris Mason5f564062008-01-22 16:47:59 -05001762 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001763 }
Chris Masonee6e6502008-07-17 12:54:40 -04001764 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001765 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001766 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001767 if (err)
1768 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001769 }
1770out:
1771 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001772
1773 if (!err && ((attr->ia_valid & ATTR_MODE)))
1774 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001775fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001776 return err;
1777}
Chris Mason61295eb2008-01-14 16:24:38 -05001778
Chris Mason39279cc2007-06-12 06:35:45 -04001779void btrfs_delete_inode(struct inode *inode)
1780{
1781 struct btrfs_trans_handle *trans;
1782 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001783 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001784 int ret;
1785
1786 truncate_inode_pages(&inode->i_data, 0);
1787 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001788 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001789 goto no_delete;
1790 }
Chris Mason4a096752008-07-21 10:29:44 -04001791 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001792
Chris Masondbe674a2008-07-17 12:54:05 -04001793 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001794 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001795
Chris Mason39279cc2007-06-12 06:35:45 -04001796 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001797 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001798 if (ret) {
1799 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001800 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001801 }
1802
1803 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001804
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001805 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001806 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001807
Chris Mason39279cc2007-06-12 06:35:45 -04001808 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001809 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001810 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001811
1812no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001813 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001814 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001815 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001816no_delete:
1817 clear_inode(inode);
1818}
1819
1820/*
1821 * this returns the key found in the dir entry in the location pointer.
1822 * If no dir entries were found, location->objectid is 0.
1823 */
1824static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1825 struct btrfs_key *location)
1826{
1827 const char *name = dentry->d_name.name;
1828 int namelen = dentry->d_name.len;
1829 struct btrfs_dir_item *di;
1830 struct btrfs_path *path;
1831 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001832 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001833
1834 path = btrfs_alloc_path();
1835 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001836
Chris Mason39279cc2007-06-12 06:35:45 -04001837 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1838 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001839 if (IS_ERR(di))
1840 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001841 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001842 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001843 }
Chris Mason5f39d392007-10-15 16:14:19 -04001844 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001845out:
Chris Mason39279cc2007-06-12 06:35:45 -04001846 btrfs_free_path(path);
1847 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001848out_err:
1849 location->objectid = 0;
1850 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001851}
1852
1853/*
1854 * when we hit a tree root in a directory, the btrfs part of the inode
1855 * needs to be changed to reflect the root directory of the tree root. This
1856 * is kind of like crossing a mount point.
1857 */
1858static int fixup_tree_root_location(struct btrfs_root *root,
1859 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001860 struct btrfs_root **sub_root,
1861 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001862{
Chris Mason39279cc2007-06-12 06:35:45 -04001863 struct btrfs_root_item *ri;
1864
1865 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1866 return 0;
1867 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1868 return 0;
1869
Josef Bacik58176a92007-08-29 15:47:34 -04001870 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1871 dentry->d_name.name,
1872 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001873 if (IS_ERR(*sub_root))
1874 return PTR_ERR(*sub_root);
1875
1876 ri = &(*sub_root)->root_item;
1877 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001878 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1879 location->offset = 0;
1880
Chris Mason39279cc2007-06-12 06:35:45 -04001881 return 0;
1882}
1883
Chris Masone02119d2008-09-05 16:13:11 -04001884static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04001885{
Chris Masone02119d2008-09-05 16:13:11 -04001886 struct btrfs_inode *bi = BTRFS_I(inode);
1887
1888 bi->i_acl = NULL;
1889 bi->i_default_acl = NULL;
1890
1891 bi->generation = 0;
1892 bi->last_trans = 0;
1893 bi->logged_trans = 0;
1894 bi->delalloc_bytes = 0;
1895 bi->disk_i_size = 0;
1896 bi->flags = 0;
1897 bi->index_cnt = (u64)-1;
Chris Mason49eb7e42008-09-11 15:53:12 -04001898 bi->log_dirty_trans = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001899 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1900 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001901 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001902 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1903 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001904 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04001905 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001906 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001907 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04001908 mutex_init(&BTRFS_I(inode)->log_mutex);
1909}
1910
1911static int btrfs_init_locked_inode(struct inode *inode, void *p)
1912{
1913 struct btrfs_iget_args *args = p;
1914 inode->i_ino = args->ino;
1915 init_btrfs_i(inode);
1916 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001917 return 0;
1918}
1919
1920static int btrfs_find_actor(struct inode *inode, void *opaque)
1921{
1922 struct btrfs_iget_args *args = opaque;
1923 return (args->ino == inode->i_ino &&
1924 args->root == BTRFS_I(inode)->root);
1925}
1926
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001927struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1928 struct btrfs_root *root, int wait)
1929{
1930 struct inode *inode;
1931 struct btrfs_iget_args args;
1932 args.ino = objectid;
1933 args.root = root;
1934
1935 if (wait) {
1936 inode = ilookup5(s, objectid, btrfs_find_actor,
1937 (void *)&args);
1938 } else {
1939 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
1940 (void *)&args);
1941 }
1942 return inode;
1943}
1944
Chris Mason39279cc2007-06-12 06:35:45 -04001945struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1946 struct btrfs_root *root)
1947{
1948 struct inode *inode;
1949 struct btrfs_iget_args args;
1950 args.ino = objectid;
1951 args.root = root;
1952
1953 inode = iget5_locked(s, objectid, btrfs_find_actor,
1954 btrfs_init_locked_inode,
1955 (void *)&args);
1956 return inode;
1957}
1958
Balaji Rao1a54ef82008-07-21 02:01:04 +05301959/* Get an inode object given its location and corresponding root.
1960 * Returns in *is_new if the inode was read from disk
1961 */
1962struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
1963 struct btrfs_root *root, int *is_new)
1964{
1965 struct inode *inode;
1966
1967 inode = btrfs_iget_locked(s, location->objectid, root);
1968 if (!inode)
1969 return ERR_PTR(-EACCES);
1970
1971 if (inode->i_state & I_NEW) {
1972 BTRFS_I(inode)->root = root;
1973 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
1974 btrfs_read_locked_inode(inode);
1975 unlock_new_inode(inode);
1976 if (is_new)
1977 *is_new = 1;
1978 } else {
1979 if (is_new)
1980 *is_new = 0;
1981 }
1982
1983 return inode;
1984}
1985
Chris Mason39279cc2007-06-12 06:35:45 -04001986static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1987 struct nameidata *nd)
1988{
1989 struct inode * inode;
1990 struct btrfs_inode *bi = BTRFS_I(dir);
1991 struct btrfs_root *root = bi->root;
1992 struct btrfs_root *sub_root = root;
1993 struct btrfs_key location;
Balaji Rao1a54ef82008-07-21 02:01:04 +05301994 int ret, new, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001995
1996 if (dentry->d_name.len > BTRFS_NAME_LEN)
1997 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001998
Chris Mason39279cc2007-06-12 06:35:45 -04001999 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002000
Chris Mason39279cc2007-06-12 06:35:45 -04002001 if (ret < 0)
2002 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002003
Chris Mason39279cc2007-06-12 06:35:45 -04002004 inode = NULL;
2005 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04002006 ret = fixup_tree_root_location(root, &location, &sub_root,
2007 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04002008 if (ret < 0)
2009 return ERR_PTR(ret);
2010 if (ret > 0)
2011 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05302012 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
2013 if (IS_ERR(inode))
2014 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002015
Balaji Rao1a54ef82008-07-21 02:01:04 +05302016 /* the inode and parent dir are two different roots */
2017 if (new && root != sub_root) {
2018 igrab(inode);
2019 sub_root->inode = inode;
2020 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002021 }
2022 }
Josef Bacik7b128762008-07-24 12:17:14 -04002023
2024 if (unlikely(do_orphan))
2025 btrfs_orphan_cleanup(sub_root);
2026
Chris Mason39279cc2007-06-12 06:35:45 -04002027 return d_splice_alias(inode, dentry);
2028}
2029
Chris Mason39279cc2007-06-12 06:35:45 -04002030static unsigned char btrfs_filetype_table[] = {
2031 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
2032};
2033
David Woodhousecbdf5a22008-08-06 19:42:33 +01002034static int btrfs_real_readdir(struct file *filp, void *dirent,
2035 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04002036{
Chris Mason6da6aba2007-12-18 16:15:09 -05002037 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002038 struct btrfs_root *root = BTRFS_I(inode)->root;
2039 struct btrfs_item *item;
2040 struct btrfs_dir_item *di;
2041 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002042 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002043 struct btrfs_path *path;
2044 int ret;
2045 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002046 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002047 int slot;
2048 int advance;
2049 unsigned char d_type;
2050 int over = 0;
2051 u32 di_cur;
2052 u32 di_total;
2053 u32 di_len;
2054 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04002055 char tmp_name[32];
2056 char *name_ptr;
2057 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04002058
2059 /* FIXME, use a real flag for deciding about the key type */
2060 if (root->fs_info->tree_root == root)
2061 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04002062
Chris Mason39544012007-12-12 14:38:19 -05002063 /* special case for "." */
2064 if (filp->f_pos == 0) {
2065 over = filldir(dirent, ".", 1,
2066 1, inode->i_ino,
2067 DT_DIR);
2068 if (over)
2069 return 0;
2070 filp->f_pos = 1;
2071 }
Chris Mason39544012007-12-12 14:38:19 -05002072 /* special case for .., just use the back ref */
2073 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01002074 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05002075 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01002076 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05002077 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01002078 return 0;
Chris Mason39544012007-12-12 14:38:19 -05002079 filp->f_pos = 2;
2080 }
2081
David Woodhouse49593bf2008-08-17 17:08:36 +01002082 path = btrfs_alloc_path();
2083 path->reada = 2;
2084
Chris Mason39279cc2007-06-12 06:35:45 -04002085 btrfs_set_key_type(&key, key_type);
2086 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002087 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04002088
Chris Mason39279cc2007-06-12 06:35:45 -04002089 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2090 if (ret < 0)
2091 goto err;
2092 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01002093
2094 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002095 leaf = path->nodes[0];
2096 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002097 slot = path->slots[0];
2098 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01002099 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002100 ret = btrfs_next_leaf(root, path);
2101 if (ret)
2102 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002103 leaf = path->nodes[0];
2104 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002105 slot = path->slots[0];
2106 } else {
2107 slot++;
2108 path->slots[0]++;
2109 }
2110 }
2111 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002112 item = btrfs_item_nr(leaf, slot);
2113 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2114
2115 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04002116 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002117 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002118 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002119 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04002120 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002121
2122 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01002123
Chris Mason39279cc2007-06-12 06:35:45 -04002124 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2125 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002126 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01002127
2128 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04002129 struct btrfs_key location;
2130
2131 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01002132 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002133 name_ptr = tmp_name;
2134 } else {
2135 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01002136 if (!name_ptr) {
2137 ret = -ENOMEM;
2138 goto err;
2139 }
Chris Mason5f39d392007-10-15 16:14:19 -04002140 }
2141 read_extent_buffer(leaf, name_ptr,
2142 (unsigned long)(di + 1), name_len);
2143
2144 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2145 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002146 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01002147 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002148 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002149
2150 if (name_ptr != tmp_name)
2151 kfree(name_ptr);
2152
Chris Mason39279cc2007-06-12 06:35:45 -04002153 if (over)
2154 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002155
Josef Bacik5103e942007-11-16 11:45:54 -05002156 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01002157 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002158 di_cur += di_len;
2159 di = (struct btrfs_dir_item *)((char *)di + di_len);
2160 }
2161 }
David Woodhouse49593bf2008-08-17 17:08:36 +01002162
2163 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05002164 if (key_type == BTRFS_DIR_INDEX_KEY)
2165 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2166 else
2167 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002168nopos:
2169 ret = 0;
2170err:
Chris Mason39279cc2007-06-12 06:35:45 -04002171 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002172 return ret;
2173}
2174
2175int btrfs_write_inode(struct inode *inode, int wait)
2176{
2177 struct btrfs_root *root = BTRFS_I(inode)->root;
2178 struct btrfs_trans_handle *trans;
2179 int ret = 0;
2180
Chris Mason4ca8b412008-08-05 13:30:48 -04002181 if (root->fs_info->closing > 1)
2182 return 0;
2183
Chris Mason39279cc2007-06-12 06:35:45 -04002184 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002185 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002186 btrfs_set_trans_block_group(trans, inode);
2187 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002188 }
2189 return ret;
2190}
2191
2192/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002193 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002194 * inode changes. But, it is most likely to find the inode in cache.
2195 * FIXME, needs more benchmarking...there are no reasons other than performance
2196 * to keep or drop this code.
2197 */
2198void btrfs_dirty_inode(struct inode *inode)
2199{
2200 struct btrfs_root *root = BTRFS_I(inode)->root;
2201 struct btrfs_trans_handle *trans;
2202
Chris Masonf9295742008-07-17 12:54:14 -04002203 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002204 btrfs_set_trans_block_group(trans, inode);
2205 btrfs_update_inode(trans, root, inode);
2206 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002207}
2208
Chris Masond352ac62008-09-29 15:18:18 -04002209/*
2210 * find the highest existing sequence number in a directory
2211 * and then set the in-memory index_cnt variable to reflect
2212 * free sequence numbers
2213 */
Josef Bacikaec74772008-07-24 12:12:38 -04002214static int btrfs_set_inode_index_count(struct inode *inode)
2215{
2216 struct btrfs_root *root = BTRFS_I(inode)->root;
2217 struct btrfs_key key, found_key;
2218 struct btrfs_path *path;
2219 struct extent_buffer *leaf;
2220 int ret;
2221
2222 key.objectid = inode->i_ino;
2223 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2224 key.offset = (u64)-1;
2225
2226 path = btrfs_alloc_path();
2227 if (!path)
2228 return -ENOMEM;
2229
2230 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2231 if (ret < 0)
2232 goto out;
2233 /* FIXME: we should be able to handle this */
2234 if (ret == 0)
2235 goto out;
2236 ret = 0;
2237
2238 /*
2239 * MAGIC NUMBER EXPLANATION:
2240 * since we search a directory based on f_pos we have to start at 2
2241 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2242 * else has to start at 2
2243 */
2244 if (path->slots[0] == 0) {
2245 BTRFS_I(inode)->index_cnt = 2;
2246 goto out;
2247 }
2248
2249 path->slots[0]--;
2250
2251 leaf = path->nodes[0];
2252 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2253
2254 if (found_key.objectid != inode->i_ino ||
2255 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2256 BTRFS_I(inode)->index_cnt = 2;
2257 goto out;
2258 }
2259
2260 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2261out:
2262 btrfs_free_path(path);
2263 return ret;
2264}
2265
Chris Masond352ac62008-09-29 15:18:18 -04002266/*
2267 * helper to find a free sequence number in a given directory. This current
2268 * code is very simple, later versions will do smarter things in the btree
2269 */
Chris Mason00e4e6b2008-08-05 11:18:09 -04002270static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2271 u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04002272{
2273 int ret = 0;
2274
2275 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2276 ret = btrfs_set_inode_index_count(dir);
Chris Mason8d5bf1c2008-09-11 15:51:21 -04002277 if (ret) {
Josef Bacikaec74772008-07-24 12:12:38 -04002278 return ret;
Chris Mason8d5bf1c2008-09-11 15:51:21 -04002279 }
Josef Bacikaec74772008-07-24 12:12:38 -04002280 }
2281
Chris Mason00e4e6b2008-08-05 11:18:09 -04002282 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04002283 BTRFS_I(dir)->index_cnt++;
2284
2285 return ret;
2286}
2287
Chris Mason39279cc2007-06-12 06:35:45 -04002288static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2289 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002290 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002291 const char *name, int name_len,
2292 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002293 u64 objectid,
2294 struct btrfs_block_group_cache *group,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002295 int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04002296{
2297 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002298 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002299 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002300 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002301 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002302 struct btrfs_inode_ref *ref;
2303 struct btrfs_key key[2];
2304 u32 sizes[2];
2305 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002306 int ret;
2307 int owner;
2308
Chris Mason5f39d392007-10-15 16:14:19 -04002309 path = btrfs_alloc_path();
2310 BUG_ON(!path);
2311
Chris Mason39279cc2007-06-12 06:35:45 -04002312 inode = new_inode(root->fs_info->sb);
2313 if (!inode)
2314 return ERR_PTR(-ENOMEM);
2315
Josef Bacikaec74772008-07-24 12:12:38 -04002316 if (dir) {
Chris Mason00e4e6b2008-08-05 11:18:09 -04002317 ret = btrfs_set_inode_index(dir, inode, index);
Josef Bacikaec74772008-07-24 12:12:38 -04002318 if (ret)
2319 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002320 }
2321 /*
2322 * index_cnt is ignored for everything but a dir,
2323 * btrfs_get_inode_index_count has an explanation for the magic
2324 * number
2325 */
Chris Masone02119d2008-09-05 16:13:11 -04002326 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04002327 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04002328 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04002329 BTRFS_I(inode)->generation = trans->transid;
Chris Masonb888db22007-08-27 16:49:44 -04002330
Chris Mason39279cc2007-06-12 06:35:45 -04002331 if (mode & S_IFDIR)
2332 owner = 0;
2333 else
2334 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002335 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002336 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002337 if (!new_inode_group) {
2338 printk("find_block group failed\n");
2339 new_inode_group = group;
2340 }
2341 BTRFS_I(inode)->block_group = new_inode_group;
Chris Mason9c583092008-01-29 15:15:18 -05002342
2343 key[0].objectid = objectid;
2344 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2345 key[0].offset = 0;
2346
2347 key[1].objectid = objectid;
2348 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2349 key[1].offset = ref_objectid;
2350
2351 sizes[0] = sizeof(struct btrfs_inode_item);
2352 sizes[1] = name_len + sizeof(*ref);
2353
2354 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2355 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002356 goto fail;
2357
Chris Mason9c583092008-01-29 15:15:18 -05002358 if (objectid > root->highest_inode)
2359 root->highest_inode = objectid;
2360
Chris Mason39279cc2007-06-12 06:35:45 -04002361 inode->i_uid = current->fsuid;
2362 inode->i_gid = current->fsgid;
2363 inode->i_mode = mode;
2364 inode->i_ino = objectid;
2365 inode->i_blocks = 0;
2366 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002367 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2368 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002369 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002370
2371 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2372 struct btrfs_inode_ref);
2373 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002374 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05002375 ptr = (unsigned long)(ref + 1);
2376 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2377
Chris Mason5f39d392007-10-15 16:14:19 -04002378 btrfs_mark_buffer_dirty(path->nodes[0]);
2379 btrfs_free_path(path);
2380
Chris Mason39279cc2007-06-12 06:35:45 -04002381 location = &BTRFS_I(inode)->location;
2382 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002383 location->offset = 0;
2384 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2385
Chris Mason39279cc2007-06-12 06:35:45 -04002386 insert_inode_hash(inode);
2387 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002388fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002389 if (dir)
2390 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002391 btrfs_free_path(path);
2392 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002393}
2394
2395static inline u8 btrfs_inode_type(struct inode *inode)
2396{
2397 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2398}
2399
Chris Masond352ac62008-09-29 15:18:18 -04002400/*
2401 * utility function to add 'inode' into 'parent_inode' with
2402 * a give name and a given sequence number.
2403 * if 'add_backref' is true, also insert a backref from the
2404 * inode to the parent directory.
2405 */
Chris Masone02119d2008-09-05 16:13:11 -04002406int btrfs_add_link(struct btrfs_trans_handle *trans,
2407 struct inode *parent_inode, struct inode *inode,
2408 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002409{
2410 int ret;
2411 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002412 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04002413
Chris Mason39279cc2007-06-12 06:35:45 -04002414 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002415 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2416 key.offset = 0;
2417
Chris Masone02119d2008-09-05 16:13:11 -04002418 ret = btrfs_insert_dir_item(trans, root, name, name_len,
2419 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002420 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04002421 index);
Chris Mason39279cc2007-06-12 06:35:45 -04002422 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002423 if (add_backref) {
2424 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04002425 name, name_len,
2426 inode->i_ino,
2427 parent_inode->i_ino,
2428 index);
Chris Mason9c583092008-01-29 15:15:18 -05002429 }
Chris Masondbe674a2008-07-17 12:54:05 -04002430 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04002431 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002432 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04002433 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002434 }
2435 return ret;
2436}
2437
2438static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002439 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002440 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002441{
Chris Masone02119d2008-09-05 16:13:11 -04002442 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2443 inode, dentry->d_name.name,
2444 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002445 if (!err) {
2446 d_instantiate(dentry, inode);
2447 return 0;
2448 }
2449 if (err > 0)
2450 err = -EEXIST;
2451 return err;
2452}
2453
Josef Bacik618e21d2007-07-11 10:18:17 -04002454static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2455 int mode, dev_t rdev)
2456{
2457 struct btrfs_trans_handle *trans;
2458 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002459 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002460 int err;
2461 int drop_inode = 0;
2462 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002463 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002464 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002465
2466 if (!new_valid_dev(rdev))
2467 return -EINVAL;
2468
Chris Mason1832a6d2007-12-21 16:27:21 -05002469 err = btrfs_check_free_space(root, 1, 0);
2470 if (err)
2471 goto fail;
2472
Josef Bacik618e21d2007-07-11 10:18:17 -04002473 trans = btrfs_start_transaction(root, 1);
2474 btrfs_set_trans_block_group(trans, dir);
2475
2476 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2477 if (err) {
2478 err = -ENOSPC;
2479 goto out_unlock;
2480 }
2481
Josef Bacikaec74772008-07-24 12:12:38 -04002482 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002483 dentry->d_name.len,
2484 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002485 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002486 err = PTR_ERR(inode);
2487 if (IS_ERR(inode))
2488 goto out_unlock;
2489
Josef Bacik33268ea2008-07-24 12:16:36 -04002490 err = btrfs_init_acl(inode, dir);
2491 if (err) {
2492 drop_inode = 1;
2493 goto out_unlock;
2494 }
2495
Josef Bacik618e21d2007-07-11 10:18:17 -04002496 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002497 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002498 if (err)
2499 drop_inode = 1;
2500 else {
2501 inode->i_op = &btrfs_special_inode_operations;
2502 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002503 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002504 }
2505 dir->i_sb->s_dirt = 1;
2506 btrfs_update_inode_block_group(trans, inode);
2507 btrfs_update_inode_block_group(trans, dir);
2508out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002509 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002510 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002511fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002512 if (drop_inode) {
2513 inode_dec_link_count(inode);
2514 iput(inode);
2515 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002516 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002517 return err;
2518}
2519
Chris Mason39279cc2007-06-12 06:35:45 -04002520static int btrfs_create(struct inode *dir, struct dentry *dentry,
2521 int mode, struct nameidata *nd)
2522{
2523 struct btrfs_trans_handle *trans;
2524 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002525 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002526 int err;
2527 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002528 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002529 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002530 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002531
Chris Mason1832a6d2007-12-21 16:27:21 -05002532 err = btrfs_check_free_space(root, 1, 0);
2533 if (err)
2534 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002535 trans = btrfs_start_transaction(root, 1);
2536 btrfs_set_trans_block_group(trans, dir);
2537
2538 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2539 if (err) {
2540 err = -ENOSPC;
2541 goto out_unlock;
2542 }
2543
Josef Bacikaec74772008-07-24 12:12:38 -04002544 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002545 dentry->d_name.len,
2546 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002547 objectid, BTRFS_I(dir)->block_group, mode,
2548 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002549 err = PTR_ERR(inode);
2550 if (IS_ERR(inode))
2551 goto out_unlock;
2552
Josef Bacik33268ea2008-07-24 12:16:36 -04002553 err = btrfs_init_acl(inode, dir);
2554 if (err) {
2555 drop_inode = 1;
2556 goto out_unlock;
2557 }
2558
Chris Mason39279cc2007-06-12 06:35:45 -04002559 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002560 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002561 if (err)
2562 drop_inode = 1;
2563 else {
2564 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002565 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002566 inode->i_fop = &btrfs_file_operations;
2567 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002568 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002569 }
2570 dir->i_sb->s_dirt = 1;
2571 btrfs_update_inode_block_group(trans, inode);
2572 btrfs_update_inode_block_group(trans, dir);
2573out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002574 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002575 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002576fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002577 if (drop_inode) {
2578 inode_dec_link_count(inode);
2579 iput(inode);
2580 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002581 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002582 return err;
2583}
2584
2585static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2586 struct dentry *dentry)
2587{
2588 struct btrfs_trans_handle *trans;
2589 struct btrfs_root *root = BTRFS_I(dir)->root;
2590 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002591 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05002592 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002593 int err;
2594 int drop_inode = 0;
2595
2596 if (inode->i_nlink == 0)
2597 return -ENOENT;
2598
Chris Masone02119d2008-09-05 16:13:11 -04002599 btrfs_inc_nlink(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05002600 err = btrfs_check_free_space(root, 1, 0);
2601 if (err)
2602 goto fail;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002603 err = btrfs_set_inode_index(dir, inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002604 if (err)
2605 goto fail;
2606
Chris Mason39279cc2007-06-12 06:35:45 -04002607 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002608
Chris Mason39279cc2007-06-12 06:35:45 -04002609 btrfs_set_trans_block_group(trans, dir);
2610 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002611
Chris Mason00e4e6b2008-08-05 11:18:09 -04002612 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04002613
Chris Mason39279cc2007-06-12 06:35:45 -04002614 if (err)
2615 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002616
Chris Mason39279cc2007-06-12 06:35:45 -04002617 dir->i_sb->s_dirt = 1;
2618 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002619 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002620
Chris Mason54aa1f42007-06-22 14:16:25 -04002621 if (err)
2622 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002623
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002624 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002625 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002626fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002627 if (drop_inode) {
2628 inode_dec_link_count(inode);
2629 iput(inode);
2630 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002631 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002632 return err;
2633}
2634
Chris Mason39279cc2007-06-12 06:35:45 -04002635static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2636{
Chris Masonb9d86662008-05-02 16:13:49 -04002637 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002638 struct btrfs_trans_handle *trans;
2639 struct btrfs_root *root = BTRFS_I(dir)->root;
2640 int err = 0;
2641 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002642 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002643 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002644 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002645
Chris Mason1832a6d2007-12-21 16:27:21 -05002646 err = btrfs_check_free_space(root, 1, 0);
2647 if (err)
2648 goto out_unlock;
2649
Chris Mason39279cc2007-06-12 06:35:45 -04002650 trans = btrfs_start_transaction(root, 1);
2651 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002652
Chris Mason39279cc2007-06-12 06:35:45 -04002653 if (IS_ERR(trans)) {
2654 err = PTR_ERR(trans);
2655 goto out_unlock;
2656 }
2657
2658 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2659 if (err) {
2660 err = -ENOSPC;
2661 goto out_unlock;
2662 }
2663
Josef Bacikaec74772008-07-24 12:12:38 -04002664 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002665 dentry->d_name.len,
2666 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002667 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2668 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002669 if (IS_ERR(inode)) {
2670 err = PTR_ERR(inode);
2671 goto out_fail;
2672 }
Chris Mason5f39d392007-10-15 16:14:19 -04002673
Chris Mason39279cc2007-06-12 06:35:45 -04002674 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002675
2676 err = btrfs_init_acl(inode, dir);
2677 if (err)
2678 goto out_fail;
2679
Chris Mason39279cc2007-06-12 06:35:45 -04002680 inode->i_op = &btrfs_dir_inode_operations;
2681 inode->i_fop = &btrfs_dir_file_operations;
2682 btrfs_set_trans_block_group(trans, inode);
2683
Chris Masondbe674a2008-07-17 12:54:05 -04002684 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002685 err = btrfs_update_inode(trans, root, inode);
2686 if (err)
2687 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002688
Chris Masone02119d2008-09-05 16:13:11 -04002689 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2690 inode, dentry->d_name.name,
2691 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002692 if (err)
2693 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002694
Chris Mason39279cc2007-06-12 06:35:45 -04002695 d_instantiate(dentry, inode);
2696 drop_on_err = 0;
2697 dir->i_sb->s_dirt = 1;
2698 btrfs_update_inode_block_group(trans, inode);
2699 btrfs_update_inode_block_group(trans, dir);
2700
2701out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002702 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002703 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002704
Chris Mason39279cc2007-06-12 06:35:45 -04002705out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002706 if (drop_on_err)
2707 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002708 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002709 return err;
2710}
2711
Chris Masond352ac62008-09-29 15:18:18 -04002712/* helper for btfs_get_extent. Given an existing extent in the tree,
2713 * and an extent that you want to insert, deal with overlap and insert
2714 * the new extent into the tree.
2715 */
Chris Mason3b951512008-04-17 11:29:12 -04002716static int merge_extent_mapping(struct extent_map_tree *em_tree,
2717 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002718 struct extent_map *em,
2719 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002720{
2721 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002722
Chris Masone6dcd2d2008-07-17 12:53:50 -04002723 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2724 start_diff = map_start - em->start;
2725 em->start = map_start;
2726 em->len = map_len;
2727 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2728 em->block_start += start_diff;
2729 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002730}
2731
Chris Masond352ac62008-09-29 15:18:18 -04002732/*
2733 * a bit scary, this does extent mapping from logical file offset to the disk.
2734 * the ugly parts come from merging extents from the disk with the
2735 * in-ram representation. This gets more complex because of the data=ordered code,
2736 * where the in-ram extents might be locked pending data=ordered completion.
2737 *
2738 * This also copies inline extents directly into the page.
2739 */
Chris Masona52d9a82007-08-27 16:49:44 -04002740struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002741 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002742 int create)
2743{
2744 int ret;
2745 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002746 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002747 u64 extent_start = 0;
2748 u64 extent_end = 0;
2749 u64 objectid = inode->i_ino;
2750 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002751 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002752 struct btrfs_root *root = BTRFS_I(inode)->root;
2753 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002754 struct extent_buffer *leaf;
2755 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002756 struct extent_map *em = NULL;
2757 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002758 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002759 struct btrfs_trans_handle *trans = NULL;
2760
Chris Masona52d9a82007-08-27 16:49:44 -04002761again:
Chris Masond1310b22008-01-24 16:13:08 -05002762 spin_lock(&em_tree->lock);
2763 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002764 if (em)
2765 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002766 spin_unlock(&em_tree->lock);
2767
Chris Masona52d9a82007-08-27 16:49:44 -04002768 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002769 if (em->start > start || em->start + em->len <= start)
2770 free_extent_map(em);
2771 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002772 free_extent_map(em);
2773 else
2774 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002775 }
Chris Masond1310b22008-01-24 16:13:08 -05002776 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002777 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002778 err = -ENOMEM;
2779 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002780 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002781 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002782 em->start = EXTENT_MAP_HOLE;
2783 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002784
2785 if (!path) {
2786 path = btrfs_alloc_path();
2787 BUG_ON(!path);
2788 }
2789
Chris Mason179e29e2007-11-01 11:28:41 -04002790 ret = btrfs_lookup_file_extent(trans, root, path,
2791 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002792 if (ret < 0) {
2793 err = ret;
2794 goto out;
2795 }
2796
2797 if (ret != 0) {
2798 if (path->slots[0] == 0)
2799 goto not_found;
2800 path->slots[0]--;
2801 }
2802
Chris Mason5f39d392007-10-15 16:14:19 -04002803 leaf = path->nodes[0];
2804 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002805 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002806 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002807 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2808 found_type = btrfs_key_type(&found_key);
2809 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002810 found_type != BTRFS_EXTENT_DATA_KEY) {
2811 goto not_found;
2812 }
2813
Chris Mason5f39d392007-10-15 16:14:19 -04002814 found_type = btrfs_file_extent_type(leaf, item);
2815 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002816 if (found_type == BTRFS_FILE_EXTENT_REG) {
2817 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002818 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002819 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002820 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002821 em->start = start;
2822 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002823 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002824 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002825 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002826 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002827 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002828 }
2829 goto not_found_em;
2830 }
Chris Masondb945352007-10-15 16:15:53 -04002831 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2832 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002833 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002834 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002835 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002836 goto insert;
2837 }
Chris Masondb945352007-10-15 16:15:53 -04002838 bytenr += btrfs_file_extent_offset(leaf, item);
2839 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002840 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002841 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002842 goto insert;
2843 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002844 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002845 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002846 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002847 size_t size;
2848 size_t extent_offset;
2849 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002850
Chris Mason5f39d392007-10-15 16:14:19 -04002851 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2852 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002853 extent_end = (extent_start + size + root->sectorsize - 1) &
2854 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002855 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002856 em->start = start;
2857 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002858 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002859 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002860 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002861 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002862 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002863 }
2864 goto not_found_em;
2865 }
2866 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002867
2868 if (!page) {
2869 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002870 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002871 goto out;
2872 }
2873
Chris Mason70dec802008-01-29 09:59:12 -05002874 page_start = page_offset(page) + pg_offset;
2875 extent_offset = page_start - extent_start;
2876 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002877 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002878 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002879 em->len = (copy_size + root->sectorsize - 1) &
2880 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002881 map = kmap(page);
2882 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002883 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002884 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002885 copy_size);
2886 flush_dcache_page(page);
2887 } else if (create && PageUptodate(page)) {
2888 if (!trans) {
2889 kunmap(page);
2890 free_extent_map(em);
2891 em = NULL;
2892 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002893 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002894 goto again;
2895 }
Chris Mason70dec802008-01-29 09:59:12 -05002896 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002897 copy_size);
2898 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002899 }
Chris Masona52d9a82007-08-27 16:49:44 -04002900 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002901 set_extent_uptodate(io_tree, em->start,
2902 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002903 goto insert;
2904 } else {
2905 printk("unkknown found_type %d\n", found_type);
2906 WARN_ON(1);
2907 }
2908not_found:
2909 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002910 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002911not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002912 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002913insert:
2914 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002915 if (em->start > start || extent_map_end(em) <= start) {
2916 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002917 err = -EIO;
2918 goto out;
2919 }
Chris Masond1310b22008-01-24 16:13:08 -05002920
2921 err = 0;
2922 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002923 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002924 /* it is possible that someone inserted the extent into the tree
2925 * while we had the lock dropped. It is also possible that
2926 * an overlapping map exists in the tree
2927 */
Chris Masona52d9a82007-08-27 16:49:44 -04002928 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002929 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002930
2931 ret = 0;
2932
Chris Mason3b951512008-04-17 11:29:12 -04002933 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002934 if (existing && (existing->start > start ||
2935 existing->start + existing->len <= start)) {
2936 free_extent_map(existing);
2937 existing = NULL;
2938 }
Chris Mason3b951512008-04-17 11:29:12 -04002939 if (!existing) {
2940 existing = lookup_extent_mapping(em_tree, em->start,
2941 em->len);
2942 if (existing) {
2943 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002944 em, start,
2945 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002946 free_extent_map(existing);
2947 if (err) {
2948 free_extent_map(em);
2949 em = NULL;
2950 }
2951 } else {
2952 err = -EIO;
2953 printk("failing to insert %Lu %Lu\n",
2954 start, len);
2955 free_extent_map(em);
2956 em = NULL;
2957 }
2958 } else {
2959 free_extent_map(em);
2960 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002961 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002962 }
Chris Masona52d9a82007-08-27 16:49:44 -04002963 }
Chris Masond1310b22008-01-24 16:13:08 -05002964 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002965out:
Chris Masonf4219502008-07-22 11:18:09 -04002966 if (path)
2967 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002968 if (trans) {
2969 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002970 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002971 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002972 }
Chris Masona52d9a82007-08-27 16:49:44 -04002973 }
Chris Masona52d9a82007-08-27 16:49:44 -04002974 if (err) {
2975 free_extent_map(em);
2976 WARN_ON(1);
2977 return ERR_PTR(err);
2978 }
2979 return em;
2980}
2981
Chris Mason16432982008-04-10 10:23:21 -04002982static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2983 const struct iovec *iov, loff_t offset,
2984 unsigned long nr_segs)
2985{
Chris Masone1c4b742008-04-22 13:26:46 -04002986 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04002987}
2988
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002989static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002990{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002991 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002992}
2993
Chris Mason9ebefb182007-06-15 13:50:00 -04002994int btrfs_readpage(struct file *file, struct page *page)
2995{
Chris Masond1310b22008-01-24 16:13:08 -05002996 struct extent_io_tree *tree;
2997 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002998 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002999}
Chris Mason1832a6d2007-12-21 16:27:21 -05003000
Chris Mason39279cc2007-06-12 06:35:45 -04003001static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3002{
Chris Masond1310b22008-01-24 16:13:08 -05003003 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04003004
3005
3006 if (current->flags & PF_MEMALLOC) {
3007 redirty_page_for_writepage(wbc, page);
3008 unlock_page(page);
3009 return 0;
3010 }
Chris Masond1310b22008-01-24 16:13:08 -05003011 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003012 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3013}
Chris Mason39279cc2007-06-12 06:35:45 -04003014
Chris Masonf4219502008-07-22 11:18:09 -04003015int btrfs_writepages(struct address_space *mapping,
3016 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04003017{
Chris Masond1310b22008-01-24 16:13:08 -05003018 struct extent_io_tree *tree;
3019 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04003020 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3021}
3022
Chris Mason3ab2fb52007-11-08 10:59:22 -05003023static int
3024btrfs_readpages(struct file *file, struct address_space *mapping,
3025 struct list_head *pages, unsigned nr_pages)
3026{
Chris Masond1310b22008-01-24 16:13:08 -05003027 struct extent_io_tree *tree;
3028 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05003029 return extent_readpages(tree, mapping, pages, nr_pages,
3030 btrfs_get_extent);
3031}
Chris Masone6dcd2d2008-07-17 12:53:50 -04003032static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04003033{
Chris Masond1310b22008-01-24 16:13:08 -05003034 struct extent_io_tree *tree;
3035 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04003036 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003037
Chris Masond1310b22008-01-24 16:13:08 -05003038 tree = &BTRFS_I(page->mapping->host)->io_tree;
3039 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05003040 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003041 if (ret == 1) {
3042 ClearPagePrivate(page);
3043 set_page_private(page, 0);
3044 page_cache_release(page);
3045 }
3046 return ret;
3047}
Chris Mason39279cc2007-06-12 06:35:45 -04003048
Chris Masone6dcd2d2008-07-17 12:53:50 -04003049static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3050{
Chris Mason98509cf2008-09-11 15:51:43 -04003051 if (PageWriteback(page) || PageDirty(page))
3052 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003053 return __btrfs_releasepage(page, gfp_flags);
3054}
3055
Chris Masona52d9a82007-08-27 16:49:44 -04003056static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3057{
Chris Masond1310b22008-01-24 16:13:08 -05003058 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003059 struct btrfs_ordered_extent *ordered;
3060 u64 page_start = page_offset(page);
3061 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003062
Chris Masone6dcd2d2008-07-17 12:53:50 -04003063 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003064 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003065 if (offset) {
3066 btrfs_releasepage(page, GFP_NOFS);
3067 return;
3068 }
3069
3070 lock_extent(tree, page_start, page_end, GFP_NOFS);
3071 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3072 page_offset(page));
3073 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003074 /*
3075 * IO on this page will never be started, so we need
3076 * to account for any ordered extents now
3077 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003078 clear_extent_bit(tree, page_start, page_end,
3079 EXTENT_DIRTY | EXTENT_DELALLOC |
3080 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003081 btrfs_finish_ordered_io(page->mapping->host,
3082 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003083 btrfs_put_ordered_extent(ordered);
3084 lock_extent(tree, page_start, page_end, GFP_NOFS);
3085 }
3086 clear_extent_bit(tree, page_start, page_end,
3087 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3088 EXTENT_ORDERED,
3089 1, 1, GFP_NOFS);
3090 __btrfs_releasepage(page, GFP_NOFS);
3091
Chris Mason4a096752008-07-21 10:29:44 -04003092 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003093 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003094 ClearPagePrivate(page);
3095 set_page_private(page, 0);
3096 page_cache_release(page);
3097 }
Chris Mason39279cc2007-06-12 06:35:45 -04003098}
3099
Chris Mason9ebefb182007-06-15 13:50:00 -04003100/*
3101 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3102 * called from a page fault handler when a page is first dirtied. Hence we must
3103 * be careful to check for EOF conditions here. We set the page up correctly
3104 * for a written page which means we get ENOSPC checking when writing into
3105 * holes and correct delalloc and unwritten extent mapping on filesystems that
3106 * support these features.
3107 *
3108 * We are not allowed to take the i_mutex here so we have to play games to
3109 * protect against truncate races as the page could now be beyond EOF. Because
3110 * vmtruncate() writes the inode size before removing pages, once we have the
3111 * page lock we can determine safely if the page is beyond EOF. If it is not
3112 * beyond EOF, then the page is guaranteed safe against truncation until we
3113 * unlock the page.
3114 */
3115int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3116{
Chris Mason6da6aba2007-12-18 16:15:09 -05003117 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003118 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003119 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3120 struct btrfs_ordered_extent *ordered;
3121 char *kaddr;
3122 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003123 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003124 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003125 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003126 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003127
Chris Mason1832a6d2007-12-21 16:27:21 -05003128 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003129 if (ret)
3130 goto out;
3131
3132 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003133again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003134 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003135 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003136 page_start = page_offset(page);
3137 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003138
Chris Mason9ebefb182007-06-15 13:50:00 -04003139 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003140 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003141 /* page got truncated out from underneath us */
3142 goto out_unlock;
3143 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003144 wait_on_page_writeback(page);
3145
3146 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3147 set_page_extent_mapped(page);
3148
Chris Masoneb84ae02008-07-17 13:53:27 -04003149 /*
3150 * we can't set the delalloc bits if there are pending ordered
3151 * extents. Drop our locks and wait for them to finish
3152 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003153 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3154 if (ordered) {
3155 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3156 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003157 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003158 btrfs_put_ordered_extent(ordered);
3159 goto again;
3160 }
3161
Chris Masonea8c2812008-08-04 23:17:27 -04003162 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003163 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003164
3165 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003166 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003167 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003168 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003169 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003170
Chris Masone6dcd2d2008-07-17 12:53:50 -04003171 if (zero_start != PAGE_CACHE_SIZE) {
3172 kaddr = kmap(page);
3173 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3174 flush_dcache_page(page);
3175 kunmap(page);
3176 }
Chris Mason247e7432008-07-17 12:53:51 -04003177 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003178 set_page_dirty(page);
3179 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003180
3181out_unlock:
3182 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003183out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003184 return ret;
3185}
3186
Chris Mason39279cc2007-06-12 06:35:45 -04003187static void btrfs_truncate(struct inode *inode)
3188{
3189 struct btrfs_root *root = BTRFS_I(inode)->root;
3190 int ret;
3191 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003192 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003193 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003194
3195 if (!S_ISREG(inode->i_mode))
3196 return;
3197 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3198 return;
3199
3200 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003201 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003202
Chris Mason39279cc2007-06-12 06:35:45 -04003203 trans = btrfs_start_transaction(root, 1);
3204 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003205 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003206
Josef Bacik7b128762008-07-24 12:17:14 -04003207 ret = btrfs_orphan_add(trans, inode);
3208 if (ret)
3209 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003210 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04003211 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05003212 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003213 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003214
Josef Bacik7b128762008-07-24 12:17:14 -04003215 ret = btrfs_orphan_del(trans, inode);
3216 BUG_ON(ret);
3217
3218out:
3219 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003220 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003221 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003222 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003223}
3224
Sven Wegener3b963622008-06-09 21:57:42 -04003225/*
3226 * Invalidate a single dcache entry at the root of the filesystem.
3227 * Needed after creation of snapshot or subvolume.
3228 */
3229void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3230 int namelen)
3231{
3232 struct dentry *alias, *entry;
3233 struct qstr qstr;
3234
3235 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3236 if (alias) {
3237 qstr.name = name;
3238 qstr.len = namelen;
3239 /* change me if btrfs ever gets a d_hash operation */
3240 qstr.hash = full_name_hash(qstr.name, qstr.len);
3241 entry = d_lookup(alias, &qstr);
3242 dput(alias);
3243 if (entry) {
3244 d_invalidate(entry);
3245 dput(entry);
3246 }
3247 }
3248}
3249
Chris Masond352ac62008-09-29 15:18:18 -04003250/*
3251 * create a new subvolume directory/inode (helper for the ioctl).
3252 */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003253int btrfs_create_subvol_root(struct btrfs_root *new_root,
3254 struct btrfs_trans_handle *trans, u64 new_dirid,
3255 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003256{
Chris Mason39279cc2007-06-12 06:35:45 -04003257 struct inode *inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003258 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003259
Josef Bacikaec74772008-07-24 12:12:38 -04003260 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003261 new_dirid, block_group, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04003262 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003263 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003264 inode->i_op = &btrfs_dir_inode_operations;
3265 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003266 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003267
Chris Mason39279cc2007-06-12 06:35:45 -04003268 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003269 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003270
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003271 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003272}
3273
Chris Masond352ac62008-09-29 15:18:18 -04003274/* helper function for file defrag and space balancing. This
3275 * forces readahead on a given range of bytes in an inode
3276 */
Chris Masonedbd8d42007-12-21 16:27:24 -05003277unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003278 struct file_ra_state *ra, struct file *file,
3279 pgoff_t offset, pgoff_t last_index)
3280{
Chris Mason8e7bf942008-04-28 09:02:36 -04003281 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003282
Chris Mason86479a02007-09-10 19:58:16 -04003283 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3284 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04003285}
3286
Chris Mason39279cc2007-06-12 06:35:45 -04003287struct inode *btrfs_alloc_inode(struct super_block *sb)
3288{
3289 struct btrfs_inode *ei;
3290
3291 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3292 if (!ei)
3293 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003294 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003295 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003296 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003297 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3298 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003299 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003300 return &ei->vfs_inode;
3301}
3302
3303void btrfs_destroy_inode(struct inode *inode)
3304{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003305 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003306 WARN_ON(!list_empty(&inode->i_dentry));
3307 WARN_ON(inode->i_data.nrpages);
3308
Josef Bacik33268ea2008-07-24 12:16:36 -04003309 if (BTRFS_I(inode)->i_acl &&
3310 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3311 posix_acl_release(BTRFS_I(inode)->i_acl);
3312 if (BTRFS_I(inode)->i_default_acl &&
3313 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3314 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3315
Yanbcc63ab2008-07-30 16:29:20 -04003316 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003317 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3318 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3319 " list\n", inode->i_ino);
3320 dump_stack();
3321 }
Yanbcc63ab2008-07-30 16:29:20 -04003322 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003323
Chris Masone6dcd2d2008-07-17 12:53:50 -04003324 while(1) {
3325 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3326 if (!ordered)
3327 break;
3328 else {
3329 printk("found ordered extent %Lu %Lu\n",
3330 ordered->file_offset, ordered->len);
3331 btrfs_remove_ordered_extent(inode, ordered);
3332 btrfs_put_ordered_extent(ordered);
3333 btrfs_put_ordered_extent(ordered);
3334 }
3335 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003336 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003337 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3338}
3339
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003340static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04003341{
3342 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3343
3344 inode_init_once(&ei->vfs_inode);
3345}
3346
3347void btrfs_destroy_cachep(void)
3348{
3349 if (btrfs_inode_cachep)
3350 kmem_cache_destroy(btrfs_inode_cachep);
3351 if (btrfs_trans_handle_cachep)
3352 kmem_cache_destroy(btrfs_trans_handle_cachep);
3353 if (btrfs_transaction_cachep)
3354 kmem_cache_destroy(btrfs_transaction_cachep);
3355 if (btrfs_bit_radix_cachep)
3356 kmem_cache_destroy(btrfs_bit_radix_cachep);
3357 if (btrfs_path_cachep)
3358 kmem_cache_destroy(btrfs_path_cachep);
3359}
3360
Chris Mason86479a02007-09-10 19:58:16 -04003361struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003362 unsigned long extra_flags,
Chris Mason2b1f55b2008-09-24 11:48:04 -04003363 void (*ctor)(void *))
Chris Mason92fee662007-07-25 12:31:35 -04003364{
3365 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
Chris Mason2b1f55b2008-09-24 11:48:04 -04003366 SLAB_MEM_SPREAD | extra_flags), ctor);
Chris Mason92fee662007-07-25 12:31:35 -04003367}
3368
Chris Mason39279cc2007-06-12 06:35:45 -04003369int btrfs_init_cachep(void)
3370{
Chris Mason86479a02007-09-10 19:58:16 -04003371 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003372 sizeof(struct btrfs_inode),
3373 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003374 if (!btrfs_inode_cachep)
3375 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003376 btrfs_trans_handle_cachep =
3377 btrfs_cache_create("btrfs_trans_handle_cache",
3378 sizeof(struct btrfs_trans_handle),
3379 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003380 if (!btrfs_trans_handle_cachep)
3381 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003382 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003383 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003384 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003385 if (!btrfs_transaction_cachep)
3386 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003387 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003388 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003389 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003390 if (!btrfs_path_cachep)
3391 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003392 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003393 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003394 if (!btrfs_bit_radix_cachep)
3395 goto fail;
3396 return 0;
3397fail:
3398 btrfs_destroy_cachep();
3399 return -ENOMEM;
3400}
3401
3402static int btrfs_getattr(struct vfsmount *mnt,
3403 struct dentry *dentry, struct kstat *stat)
3404{
3405 struct inode *inode = dentry->d_inode;
3406 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003407 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003408 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003409 return 0;
3410}
3411
3412static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3413 struct inode * new_dir,struct dentry *new_dentry)
3414{
3415 struct btrfs_trans_handle *trans;
3416 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3417 struct inode *new_inode = new_dentry->d_inode;
3418 struct inode *old_inode = old_dentry->d_inode;
3419 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003420 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003421 int ret;
3422
3423 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3424 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3425 return -ENOTEMPTY;
3426 }
Chris Mason5f39d392007-10-15 16:14:19 -04003427
Chris Mason1832a6d2007-12-21 16:27:21 -05003428 ret = btrfs_check_free_space(root, 1, 0);
3429 if (ret)
3430 goto out_unlock;
3431
Chris Mason39279cc2007-06-12 06:35:45 -04003432 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003433
Chris Mason39279cc2007-06-12 06:35:45 -04003434 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003435
Chris Masone02119d2008-09-05 16:13:11 -04003436 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003437 old_dir->i_ctime = old_dir->i_mtime = ctime;
3438 new_dir->i_ctime = new_dir->i_mtime = ctime;
3439 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003440
Chris Masone02119d2008-09-05 16:13:11 -04003441 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
3442 old_dentry->d_name.name,
3443 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04003444 if (ret)
3445 goto out_fail;
3446
3447 if (new_inode) {
3448 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003449 ret = btrfs_unlink_inode(trans, root, new_dir,
3450 new_dentry->d_inode,
3451 new_dentry->d_name.name,
3452 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04003453 if (ret)
3454 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003455 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003456 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003457 if (ret)
3458 goto out_fail;
3459 }
Chris Masone02119d2008-09-05 16:13:11 -04003460
Chris Mason39279cc2007-06-12 06:35:45 -04003461 }
Chris Mason00e4e6b2008-08-05 11:18:09 -04003462 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003463 if (ret)
3464 goto out_fail;
3465
Chris Masone02119d2008-09-05 16:13:11 -04003466 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
3467 old_inode, new_dentry->d_name.name,
3468 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003469 if (ret)
3470 goto out_fail;
3471
3472out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003473 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003474out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003475 return ret;
3476}
3477
Chris Masond352ac62008-09-29 15:18:18 -04003478/*
3479 * some fairly slow code that needs optimization. This walks the list
3480 * of all the inodes with pending delalloc and forces them to disk.
3481 */
Chris Masonea8c2812008-08-04 23:17:27 -04003482int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3483{
3484 struct list_head *head = &root->fs_info->delalloc_inodes;
3485 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003486 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04003487 unsigned long flags;
3488
3489 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3490 while(!list_empty(head)) {
3491 binode = list_entry(head->next, struct btrfs_inode,
3492 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003493 inode = igrab(&binode->vfs_inode);
3494 if (!inode)
3495 list_del_init(&binode->delalloc_inodes);
Chris Masonea8c2812008-08-04 23:17:27 -04003496 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003497 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04003498 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003499 iput(inode);
3500 }
3501 cond_resched();
Chris Masonea8c2812008-08-04 23:17:27 -04003502 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3503 }
3504 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason8c8bee12008-09-29 11:19:10 -04003505
3506 /* the filemap_flush will queue IO into the worker threads, but
3507 * we have to make sure the IO is actually started and that
3508 * ordered extents get created before we return
3509 */
3510 atomic_inc(&root->fs_info->async_submit_draining);
3511 while(atomic_read(&root->fs_info->nr_async_submits)) {
3512 wait_event(root->fs_info->async_submit_wait,
3513 (atomic_read(&root->fs_info->nr_async_submits) == 0));
3514 }
3515 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04003516 return 0;
3517}
3518
Chris Mason39279cc2007-06-12 06:35:45 -04003519static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3520 const char *symname)
3521{
3522 struct btrfs_trans_handle *trans;
3523 struct btrfs_root *root = BTRFS_I(dir)->root;
3524 struct btrfs_path *path;
3525 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003526 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003527 int err;
3528 int drop_inode = 0;
3529 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003530 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04003531 int name_len;
3532 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003533 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003534 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003535 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003536 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003537
3538 name_len = strlen(symname) + 1;
3539 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3540 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003541
Chris Mason1832a6d2007-12-21 16:27:21 -05003542 err = btrfs_check_free_space(root, 1, 0);
3543 if (err)
3544 goto out_fail;
3545
Chris Mason39279cc2007-06-12 06:35:45 -04003546 trans = btrfs_start_transaction(root, 1);
3547 btrfs_set_trans_block_group(trans, dir);
3548
3549 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3550 if (err) {
3551 err = -ENOSPC;
3552 goto out_unlock;
3553 }
3554
Josef Bacikaec74772008-07-24 12:12:38 -04003555 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003556 dentry->d_name.len,
3557 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003558 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3559 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003560 err = PTR_ERR(inode);
3561 if (IS_ERR(inode))
3562 goto out_unlock;
3563
Josef Bacik33268ea2008-07-24 12:16:36 -04003564 err = btrfs_init_acl(inode, dir);
3565 if (err) {
3566 drop_inode = 1;
3567 goto out_unlock;
3568 }
3569
Chris Mason39279cc2007-06-12 06:35:45 -04003570 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003571 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003572 if (err)
3573 drop_inode = 1;
3574 else {
3575 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003576 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003577 inode->i_fop = &btrfs_file_operations;
3578 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003579 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003580 }
3581 dir->i_sb->s_dirt = 1;
3582 btrfs_update_inode_block_group(trans, inode);
3583 btrfs_update_inode_block_group(trans, dir);
3584 if (drop_inode)
3585 goto out_unlock;
3586
3587 path = btrfs_alloc_path();
3588 BUG_ON(!path);
3589 key.objectid = inode->i_ino;
3590 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003591 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3592 datasize = btrfs_file_extent_calc_inline_size(name_len);
3593 err = btrfs_insert_empty_item(trans, root, path, &key,
3594 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003595 if (err) {
3596 drop_inode = 1;
3597 goto out_unlock;
3598 }
Chris Mason5f39d392007-10-15 16:14:19 -04003599 leaf = path->nodes[0];
3600 ei = btrfs_item_ptr(leaf, path->slots[0],
3601 struct btrfs_file_extent_item);
3602 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3603 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003604 BTRFS_FILE_EXTENT_INLINE);
3605 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003606 write_extent_buffer(leaf, symname, ptr, name_len);
3607 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003608 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003609
Chris Mason39279cc2007-06-12 06:35:45 -04003610 inode->i_op = &btrfs_symlink_inode_operations;
3611 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003612 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003613 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003614 err = btrfs_update_inode(trans, root, inode);
3615 if (err)
3616 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003617
3618out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003619 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003620 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003621out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003622 if (drop_inode) {
3623 inode_dec_link_count(inode);
3624 iput(inode);
3625 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003626 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003627 return err;
3628}
Chris Mason16432982008-04-10 10:23:21 -04003629
Chris Masone6dcd2d2008-07-17 12:53:50 -04003630static int btrfs_set_page_dirty(struct page *page)
3631{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003632 return __set_page_dirty_nobuffers(page);
3633}
3634
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003635static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05003636{
3637 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3638 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003639 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003640}
Chris Mason39279cc2007-06-12 06:35:45 -04003641
3642static struct inode_operations btrfs_dir_inode_operations = {
3643 .lookup = btrfs_lookup,
3644 .create = btrfs_create,
3645 .unlink = btrfs_unlink,
3646 .link = btrfs_link,
3647 .mkdir = btrfs_mkdir,
3648 .rmdir = btrfs_rmdir,
3649 .rename = btrfs_rename,
3650 .symlink = btrfs_symlink,
3651 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003652 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003653 .setxattr = btrfs_setxattr,
3654 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003655 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003656 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003657 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003658};
Chris Mason39279cc2007-06-12 06:35:45 -04003659static struct inode_operations btrfs_dir_ro_inode_operations = {
3660 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003661 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003662};
Chris Mason39279cc2007-06-12 06:35:45 -04003663static struct file_operations btrfs_dir_file_operations = {
3664 .llseek = generic_file_llseek,
3665 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01003666 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003667 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003668#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003669 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003670#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003671 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04003672 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003673};
3674
Chris Masond1310b22008-01-24 16:13:08 -05003675static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003676 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003677 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003678 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003679 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003680 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003681 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003682 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003683 .set_bit_hook = btrfs_set_bit_hook,
3684 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003685};
3686
Chris Mason39279cc2007-06-12 06:35:45 -04003687static struct address_space_operations btrfs_aops = {
3688 .readpage = btrfs_readpage,
3689 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003690 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003691 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003692 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003693 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003694 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003695 .invalidatepage = btrfs_invalidatepage,
3696 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003697 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003698};
3699
3700static struct address_space_operations btrfs_symlink_aops = {
3701 .readpage = btrfs_readpage,
3702 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003703 .invalidatepage = btrfs_invalidatepage,
3704 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003705};
3706
3707static struct inode_operations btrfs_file_inode_operations = {
3708 .truncate = btrfs_truncate,
3709 .getattr = btrfs_getattr,
3710 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003711 .setxattr = btrfs_setxattr,
3712 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003713 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003714 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003715 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003716};
Josef Bacik618e21d2007-07-11 10:18:17 -04003717static struct inode_operations btrfs_special_inode_operations = {
3718 .getattr = btrfs_getattr,
3719 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003720 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003721 .setxattr = btrfs_setxattr,
3722 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04003723 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003724 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003725};
Chris Mason39279cc2007-06-12 06:35:45 -04003726static struct inode_operations btrfs_symlink_inode_operations = {
3727 .readlink = generic_readlink,
3728 .follow_link = page_follow_link_light,
3729 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003730 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003731};