blob: 3ab147dc3c05fcde7e8afd034684feaaef1739e5 [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 Masona62b9402008-10-03 16:31:08 -0400389 u64 logical = (u64)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,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400650 trans->transid, inode->i_ino, &ins);
Zheng Yan31840ae2008-09-23 13:14:14 -0400651 BUG_ON(ret);
652 btrfs_release_path(root, path);
653
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400654 inode_add_bytes(inode, ordered_extent->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400655 unlock_extent(io_tree, ordered_extent->file_offset,
656 ordered_extent->file_offset + ordered_extent->len - 1,
657 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400658nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -0400659 add_pending_csums(trans, inode, ordered_extent->file_offset,
660 &ordered_extent->list);
661
Chris Mason34353022008-09-23 20:19:49 -0400662 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -0400663 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -0400664 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400665 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -0400666 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400667
Chris Masone6dcd2d2008-07-17 12:53:50 -0400668 /* once for us */
669 btrfs_put_ordered_extent(ordered_extent);
670 /* once for the tree */
671 btrfs_put_ordered_extent(ordered_extent);
672
Chris Masone6dcd2d2008-07-17 12:53:50 -0400673 btrfs_end_transaction(trans, root);
Zheng Yan31840ae2008-09-23 13:14:14 -0400674 if (path)
675 btrfs_free_path(path);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400676 return 0;
677}
678
Chris Mason211f90e2008-07-18 11:56:15 -0400679int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
680 struct extent_state *state, int uptodate)
681{
682 return btrfs_finish_ordered_io(page->mapping->host, start, end);
683}
684
Chris Masond352ac62008-09-29 15:18:18 -0400685/*
686 * When IO fails, either with EIO or csum verification fails, we
687 * try other mirrors that might have a good copy of the data. This
688 * io_failure_record is used to record state as we go through all the
689 * mirrors. If another mirror has good data, the page is set up to date
690 * and things continue. If a good mirror can't be found, the original
691 * bio end_io callback is called to indicate things have failed.
692 */
Chris Mason7e383262008-04-09 16:28:12 -0400693struct io_failure_record {
694 struct page *page;
695 u64 start;
696 u64 len;
697 u64 logical;
698 int last_mirror;
699};
700
Chris Mason1259ab72008-05-12 13:39:03 -0400701int btrfs_io_failed_hook(struct bio *failed_bio,
702 struct page *page, u64 start, u64 end,
703 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400704{
705 struct io_failure_record *failrec = NULL;
706 u64 private;
707 struct extent_map *em;
708 struct inode *inode = page->mapping->host;
709 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400710 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400711 struct bio *bio;
712 int num_copies;
713 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400714 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400715 u64 logical;
716
717 ret = get_state_private(failure_tree, start, &private);
718 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400719 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
720 if (!failrec)
721 return -ENOMEM;
722 failrec->start = start;
723 failrec->len = end - start + 1;
724 failrec->last_mirror = 0;
725
Chris Mason3b951512008-04-17 11:29:12 -0400726 spin_lock(&em_tree->lock);
727 em = lookup_extent_mapping(em_tree, start, failrec->len);
728 if (em->start > start || em->start + em->len < start) {
729 free_extent_map(em);
730 em = NULL;
731 }
732 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400733
734 if (!em || IS_ERR(em)) {
735 kfree(failrec);
736 return -EIO;
737 }
738 logical = start - em->start;
739 logical = em->block_start + logical;
740 failrec->logical = logical;
741 free_extent_map(em);
742 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
743 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400744 set_state_private(failure_tree, start,
745 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400746 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400747 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400748 }
749 num_copies = btrfs_num_copies(
750 &BTRFS_I(inode)->root->fs_info->mapping_tree,
751 failrec->logical, failrec->len);
752 failrec->last_mirror++;
753 if (!state) {
754 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
755 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
756 failrec->start,
757 EXTENT_LOCKED);
758 if (state && state->start != failrec->start)
759 state = NULL;
760 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
761 }
762 if (!state || failrec->last_mirror > num_copies) {
763 set_state_private(failure_tree, failrec->start, 0);
764 clear_extent_bits(failure_tree, failrec->start,
765 failrec->start + failrec->len - 1,
766 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
767 kfree(failrec);
768 return -EIO;
769 }
770 bio = bio_alloc(GFP_NOFS, 1);
771 bio->bi_private = state;
772 bio->bi_end_io = failed_bio->bi_end_io;
773 bio->bi_sector = failrec->logical >> 9;
774 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400775 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400776 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400777 if (failed_bio->bi_rw & (1 << BIO_RW))
778 rw = WRITE;
779 else
780 rw = READ;
781
782 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
783 failrec->last_mirror);
784 return 0;
785}
786
Chris Masond352ac62008-09-29 15:18:18 -0400787/*
788 * each time an IO finishes, we do a fast check in the IO failure tree
789 * to see if we need to process or clean up an io_failure_record
790 */
Chris Mason1259ab72008-05-12 13:39:03 -0400791int btrfs_clean_io_failures(struct inode *inode, u64 start)
792{
793 u64 private;
794 u64 private_failure;
795 struct io_failure_record *failure;
796 int ret;
797
798 private = 0;
799 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
800 (u64)-1, 1, EXTENT_DIRTY)) {
801 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
802 start, &private_failure);
803 if (ret == 0) {
804 failure = (struct io_failure_record *)(unsigned long)
805 private_failure;
806 set_state_private(&BTRFS_I(inode)->io_failure_tree,
807 failure->start, 0);
808 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
809 failure->start,
810 failure->start + failure->len - 1,
811 EXTENT_DIRTY | EXTENT_LOCKED,
812 GFP_NOFS);
813 kfree(failure);
814 }
815 }
Chris Mason7e383262008-04-09 16:28:12 -0400816 return 0;
817}
818
Chris Masond352ac62008-09-29 15:18:18 -0400819/*
820 * when reads are done, we need to check csums to verify the data is correct
821 * if there's a match, we allow the bio to finish. If not, we go through
822 * the io_failure_record routines to find good copies
823 */
Chris Mason70dec802008-01-29 09:59:12 -0500824int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
825 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400826{
Chris Mason35ebb932007-10-30 16:56:53 -0400827 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400828 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500829 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400830 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500831 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400832 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400833 struct btrfs_root *root = BTRFS_I(inode)->root;
834 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400835 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500836
Yanb98b6762008-01-08 15:54:37 -0500837 if (btrfs_test_opt(root, NODATASUM) ||
838 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500839 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500840 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500841 private = state->private;
842 ret = 0;
843 } else {
844 ret = get_state_private(io_tree, start, &private);
845 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400846 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400847 kaddr = kmap_atomic(page, KM_IRQ0);
848 if (ret) {
849 goto zeroit;
850 }
Chris Masonff79f812007-10-15 16:22:25 -0400851 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
852 btrfs_csum_final(csum, (char *)&csum);
853 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400854 goto zeroit;
855 }
856 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400857 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400858
859 /* if the io failure tree for this inode is non-empty,
860 * check to see if we've recovered from a failed IO
861 */
Chris Mason1259ab72008-05-12 13:39:03 -0400862 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400863 return 0;
864
865zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500866 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
867 page->mapping->host->i_ino, (unsigned long long)start, csum,
868 private);
Chris Masondb945352007-10-15 16:15:53 -0400869 memset(kaddr + offset, 1, end - start + 1);
870 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400871 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400872 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400873 if (private == 0)
874 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400875 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400876}
Chris Masonb888db22007-08-27 16:49:44 -0400877
Josef Bacik7b128762008-07-24 12:17:14 -0400878/*
879 * This creates an orphan entry for the given inode in case something goes
880 * wrong in the middle of an unlink/truncate.
881 */
882int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
883{
884 struct btrfs_root *root = BTRFS_I(inode)->root;
885 int ret = 0;
886
Yanbcc63ab2008-07-30 16:29:20 -0400887 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400888
889 /* already on the orphan list, we're good */
890 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400891 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400892 return 0;
893 }
894
895 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
896
Yanbcc63ab2008-07-30 16:29:20 -0400897 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400898
899 /*
900 * insert an orphan item to track this unlinked/truncated file
901 */
902 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
903
904 return ret;
905}
906
907/*
908 * We have done the truncate/delete so we can go ahead and remove the orphan
909 * item for this particular inode.
910 */
911int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
912{
913 struct btrfs_root *root = BTRFS_I(inode)->root;
914 int ret = 0;
915
Yanbcc63ab2008-07-30 16:29:20 -0400916 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400917
918 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400919 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400920 return 0;
921 }
922
923 list_del_init(&BTRFS_I(inode)->i_orphan);
924 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400925 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400926 return 0;
927 }
928
Yanbcc63ab2008-07-30 16:29:20 -0400929 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400930
931 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
932
933 return ret;
934}
935
936/*
937 * this cleans up any orphans that may be left on the list from the last use
938 * of this root.
939 */
940void btrfs_orphan_cleanup(struct btrfs_root *root)
941{
942 struct btrfs_path *path;
943 struct extent_buffer *leaf;
944 struct btrfs_item *item;
945 struct btrfs_key key, found_key;
946 struct btrfs_trans_handle *trans;
947 struct inode *inode;
948 int ret = 0, nr_unlink = 0, nr_truncate = 0;
949
950 /* don't do orphan cleanup if the fs is readonly. */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400951 if (root->fs_info->sb->s_flags & MS_RDONLY)
Josef Bacik7b128762008-07-24 12:17:14 -0400952 return;
953
954 path = btrfs_alloc_path();
955 if (!path)
956 return;
957 path->reada = -1;
958
959 key.objectid = BTRFS_ORPHAN_OBJECTID;
960 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
961 key.offset = (u64)-1;
962
Josef Bacik7b128762008-07-24 12:17:14 -0400963
964 while (1) {
965 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
966 if (ret < 0) {
967 printk(KERN_ERR "Error searching slot for orphan: %d"
968 "\n", ret);
969 break;
970 }
971
972 /*
973 * if ret == 0 means we found what we were searching for, which
974 * is weird, but possible, so only screw with path if we didnt
975 * find the key and see if we have stuff that matches
976 */
977 if (ret > 0) {
978 if (path->slots[0] == 0)
979 break;
980 path->slots[0]--;
981 }
982
983 /* pull out the item */
984 leaf = path->nodes[0];
985 item = btrfs_item_nr(leaf, path->slots[0]);
986 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
987
988 /* make sure the item matches what we want */
989 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
990 break;
991 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
992 break;
993
994 /* release the path since we're done with it */
995 btrfs_release_path(root, path);
996
997 /*
998 * this is where we are basically btrfs_lookup, without the
999 * crossing root thing. we store the inode number in the
1000 * offset of the orphan item.
1001 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001002 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001003 found_key.offset, root);
1004 if (!inode)
1005 break;
1006
1007 if (inode->i_state & I_NEW) {
1008 BTRFS_I(inode)->root = root;
1009
1010 /* have to set the location manually */
1011 BTRFS_I(inode)->location.objectid = inode->i_ino;
1012 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1013 BTRFS_I(inode)->location.offset = 0;
1014
1015 btrfs_read_locked_inode(inode);
1016 unlock_new_inode(inode);
1017 }
1018
1019 /*
1020 * add this inode to the orphan list so btrfs_orphan_del does
1021 * the proper thing when we hit it
1022 */
Yanbcc63ab2008-07-30 16:29:20 -04001023 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001024 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001025 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001026
1027 /*
1028 * if this is a bad inode, means we actually succeeded in
1029 * removing the inode, but not the orphan record, which means
1030 * we need to manually delete the orphan since iput will just
1031 * do a destroy_inode
1032 */
1033 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001034 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001035 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001036 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001037 iput(inode);
1038 continue;
1039 }
1040
1041 /* if we have links, this was a truncate, lets do that */
1042 if (inode->i_nlink) {
1043 nr_truncate++;
1044 btrfs_truncate(inode);
1045 } else {
1046 nr_unlink++;
1047 }
1048
1049 /* this will do delete_inode and everything for us */
1050 iput(inode);
1051 }
1052
1053 if (nr_unlink)
1054 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1055 if (nr_truncate)
1056 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1057
1058 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04001059}
1060
Chris Masond352ac62008-09-29 15:18:18 -04001061/*
1062 * read an inode from the btree into the in-memory inode
1063 */
Chris Mason39279cc2007-06-12 06:35:45 -04001064void btrfs_read_locked_inode(struct inode *inode)
1065{
1066 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001067 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001068 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001069 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001070 struct btrfs_root *root = BTRFS_I(inode)->root;
1071 struct btrfs_key location;
1072 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001073 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001074 int ret;
1075
1076 path = btrfs_alloc_path();
1077 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001078 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001079
Chris Mason39279cc2007-06-12 06:35:45 -04001080 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001081 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001082 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001083
Chris Mason5f39d392007-10-15 16:14:19 -04001084 leaf = path->nodes[0];
1085 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1086 struct btrfs_inode_item);
1087
1088 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1089 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1090 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1091 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001092 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001093
1094 tspec = btrfs_inode_atime(inode_item);
1095 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1096 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1097
1098 tspec = btrfs_inode_mtime(inode_item);
1099 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1100 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1101
1102 tspec = btrfs_inode_ctime(inode_item);
1103 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1104 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1105
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001106 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04001107 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
1108 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04001109 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001110 rdev = btrfs_inode_rdev(leaf, inode_item);
1111
Josef Bacikaec74772008-07-24 12:12:38 -04001112 BTRFS_I(inode)->index_cnt = (u64)-1;
1113
Chris Mason5f39d392007-10-15 16:14:19 -04001114 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001115 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1116 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001117 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001118 if (!BTRFS_I(inode)->block_group) {
1119 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001120 NULL, 0,
1121 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001122 }
Chris Mason39279cc2007-06-12 06:35:45 -04001123 btrfs_free_path(path);
1124 inode_item = NULL;
1125
Chris Mason39279cc2007-06-12 06:35:45 -04001126 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001127 case S_IFREG:
1128 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001129 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001130 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001131 inode->i_fop = &btrfs_file_operations;
1132 inode->i_op = &btrfs_file_inode_operations;
1133 break;
1134 case S_IFDIR:
1135 inode->i_fop = &btrfs_dir_file_operations;
1136 if (root == root->fs_info->tree_root)
1137 inode->i_op = &btrfs_dir_ro_inode_operations;
1138 else
1139 inode->i_op = &btrfs_dir_inode_operations;
1140 break;
1141 case S_IFLNK:
1142 inode->i_op = &btrfs_symlink_inode_operations;
1143 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001144 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001145 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001146 default:
1147 init_special_inode(inode, inode->i_mode, rdev);
1148 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001149 }
1150 return;
1151
1152make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001153 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001154 make_bad_inode(inode);
1155}
1156
Chris Masond352ac62008-09-29 15:18:18 -04001157/*
1158 * given a leaf and an inode, copy the inode fields into the leaf
1159 */
Chris Masone02119d2008-09-05 16:13:11 -04001160static void fill_inode_item(struct btrfs_trans_handle *trans,
1161 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04001162 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001163 struct inode *inode)
1164{
Chris Mason5f39d392007-10-15 16:14:19 -04001165 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1166 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001167 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001168 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1169 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1170
1171 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1172 inode->i_atime.tv_sec);
1173 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1174 inode->i_atime.tv_nsec);
1175
1176 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1177 inode->i_mtime.tv_sec);
1178 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1179 inode->i_mtime.tv_nsec);
1180
1181 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1182 inode->i_ctime.tv_sec);
1183 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1184 inode->i_ctime.tv_nsec);
1185
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001186 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04001187 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
1188 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001189 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001190 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001191 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001192 BTRFS_I(inode)->block_group->key.objectid);
1193}
1194
Chris Masond352ac62008-09-29 15:18:18 -04001195/*
1196 * copy everything in the in-memory inode into the btree.
1197 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001198int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001199 struct btrfs_root *root,
1200 struct inode *inode)
1201{
1202 struct btrfs_inode_item *inode_item;
1203 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001204 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001205 int ret;
1206
1207 path = btrfs_alloc_path();
1208 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001209 ret = btrfs_lookup_inode(trans, root, path,
1210 &BTRFS_I(inode)->location, 1);
1211 if (ret) {
1212 if (ret > 0)
1213 ret = -ENOENT;
1214 goto failed;
1215 }
1216
Chris Mason5f39d392007-10-15 16:14:19 -04001217 leaf = path->nodes[0];
1218 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001219 struct btrfs_inode_item);
1220
Chris Masone02119d2008-09-05 16:13:11 -04001221 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001222 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001223 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001224 ret = 0;
1225failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001226 btrfs_free_path(path);
1227 return ret;
1228}
1229
1230
Chris Masond352ac62008-09-29 15:18:18 -04001231/*
1232 * unlink helper that gets used here in inode.c and in the tree logging
1233 * recovery code. It remove a link in a directory with a given name, and
1234 * also drops the back refs in the inode to the directory
1235 */
Chris Masone02119d2008-09-05 16:13:11 -04001236int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
1237 struct btrfs_root *root,
1238 struct inode *dir, struct inode *inode,
1239 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04001240{
1241 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001242 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001243 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001244 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001245 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001246 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001247
1248 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001249 if (!path) {
1250 ret = -ENOMEM;
1251 goto err;
1252 }
1253
Chris Mason39279cc2007-06-12 06:35:45 -04001254 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1255 name, name_len, -1);
1256 if (IS_ERR(di)) {
1257 ret = PTR_ERR(di);
1258 goto err;
1259 }
1260 if (!di) {
1261 ret = -ENOENT;
1262 goto err;
1263 }
Chris Mason5f39d392007-10-15 16:14:19 -04001264 leaf = path->nodes[0];
1265 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001266 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001267 if (ret)
1268 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001269 btrfs_release_path(root, path);
1270
Josef Bacikaec74772008-07-24 12:12:38 -04001271 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001272 inode->i_ino,
1273 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04001274 if (ret) {
1275 printk("failed to delete reference to %.*s, "
1276 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04001277 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04001278 goto err;
1279 }
1280
Chris Mason39279cc2007-06-12 06:35:45 -04001281 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001282 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001283 if (IS_ERR(di)) {
1284 ret = PTR_ERR(di);
1285 goto err;
1286 }
1287 if (!di) {
1288 ret = -ENOENT;
1289 goto err;
1290 }
1291 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001292 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001293
Chris Masone02119d2008-09-05 16:13:11 -04001294 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
1295 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04001296 BUG_ON(ret != 0 && ret != -ENOENT);
1297 if (ret != -ENOENT)
1298 BTRFS_I(dir)->log_dirty_trans = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04001299
1300 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
1301 dir, index);
1302 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001303err:
1304 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001305 if (ret)
1306 goto out;
1307
1308 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1309 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1310 btrfs_update_inode(trans, root, dir);
1311 btrfs_drop_nlink(inode);
1312 ret = btrfs_update_inode(trans, root, inode);
1313 dir->i_sb->s_dirt = 1;
1314out:
Chris Mason39279cc2007-06-12 06:35:45 -04001315 return ret;
1316}
1317
1318static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1319{
1320 struct btrfs_root *root;
1321 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001322 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001323 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001324 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001325
1326 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001327
1328 ret = btrfs_check_free_space(root, 1, 1);
1329 if (ret)
1330 goto fail;
1331
Chris Mason39279cc2007-06-12 06:35:45 -04001332 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001333
Chris Mason39279cc2007-06-12 06:35:45 -04001334 btrfs_set_trans_block_group(trans, dir);
Chris Masone02119d2008-09-05 16:13:11 -04001335 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1336 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04001337
1338 if (inode->i_nlink == 0)
1339 ret = btrfs_orphan_add(trans, inode);
1340
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001341 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001342
Chris Mason89ce8a62008-06-25 16:01:31 -04001343 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001344fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001345 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001346 return ret;
1347}
1348
1349static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1350{
1351 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001352 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001353 int ret;
1354 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001355 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001356 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001357
Chris Mason925baed2008-06-25 16:01:30 -04001358 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001359 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001360 }
Yan134d4512007-10-25 15:49:25 -04001361
Chris Mason1832a6d2007-12-21 16:27:21 -05001362 ret = btrfs_check_free_space(root, 1, 1);
1363 if (ret)
1364 goto fail;
1365
Chris Mason39279cc2007-06-12 06:35:45 -04001366 trans = btrfs_start_transaction(root, 1);
1367 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001368
Josef Bacik7b128762008-07-24 12:17:14 -04001369 err = btrfs_orphan_add(trans, inode);
1370 if (err)
1371 goto fail_trans;
1372
Chris Mason39279cc2007-06-12 06:35:45 -04001373 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04001374 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1375 dentry->d_name.name, dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001376 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001377 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001378 }
Chris Mason39544012007-12-12 14:38:19 -05001379
Josef Bacik7b128762008-07-24 12:17:14 -04001380fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001381 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001382 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001383fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001384 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001385
Chris Mason39279cc2007-06-12 06:35:45 -04001386 if (ret && !err)
1387 err = ret;
1388 return err;
1389}
1390
Chris Mason39279cc2007-06-12 06:35:45 -04001391/*
Chris Mason323ac952008-10-01 19:05:46 -04001392 * when truncating bytes in a file, it is possible to avoid reading
1393 * the leaves that contain only checksum items. This can be the
1394 * majority of the IO required to delete a large file, but it must
1395 * be done carefully.
1396 *
1397 * The keys in the level just above the leaves are checked to make sure
1398 * the lowest key in a given leaf is a csum key, and starts at an offset
1399 * after the new size.
1400 *
1401 * Then the key for the next leaf is checked to make sure it also has
1402 * a checksum item for the same file. If it does, we know our target leaf
1403 * contains only checksum items, and it can be safely freed without reading
1404 * it.
1405 *
1406 * This is just an optimization targeted at large files. It may do
1407 * nothing. It will return 0 unless things went badly.
1408 */
1409static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
1410 struct btrfs_root *root,
1411 struct btrfs_path *path,
1412 struct inode *inode, u64 new_size)
1413{
1414 struct btrfs_key key;
1415 int ret;
1416 int nritems;
1417 struct btrfs_key found_key;
1418 struct btrfs_key other_key;
1419
1420 path->lowest_level = 1;
1421 key.objectid = inode->i_ino;
1422 key.type = BTRFS_CSUM_ITEM_KEY;
1423 key.offset = new_size;
1424again:
1425 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1426 if (ret < 0)
1427 goto out;
1428
1429 if (path->nodes[1] == NULL) {
1430 ret = 0;
1431 goto out;
1432 }
1433 ret = 0;
1434 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
1435 nritems = btrfs_header_nritems(path->nodes[1]);
1436
1437 if (!nritems)
1438 goto out;
1439
1440 if (path->slots[1] >= nritems)
1441 goto next_node;
1442
1443 /* did we find a key greater than anything we want to delete? */
1444 if (found_key.objectid > inode->i_ino ||
1445 (found_key.objectid == inode->i_ino && found_key.type > key.type))
1446 goto out;
1447
1448 /* we check the next key in the node to make sure the leave contains
1449 * only checksum items. This comparison doesn't work if our
1450 * leaf is the last one in the node
1451 */
1452 if (path->slots[1] + 1 >= nritems) {
1453next_node:
1454 /* search forward from the last key in the node, this
1455 * will bring us into the next node in the tree
1456 */
1457 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
1458
1459 /* unlikely, but we inc below, so check to be safe */
1460 if (found_key.offset == (u64)-1)
1461 goto out;
1462
1463 /* search_forward needs a path with locks held, do the
1464 * search again for the original key. It is possible
1465 * this will race with a balance and return a path that
1466 * we could modify, but this drop is just an optimization
1467 * and is allowed to miss some leaves.
1468 */
1469 btrfs_release_path(root, path);
1470 found_key.offset++;
1471
1472 /* setup a max key for search_forward */
1473 other_key.offset = (u64)-1;
1474 other_key.type = key.type;
1475 other_key.objectid = key.objectid;
1476
1477 path->keep_locks = 1;
1478 ret = btrfs_search_forward(root, &found_key, &other_key,
1479 path, 0, 0);
1480 path->keep_locks = 0;
1481 if (ret || found_key.objectid != key.objectid ||
1482 found_key.type != key.type) {
1483 ret = 0;
1484 goto out;
1485 }
1486
1487 key.offset = found_key.offset;
1488 btrfs_release_path(root, path);
1489 cond_resched();
1490 goto again;
1491 }
1492
1493 /* we know there's one more slot after us in the tree,
1494 * read that key so we can verify it is also a checksum item
1495 */
1496 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
1497
1498 if (found_key.objectid < inode->i_ino)
1499 goto next_key;
1500
1501 if (found_key.type != key.type || found_key.offset < new_size)
1502 goto next_key;
1503
1504 /*
1505 * if the key for the next leaf isn't a csum key from this objectid,
1506 * we can't be sure there aren't good items inside this leaf.
1507 * Bail out
1508 */
1509 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
1510 goto out;
1511
1512 /*
1513 * it is safe to delete this leaf, it contains only
1514 * csum items from this inode at an offset >= new_size
1515 */
1516 ret = btrfs_del_leaf(trans, root, path,
1517 btrfs_node_blockptr(path->nodes[1],
1518 path->slots[1]));
1519 BUG_ON(ret);
1520
1521next_key:
1522 btrfs_release_path(root, path);
1523
1524 if (other_key.objectid == inode->i_ino &&
1525 other_key.type == key.type && other_key.offset > key.offset) {
1526 key.offset = other_key.offset;
1527 cond_resched();
1528 goto again;
1529 }
1530 ret = 0;
1531out:
1532 /* fixup any changes we've made to the path */
1533 path->lowest_level = 0;
1534 path->keep_locks = 0;
1535 btrfs_release_path(root, path);
1536 return ret;
1537}
1538
1539/*
Chris Mason39279cc2007-06-12 06:35:45 -04001540 * this can truncate away extent items, csum items and directory items.
1541 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04001542 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04001543 *
1544 * csum items that cross the new i_size are truncated to the new size
1545 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001546 *
1547 * min_type is the minimum key type to truncate down to. If set to 0, this
1548 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001549 */
Chris Masone02119d2008-09-05 16:13:11 -04001550noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
1551 struct btrfs_root *root,
1552 struct inode *inode,
1553 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001554{
1555 int ret;
1556 struct btrfs_path *path;
1557 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001558 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001559 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001560 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001561 struct btrfs_file_extent_item *fi;
1562 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001563 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001564 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001565 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001566 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001567 int found_extent;
1568 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001569 int pending_del_nr = 0;
1570 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001571 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001572 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001573
Chris Masone02119d2008-09-05 16:13:11 -04001574 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001575 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001576 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001577 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001578 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001579
Chris Mason39279cc2007-06-12 06:35:45 -04001580 /* FIXME, add redo link to tree so we don't leak on crash */
1581 key.objectid = inode->i_ino;
1582 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001583 key.type = (u8)-1;
1584
Chris Mason85e21ba2008-01-29 15:11:36 -05001585 btrfs_init_path(path);
Chris Mason323ac952008-10-01 19:05:46 -04001586
1587 ret = drop_csum_leaves(trans, root, path, inode, new_size);
1588 BUG_ON(ret);
1589
Chris Mason85e21ba2008-01-29 15:11:36 -05001590search_again:
1591 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1592 if (ret < 0) {
1593 goto error;
1594 }
1595 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001596 /* there are no items in the tree for us to truncate, we're
1597 * done
1598 */
1599 if (path->slots[0] == 0) {
1600 ret = 0;
1601 goto error;
1602 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001603 path->slots[0]--;
1604 }
1605
Chris Mason39279cc2007-06-12 06:35:45 -04001606 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001607 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001608 leaf = path->nodes[0];
1609 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1610 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001611
Chris Mason5f39d392007-10-15 16:14:19 -04001612 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001613 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001614
Chris Mason85e21ba2008-01-29 15:11:36 -05001615 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001616 break;
1617
Chris Mason5f39d392007-10-15 16:14:19 -04001618 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001619 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001620 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001621 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001622 extent_type = btrfs_file_extent_type(leaf, fi);
1623 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001624 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001625 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001626 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1627 struct btrfs_item *item = btrfs_item_nr(leaf,
1628 path->slots[0]);
1629 item_end += btrfs_file_extent_inline_len(leaf,
1630 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001631 }
Yan008630c2007-11-07 13:31:09 -05001632 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001633 }
1634 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1635 ret = btrfs_csum_truncate(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04001636 new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04001637 BUG_ON(ret);
1638 }
Chris Masone02119d2008-09-05 16:13:11 -04001639 if (item_end < new_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001640 if (found_type == BTRFS_DIR_ITEM_KEY) {
1641 found_type = BTRFS_INODE_ITEM_KEY;
1642 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1643 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001644 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1645 found_type = BTRFS_XATTR_ITEM_KEY;
1646 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1647 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001648 } else if (found_type) {
1649 found_type--;
1650 } else {
1651 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001652 }
Yana61721d2007-09-17 11:08:38 -04001653 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001654 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001655 }
Chris Masone02119d2008-09-05 16:13:11 -04001656 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001657 del_item = 1;
1658 else
1659 del_item = 0;
1660 found_extent = 0;
1661
1662 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001663 if (found_type != BTRFS_EXTENT_DATA_KEY)
1664 goto delete;
1665
1666 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001667 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001668 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001669 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001670 u64 orig_num_bytes =
1671 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04001672 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001673 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001674 extent_num_bytes = extent_num_bytes &
1675 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001676 btrfs_set_file_extent_num_bytes(leaf, fi,
1677 extent_num_bytes);
1678 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001679 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04001680 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001681 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001682 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001683 } else {
Chris Masondb945352007-10-15 16:15:53 -04001684 extent_num_bytes =
1685 btrfs_file_extent_disk_num_bytes(leaf,
1686 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001687 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001688 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001689 if (extent_start != 0) {
1690 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04001691 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001692 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001693 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001694 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001695 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001696 }
Chris Mason90692182008-02-08 13:49:28 -05001697 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1698 if (!del_item) {
Chris Masone02119d2008-09-05 16:13:11 -04001699 u32 size = new_size - found_key.offset;
1700
1701 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001702 inode_sub_bytes(inode, item_end + 1 -
1703 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04001704 }
1705 size =
1706 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05001707 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04001708 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05001709 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001710 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001711 inode_sub_bytes(inode, item_end + 1 -
1712 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05001713 }
Chris Mason39279cc2007-06-12 06:35:45 -04001714 }
Chris Mason179e29e2007-11-01 11:28:41 -04001715delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001716 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001717 if (!pending_del_nr) {
1718 /* no pending yet, add ourselves */
1719 pending_del_slot = path->slots[0];
1720 pending_del_nr = 1;
1721 } else if (pending_del_nr &&
1722 path->slots[0] + 1 == pending_del_slot) {
1723 /* hop on the pending chunk */
1724 pending_del_nr++;
1725 pending_del_slot = path->slots[0];
1726 } else {
1727 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1728 }
Chris Mason39279cc2007-06-12 06:35:45 -04001729 } else {
1730 break;
1731 }
Chris Mason39279cc2007-06-12 06:35:45 -04001732 if (found_extent) {
1733 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001734 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04001735 leaf->start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001736 root_gen, inode->i_ino, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001737 BUG_ON(ret);
1738 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001739next:
1740 if (path->slots[0] == 0) {
1741 if (pending_del_nr)
1742 goto del_pending;
1743 btrfs_release_path(root, path);
1744 goto search_again;
1745 }
1746
1747 path->slots[0]--;
1748 if (pending_del_nr &&
1749 path->slots[0] + 1 != pending_del_slot) {
1750 struct btrfs_key debug;
1751del_pending:
1752 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1753 pending_del_slot);
1754 ret = btrfs_del_items(trans, root, path,
1755 pending_del_slot,
1756 pending_del_nr);
1757 BUG_ON(ret);
1758 pending_del_nr = 0;
1759 btrfs_release_path(root, path);
1760 goto search_again;
1761 }
Chris Mason39279cc2007-06-12 06:35:45 -04001762 }
1763 ret = 0;
1764error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001765 if (pending_del_nr) {
1766 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1767 pending_del_nr);
1768 }
Chris Mason39279cc2007-06-12 06:35:45 -04001769 btrfs_free_path(path);
1770 inode->i_sb->s_dirt = 1;
1771 return ret;
1772}
1773
Chris Masona52d9a82007-08-27 16:49:44 -04001774/*
1775 * taken from block_truncate_page, but does cow as it zeros out
1776 * any bytes left in the last page in the file.
1777 */
1778static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1779{
1780 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001781 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001782 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1783 struct btrfs_ordered_extent *ordered;
1784 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001785 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001786 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1787 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1788 struct page *page;
1789 int ret = 0;
1790 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001791 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001792
1793 if ((offset & (blocksize - 1)) == 0)
1794 goto out;
1795
1796 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001797again:
Chris Masona52d9a82007-08-27 16:49:44 -04001798 page = grab_cache_page(mapping, index);
1799 if (!page)
1800 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001801
1802 page_start = page_offset(page);
1803 page_end = page_start + PAGE_CACHE_SIZE - 1;
1804
Chris Masona52d9a82007-08-27 16:49:44 -04001805 if (!PageUptodate(page)) {
1806 ret = btrfs_readpage(NULL, page);
1807 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001808 if (page->mapping != mapping) {
1809 unlock_page(page);
1810 page_cache_release(page);
1811 goto again;
1812 }
Chris Masona52d9a82007-08-27 16:49:44 -04001813 if (!PageUptodate(page)) {
1814 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001815 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001816 }
1817 }
Chris Mason211c17f2008-05-15 09:13:45 -04001818 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001819
1820 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1821 set_page_extent_mapped(page);
1822
1823 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1824 if (ordered) {
1825 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1826 unlock_page(page);
1827 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001828 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001829 btrfs_put_ordered_extent(ordered);
1830 goto again;
1831 }
1832
Chris Masonea8c2812008-08-04 23:17:27 -04001833 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001834 ret = 0;
1835 if (offset != PAGE_CACHE_SIZE) {
1836 kaddr = kmap(page);
1837 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1838 flush_dcache_page(page);
1839 kunmap(page);
1840 }
Chris Mason247e7432008-07-17 12:53:51 -04001841 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001842 set_page_dirty(page);
1843 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001844
Chris Mason89642222008-07-24 09:41:53 -04001845out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001846 unlock_page(page);
1847 page_cache_release(page);
1848out:
1849 return ret;
1850}
1851
1852static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1853{
1854 struct inode *inode = dentry->d_inode;
1855 int err;
1856
1857 err = inode_change_ok(inode, attr);
1858 if (err)
1859 return err;
1860
1861 if (S_ISREG(inode->i_mode) &&
1862 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1863 struct btrfs_trans_handle *trans;
1864 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001865 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001866
Chris Mason5f39d392007-10-15 16:14:19 -04001867 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001868 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001869 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001870 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001871 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001872
Chris Mason1b0f7c22008-01-30 14:33:02 -05001873 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001874 goto out;
1875
Chris Mason1832a6d2007-12-21 16:27:21 -05001876 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001877 if (err)
1878 goto fail;
1879
Chris Mason39279cc2007-06-12 06:35:45 -04001880 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1881
Chris Mason5f564062008-01-22 16:47:59 -05001882 hole_size = block_end - hole_start;
Chris Mason7c2fe322008-08-20 08:51:50 -04001883 while(1) {
1884 struct btrfs_ordered_extent *ordered;
1885 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1886
1887 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1888 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
1889 if (ordered) {
1890 unlock_extent(io_tree, hole_start,
1891 block_end - 1, GFP_NOFS);
1892 btrfs_put_ordered_extent(ordered);
1893 } else {
1894 break;
1895 }
1896 }
Chris Mason39279cc2007-06-12 06:35:45 -04001897
Chris Mason39279cc2007-06-12 06:35:45 -04001898 trans = btrfs_start_transaction(root, 1);
1899 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001900 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001901 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001902 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001903 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001904
Chris Mason179e29e2007-11-01 11:28:41 -04001905 if (alloc_hint != EXTENT_MAP_INLINE) {
1906 err = btrfs_insert_file_extent(trans, root,
1907 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001908 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001909 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001910 btrfs_drop_extent_cache(inode, hole_start,
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001911 (u64)-1, 0);
Chris Mason5f564062008-01-22 16:47:59 -05001912 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001913 }
Chris Masonee6e6502008-07-17 12:54:40 -04001914 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001915 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001916 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001917 if (err)
1918 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001919 }
1920out:
1921 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001922
1923 if (!err && ((attr->ia_valid & ATTR_MODE)))
1924 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001925fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001926 return err;
1927}
Chris Mason61295eb2008-01-14 16:24:38 -05001928
Chris Mason39279cc2007-06-12 06:35:45 -04001929void btrfs_delete_inode(struct inode *inode)
1930{
1931 struct btrfs_trans_handle *trans;
1932 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001933 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001934 int ret;
1935
1936 truncate_inode_pages(&inode->i_data, 0);
1937 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001938 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001939 goto no_delete;
1940 }
Chris Mason4a096752008-07-21 10:29:44 -04001941 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001942
Chris Masondbe674a2008-07-17 12:54:05 -04001943 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001944 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001945
Chris Mason39279cc2007-06-12 06:35:45 -04001946 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001947 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001948 if (ret) {
1949 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001950 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001951 }
1952
1953 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001954
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001955 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001956 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001957
Chris Mason39279cc2007-06-12 06:35:45 -04001958 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001959 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001960 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001961
1962no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001963 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001964 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001965 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001966no_delete:
1967 clear_inode(inode);
1968}
1969
1970/*
1971 * this returns the key found in the dir entry in the location pointer.
1972 * If no dir entries were found, location->objectid is 0.
1973 */
1974static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1975 struct btrfs_key *location)
1976{
1977 const char *name = dentry->d_name.name;
1978 int namelen = dentry->d_name.len;
1979 struct btrfs_dir_item *di;
1980 struct btrfs_path *path;
1981 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001982 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001983
1984 path = btrfs_alloc_path();
1985 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001986
Chris Mason39279cc2007-06-12 06:35:45 -04001987 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1988 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001989 if (IS_ERR(di))
1990 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001991 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001992 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001993 }
Chris Mason5f39d392007-10-15 16:14:19 -04001994 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001995out:
Chris Mason39279cc2007-06-12 06:35:45 -04001996 btrfs_free_path(path);
1997 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001998out_err:
1999 location->objectid = 0;
2000 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04002001}
2002
2003/*
2004 * when we hit a tree root in a directory, the btrfs part of the inode
2005 * needs to be changed to reflect the root directory of the tree root. This
2006 * is kind of like crossing a mount point.
2007 */
2008static int fixup_tree_root_location(struct btrfs_root *root,
2009 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04002010 struct btrfs_root **sub_root,
2011 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04002012{
Chris Mason39279cc2007-06-12 06:35:45 -04002013 struct btrfs_root_item *ri;
2014
2015 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2016 return 0;
2017 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2018 return 0;
2019
Josef Bacik58176a92007-08-29 15:47:34 -04002020 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2021 dentry->d_name.name,
2022 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04002023 if (IS_ERR(*sub_root))
2024 return PTR_ERR(*sub_root);
2025
2026 ri = &(*sub_root)->root_item;
2027 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04002028 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2029 location->offset = 0;
2030
Chris Mason39279cc2007-06-12 06:35:45 -04002031 return 0;
2032}
2033
Chris Masone02119d2008-09-05 16:13:11 -04002034static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002035{
Chris Masone02119d2008-09-05 16:13:11 -04002036 struct btrfs_inode *bi = BTRFS_I(inode);
2037
2038 bi->i_acl = NULL;
2039 bi->i_default_acl = NULL;
2040
2041 bi->generation = 0;
2042 bi->last_trans = 0;
2043 bi->logged_trans = 0;
2044 bi->delalloc_bytes = 0;
2045 bi->disk_i_size = 0;
2046 bi->flags = 0;
2047 bi->index_cnt = (u64)-1;
Chris Mason49eb7e42008-09-11 15:53:12 -04002048 bi->log_dirty_trans = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002049 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2050 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002051 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002052 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2053 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04002054 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04002055 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04002056 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002057 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002058 mutex_init(&BTRFS_I(inode)->log_mutex);
2059}
2060
2061static int btrfs_init_locked_inode(struct inode *inode, void *p)
2062{
2063 struct btrfs_iget_args *args = p;
2064 inode->i_ino = args->ino;
2065 init_btrfs_i(inode);
2066 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002067 return 0;
2068}
2069
2070static int btrfs_find_actor(struct inode *inode, void *opaque)
2071{
2072 struct btrfs_iget_args *args = opaque;
2073 return (args->ino == inode->i_ino &&
2074 args->root == BTRFS_I(inode)->root);
2075}
2076
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002077struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
2078 struct btrfs_root *root, int wait)
2079{
2080 struct inode *inode;
2081 struct btrfs_iget_args args;
2082 args.ino = objectid;
2083 args.root = root;
2084
2085 if (wait) {
2086 inode = ilookup5(s, objectid, btrfs_find_actor,
2087 (void *)&args);
2088 } else {
2089 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
2090 (void *)&args);
2091 }
2092 return inode;
2093}
2094
Chris Mason39279cc2007-06-12 06:35:45 -04002095struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
2096 struct btrfs_root *root)
2097{
2098 struct inode *inode;
2099 struct btrfs_iget_args args;
2100 args.ino = objectid;
2101 args.root = root;
2102
2103 inode = iget5_locked(s, objectid, btrfs_find_actor,
2104 btrfs_init_locked_inode,
2105 (void *)&args);
2106 return inode;
2107}
2108
Balaji Rao1a54ef82008-07-21 02:01:04 +05302109/* Get an inode object given its location and corresponding root.
2110 * Returns in *is_new if the inode was read from disk
2111 */
2112struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
2113 struct btrfs_root *root, int *is_new)
2114{
2115 struct inode *inode;
2116
2117 inode = btrfs_iget_locked(s, location->objectid, root);
2118 if (!inode)
2119 return ERR_PTR(-EACCES);
2120
2121 if (inode->i_state & I_NEW) {
2122 BTRFS_I(inode)->root = root;
2123 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
2124 btrfs_read_locked_inode(inode);
2125 unlock_new_inode(inode);
2126 if (is_new)
2127 *is_new = 1;
2128 } else {
2129 if (is_new)
2130 *is_new = 0;
2131 }
2132
2133 return inode;
2134}
2135
Chris Mason39279cc2007-06-12 06:35:45 -04002136static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
2137 struct nameidata *nd)
2138{
2139 struct inode * inode;
2140 struct btrfs_inode *bi = BTRFS_I(dir);
2141 struct btrfs_root *root = bi->root;
2142 struct btrfs_root *sub_root = root;
2143 struct btrfs_key location;
Balaji Rao1a54ef82008-07-21 02:01:04 +05302144 int ret, new, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002145
2146 if (dentry->d_name.len > BTRFS_NAME_LEN)
2147 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04002148
Chris Mason39279cc2007-06-12 06:35:45 -04002149 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002150
Chris Mason39279cc2007-06-12 06:35:45 -04002151 if (ret < 0)
2152 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002153
Chris Mason39279cc2007-06-12 06:35:45 -04002154 inode = NULL;
2155 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04002156 ret = fixup_tree_root_location(root, &location, &sub_root,
2157 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04002158 if (ret < 0)
2159 return ERR_PTR(ret);
2160 if (ret > 0)
2161 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05302162 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
2163 if (IS_ERR(inode))
2164 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002165
Balaji Rao1a54ef82008-07-21 02:01:04 +05302166 /* the inode and parent dir are two different roots */
2167 if (new && root != sub_root) {
2168 igrab(inode);
2169 sub_root->inode = inode;
2170 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002171 }
2172 }
Josef Bacik7b128762008-07-24 12:17:14 -04002173
2174 if (unlikely(do_orphan))
2175 btrfs_orphan_cleanup(sub_root);
2176
Chris Mason39279cc2007-06-12 06:35:45 -04002177 return d_splice_alias(inode, dentry);
2178}
2179
Chris Mason39279cc2007-06-12 06:35:45 -04002180static unsigned char btrfs_filetype_table[] = {
2181 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
2182};
2183
David Woodhousecbdf5a22008-08-06 19:42:33 +01002184static int btrfs_real_readdir(struct file *filp, void *dirent,
2185 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04002186{
Chris Mason6da6aba2007-12-18 16:15:09 -05002187 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002188 struct btrfs_root *root = BTRFS_I(inode)->root;
2189 struct btrfs_item *item;
2190 struct btrfs_dir_item *di;
2191 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002192 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002193 struct btrfs_path *path;
2194 int ret;
2195 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002196 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002197 int slot;
2198 int advance;
2199 unsigned char d_type;
2200 int over = 0;
2201 u32 di_cur;
2202 u32 di_total;
2203 u32 di_len;
2204 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04002205 char tmp_name[32];
2206 char *name_ptr;
2207 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04002208
2209 /* FIXME, use a real flag for deciding about the key type */
2210 if (root->fs_info->tree_root == root)
2211 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04002212
Chris Mason39544012007-12-12 14:38:19 -05002213 /* special case for "." */
2214 if (filp->f_pos == 0) {
2215 over = filldir(dirent, ".", 1,
2216 1, inode->i_ino,
2217 DT_DIR);
2218 if (over)
2219 return 0;
2220 filp->f_pos = 1;
2221 }
Chris Mason39544012007-12-12 14:38:19 -05002222 /* special case for .., just use the back ref */
2223 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01002224 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05002225 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01002226 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05002227 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01002228 return 0;
Chris Mason39544012007-12-12 14:38:19 -05002229 filp->f_pos = 2;
2230 }
2231
David Woodhouse49593bf2008-08-17 17:08:36 +01002232 path = btrfs_alloc_path();
2233 path->reada = 2;
2234
Chris Mason39279cc2007-06-12 06:35:45 -04002235 btrfs_set_key_type(&key, key_type);
2236 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002237 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04002238
Chris Mason39279cc2007-06-12 06:35:45 -04002239 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2240 if (ret < 0)
2241 goto err;
2242 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01002243
2244 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002245 leaf = path->nodes[0];
2246 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002247 slot = path->slots[0];
2248 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01002249 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002250 ret = btrfs_next_leaf(root, path);
2251 if (ret)
2252 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002253 leaf = path->nodes[0];
2254 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002255 slot = path->slots[0];
2256 } else {
2257 slot++;
2258 path->slots[0]++;
2259 }
2260 }
2261 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002262 item = btrfs_item_nr(leaf, slot);
2263 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2264
2265 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04002266 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002267 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002268 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002269 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04002270 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002271
2272 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01002273
Chris Mason39279cc2007-06-12 06:35:45 -04002274 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2275 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002276 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01002277
2278 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04002279 struct btrfs_key location;
2280
2281 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01002282 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002283 name_ptr = tmp_name;
2284 } else {
2285 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01002286 if (!name_ptr) {
2287 ret = -ENOMEM;
2288 goto err;
2289 }
Chris Mason5f39d392007-10-15 16:14:19 -04002290 }
2291 read_extent_buffer(leaf, name_ptr,
2292 (unsigned long)(di + 1), name_len);
2293
2294 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2295 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002296 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01002297 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002298 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002299
2300 if (name_ptr != tmp_name)
2301 kfree(name_ptr);
2302
Chris Mason39279cc2007-06-12 06:35:45 -04002303 if (over)
2304 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002305
Josef Bacik5103e942007-11-16 11:45:54 -05002306 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01002307 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002308 di_cur += di_len;
2309 di = (struct btrfs_dir_item *)((char *)di + di_len);
2310 }
2311 }
David Woodhouse49593bf2008-08-17 17:08:36 +01002312
2313 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05002314 if (key_type == BTRFS_DIR_INDEX_KEY)
2315 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2316 else
2317 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002318nopos:
2319 ret = 0;
2320err:
Chris Mason39279cc2007-06-12 06:35:45 -04002321 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002322 return ret;
2323}
2324
2325int btrfs_write_inode(struct inode *inode, int wait)
2326{
2327 struct btrfs_root *root = BTRFS_I(inode)->root;
2328 struct btrfs_trans_handle *trans;
2329 int ret = 0;
2330
Chris Mason4ca8b412008-08-05 13:30:48 -04002331 if (root->fs_info->closing > 1)
2332 return 0;
2333
Chris Mason39279cc2007-06-12 06:35:45 -04002334 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002335 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002336 btrfs_set_trans_block_group(trans, inode);
2337 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002338 }
2339 return ret;
2340}
2341
2342/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002343 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002344 * inode changes. But, it is most likely to find the inode in cache.
2345 * FIXME, needs more benchmarking...there are no reasons other than performance
2346 * to keep or drop this code.
2347 */
2348void btrfs_dirty_inode(struct inode *inode)
2349{
2350 struct btrfs_root *root = BTRFS_I(inode)->root;
2351 struct btrfs_trans_handle *trans;
2352
Chris Masonf9295742008-07-17 12:54:14 -04002353 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002354 btrfs_set_trans_block_group(trans, inode);
2355 btrfs_update_inode(trans, root, inode);
2356 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002357}
2358
Chris Masond352ac62008-09-29 15:18:18 -04002359/*
2360 * find the highest existing sequence number in a directory
2361 * and then set the in-memory index_cnt variable to reflect
2362 * free sequence numbers
2363 */
Josef Bacikaec74772008-07-24 12:12:38 -04002364static int btrfs_set_inode_index_count(struct inode *inode)
2365{
2366 struct btrfs_root *root = BTRFS_I(inode)->root;
2367 struct btrfs_key key, found_key;
2368 struct btrfs_path *path;
2369 struct extent_buffer *leaf;
2370 int ret;
2371
2372 key.objectid = inode->i_ino;
2373 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2374 key.offset = (u64)-1;
2375
2376 path = btrfs_alloc_path();
2377 if (!path)
2378 return -ENOMEM;
2379
2380 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2381 if (ret < 0)
2382 goto out;
2383 /* FIXME: we should be able to handle this */
2384 if (ret == 0)
2385 goto out;
2386 ret = 0;
2387
2388 /*
2389 * MAGIC NUMBER EXPLANATION:
2390 * since we search a directory based on f_pos we have to start at 2
2391 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2392 * else has to start at 2
2393 */
2394 if (path->slots[0] == 0) {
2395 BTRFS_I(inode)->index_cnt = 2;
2396 goto out;
2397 }
2398
2399 path->slots[0]--;
2400
2401 leaf = path->nodes[0];
2402 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2403
2404 if (found_key.objectid != inode->i_ino ||
2405 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2406 BTRFS_I(inode)->index_cnt = 2;
2407 goto out;
2408 }
2409
2410 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2411out:
2412 btrfs_free_path(path);
2413 return ret;
2414}
2415
Chris Masond352ac62008-09-29 15:18:18 -04002416/*
2417 * helper to find a free sequence number in a given directory. This current
2418 * code is very simple, later versions will do smarter things in the btree
2419 */
Chris Mason00e4e6b2008-08-05 11:18:09 -04002420static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2421 u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04002422{
2423 int ret = 0;
2424
2425 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2426 ret = btrfs_set_inode_index_count(dir);
Chris Mason8d5bf1c2008-09-11 15:51:21 -04002427 if (ret) {
Josef Bacikaec74772008-07-24 12:12:38 -04002428 return ret;
Chris Mason8d5bf1c2008-09-11 15:51:21 -04002429 }
Josef Bacikaec74772008-07-24 12:12:38 -04002430 }
2431
Chris Mason00e4e6b2008-08-05 11:18:09 -04002432 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04002433 BTRFS_I(dir)->index_cnt++;
2434
2435 return ret;
2436}
2437
Chris Mason39279cc2007-06-12 06:35:45 -04002438static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2439 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002440 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002441 const char *name, int name_len,
2442 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002443 u64 objectid,
2444 struct btrfs_block_group_cache *group,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002445 int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04002446{
2447 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002448 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002449 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002450 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002451 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002452 struct btrfs_inode_ref *ref;
2453 struct btrfs_key key[2];
2454 u32 sizes[2];
2455 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002456 int ret;
2457 int owner;
2458
Chris Mason5f39d392007-10-15 16:14:19 -04002459 path = btrfs_alloc_path();
2460 BUG_ON(!path);
2461
Chris Mason39279cc2007-06-12 06:35:45 -04002462 inode = new_inode(root->fs_info->sb);
2463 if (!inode)
2464 return ERR_PTR(-ENOMEM);
2465
Josef Bacikaec74772008-07-24 12:12:38 -04002466 if (dir) {
Chris Mason00e4e6b2008-08-05 11:18:09 -04002467 ret = btrfs_set_inode_index(dir, inode, index);
Josef Bacikaec74772008-07-24 12:12:38 -04002468 if (ret)
2469 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002470 }
2471 /*
2472 * index_cnt is ignored for everything but a dir,
2473 * btrfs_get_inode_index_count has an explanation for the magic
2474 * number
2475 */
Chris Masone02119d2008-09-05 16:13:11 -04002476 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04002477 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04002478 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04002479 BTRFS_I(inode)->generation = trans->transid;
Chris Masonb888db22007-08-27 16:49:44 -04002480
Chris Mason39279cc2007-06-12 06:35:45 -04002481 if (mode & S_IFDIR)
2482 owner = 0;
2483 else
2484 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002485 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002486 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002487 if (!new_inode_group) {
2488 printk("find_block group failed\n");
2489 new_inode_group = group;
2490 }
2491 BTRFS_I(inode)->block_group = new_inode_group;
Chris Mason9c583092008-01-29 15:15:18 -05002492
2493 key[0].objectid = objectid;
2494 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2495 key[0].offset = 0;
2496
2497 key[1].objectid = objectid;
2498 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2499 key[1].offset = ref_objectid;
2500
2501 sizes[0] = sizeof(struct btrfs_inode_item);
2502 sizes[1] = name_len + sizeof(*ref);
2503
2504 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2505 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002506 goto fail;
2507
Chris Mason9c583092008-01-29 15:15:18 -05002508 if (objectid > root->highest_inode)
2509 root->highest_inode = objectid;
2510
Chris Mason39279cc2007-06-12 06:35:45 -04002511 inode->i_uid = current->fsuid;
2512 inode->i_gid = current->fsgid;
2513 inode->i_mode = mode;
2514 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002515 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002516 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002517 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2518 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002519 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002520
2521 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2522 struct btrfs_inode_ref);
2523 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002524 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05002525 ptr = (unsigned long)(ref + 1);
2526 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2527
Chris Mason5f39d392007-10-15 16:14:19 -04002528 btrfs_mark_buffer_dirty(path->nodes[0]);
2529 btrfs_free_path(path);
2530
Chris Mason39279cc2007-06-12 06:35:45 -04002531 location = &BTRFS_I(inode)->location;
2532 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002533 location->offset = 0;
2534 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2535
Chris Mason39279cc2007-06-12 06:35:45 -04002536 insert_inode_hash(inode);
2537 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002538fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002539 if (dir)
2540 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002541 btrfs_free_path(path);
2542 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002543}
2544
2545static inline u8 btrfs_inode_type(struct inode *inode)
2546{
2547 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2548}
2549
Chris Masond352ac62008-09-29 15:18:18 -04002550/*
2551 * utility function to add 'inode' into 'parent_inode' with
2552 * a give name and a given sequence number.
2553 * if 'add_backref' is true, also insert a backref from the
2554 * inode to the parent directory.
2555 */
Chris Masone02119d2008-09-05 16:13:11 -04002556int btrfs_add_link(struct btrfs_trans_handle *trans,
2557 struct inode *parent_inode, struct inode *inode,
2558 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002559{
2560 int ret;
2561 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002562 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04002563
Chris Mason39279cc2007-06-12 06:35:45 -04002564 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002565 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2566 key.offset = 0;
2567
Chris Masone02119d2008-09-05 16:13:11 -04002568 ret = btrfs_insert_dir_item(trans, root, name, name_len,
2569 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002570 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04002571 index);
Chris Mason39279cc2007-06-12 06:35:45 -04002572 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002573 if (add_backref) {
2574 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04002575 name, name_len,
2576 inode->i_ino,
2577 parent_inode->i_ino,
2578 index);
Chris Mason9c583092008-01-29 15:15:18 -05002579 }
Chris Masondbe674a2008-07-17 12:54:05 -04002580 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04002581 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002582 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04002583 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002584 }
2585 return ret;
2586}
2587
2588static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002589 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002590 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002591{
Chris Masone02119d2008-09-05 16:13:11 -04002592 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2593 inode, dentry->d_name.name,
2594 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002595 if (!err) {
2596 d_instantiate(dentry, inode);
2597 return 0;
2598 }
2599 if (err > 0)
2600 err = -EEXIST;
2601 return err;
2602}
2603
Josef Bacik618e21d2007-07-11 10:18:17 -04002604static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2605 int mode, dev_t rdev)
2606{
2607 struct btrfs_trans_handle *trans;
2608 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002609 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002610 int err;
2611 int drop_inode = 0;
2612 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002613 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002614 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002615
2616 if (!new_valid_dev(rdev))
2617 return -EINVAL;
2618
Chris Mason1832a6d2007-12-21 16:27:21 -05002619 err = btrfs_check_free_space(root, 1, 0);
2620 if (err)
2621 goto fail;
2622
Josef Bacik618e21d2007-07-11 10:18:17 -04002623 trans = btrfs_start_transaction(root, 1);
2624 btrfs_set_trans_block_group(trans, dir);
2625
2626 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2627 if (err) {
2628 err = -ENOSPC;
2629 goto out_unlock;
2630 }
2631
Josef Bacikaec74772008-07-24 12:12:38 -04002632 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002633 dentry->d_name.len,
2634 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002635 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002636 err = PTR_ERR(inode);
2637 if (IS_ERR(inode))
2638 goto out_unlock;
2639
Josef Bacik33268ea2008-07-24 12:16:36 -04002640 err = btrfs_init_acl(inode, dir);
2641 if (err) {
2642 drop_inode = 1;
2643 goto out_unlock;
2644 }
2645
Josef Bacik618e21d2007-07-11 10:18:17 -04002646 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002647 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002648 if (err)
2649 drop_inode = 1;
2650 else {
2651 inode->i_op = &btrfs_special_inode_operations;
2652 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002653 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002654 }
2655 dir->i_sb->s_dirt = 1;
2656 btrfs_update_inode_block_group(trans, inode);
2657 btrfs_update_inode_block_group(trans, dir);
2658out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002659 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002660 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002661fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002662 if (drop_inode) {
2663 inode_dec_link_count(inode);
2664 iput(inode);
2665 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002666 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002667 return err;
2668}
2669
Chris Mason39279cc2007-06-12 06:35:45 -04002670static int btrfs_create(struct inode *dir, struct dentry *dentry,
2671 int mode, struct nameidata *nd)
2672{
2673 struct btrfs_trans_handle *trans;
2674 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002675 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002676 int err;
2677 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002678 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002679 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002680 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002681
Chris Mason1832a6d2007-12-21 16:27:21 -05002682 err = btrfs_check_free_space(root, 1, 0);
2683 if (err)
2684 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002685 trans = btrfs_start_transaction(root, 1);
2686 btrfs_set_trans_block_group(trans, dir);
2687
2688 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2689 if (err) {
2690 err = -ENOSPC;
2691 goto out_unlock;
2692 }
2693
Josef Bacikaec74772008-07-24 12:12:38 -04002694 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002695 dentry->d_name.len,
2696 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002697 objectid, BTRFS_I(dir)->block_group, mode,
2698 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002699 err = PTR_ERR(inode);
2700 if (IS_ERR(inode))
2701 goto out_unlock;
2702
Josef Bacik33268ea2008-07-24 12:16:36 -04002703 err = btrfs_init_acl(inode, dir);
2704 if (err) {
2705 drop_inode = 1;
2706 goto out_unlock;
2707 }
2708
Chris Mason39279cc2007-06-12 06:35:45 -04002709 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002710 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002711 if (err)
2712 drop_inode = 1;
2713 else {
2714 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002715 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002716 inode->i_fop = &btrfs_file_operations;
2717 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002718 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002719 }
2720 dir->i_sb->s_dirt = 1;
2721 btrfs_update_inode_block_group(trans, inode);
2722 btrfs_update_inode_block_group(trans, dir);
2723out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002724 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002725 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002726fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002727 if (drop_inode) {
2728 inode_dec_link_count(inode);
2729 iput(inode);
2730 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002731 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002732 return err;
2733}
2734
2735static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2736 struct dentry *dentry)
2737{
2738 struct btrfs_trans_handle *trans;
2739 struct btrfs_root *root = BTRFS_I(dir)->root;
2740 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002741 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05002742 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002743 int err;
2744 int drop_inode = 0;
2745
2746 if (inode->i_nlink == 0)
2747 return -ENOENT;
2748
Chris Masone02119d2008-09-05 16:13:11 -04002749 btrfs_inc_nlink(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05002750 err = btrfs_check_free_space(root, 1, 0);
2751 if (err)
2752 goto fail;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002753 err = btrfs_set_inode_index(dir, inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002754 if (err)
2755 goto fail;
2756
Chris Mason39279cc2007-06-12 06:35:45 -04002757 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002758
Chris Mason39279cc2007-06-12 06:35:45 -04002759 btrfs_set_trans_block_group(trans, dir);
2760 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002761
Chris Mason00e4e6b2008-08-05 11:18:09 -04002762 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04002763
Chris Mason39279cc2007-06-12 06:35:45 -04002764 if (err)
2765 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002766
Chris Mason39279cc2007-06-12 06:35:45 -04002767 dir->i_sb->s_dirt = 1;
2768 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002769 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002770
Chris Mason54aa1f42007-06-22 14:16:25 -04002771 if (err)
2772 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002773
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002774 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002775 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002776fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002777 if (drop_inode) {
2778 inode_dec_link_count(inode);
2779 iput(inode);
2780 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002781 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002782 return err;
2783}
2784
Chris Mason39279cc2007-06-12 06:35:45 -04002785static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2786{
Chris Masonb9d86662008-05-02 16:13:49 -04002787 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002788 struct btrfs_trans_handle *trans;
2789 struct btrfs_root *root = BTRFS_I(dir)->root;
2790 int err = 0;
2791 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002792 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002793 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002794 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002795
Chris Mason1832a6d2007-12-21 16:27:21 -05002796 err = btrfs_check_free_space(root, 1, 0);
2797 if (err)
2798 goto out_unlock;
2799
Chris Mason39279cc2007-06-12 06:35:45 -04002800 trans = btrfs_start_transaction(root, 1);
2801 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002802
Chris Mason39279cc2007-06-12 06:35:45 -04002803 if (IS_ERR(trans)) {
2804 err = PTR_ERR(trans);
2805 goto out_unlock;
2806 }
2807
2808 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2809 if (err) {
2810 err = -ENOSPC;
2811 goto out_unlock;
2812 }
2813
Josef Bacikaec74772008-07-24 12:12:38 -04002814 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002815 dentry->d_name.len,
2816 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002817 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2818 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002819 if (IS_ERR(inode)) {
2820 err = PTR_ERR(inode);
2821 goto out_fail;
2822 }
Chris Mason5f39d392007-10-15 16:14:19 -04002823
Chris Mason39279cc2007-06-12 06:35:45 -04002824 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002825
2826 err = btrfs_init_acl(inode, dir);
2827 if (err)
2828 goto out_fail;
2829
Chris Mason39279cc2007-06-12 06:35:45 -04002830 inode->i_op = &btrfs_dir_inode_operations;
2831 inode->i_fop = &btrfs_dir_file_operations;
2832 btrfs_set_trans_block_group(trans, inode);
2833
Chris Masondbe674a2008-07-17 12:54:05 -04002834 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002835 err = btrfs_update_inode(trans, root, inode);
2836 if (err)
2837 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002838
Chris Masone02119d2008-09-05 16:13:11 -04002839 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2840 inode, dentry->d_name.name,
2841 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002842 if (err)
2843 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002844
Chris Mason39279cc2007-06-12 06:35:45 -04002845 d_instantiate(dentry, inode);
2846 drop_on_err = 0;
2847 dir->i_sb->s_dirt = 1;
2848 btrfs_update_inode_block_group(trans, inode);
2849 btrfs_update_inode_block_group(trans, dir);
2850
2851out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002852 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002853 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002854
Chris Mason39279cc2007-06-12 06:35:45 -04002855out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002856 if (drop_on_err)
2857 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002858 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002859 return err;
2860}
2861
Chris Masond352ac62008-09-29 15:18:18 -04002862/* helper for btfs_get_extent. Given an existing extent in the tree,
2863 * and an extent that you want to insert, deal with overlap and insert
2864 * the new extent into the tree.
2865 */
Chris Mason3b951512008-04-17 11:29:12 -04002866static int merge_extent_mapping(struct extent_map_tree *em_tree,
2867 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002868 struct extent_map *em,
2869 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002870{
2871 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002872
Chris Masone6dcd2d2008-07-17 12:53:50 -04002873 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2874 start_diff = map_start - em->start;
2875 em->start = map_start;
2876 em->len = map_len;
2877 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2878 em->block_start += start_diff;
2879 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002880}
2881
Chris Masond352ac62008-09-29 15:18:18 -04002882/*
2883 * a bit scary, this does extent mapping from logical file offset to the disk.
2884 * the ugly parts come from merging extents from the disk with the
2885 * in-ram representation. This gets more complex because of the data=ordered code,
2886 * where the in-ram extents might be locked pending data=ordered completion.
2887 *
2888 * This also copies inline extents directly into the page.
2889 */
Chris Masona52d9a82007-08-27 16:49:44 -04002890struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002891 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002892 int create)
2893{
2894 int ret;
2895 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002896 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002897 u64 extent_start = 0;
2898 u64 extent_end = 0;
2899 u64 objectid = inode->i_ino;
2900 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002901 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002902 struct btrfs_root *root = BTRFS_I(inode)->root;
2903 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002904 struct extent_buffer *leaf;
2905 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002906 struct extent_map *em = NULL;
2907 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002908 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002909 struct btrfs_trans_handle *trans = NULL;
2910
Chris Masona52d9a82007-08-27 16:49:44 -04002911again:
Chris Masond1310b22008-01-24 16:13:08 -05002912 spin_lock(&em_tree->lock);
2913 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002914 if (em)
2915 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002916 spin_unlock(&em_tree->lock);
2917
Chris Masona52d9a82007-08-27 16:49:44 -04002918 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002919 if (em->start > start || em->start + em->len <= start)
2920 free_extent_map(em);
2921 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002922 free_extent_map(em);
2923 else
2924 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002925 }
Chris Masond1310b22008-01-24 16:13:08 -05002926 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002927 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002928 err = -ENOMEM;
2929 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002930 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002931 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002932 em->start = EXTENT_MAP_HOLE;
2933 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002934
2935 if (!path) {
2936 path = btrfs_alloc_path();
2937 BUG_ON(!path);
2938 }
2939
Chris Mason179e29e2007-11-01 11:28:41 -04002940 ret = btrfs_lookup_file_extent(trans, root, path,
2941 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002942 if (ret < 0) {
2943 err = ret;
2944 goto out;
2945 }
2946
2947 if (ret != 0) {
2948 if (path->slots[0] == 0)
2949 goto not_found;
2950 path->slots[0]--;
2951 }
2952
Chris Mason5f39d392007-10-15 16:14:19 -04002953 leaf = path->nodes[0];
2954 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002955 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002956 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002957 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2958 found_type = btrfs_key_type(&found_key);
2959 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002960 found_type != BTRFS_EXTENT_DATA_KEY) {
2961 goto not_found;
2962 }
2963
Chris Mason5f39d392007-10-15 16:14:19 -04002964 found_type = btrfs_file_extent_type(leaf, item);
2965 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002966 if (found_type == BTRFS_FILE_EXTENT_REG) {
2967 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002968 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002969 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002970 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002971 em->start = start;
2972 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002973 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002974 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002975 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002976 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002977 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002978 }
2979 goto not_found_em;
2980 }
Chris Masondb945352007-10-15 16:15:53 -04002981 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2982 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002983 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002984 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002985 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002986 goto insert;
2987 }
Chris Masondb945352007-10-15 16:15:53 -04002988 bytenr += btrfs_file_extent_offset(leaf, item);
2989 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002990 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002991 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002992 goto insert;
2993 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002994 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002995 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002996 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002997 size_t size;
2998 size_t extent_offset;
2999 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04003000
Chris Mason5f39d392007-10-15 16:14:19 -04003001 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
3002 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05003003 extent_end = (extent_start + size + root->sectorsize - 1) &
3004 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04003005 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04003006 em->start = start;
3007 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05003008 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04003009 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05003010 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04003011 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003012 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04003013 }
3014 goto not_found_em;
3015 }
3016 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04003017
3018 if (!page) {
3019 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05003020 em->len = size;
Yan689f9342007-10-29 11:41:07 -04003021 goto out;
3022 }
3023
Chris Mason70dec802008-01-29 09:59:12 -05003024 page_start = page_offset(page) + pg_offset;
3025 extent_offset = page_start - extent_start;
3026 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04003027 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04003028 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05003029 em->len = (copy_size + root->sectorsize - 1) &
3030 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04003031 map = kmap(page);
3032 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04003033 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05003034 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04003035 copy_size);
3036 flush_dcache_page(page);
3037 } else if (create && PageUptodate(page)) {
3038 if (!trans) {
3039 kunmap(page);
3040 free_extent_map(em);
3041 em = NULL;
3042 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04003043 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04003044 goto again;
3045 }
Chris Mason70dec802008-01-29 09:59:12 -05003046 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04003047 copy_size);
3048 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04003049 }
Chris Masona52d9a82007-08-27 16:49:44 -04003050 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05003051 set_extent_uptodate(io_tree, em->start,
3052 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003053 goto insert;
3054 } else {
3055 printk("unkknown found_type %d\n", found_type);
3056 WARN_ON(1);
3057 }
3058not_found:
3059 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05003060 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04003061not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04003062 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04003063insert:
3064 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05003065 if (em->start > start || extent_map_end(em) <= start) {
3066 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04003067 err = -EIO;
3068 goto out;
3069 }
Chris Masond1310b22008-01-24 16:13:08 -05003070
3071 err = 0;
3072 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04003073 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003074 /* it is possible that someone inserted the extent into the tree
3075 * while we had the lock dropped. It is also possible that
3076 * an overlapping map exists in the tree
3077 */
Chris Masona52d9a82007-08-27 16:49:44 -04003078 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04003079 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003080
3081 ret = 0;
3082
Chris Mason3b951512008-04-17 11:29:12 -04003083 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04003084 if (existing && (existing->start > start ||
3085 existing->start + existing->len <= start)) {
3086 free_extent_map(existing);
3087 existing = NULL;
3088 }
Chris Mason3b951512008-04-17 11:29:12 -04003089 if (!existing) {
3090 existing = lookup_extent_mapping(em_tree, em->start,
3091 em->len);
3092 if (existing) {
3093 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003094 em, start,
3095 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04003096 free_extent_map(existing);
3097 if (err) {
3098 free_extent_map(em);
3099 em = NULL;
3100 }
3101 } else {
3102 err = -EIO;
3103 printk("failing to insert %Lu %Lu\n",
3104 start, len);
3105 free_extent_map(em);
3106 em = NULL;
3107 }
3108 } else {
3109 free_extent_map(em);
3110 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003111 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04003112 }
Chris Masona52d9a82007-08-27 16:49:44 -04003113 }
Chris Masond1310b22008-01-24 16:13:08 -05003114 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04003115out:
Chris Masonf4219502008-07-22 11:18:09 -04003116 if (path)
3117 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04003118 if (trans) {
3119 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003120 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04003121 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003122 }
Chris Masona52d9a82007-08-27 16:49:44 -04003123 }
Chris Masona52d9a82007-08-27 16:49:44 -04003124 if (err) {
3125 free_extent_map(em);
3126 WARN_ON(1);
3127 return ERR_PTR(err);
3128 }
3129 return em;
3130}
3131
Chris Mason16432982008-04-10 10:23:21 -04003132static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
3133 const struct iovec *iov, loff_t offset,
3134 unsigned long nr_segs)
3135{
Chris Masone1c4b742008-04-22 13:26:46 -04003136 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04003137}
3138
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04003139static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04003140{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04003141 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04003142}
3143
Chris Mason9ebefb182007-06-15 13:50:00 -04003144int btrfs_readpage(struct file *file, struct page *page)
3145{
Chris Masond1310b22008-01-24 16:13:08 -05003146 struct extent_io_tree *tree;
3147 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003148 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04003149}
Chris Mason1832a6d2007-12-21 16:27:21 -05003150
Chris Mason39279cc2007-06-12 06:35:45 -04003151static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3152{
Chris Masond1310b22008-01-24 16:13:08 -05003153 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04003154
3155
3156 if (current->flags & PF_MEMALLOC) {
3157 redirty_page_for_writepage(wbc, page);
3158 unlock_page(page);
3159 return 0;
3160 }
Chris Masond1310b22008-01-24 16:13:08 -05003161 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003162 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3163}
Chris Mason39279cc2007-06-12 06:35:45 -04003164
Chris Masonf4219502008-07-22 11:18:09 -04003165int btrfs_writepages(struct address_space *mapping,
3166 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04003167{
Chris Masond1310b22008-01-24 16:13:08 -05003168 struct extent_io_tree *tree;
3169 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04003170 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3171}
3172
Chris Mason3ab2fb52007-11-08 10:59:22 -05003173static int
3174btrfs_readpages(struct file *file, struct address_space *mapping,
3175 struct list_head *pages, unsigned nr_pages)
3176{
Chris Masond1310b22008-01-24 16:13:08 -05003177 struct extent_io_tree *tree;
3178 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05003179 return extent_readpages(tree, mapping, pages, nr_pages,
3180 btrfs_get_extent);
3181}
Chris Masone6dcd2d2008-07-17 12:53:50 -04003182static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04003183{
Chris Masond1310b22008-01-24 16:13:08 -05003184 struct extent_io_tree *tree;
3185 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04003186 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003187
Chris Masond1310b22008-01-24 16:13:08 -05003188 tree = &BTRFS_I(page->mapping->host)->io_tree;
3189 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05003190 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003191 if (ret == 1) {
3192 ClearPagePrivate(page);
3193 set_page_private(page, 0);
3194 page_cache_release(page);
3195 }
3196 return ret;
3197}
Chris Mason39279cc2007-06-12 06:35:45 -04003198
Chris Masone6dcd2d2008-07-17 12:53:50 -04003199static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3200{
Chris Mason98509cf2008-09-11 15:51:43 -04003201 if (PageWriteback(page) || PageDirty(page))
3202 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003203 return __btrfs_releasepage(page, gfp_flags);
3204}
3205
Chris Masona52d9a82007-08-27 16:49:44 -04003206static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3207{
Chris Masond1310b22008-01-24 16:13:08 -05003208 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003209 struct btrfs_ordered_extent *ordered;
3210 u64 page_start = page_offset(page);
3211 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003212
Chris Masone6dcd2d2008-07-17 12:53:50 -04003213 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003214 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003215 if (offset) {
3216 btrfs_releasepage(page, GFP_NOFS);
3217 return;
3218 }
3219
3220 lock_extent(tree, page_start, page_end, GFP_NOFS);
3221 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3222 page_offset(page));
3223 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003224 /*
3225 * IO on this page will never be started, so we need
3226 * to account for any ordered extents now
3227 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003228 clear_extent_bit(tree, page_start, page_end,
3229 EXTENT_DIRTY | EXTENT_DELALLOC |
3230 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003231 btrfs_finish_ordered_io(page->mapping->host,
3232 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003233 btrfs_put_ordered_extent(ordered);
3234 lock_extent(tree, page_start, page_end, GFP_NOFS);
3235 }
3236 clear_extent_bit(tree, page_start, page_end,
3237 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3238 EXTENT_ORDERED,
3239 1, 1, GFP_NOFS);
3240 __btrfs_releasepage(page, GFP_NOFS);
3241
Chris Mason4a096752008-07-21 10:29:44 -04003242 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003243 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003244 ClearPagePrivate(page);
3245 set_page_private(page, 0);
3246 page_cache_release(page);
3247 }
Chris Mason39279cc2007-06-12 06:35:45 -04003248}
3249
Chris Mason9ebefb182007-06-15 13:50:00 -04003250/*
3251 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3252 * called from a page fault handler when a page is first dirtied. Hence we must
3253 * be careful to check for EOF conditions here. We set the page up correctly
3254 * for a written page which means we get ENOSPC checking when writing into
3255 * holes and correct delalloc and unwritten extent mapping on filesystems that
3256 * support these features.
3257 *
3258 * We are not allowed to take the i_mutex here so we have to play games to
3259 * protect against truncate races as the page could now be beyond EOF. Because
3260 * vmtruncate() writes the inode size before removing pages, once we have the
3261 * page lock we can determine safely if the page is beyond EOF. If it is not
3262 * beyond EOF, then the page is guaranteed safe against truncation until we
3263 * unlock the page.
3264 */
3265int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3266{
Chris Mason6da6aba2007-12-18 16:15:09 -05003267 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003268 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003269 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3270 struct btrfs_ordered_extent *ordered;
3271 char *kaddr;
3272 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003273 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003274 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003275 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003276 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003277
Chris Mason1832a6d2007-12-21 16:27:21 -05003278 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003279 if (ret)
3280 goto out;
3281
3282 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003283again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003284 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003285 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003286 page_start = page_offset(page);
3287 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003288
Chris Mason9ebefb182007-06-15 13:50:00 -04003289 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003290 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003291 /* page got truncated out from underneath us */
3292 goto out_unlock;
3293 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003294 wait_on_page_writeback(page);
3295
3296 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3297 set_page_extent_mapped(page);
3298
Chris Masoneb84ae02008-07-17 13:53:27 -04003299 /*
3300 * we can't set the delalloc bits if there are pending ordered
3301 * extents. Drop our locks and wait for them to finish
3302 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003303 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3304 if (ordered) {
3305 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3306 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003307 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003308 btrfs_put_ordered_extent(ordered);
3309 goto again;
3310 }
3311
Chris Masonea8c2812008-08-04 23:17:27 -04003312 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003313 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003314
3315 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003316 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003317 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003318 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003319 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003320
Chris Masone6dcd2d2008-07-17 12:53:50 -04003321 if (zero_start != PAGE_CACHE_SIZE) {
3322 kaddr = kmap(page);
3323 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3324 flush_dcache_page(page);
3325 kunmap(page);
3326 }
Chris Mason247e7432008-07-17 12:53:51 -04003327 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003328 set_page_dirty(page);
3329 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003330
3331out_unlock:
3332 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003333out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003334 return ret;
3335}
3336
Chris Mason39279cc2007-06-12 06:35:45 -04003337static void btrfs_truncate(struct inode *inode)
3338{
3339 struct btrfs_root *root = BTRFS_I(inode)->root;
3340 int ret;
3341 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003342 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003343 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003344
3345 if (!S_ISREG(inode->i_mode))
3346 return;
3347 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3348 return;
3349
3350 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003351 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003352
Chris Mason39279cc2007-06-12 06:35:45 -04003353 trans = btrfs_start_transaction(root, 1);
3354 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003355 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003356
Josef Bacik7b128762008-07-24 12:17:14 -04003357 ret = btrfs_orphan_add(trans, inode);
3358 if (ret)
3359 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003360 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04003361 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05003362 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003363 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003364
Josef Bacik7b128762008-07-24 12:17:14 -04003365 ret = btrfs_orphan_del(trans, inode);
3366 BUG_ON(ret);
3367
3368out:
3369 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003370 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003371 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003372 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003373}
3374
Sven Wegener3b963622008-06-09 21:57:42 -04003375/*
3376 * Invalidate a single dcache entry at the root of the filesystem.
3377 * Needed after creation of snapshot or subvolume.
3378 */
3379void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3380 int namelen)
3381{
3382 struct dentry *alias, *entry;
3383 struct qstr qstr;
3384
3385 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3386 if (alias) {
3387 qstr.name = name;
3388 qstr.len = namelen;
3389 /* change me if btrfs ever gets a d_hash operation */
3390 qstr.hash = full_name_hash(qstr.name, qstr.len);
3391 entry = d_lookup(alias, &qstr);
3392 dput(alias);
3393 if (entry) {
3394 d_invalidate(entry);
3395 dput(entry);
3396 }
3397 }
3398}
3399
Chris Masond352ac62008-09-29 15:18:18 -04003400/*
3401 * create a new subvolume directory/inode (helper for the ioctl).
3402 */
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003403int btrfs_create_subvol_root(struct btrfs_root *new_root,
3404 struct btrfs_trans_handle *trans, u64 new_dirid,
3405 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003406{
Chris Mason39279cc2007-06-12 06:35:45 -04003407 struct inode *inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003408 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003409
Josef Bacikaec74772008-07-24 12:12:38 -04003410 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003411 new_dirid, block_group, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04003412 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003413 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003414 inode->i_op = &btrfs_dir_inode_operations;
3415 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003416 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003417
Chris Mason39279cc2007-06-12 06:35:45 -04003418 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003419 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003420
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003421 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003422}
3423
Chris Masond352ac62008-09-29 15:18:18 -04003424/* helper function for file defrag and space balancing. This
3425 * forces readahead on a given range of bytes in an inode
3426 */
Chris Masonedbd8d42007-12-21 16:27:24 -05003427unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003428 struct file_ra_state *ra, struct file *file,
3429 pgoff_t offset, pgoff_t last_index)
3430{
Chris Mason8e7bf942008-04-28 09:02:36 -04003431 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003432
Chris Mason86479a02007-09-10 19:58:16 -04003433 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3434 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04003435}
3436
Chris Mason39279cc2007-06-12 06:35:45 -04003437struct inode *btrfs_alloc_inode(struct super_block *sb)
3438{
3439 struct btrfs_inode *ei;
3440
3441 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3442 if (!ei)
3443 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003444 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003445 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003446 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003447 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3448 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003449 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003450 return &ei->vfs_inode;
3451}
3452
3453void btrfs_destroy_inode(struct inode *inode)
3454{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003455 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003456 WARN_ON(!list_empty(&inode->i_dentry));
3457 WARN_ON(inode->i_data.nrpages);
3458
Josef Bacik33268ea2008-07-24 12:16:36 -04003459 if (BTRFS_I(inode)->i_acl &&
3460 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3461 posix_acl_release(BTRFS_I(inode)->i_acl);
3462 if (BTRFS_I(inode)->i_default_acl &&
3463 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3464 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3465
Yanbcc63ab2008-07-30 16:29:20 -04003466 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003467 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3468 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3469 " list\n", inode->i_ino);
3470 dump_stack();
3471 }
Yanbcc63ab2008-07-30 16:29:20 -04003472 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003473
Chris Masone6dcd2d2008-07-17 12:53:50 -04003474 while(1) {
3475 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3476 if (!ordered)
3477 break;
3478 else {
3479 printk("found ordered extent %Lu %Lu\n",
3480 ordered->file_offset, ordered->len);
3481 btrfs_remove_ordered_extent(inode, ordered);
3482 btrfs_put_ordered_extent(ordered);
3483 btrfs_put_ordered_extent(ordered);
3484 }
3485 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003486 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003487 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3488}
3489
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003490static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04003491{
3492 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3493
3494 inode_init_once(&ei->vfs_inode);
3495}
3496
3497void btrfs_destroy_cachep(void)
3498{
3499 if (btrfs_inode_cachep)
3500 kmem_cache_destroy(btrfs_inode_cachep);
3501 if (btrfs_trans_handle_cachep)
3502 kmem_cache_destroy(btrfs_trans_handle_cachep);
3503 if (btrfs_transaction_cachep)
3504 kmem_cache_destroy(btrfs_transaction_cachep);
3505 if (btrfs_bit_radix_cachep)
3506 kmem_cache_destroy(btrfs_bit_radix_cachep);
3507 if (btrfs_path_cachep)
3508 kmem_cache_destroy(btrfs_path_cachep);
3509}
3510
Chris Mason86479a02007-09-10 19:58:16 -04003511struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003512 unsigned long extra_flags,
Chris Mason2b1f55b2008-09-24 11:48:04 -04003513 void (*ctor)(void *))
Chris Mason92fee662007-07-25 12:31:35 -04003514{
3515 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
Chris Mason2b1f55b2008-09-24 11:48:04 -04003516 SLAB_MEM_SPREAD | extra_flags), ctor);
Chris Mason92fee662007-07-25 12:31:35 -04003517}
3518
Chris Mason39279cc2007-06-12 06:35:45 -04003519int btrfs_init_cachep(void)
3520{
Chris Mason86479a02007-09-10 19:58:16 -04003521 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003522 sizeof(struct btrfs_inode),
3523 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003524 if (!btrfs_inode_cachep)
3525 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003526 btrfs_trans_handle_cachep =
3527 btrfs_cache_create("btrfs_trans_handle_cache",
3528 sizeof(struct btrfs_trans_handle),
3529 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003530 if (!btrfs_trans_handle_cachep)
3531 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003532 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003533 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003534 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003535 if (!btrfs_transaction_cachep)
3536 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003537 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003538 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003539 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003540 if (!btrfs_path_cachep)
3541 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003542 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003543 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003544 if (!btrfs_bit_radix_cachep)
3545 goto fail;
3546 return 0;
3547fail:
3548 btrfs_destroy_cachep();
3549 return -ENOMEM;
3550}
3551
3552static int btrfs_getattr(struct vfsmount *mnt,
3553 struct dentry *dentry, struct kstat *stat)
3554{
3555 struct inode *inode = dentry->d_inode;
3556 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003557 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003558 stat->blocks = (inode_get_bytes(inode) +
3559 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04003560 return 0;
3561}
3562
3563static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3564 struct inode * new_dir,struct dentry *new_dentry)
3565{
3566 struct btrfs_trans_handle *trans;
3567 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3568 struct inode *new_inode = new_dentry->d_inode;
3569 struct inode *old_inode = old_dentry->d_inode;
3570 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003571 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003572 int ret;
3573
3574 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3575 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3576 return -ENOTEMPTY;
3577 }
Chris Mason5f39d392007-10-15 16:14:19 -04003578
Chris Mason1832a6d2007-12-21 16:27:21 -05003579 ret = btrfs_check_free_space(root, 1, 0);
3580 if (ret)
3581 goto out_unlock;
3582
Chris Mason39279cc2007-06-12 06:35:45 -04003583 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003584
Chris Mason39279cc2007-06-12 06:35:45 -04003585 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003586
Chris Masone02119d2008-09-05 16:13:11 -04003587 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003588 old_dir->i_ctime = old_dir->i_mtime = ctime;
3589 new_dir->i_ctime = new_dir->i_mtime = ctime;
3590 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003591
Chris Masone02119d2008-09-05 16:13:11 -04003592 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
3593 old_dentry->d_name.name,
3594 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04003595 if (ret)
3596 goto out_fail;
3597
3598 if (new_inode) {
3599 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003600 ret = btrfs_unlink_inode(trans, root, new_dir,
3601 new_dentry->d_inode,
3602 new_dentry->d_name.name,
3603 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04003604 if (ret)
3605 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003606 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003607 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003608 if (ret)
3609 goto out_fail;
3610 }
Chris Masone02119d2008-09-05 16:13:11 -04003611
Chris Mason39279cc2007-06-12 06:35:45 -04003612 }
Chris Mason00e4e6b2008-08-05 11:18:09 -04003613 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003614 if (ret)
3615 goto out_fail;
3616
Chris Masone02119d2008-09-05 16:13:11 -04003617 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
3618 old_inode, new_dentry->d_name.name,
3619 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003620 if (ret)
3621 goto out_fail;
3622
3623out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003624 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003625out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003626 return ret;
3627}
3628
Chris Masond352ac62008-09-29 15:18:18 -04003629/*
3630 * some fairly slow code that needs optimization. This walks the list
3631 * of all the inodes with pending delalloc and forces them to disk.
3632 */
Chris Masonea8c2812008-08-04 23:17:27 -04003633int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3634{
3635 struct list_head *head = &root->fs_info->delalloc_inodes;
3636 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003637 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04003638 unsigned long flags;
3639
3640 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3641 while(!list_empty(head)) {
3642 binode = list_entry(head->next, struct btrfs_inode,
3643 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003644 inode = igrab(&binode->vfs_inode);
3645 if (!inode)
3646 list_del_init(&binode->delalloc_inodes);
Chris Masonea8c2812008-08-04 23:17:27 -04003647 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003648 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04003649 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003650 iput(inode);
3651 }
3652 cond_resched();
Chris Masonea8c2812008-08-04 23:17:27 -04003653 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3654 }
3655 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason8c8bee12008-09-29 11:19:10 -04003656
3657 /* the filemap_flush will queue IO into the worker threads, but
3658 * we have to make sure the IO is actually started and that
3659 * ordered extents get created before we return
3660 */
3661 atomic_inc(&root->fs_info->async_submit_draining);
3662 while(atomic_read(&root->fs_info->nr_async_submits)) {
3663 wait_event(root->fs_info->async_submit_wait,
3664 (atomic_read(&root->fs_info->nr_async_submits) == 0));
3665 }
3666 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04003667 return 0;
3668}
3669
Chris Mason39279cc2007-06-12 06:35:45 -04003670static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3671 const char *symname)
3672{
3673 struct btrfs_trans_handle *trans;
3674 struct btrfs_root *root = BTRFS_I(dir)->root;
3675 struct btrfs_path *path;
3676 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003677 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003678 int err;
3679 int drop_inode = 0;
3680 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003681 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04003682 int name_len;
3683 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003684 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003685 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003686 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003687 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003688
3689 name_len = strlen(symname) + 1;
3690 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3691 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003692
Chris Mason1832a6d2007-12-21 16:27:21 -05003693 err = btrfs_check_free_space(root, 1, 0);
3694 if (err)
3695 goto out_fail;
3696
Chris Mason39279cc2007-06-12 06:35:45 -04003697 trans = btrfs_start_transaction(root, 1);
3698 btrfs_set_trans_block_group(trans, dir);
3699
3700 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3701 if (err) {
3702 err = -ENOSPC;
3703 goto out_unlock;
3704 }
3705
Josef Bacikaec74772008-07-24 12:12:38 -04003706 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003707 dentry->d_name.len,
3708 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003709 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3710 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003711 err = PTR_ERR(inode);
3712 if (IS_ERR(inode))
3713 goto out_unlock;
3714
Josef Bacik33268ea2008-07-24 12:16:36 -04003715 err = btrfs_init_acl(inode, dir);
3716 if (err) {
3717 drop_inode = 1;
3718 goto out_unlock;
3719 }
3720
Chris Mason39279cc2007-06-12 06:35:45 -04003721 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003722 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003723 if (err)
3724 drop_inode = 1;
3725 else {
3726 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003727 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003728 inode->i_fop = &btrfs_file_operations;
3729 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003730 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003731 }
3732 dir->i_sb->s_dirt = 1;
3733 btrfs_update_inode_block_group(trans, inode);
3734 btrfs_update_inode_block_group(trans, dir);
3735 if (drop_inode)
3736 goto out_unlock;
3737
3738 path = btrfs_alloc_path();
3739 BUG_ON(!path);
3740 key.objectid = inode->i_ino;
3741 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003742 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3743 datasize = btrfs_file_extent_calc_inline_size(name_len);
3744 err = btrfs_insert_empty_item(trans, root, path, &key,
3745 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003746 if (err) {
3747 drop_inode = 1;
3748 goto out_unlock;
3749 }
Chris Mason5f39d392007-10-15 16:14:19 -04003750 leaf = path->nodes[0];
3751 ei = btrfs_item_ptr(leaf, path->slots[0],
3752 struct btrfs_file_extent_item);
3753 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3754 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003755 BTRFS_FILE_EXTENT_INLINE);
3756 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003757 write_extent_buffer(leaf, symname, ptr, name_len);
3758 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003759 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003760
Chris Mason39279cc2007-06-12 06:35:45 -04003761 inode->i_op = &btrfs_symlink_inode_operations;
3762 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003763 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003764 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003765 err = btrfs_update_inode(trans, root, inode);
3766 if (err)
3767 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003768
3769out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003770 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003771 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003772out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003773 if (drop_inode) {
3774 inode_dec_link_count(inode);
3775 iput(inode);
3776 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003777 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003778 return err;
3779}
Chris Mason16432982008-04-10 10:23:21 -04003780
Chris Masone6dcd2d2008-07-17 12:53:50 -04003781static int btrfs_set_page_dirty(struct page *page)
3782{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003783 return __set_page_dirty_nobuffers(page);
3784}
3785
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003786static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05003787{
3788 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3789 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003790 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003791}
Chris Mason39279cc2007-06-12 06:35:45 -04003792
3793static struct inode_operations btrfs_dir_inode_operations = {
3794 .lookup = btrfs_lookup,
3795 .create = btrfs_create,
3796 .unlink = btrfs_unlink,
3797 .link = btrfs_link,
3798 .mkdir = btrfs_mkdir,
3799 .rmdir = btrfs_rmdir,
3800 .rename = btrfs_rename,
3801 .symlink = btrfs_symlink,
3802 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003803 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003804 .setxattr = btrfs_setxattr,
3805 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003806 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003807 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003808 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003809};
Chris Mason39279cc2007-06-12 06:35:45 -04003810static struct inode_operations btrfs_dir_ro_inode_operations = {
3811 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003812 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003813};
Chris Mason39279cc2007-06-12 06:35:45 -04003814static struct file_operations btrfs_dir_file_operations = {
3815 .llseek = generic_file_llseek,
3816 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01003817 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003818 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003819#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003820 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003821#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003822 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04003823 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003824};
3825
Chris Masond1310b22008-01-24 16:13:08 -05003826static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003827 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003828 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003829 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003830 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003831 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003832 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003833 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003834 .set_bit_hook = btrfs_set_bit_hook,
3835 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003836};
3837
Chris Mason39279cc2007-06-12 06:35:45 -04003838static struct address_space_operations btrfs_aops = {
3839 .readpage = btrfs_readpage,
3840 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003841 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003842 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003843 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003844 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003845 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003846 .invalidatepage = btrfs_invalidatepage,
3847 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003848 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003849};
3850
3851static struct address_space_operations btrfs_symlink_aops = {
3852 .readpage = btrfs_readpage,
3853 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003854 .invalidatepage = btrfs_invalidatepage,
3855 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003856};
3857
3858static struct inode_operations btrfs_file_inode_operations = {
3859 .truncate = btrfs_truncate,
3860 .getattr = btrfs_getattr,
3861 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003862 .setxattr = btrfs_setxattr,
3863 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003864 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003865 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003866 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003867};
Josef Bacik618e21d2007-07-11 10:18:17 -04003868static struct inode_operations btrfs_special_inode_operations = {
3869 .getattr = btrfs_getattr,
3870 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003871 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003872 .setxattr = btrfs_setxattr,
3873 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04003874 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003875 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003876};
Chris Mason39279cc2007-06-12 06:35:45 -04003877static struct inode_operations btrfs_symlink_inode_operations = {
3878 .readlink = generic_readlink,
3879 .follow_link = page_follow_link_light,
3880 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003881 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003882};