blob: bf4bed6ca4d601b2773fe9c9bb17890637a5aa99 [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"
Yan Zheng5b84e8d2008-10-09 11:46:19 -040051#include "ref-cache.h"
Chris Mason39279cc2007-06-12 06:35:45 -040052
53struct btrfs_iget_args {
54 u64 ino;
55 struct btrfs_root *root;
56};
57
58static struct inode_operations btrfs_dir_inode_operations;
59static struct inode_operations btrfs_symlink_inode_operations;
60static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040061static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040062static struct inode_operations btrfs_file_inode_operations;
63static struct address_space_operations btrfs_aops;
64static struct address_space_operations btrfs_symlink_aops;
65static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050066static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040067
68static struct kmem_cache *btrfs_inode_cachep;
69struct kmem_cache *btrfs_trans_handle_cachep;
70struct kmem_cache *btrfs_transaction_cachep;
71struct kmem_cache *btrfs_bit_radix_cachep;
72struct kmem_cache *btrfs_path_cachep;
73
74#define S_SHIFT 12
75static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
76 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
77 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
78 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
79 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
80 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
81 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
82 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
83};
84
Josef Bacik7b128762008-07-24 12:17:14 -040085static void btrfs_truncate(struct inode *inode);
86
Chris Masond352ac62008-09-29 15:18:18 -040087/*
88 * a very lame attempt at stopping writes when the FS is 85% full. There
89 * are countless ways this is incorrect, but it is better than nothing.
90 */
Chris Mason1832a6d2007-12-21 16:27:21 -050091int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
92 int for_del)
93{
Chris Masona2135012008-06-25 16:01:30 -040094 u64 total;
95 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050096 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040097 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050098 int ret = 0;
99
Chris Masona2135012008-06-25 16:01:30 -0400100 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
101 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
102 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -0500103 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -0500104 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -0500105 else
Chris Masonf9ef6602008-01-03 09:22:38 -0500106 thresh = total * 85;
107
108 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500109
Chris Mason1832a6d2007-12-21 16:27:21 -0500110 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
111 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400112 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500113 return ret;
114}
115
Chris Masond352ac62008-09-29 15:18:18 -0400116/*
117 * when extent_io.c finds a delayed allocation range in the file,
118 * the call backs end up in this code. The basic idea is to
119 * allocate extents on disk for the range, and create ordered data structs
120 * in ram to track those extents.
121 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500122static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400123{
124 struct btrfs_root *root = BTRFS_I(inode)->root;
125 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400126 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400127 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500128 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400129 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500130 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500131 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400132 struct extent_map *em;
133 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
134 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400135
Chris Masonf9295742008-07-17 12:54:14 -0400136 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400137 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500138 btrfs_set_trans_block_group(trans, inode);
139
Chris Masondb945352007-10-15 16:15:53 -0400140 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500141 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500142 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400143
Chris Mason179e29e2007-11-01 11:28:41 -0400144 if (alloc_hint == EXTENT_MAP_INLINE)
145 goto out;
146
Chris Mason3b951512008-04-17 11:29:12 -0400147 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone5a22172008-07-18 20:42:20 -0400148 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400149 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
Chris Masone5a22172008-07-18 20:42:20 -0400150 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400151
Chris Masonc59f8952007-12-17 20:14:04 -0500152 while(num_bytes > 0) {
153 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400154 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
Josef Bacik0f9dd462008-09-23 13:14:11 -0400155 root->sectorsize, 0, alloc_hint,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400156 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500157 if (ret) {
158 WARN_ON(1);
159 goto out;
160 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400161 em = alloc_extent_map(GFP_NOFS);
162 em->start = start;
163 em->len = ins.offset;
164 em->block_start = ins.objectid;
165 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masone5a22172008-07-18 20:42:20 -0400166 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400167 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400168 while(1) {
169 spin_lock(&em_tree->lock);
170 ret = add_extent_mapping(em_tree, em);
171 spin_unlock(&em_tree->lock);
172 if (ret != -EEXIST) {
173 free_extent_map(em);
174 break;
175 }
176 btrfs_drop_extent_cache(inode, start,
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400177 start + ins.offset - 1, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400178 }
Chris Masone5a22172008-07-18 20:42:20 -0400179 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400180
Chris Mason98d20f62008-04-14 09:46:10 -0400181 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400182 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
Yan Zheng7ea394f2008-08-05 13:05:02 -0400183 ins.offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400184 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400185 if (num_bytes < cur_alloc_size) {
186 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
187 cur_alloc_size);
188 break;
189 }
Chris Masonc59f8952007-12-17 20:14:04 -0500190 num_bytes -= cur_alloc_size;
191 alloc_hint = ins.objectid + ins.offset;
192 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400193 }
Chris Masonb888db22007-08-27 16:49:44 -0400194out:
195 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500196 return ret;
197}
198
Chris Masond352ac62008-09-29 15:18:18 -0400199/*
200 * when nowcow writeback call back. This checks for snapshots or COW copies
201 * of the extents that exist in the file, and COWs the file as required.
202 *
203 * If no cow copies or snapshots exist, we write directly to the existing
204 * blocks on disk
205 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500206static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
207{
208 u64 extent_start;
209 u64 extent_end;
210 u64 bytenr;
Chris Mason1832a6d2007-12-21 16:27:21 -0500211 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500212 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500213 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400214 struct btrfs_block_group_cache *block_group;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400215 struct btrfs_trans_handle *trans;
Chris Masonbe20aa92007-12-17 20:14:01 -0500216 struct extent_buffer *leaf;
217 int found_type;
218 struct btrfs_path *path;
219 struct btrfs_file_extent_item *item;
220 int ret;
Yan Zheng7ea394f2008-08-05 13:05:02 -0400221 int err = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500222 struct btrfs_key found_key;
223
Chris Masonc31f8832008-01-08 15:46:31 -0500224 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500225 path = btrfs_alloc_path();
226 BUG_ON(!path);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400227 trans = btrfs_join_transaction(root, 1);
228 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500229again:
230 ret = btrfs_lookup_file_extent(NULL, root, path,
231 inode->i_ino, start, 0);
232 if (ret < 0) {
Yan Zheng7ea394f2008-08-05 13:05:02 -0400233 err = ret;
234 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500235 }
236
Chris Masonbe20aa92007-12-17 20:14:01 -0500237 if (ret != 0) {
238 if (path->slots[0] == 0)
239 goto not_found;
240 path->slots[0]--;
241 }
242
243 leaf = path->nodes[0];
244 item = btrfs_item_ptr(leaf, path->slots[0],
245 struct btrfs_file_extent_item);
246
247 /* are we inside the extent that was found? */
248 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
249 found_type = btrfs_key_type(&found_key);
250 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400251 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500252 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500253
254 found_type = btrfs_file_extent_type(leaf, item);
255 extent_start = found_key.offset;
256 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500257 u64 extent_num_bytes;
258
259 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
260 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500261 err = 0;
262
Chris Mason1832a6d2007-12-21 16:27:21 -0500263 if (loops && start != extent_start)
264 goto not_found;
265
Chris Masonbe20aa92007-12-17 20:14:01 -0500266 if (start < extent_start || start >= extent_end)
267 goto not_found;
268
Chris Masonbe20aa92007-12-17 20:14:01 -0500269 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
270 if (bytenr == 0)
271 goto not_found;
272
Yan Zheng7ea394f2008-08-05 13:05:02 -0400273 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
Chris Masona68d5932008-05-08 14:11:56 -0400274 goto not_found;
Chris Masonc31f8832008-01-08 15:46:31 -0500275 /*
276 * we may be called by the resizer, make sure we're inside
277 * the limits of the FS
278 */
Chris Masona68d5932008-05-08 14:11:56 -0400279 block_group = btrfs_lookup_block_group(root->fs_info,
280 bytenr);
281 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500282 goto not_found;
283
Yan Zheng7ea394f2008-08-05 13:05:02 -0400284 bytenr += btrfs_file_extent_offset(leaf, item);
285 extent_num_bytes = min(end + 1, extent_end) - start;
286 ret = btrfs_add_ordered_extent(inode, start, bytenr,
287 extent_num_bytes, 1);
288 if (ret) {
289 err = ret;
290 goto out;
291 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500292
Yan Zheng7ea394f2008-08-05 13:05:02 -0400293 btrfs_release_path(root, path);
294 start = extent_end;
295 if (start <= end) {
296 loops++;
297 goto again;
298 }
299 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500300not_found:
Yan Zheng7ea394f2008-08-05 13:05:02 -0400301 btrfs_end_transaction(trans, root);
302 btrfs_free_path(path);
303 return cow_file_range(inode, start, end);
304 }
305out:
306 WARN_ON(err);
307 btrfs_end_transaction(trans, root);
308 btrfs_free_path(path);
309 return err;
Chris Masonbe20aa92007-12-17 20:14:01 -0500310}
311
Chris Masond352ac62008-09-29 15:18:18 -0400312/*
313 * extent_io.c call back to do delayed allocation processing
314 */
Chris Masonbe20aa92007-12-17 20:14:01 -0500315static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
316{
317 struct btrfs_root *root = BTRFS_I(inode)->root;
318 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400319
Yanb98b6762008-01-08 15:54:37 -0500320 if (btrfs_test_opt(root, NODATACOW) ||
321 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500322 ret = run_delalloc_nocow(inode, start, end);
323 else
324 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500325
Chris Masonb888db22007-08-27 16:49:44 -0400326 return ret;
327}
328
Chris Masond352ac62008-09-29 15:18:18 -0400329/*
330 * extent_io.c set_bit_hook, used to track delayed allocation
331 * bytes in this file, and to maintain the list of inodes that
332 * have pending delalloc work to be done.
333 */
Chris Mason291d6732008-01-29 15:55:23 -0500334int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500335 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500336{
Chris Masonbcbfce82008-04-22 13:26:47 -0400337 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500338 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500339 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400340 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500341 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500342 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -0400343 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
344 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
345 &root->fs_info->delalloc_inodes);
346 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400347 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500348 }
349 return 0;
350}
351
Chris Masond352ac62008-09-29 15:18:18 -0400352/*
353 * extent_io.c clear_bit_hook, see set_bit_hook for why
354 */
Chris Mason291d6732008-01-29 15:55:23 -0500355int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500356 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500357{
Chris Masonb0c68f82008-01-31 11:05:37 -0500358 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500359 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400360 unsigned long flags;
361
362 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500363 if (end - start + 1 > root->fs_info->delalloc_bytes) {
364 printk("warning: delalloc account %Lu %Lu\n",
365 end - start + 1, root->fs_info->delalloc_bytes);
366 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500367 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500368 } else {
369 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500370 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500371 }
Chris Masonea8c2812008-08-04 23:17:27 -0400372 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
373 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
374 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
375 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400376 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500377 }
378 return 0;
379}
380
Chris Masond352ac62008-09-29 15:18:18 -0400381/*
382 * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
383 * we don't create bios that span stripes or chunks
384 */
Chris Mason239b14b2008-03-24 15:02:07 -0400385int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
386 size_t size, struct bio *bio)
387{
388 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
389 struct btrfs_mapping_tree *map_tree;
Chris Masona62b9402008-10-03 16:31:08 -0400390 u64 logical = (u64)bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400391 u64 length = 0;
392 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400393 int ret;
394
Chris Masonf2d8d742008-04-21 10:03:05 -0400395 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400396 map_tree = &root->fs_info->mapping_tree;
397 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400398 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400399 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400400
Chris Mason239b14b2008-03-24 15:02:07 -0400401 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400402 return 1;
403 }
404 return 0;
405}
406
Chris Masond352ac62008-09-29 15:18:18 -0400407/*
408 * in order to insert checksums into the metadata in large chunks,
409 * we wait until bio submission time. All the pages in the bio are
410 * checksummed and sums are attached onto the ordered extent record.
411 *
412 * At IO completion time the cums attached on the ordered extent record
413 * are inserted into the btree
414 */
Chris Mason44b8bd72008-04-16 11:14:51 -0400415int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400416 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500417{
Chris Mason065631f2008-02-20 12:07:25 -0500418 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500419 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400420
Chris Mason3edf7d32008-07-18 06:17:13 -0400421 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400422 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400423
Chris Mason8b712842008-06-11 16:50:36 -0400424 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400425}
426
Chris Masond352ac62008-09-29 15:18:18 -0400427/*
428 * extent_io.c submission hook. This does the right thing for csum calculation on write,
429 * or reading the csums from the tree before a read
430 */
Chris Mason44b8bd72008-04-16 11:14:51 -0400431int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
432 int mirror_num)
433{
434 struct btrfs_root *root = BTRFS_I(inode)->root;
435 int ret = 0;
436
Chris Masone6dcd2d2008-07-17 12:53:50 -0400437 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
438 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500439
Yan Zheng7ea394f2008-08-05 13:05:02 -0400440 if (btrfs_test_opt(root, NODATASUM) ||
441 btrfs_test_flag(inode, NODATASUM)) {
442 goto mapit;
443 }
444
Chris Mason4d1b5fb2008-08-20 09:44:52 -0400445 if (!(rw & (1 << BIO_RW))) {
446 btrfs_lookup_bio_sums(root, inode, bio);
447 goto mapit;
448 }
Chris Mason44b8bd72008-04-16 11:14:51 -0400449 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
450 inode, rw, bio, mirror_num,
451 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400452mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400453 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500454}
Chris Mason6885f302008-02-20 16:11:05 -0500455
Chris Masond352ac62008-09-29 15:18:18 -0400456/*
457 * given a list of ordered sums record them in the inode. This happens
458 * at IO completion time based on sums calculated at bio submission time.
459 */
Chris Masonba1da2f2008-07-17 12:54:15 -0400460static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400461 struct inode *inode, u64 file_offset,
462 struct list_head *list)
463{
464 struct list_head *cur;
465 struct btrfs_ordered_sum *sum;
466
467 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400468 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400469 sum = list_entry(cur, struct btrfs_ordered_sum, list);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400470 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
471 inode, sum);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400472 }
473 return 0;
474}
475
Chris Masonea8c2812008-08-04 23:17:27 -0400476int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
477{
478 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
479 GFP_NOFS);
480}
481
Chris Masond352ac62008-09-29 15:18:18 -0400482/* see btrfs_writepage_start_hook for details on why this is required */
Chris Mason247e7432008-07-17 12:53:51 -0400483struct btrfs_writepage_fixup {
484 struct page *page;
485 struct btrfs_work work;
486};
487
Chris Mason247e7432008-07-17 12:53:51 -0400488void btrfs_writepage_fixup_worker(struct btrfs_work *work)
489{
490 struct btrfs_writepage_fixup *fixup;
491 struct btrfs_ordered_extent *ordered;
492 struct page *page;
493 struct inode *inode;
494 u64 page_start;
495 u64 page_end;
496
497 fixup = container_of(work, struct btrfs_writepage_fixup, work);
498 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400499again:
Chris Mason247e7432008-07-17 12:53:51 -0400500 lock_page(page);
501 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
502 ClearPageChecked(page);
503 goto out_page;
504 }
505
506 inode = page->mapping->host;
507 page_start = page_offset(page);
508 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
509
510 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400511
512 /* already ordered? We're done */
513 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
514 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400515 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400516 }
517
518 ordered = btrfs_lookup_ordered_extent(inode, page_start);
519 if (ordered) {
520 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
521 page_end, GFP_NOFS);
522 unlock_page(page);
523 btrfs_start_ordered_extent(inode, ordered, 1);
524 goto again;
525 }
Chris Mason247e7432008-07-17 12:53:51 -0400526
Chris Masonea8c2812008-08-04 23:17:27 -0400527 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -0400528 ClearPageChecked(page);
529out:
530 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
531out_page:
532 unlock_page(page);
533 page_cache_release(page);
534}
535
536/*
537 * There are a few paths in the higher layers of the kernel that directly
538 * set the page dirty bit without asking the filesystem if it is a
539 * good idea. This causes problems because we want to make sure COW
540 * properly happens and the data=ordered rules are followed.
541 *
542 * In our case any range that doesn't have the EXTENT_ORDERED bit set
543 * hasn't been properly setup for IO. We kick off an async process
544 * to fix it up. The async helper will wait for ordered extents, set
545 * the delalloc bit and make it safe to write the page.
546 */
547int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
548{
549 struct inode *inode = page->mapping->host;
550 struct btrfs_writepage_fixup *fixup;
551 struct btrfs_root *root = BTRFS_I(inode)->root;
552 int ret;
553
554 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
555 EXTENT_ORDERED, 0);
556 if (ret)
557 return 0;
558
559 if (PageChecked(page))
560 return -EAGAIN;
561
562 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
563 if (!fixup)
564 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400565
Chris Mason247e7432008-07-17 12:53:51 -0400566 SetPageChecked(page);
567 page_cache_get(page);
568 fixup->work.func = btrfs_writepage_fixup_worker;
569 fixup->page = page;
570 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
571 return -EAGAIN;
572}
573
Chris Masond352ac62008-09-29 15:18:18 -0400574/* as ordered data IO finishes, this gets called so we can finish
575 * an ordered extent if the range of bytes in the file it covers are
576 * fully written.
577 */
Chris Mason211f90e2008-07-18 11:56:15 -0400578static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400579{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400580 struct btrfs_root *root = BTRFS_I(inode)->root;
581 struct btrfs_trans_handle *trans;
582 struct btrfs_ordered_extent *ordered_extent;
583 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Zheng Yan31840ae2008-09-23 13:14:14 -0400584 struct btrfs_file_extent_item *extent_item;
585 struct btrfs_path *path = NULL;
586 struct extent_buffer *leaf;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400587 u64 alloc_hint = 0;
588 struct list_head list;
589 struct btrfs_key ins;
590 int ret;
591
592 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400593 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400594 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400595
Chris Masonf9295742008-07-17 12:54:14 -0400596 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400597
598 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
599 BUG_ON(!ordered_extent);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400600 if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
601 goto nocow;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400602
Zheng Yan31840ae2008-09-23 13:14:14 -0400603 path = btrfs_alloc_path();
604 BUG_ON(!path);
605
Chris Masone6dcd2d2008-07-17 12:53:50 -0400606 lock_extent(io_tree, ordered_extent->file_offset,
607 ordered_extent->file_offset + ordered_extent->len - 1,
608 GFP_NOFS);
609
610 INIT_LIST_HEAD(&list);
611
Chris Masonee6e6502008-07-17 12:54:40 -0400612 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400613
Chris Masone6dcd2d2008-07-17 12:53:50 -0400614 ret = btrfs_drop_extents(trans, root, inode,
615 ordered_extent->file_offset,
616 ordered_extent->file_offset +
617 ordered_extent->len,
618 ordered_extent->file_offset, &alloc_hint);
619 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -0400620
621 ins.objectid = inode->i_ino;
622 ins.offset = ordered_extent->file_offset;
623 ins.type = BTRFS_EXTENT_DATA_KEY;
624 ret = btrfs_insert_empty_item(trans, root, path, &ins,
625 sizeof(*extent_item));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400626 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -0400627 leaf = path->nodes[0];
628 extent_item = btrfs_item_ptr(leaf, path->slots[0],
629 struct btrfs_file_extent_item);
630 btrfs_set_file_extent_generation(leaf, extent_item, trans->transid);
631 btrfs_set_file_extent_type(leaf, extent_item, BTRFS_FILE_EXTENT_REG);
632 btrfs_set_file_extent_disk_bytenr(leaf, extent_item,
633 ordered_extent->start);
634 btrfs_set_file_extent_disk_num_bytes(leaf, extent_item,
635 ordered_extent->len);
636 btrfs_set_file_extent_offset(leaf, extent_item, 0);
637 btrfs_set_file_extent_num_bytes(leaf, extent_item,
638 ordered_extent->len);
639 btrfs_mark_buffer_dirty(leaf);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400640
Chris Masone6dcd2d2008-07-17 12:53:50 -0400641 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
642 ordered_extent->file_offset +
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400643 ordered_extent->len - 1, 0);
Chris Masonee6e6502008-07-17 12:54:40 -0400644 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
645
Zheng Yan31840ae2008-09-23 13:14:14 -0400646 ins.objectid = ordered_extent->start;
647 ins.offset = ordered_extent->len;
648 ins.type = BTRFS_EXTENT_ITEM_KEY;
649 ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
650 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400651 trans->transid, inode->i_ino, &ins);
Zheng Yan31840ae2008-09-23 13:14:14 -0400652 BUG_ON(ret);
653 btrfs_release_path(root, path);
654
Yan Zhenga76a3cd2008-10-09 11:46:29 -0400655 inode_add_bytes(inode, ordered_extent->len);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400656 unlock_extent(io_tree, ordered_extent->file_offset,
657 ordered_extent->file_offset + ordered_extent->len - 1,
658 GFP_NOFS);
Yan Zheng7ea394f2008-08-05 13:05:02 -0400659nocow:
Chris Masone6dcd2d2008-07-17 12:53:50 -0400660 add_pending_csums(trans, inode, ordered_extent->file_offset,
661 &ordered_extent->list);
662
Chris Mason34353022008-09-23 20:19:49 -0400663 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masondbe674a2008-07-17 12:54:05 -0400664 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone02119d2008-09-05 16:13:11 -0400665 btrfs_update_inode(trans, root, inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400666 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason34353022008-09-23 20:19:49 -0400667 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400668
Chris Masone6dcd2d2008-07-17 12:53:50 -0400669 /* once for us */
670 btrfs_put_ordered_extent(ordered_extent);
671 /* once for the tree */
672 btrfs_put_ordered_extent(ordered_extent);
673
Chris Masone6dcd2d2008-07-17 12:53:50 -0400674 btrfs_end_transaction(trans, root);
Zheng Yan31840ae2008-09-23 13:14:14 -0400675 if (path)
676 btrfs_free_path(path);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400677 return 0;
678}
679
Chris Mason211f90e2008-07-18 11:56:15 -0400680int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
681 struct extent_state *state, int uptodate)
682{
683 return btrfs_finish_ordered_io(page->mapping->host, start, end);
684}
685
Chris Masond352ac62008-09-29 15:18:18 -0400686/*
687 * When IO fails, either with EIO or csum verification fails, we
688 * try other mirrors that might have a good copy of the data. This
689 * io_failure_record is used to record state as we go through all the
690 * mirrors. If another mirror has good data, the page is set up to date
691 * and things continue. If a good mirror can't be found, the original
692 * bio end_io callback is called to indicate things have failed.
693 */
Chris Mason7e383262008-04-09 16:28:12 -0400694struct io_failure_record {
695 struct page *page;
696 u64 start;
697 u64 len;
698 u64 logical;
699 int last_mirror;
700};
701
Chris Mason1259ab72008-05-12 13:39:03 -0400702int btrfs_io_failed_hook(struct bio *failed_bio,
703 struct page *page, u64 start, u64 end,
704 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400705{
706 struct io_failure_record *failrec = NULL;
707 u64 private;
708 struct extent_map *em;
709 struct inode *inode = page->mapping->host;
710 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400711 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400712 struct bio *bio;
713 int num_copies;
714 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400715 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400716 u64 logical;
717
718 ret = get_state_private(failure_tree, start, &private);
719 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400720 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
721 if (!failrec)
722 return -ENOMEM;
723 failrec->start = start;
724 failrec->len = end - start + 1;
725 failrec->last_mirror = 0;
726
Chris Mason3b951512008-04-17 11:29:12 -0400727 spin_lock(&em_tree->lock);
728 em = lookup_extent_mapping(em_tree, start, failrec->len);
729 if (em->start > start || em->start + em->len < start) {
730 free_extent_map(em);
731 em = NULL;
732 }
733 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400734
735 if (!em || IS_ERR(em)) {
736 kfree(failrec);
737 return -EIO;
738 }
739 logical = start - em->start;
740 logical = em->block_start + logical;
741 failrec->logical = logical;
742 free_extent_map(em);
743 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
744 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400745 set_state_private(failure_tree, start,
746 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400747 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400748 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400749 }
750 num_copies = btrfs_num_copies(
751 &BTRFS_I(inode)->root->fs_info->mapping_tree,
752 failrec->logical, failrec->len);
753 failrec->last_mirror++;
754 if (!state) {
755 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
756 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
757 failrec->start,
758 EXTENT_LOCKED);
759 if (state && state->start != failrec->start)
760 state = NULL;
761 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
762 }
763 if (!state || failrec->last_mirror > num_copies) {
764 set_state_private(failure_tree, failrec->start, 0);
765 clear_extent_bits(failure_tree, failrec->start,
766 failrec->start + failrec->len - 1,
767 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
768 kfree(failrec);
769 return -EIO;
770 }
771 bio = bio_alloc(GFP_NOFS, 1);
772 bio->bi_private = state;
773 bio->bi_end_io = failed_bio->bi_end_io;
774 bio->bi_sector = failrec->logical >> 9;
775 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400776 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400777 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400778 if (failed_bio->bi_rw & (1 << BIO_RW))
779 rw = WRITE;
780 else
781 rw = READ;
782
783 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
784 failrec->last_mirror);
785 return 0;
786}
787
Chris Masond352ac62008-09-29 15:18:18 -0400788/*
789 * each time an IO finishes, we do a fast check in the IO failure tree
790 * to see if we need to process or clean up an io_failure_record
791 */
Chris Mason1259ab72008-05-12 13:39:03 -0400792int btrfs_clean_io_failures(struct inode *inode, u64 start)
793{
794 u64 private;
795 u64 private_failure;
796 struct io_failure_record *failure;
797 int ret;
798
799 private = 0;
800 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
801 (u64)-1, 1, EXTENT_DIRTY)) {
802 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
803 start, &private_failure);
804 if (ret == 0) {
805 failure = (struct io_failure_record *)(unsigned long)
806 private_failure;
807 set_state_private(&BTRFS_I(inode)->io_failure_tree,
808 failure->start, 0);
809 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
810 failure->start,
811 failure->start + failure->len - 1,
812 EXTENT_DIRTY | EXTENT_LOCKED,
813 GFP_NOFS);
814 kfree(failure);
815 }
816 }
Chris Mason7e383262008-04-09 16:28:12 -0400817 return 0;
818}
819
Chris Masond352ac62008-09-29 15:18:18 -0400820/*
821 * when reads are done, we need to check csums to verify the data is correct
822 * if there's a match, we allow the bio to finish. If not, we go through
823 * the io_failure_record routines to find good copies
824 */
Chris Mason70dec802008-01-29 09:59:12 -0500825int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
826 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400827{
Chris Mason35ebb932007-10-30 16:56:53 -0400828 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400829 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500830 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400831 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500832 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400833 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400834 struct btrfs_root *root = BTRFS_I(inode)->root;
835 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400836 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500837
Yanb98b6762008-01-08 15:54:37 -0500838 if (btrfs_test_opt(root, NODATASUM) ||
839 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500840 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500841 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500842 private = state->private;
843 ret = 0;
844 } else {
845 ret = get_state_private(io_tree, start, &private);
846 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400847 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400848 kaddr = kmap_atomic(page, KM_IRQ0);
849 if (ret) {
850 goto zeroit;
851 }
Chris Masonff79f812007-10-15 16:22:25 -0400852 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
853 btrfs_csum_final(csum, (char *)&csum);
854 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400855 goto zeroit;
856 }
857 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400858 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400859
860 /* if the io failure tree for this inode is non-empty,
861 * check to see if we've recovered from a failed IO
862 */
Chris Mason1259ab72008-05-12 13:39:03 -0400863 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400864 return 0;
865
866zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500867 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
868 page->mapping->host->i_ino, (unsigned long long)start, csum,
869 private);
Chris Masondb945352007-10-15 16:15:53 -0400870 memset(kaddr + offset, 1, end - start + 1);
871 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400872 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400873 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400874 if (private == 0)
875 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400876 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400877}
Chris Masonb888db22007-08-27 16:49:44 -0400878
Josef Bacik7b128762008-07-24 12:17:14 -0400879/*
880 * This creates an orphan entry for the given inode in case something goes
881 * wrong in the middle of an unlink/truncate.
882 */
883int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
884{
885 struct btrfs_root *root = BTRFS_I(inode)->root;
886 int ret = 0;
887
Yanbcc63ab2008-07-30 16:29:20 -0400888 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400889
890 /* already on the orphan list, we're good */
891 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400892 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400893 return 0;
894 }
895
896 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
897
Yanbcc63ab2008-07-30 16:29:20 -0400898 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400899
900 /*
901 * insert an orphan item to track this unlinked/truncated file
902 */
903 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
904
905 return ret;
906}
907
908/*
909 * We have done the truncate/delete so we can go ahead and remove the orphan
910 * item for this particular inode.
911 */
912int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
913{
914 struct btrfs_root *root = BTRFS_I(inode)->root;
915 int ret = 0;
916
Yanbcc63ab2008-07-30 16:29:20 -0400917 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400918
919 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400920 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400921 return 0;
922 }
923
924 list_del_init(&BTRFS_I(inode)->i_orphan);
925 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400926 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400927 return 0;
928 }
929
Yanbcc63ab2008-07-30 16:29:20 -0400930 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400931
932 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
933
934 return ret;
935}
936
937/*
938 * this cleans up any orphans that may be left on the list from the last use
939 * of this root.
940 */
941void btrfs_orphan_cleanup(struct btrfs_root *root)
942{
943 struct btrfs_path *path;
944 struct extent_buffer *leaf;
945 struct btrfs_item *item;
946 struct btrfs_key key, found_key;
947 struct btrfs_trans_handle *trans;
948 struct inode *inode;
949 int ret = 0, nr_unlink = 0, nr_truncate = 0;
950
951 /* don't do orphan cleanup if the fs is readonly. */
Zheng Yan5b21f2e2008-09-26 10:05:38 -0400952 if (root->fs_info->sb->s_flags & MS_RDONLY)
Josef Bacik7b128762008-07-24 12:17:14 -0400953 return;
954
955 path = btrfs_alloc_path();
956 if (!path)
957 return;
958 path->reada = -1;
959
960 key.objectid = BTRFS_ORPHAN_OBJECTID;
961 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
962 key.offset = (u64)-1;
963
Josef Bacik7b128762008-07-24 12:17:14 -0400964
965 while (1) {
966 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
967 if (ret < 0) {
968 printk(KERN_ERR "Error searching slot for orphan: %d"
969 "\n", ret);
970 break;
971 }
972
973 /*
974 * if ret == 0 means we found what we were searching for, which
975 * is weird, but possible, so only screw with path if we didnt
976 * find the key and see if we have stuff that matches
977 */
978 if (ret > 0) {
979 if (path->slots[0] == 0)
980 break;
981 path->slots[0]--;
982 }
983
984 /* pull out the item */
985 leaf = path->nodes[0];
986 item = btrfs_item_nr(leaf, path->slots[0]);
987 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
988
989 /* make sure the item matches what we want */
990 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
991 break;
992 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
993 break;
994
995 /* release the path since we're done with it */
996 btrfs_release_path(root, path);
997
998 /*
999 * this is where we are basically btrfs_lookup, without the
1000 * crossing root thing. we store the inode number in the
1001 * offset of the orphan item.
1002 */
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001003 inode = btrfs_iget_locked(root->fs_info->sb,
Josef Bacik7b128762008-07-24 12:17:14 -04001004 found_key.offset, root);
1005 if (!inode)
1006 break;
1007
1008 if (inode->i_state & I_NEW) {
1009 BTRFS_I(inode)->root = root;
1010
1011 /* have to set the location manually */
1012 BTRFS_I(inode)->location.objectid = inode->i_ino;
1013 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
1014 BTRFS_I(inode)->location.offset = 0;
1015
1016 btrfs_read_locked_inode(inode);
1017 unlock_new_inode(inode);
1018 }
1019
1020 /*
1021 * add this inode to the orphan list so btrfs_orphan_del does
1022 * the proper thing when we hit it
1023 */
Yanbcc63ab2008-07-30 16:29:20 -04001024 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001025 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -04001026 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04001027
1028 /*
1029 * if this is a bad inode, means we actually succeeded in
1030 * removing the inode, but not the orphan record, which means
1031 * we need to manually delete the orphan since iput will just
1032 * do a destroy_inode
1033 */
1034 if (is_bad_inode(inode)) {
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001035 trans = btrfs_start_transaction(root, 1);
Josef Bacik7b128762008-07-24 12:17:14 -04001036 btrfs_orphan_del(trans, inode);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001037 btrfs_end_transaction(trans, root);
Josef Bacik7b128762008-07-24 12:17:14 -04001038 iput(inode);
1039 continue;
1040 }
1041
1042 /* if we have links, this was a truncate, lets do that */
1043 if (inode->i_nlink) {
1044 nr_truncate++;
1045 btrfs_truncate(inode);
1046 } else {
1047 nr_unlink++;
1048 }
1049
1050 /* this will do delete_inode and everything for us */
1051 iput(inode);
1052 }
1053
1054 if (nr_unlink)
1055 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
1056 if (nr_truncate)
1057 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
1058
1059 btrfs_free_path(path);
Josef Bacik7b128762008-07-24 12:17:14 -04001060}
1061
Chris Masond352ac62008-09-29 15:18:18 -04001062/*
1063 * read an inode from the btree into the in-memory inode
1064 */
Chris Mason39279cc2007-06-12 06:35:45 -04001065void btrfs_read_locked_inode(struct inode *inode)
1066{
1067 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001068 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001069 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001070 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001071 struct btrfs_root *root = BTRFS_I(inode)->root;
1072 struct btrfs_key location;
1073 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001074 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001075 int ret;
1076
1077 path = btrfs_alloc_path();
1078 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001079 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001080
Chris Mason39279cc2007-06-12 06:35:45 -04001081 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001082 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001083 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001084
Chris Mason5f39d392007-10-15 16:14:19 -04001085 leaf = path->nodes[0];
1086 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1087 struct btrfs_inode_item);
1088
1089 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1090 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1091 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1092 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001093 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001094
1095 tspec = btrfs_inode_atime(inode_item);
1096 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1097 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1098
1099 tspec = btrfs_inode_mtime(inode_item);
1100 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1101 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1102
1103 tspec = btrfs_inode_ctime(inode_item);
1104 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1105 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1106
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001107 inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
Chris Masone02119d2008-09-05 16:13:11 -04001108 BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
1109 inode->i_generation = BTRFS_I(inode)->generation;
Josef Bacik618e21d2007-07-11 10:18:17 -04001110 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001111 rdev = btrfs_inode_rdev(leaf, inode_item);
1112
Josef Bacikaec74772008-07-24 12:12:38 -04001113 BTRFS_I(inode)->index_cnt = (u64)-1;
1114
Chris Mason5f39d392007-10-15 16:14:19 -04001115 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001116 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1117 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001118 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001119 if (!BTRFS_I(inode)->block_group) {
1120 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001121 NULL, 0,
1122 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001123 }
Chris Mason39279cc2007-06-12 06:35:45 -04001124 btrfs_free_path(path);
1125 inode_item = NULL;
1126
Chris Mason39279cc2007-06-12 06:35:45 -04001127 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001128 case S_IFREG:
1129 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001130 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001131 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001132 inode->i_fop = &btrfs_file_operations;
1133 inode->i_op = &btrfs_file_inode_operations;
1134 break;
1135 case S_IFDIR:
1136 inode->i_fop = &btrfs_dir_file_operations;
1137 if (root == root->fs_info->tree_root)
1138 inode->i_op = &btrfs_dir_ro_inode_operations;
1139 else
1140 inode->i_op = &btrfs_dir_inode_operations;
1141 break;
1142 case S_IFLNK:
1143 inode->i_op = &btrfs_symlink_inode_operations;
1144 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001145 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001146 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001147 default:
1148 init_special_inode(inode, inode->i_mode, rdev);
1149 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001150 }
1151 return;
1152
1153make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001154 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001155 make_bad_inode(inode);
1156}
1157
Chris Masond352ac62008-09-29 15:18:18 -04001158/*
1159 * given a leaf and an inode, copy the inode fields into the leaf
1160 */
Chris Masone02119d2008-09-05 16:13:11 -04001161static void fill_inode_item(struct btrfs_trans_handle *trans,
1162 struct extent_buffer *leaf,
Chris Mason5f39d392007-10-15 16:14:19 -04001163 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001164 struct inode *inode)
1165{
Chris Mason5f39d392007-10-15 16:14:19 -04001166 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1167 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001168 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001169 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1170 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1171
1172 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1173 inode->i_atime.tv_sec);
1174 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1175 inode->i_atime.tv_nsec);
1176
1177 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1178 inode->i_mtime.tv_sec);
1179 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1180 inode->i_mtime.tv_nsec);
1181
1182 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1183 inode->i_ctime.tv_sec);
1184 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1185 inode->i_ctime.tv_nsec);
1186
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001187 btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
Chris Masone02119d2008-09-05 16:13:11 -04001188 btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
1189 btrfs_set_inode_transid(leaf, item, trans->transid);
Chris Mason5f39d392007-10-15 16:14:19 -04001190 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001191 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001192 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001193 BTRFS_I(inode)->block_group->key.objectid);
1194}
1195
Chris Masond352ac62008-09-29 15:18:18 -04001196/*
1197 * copy everything in the in-memory inode into the btree.
1198 */
Chris Masonba1da2f2008-07-17 12:54:15 -04001199int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001200 struct btrfs_root *root,
1201 struct inode *inode)
1202{
1203 struct btrfs_inode_item *inode_item;
1204 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001205 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001206 int ret;
1207
1208 path = btrfs_alloc_path();
1209 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001210 ret = btrfs_lookup_inode(trans, root, path,
1211 &BTRFS_I(inode)->location, 1);
1212 if (ret) {
1213 if (ret > 0)
1214 ret = -ENOENT;
1215 goto failed;
1216 }
1217
Chris Mason5f39d392007-10-15 16:14:19 -04001218 leaf = path->nodes[0];
1219 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001220 struct btrfs_inode_item);
1221
Chris Masone02119d2008-09-05 16:13:11 -04001222 fill_inode_item(trans, leaf, inode_item, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001223 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001224 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001225 ret = 0;
1226failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001227 btrfs_free_path(path);
1228 return ret;
1229}
1230
1231
Chris Masond352ac62008-09-29 15:18:18 -04001232/*
1233 * unlink helper that gets used here in inode.c and in the tree logging
1234 * recovery code. It remove a link in a directory with a given name, and
1235 * also drops the back refs in the inode to the directory
1236 */
Chris Masone02119d2008-09-05 16:13:11 -04001237int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
1238 struct btrfs_root *root,
1239 struct inode *dir, struct inode *inode,
1240 const char *name, int name_len)
Chris Mason39279cc2007-06-12 06:35:45 -04001241{
1242 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04001243 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001244 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001245 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001246 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001247 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001248
1249 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001250 if (!path) {
1251 ret = -ENOMEM;
1252 goto err;
1253 }
1254
Chris Mason39279cc2007-06-12 06:35:45 -04001255 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1256 name, name_len, -1);
1257 if (IS_ERR(di)) {
1258 ret = PTR_ERR(di);
1259 goto err;
1260 }
1261 if (!di) {
1262 ret = -ENOENT;
1263 goto err;
1264 }
Chris Mason5f39d392007-10-15 16:14:19 -04001265 leaf = path->nodes[0];
1266 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001267 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001268 if (ret)
1269 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001270 btrfs_release_path(root, path);
1271
Josef Bacikaec74772008-07-24 12:12:38 -04001272 ret = btrfs_del_inode_ref(trans, root, name, name_len,
Chris Masone02119d2008-09-05 16:13:11 -04001273 inode->i_ino,
1274 dir->i_ino, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04001275 if (ret) {
1276 printk("failed to delete reference to %.*s, "
1277 "inode %lu parent %lu\n", name_len, name,
Chris Masone02119d2008-09-05 16:13:11 -04001278 inode->i_ino, dir->i_ino);
Josef Bacikaec74772008-07-24 12:12:38 -04001279 goto err;
1280 }
1281
Chris Mason39279cc2007-06-12 06:35:45 -04001282 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001283 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001284 if (IS_ERR(di)) {
1285 ret = PTR_ERR(di);
1286 goto err;
1287 }
1288 if (!di) {
1289 ret = -ENOENT;
1290 goto err;
1291 }
1292 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001293 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001294
Chris Masone02119d2008-09-05 16:13:11 -04001295 ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
1296 inode, dir->i_ino);
Chris Mason49eb7e42008-09-11 15:53:12 -04001297 BUG_ON(ret != 0 && ret != -ENOENT);
1298 if (ret != -ENOENT)
1299 BTRFS_I(dir)->log_dirty_trans = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04001300
1301 ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
1302 dir, index);
1303 BUG_ON(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001304err:
1305 btrfs_free_path(path);
Chris Masone02119d2008-09-05 16:13:11 -04001306 if (ret)
1307 goto out;
1308
1309 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1310 inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1311 btrfs_update_inode(trans, root, dir);
1312 btrfs_drop_nlink(inode);
1313 ret = btrfs_update_inode(trans, root, inode);
1314 dir->i_sb->s_dirt = 1;
1315out:
Chris Mason39279cc2007-06-12 06:35:45 -04001316 return ret;
1317}
1318
1319static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1320{
1321 struct btrfs_root *root;
1322 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001323 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001324 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001325 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001326
1327 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001328
1329 ret = btrfs_check_free_space(root, 1, 1);
1330 if (ret)
1331 goto fail;
1332
Chris Mason39279cc2007-06-12 06:35:45 -04001333 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001334
Chris Mason39279cc2007-06-12 06:35:45 -04001335 btrfs_set_trans_block_group(trans, dir);
Chris Masone02119d2008-09-05 16:13:11 -04001336 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1337 dentry->d_name.name, dentry->d_name.len);
Josef Bacik7b128762008-07-24 12:17:14 -04001338
1339 if (inode->i_nlink == 0)
1340 ret = btrfs_orphan_add(trans, inode);
1341
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001342 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001343
Chris Mason89ce8a62008-06-25 16:01:31 -04001344 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001345fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001346 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001347 return ret;
1348}
1349
1350static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1351{
1352 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001353 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001354 int ret;
1355 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001356 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001357 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001358
Chris Mason925baed2008-06-25 16:01:30 -04001359 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001360 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001361 }
Yan134d4512007-10-25 15:49:25 -04001362
Chris Mason1832a6d2007-12-21 16:27:21 -05001363 ret = btrfs_check_free_space(root, 1, 1);
1364 if (ret)
1365 goto fail;
1366
Chris Mason39279cc2007-06-12 06:35:45 -04001367 trans = btrfs_start_transaction(root, 1);
1368 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001369
Josef Bacik7b128762008-07-24 12:17:14 -04001370 err = btrfs_orphan_add(trans, inode);
1371 if (err)
1372 goto fail_trans;
1373
Chris Mason39279cc2007-06-12 06:35:45 -04001374 /* now the directory is empty */
Chris Masone02119d2008-09-05 16:13:11 -04001375 err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1376 dentry->d_name.name, dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001377 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001378 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001379 }
Chris Mason39544012007-12-12 14:38:19 -05001380
Josef Bacik7b128762008-07-24 12:17:14 -04001381fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001382 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001383 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001384fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001385 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001386
Chris Mason39279cc2007-06-12 06:35:45 -04001387 if (ret && !err)
1388 err = ret;
1389 return err;
1390}
1391
Chris Mason39279cc2007-06-12 06:35:45 -04001392/*
Chris Mason323ac952008-10-01 19:05:46 -04001393 * when truncating bytes in a file, it is possible to avoid reading
1394 * the leaves that contain only checksum items. This can be the
1395 * majority of the IO required to delete a large file, but it must
1396 * be done carefully.
1397 *
1398 * The keys in the level just above the leaves are checked to make sure
1399 * the lowest key in a given leaf is a csum key, and starts at an offset
1400 * after the new size.
1401 *
1402 * Then the key for the next leaf is checked to make sure it also has
1403 * a checksum item for the same file. If it does, we know our target leaf
1404 * contains only checksum items, and it can be safely freed without reading
1405 * it.
1406 *
1407 * This is just an optimization targeted at large files. It may do
1408 * nothing. It will return 0 unless things went badly.
1409 */
1410static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
1411 struct btrfs_root *root,
1412 struct btrfs_path *path,
1413 struct inode *inode, u64 new_size)
1414{
1415 struct btrfs_key key;
1416 int ret;
1417 int nritems;
1418 struct btrfs_key found_key;
1419 struct btrfs_key other_key;
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001420 struct btrfs_leaf_ref *ref;
1421 u64 leaf_gen;
1422 u64 leaf_start;
Chris Mason323ac952008-10-01 19:05:46 -04001423
1424 path->lowest_level = 1;
1425 key.objectid = inode->i_ino;
1426 key.type = BTRFS_CSUM_ITEM_KEY;
1427 key.offset = new_size;
1428again:
1429 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1430 if (ret < 0)
1431 goto out;
1432
1433 if (path->nodes[1] == NULL) {
1434 ret = 0;
1435 goto out;
1436 }
1437 ret = 0;
1438 btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
1439 nritems = btrfs_header_nritems(path->nodes[1]);
1440
1441 if (!nritems)
1442 goto out;
1443
1444 if (path->slots[1] >= nritems)
1445 goto next_node;
1446
1447 /* did we find a key greater than anything we want to delete? */
1448 if (found_key.objectid > inode->i_ino ||
1449 (found_key.objectid == inode->i_ino && found_key.type > key.type))
1450 goto out;
1451
1452 /* we check the next key in the node to make sure the leave contains
1453 * only checksum items. This comparison doesn't work if our
1454 * leaf is the last one in the node
1455 */
1456 if (path->slots[1] + 1 >= nritems) {
1457next_node:
1458 /* search forward from the last key in the node, this
1459 * will bring us into the next node in the tree
1460 */
1461 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
1462
1463 /* unlikely, but we inc below, so check to be safe */
1464 if (found_key.offset == (u64)-1)
1465 goto out;
1466
1467 /* search_forward needs a path with locks held, do the
1468 * search again for the original key. It is possible
1469 * this will race with a balance and return a path that
1470 * we could modify, but this drop is just an optimization
1471 * and is allowed to miss some leaves.
1472 */
1473 btrfs_release_path(root, path);
1474 found_key.offset++;
1475
1476 /* setup a max key for search_forward */
1477 other_key.offset = (u64)-1;
1478 other_key.type = key.type;
1479 other_key.objectid = key.objectid;
1480
1481 path->keep_locks = 1;
1482 ret = btrfs_search_forward(root, &found_key, &other_key,
1483 path, 0, 0);
1484 path->keep_locks = 0;
1485 if (ret || found_key.objectid != key.objectid ||
1486 found_key.type != key.type) {
1487 ret = 0;
1488 goto out;
1489 }
1490
1491 key.offset = found_key.offset;
1492 btrfs_release_path(root, path);
1493 cond_resched();
1494 goto again;
1495 }
1496
1497 /* we know there's one more slot after us in the tree,
1498 * read that key so we can verify it is also a checksum item
1499 */
1500 btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
1501
1502 if (found_key.objectid < inode->i_ino)
1503 goto next_key;
1504
1505 if (found_key.type != key.type || found_key.offset < new_size)
1506 goto next_key;
1507
1508 /*
1509 * if the key for the next leaf isn't a csum key from this objectid,
1510 * we can't be sure there aren't good items inside this leaf.
1511 * Bail out
1512 */
1513 if (other_key.objectid != inode->i_ino || other_key.type != key.type)
1514 goto out;
1515
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001516 leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
1517 leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
Chris Mason323ac952008-10-01 19:05:46 -04001518 /*
1519 * it is safe to delete this leaf, it contains only
1520 * csum items from this inode at an offset >= new_size
1521 */
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001522 ret = btrfs_del_leaf(trans, root, path, leaf_start);
Chris Mason323ac952008-10-01 19:05:46 -04001523 BUG_ON(ret);
1524
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001525 if (root->ref_cows && leaf_gen < trans->transid) {
1526 ref = btrfs_alloc_leaf_ref(root, 0);
1527 if (ref) {
1528 ref->root_gen = root->root_key.offset;
1529 ref->bytenr = leaf_start;
1530 ref->owner = 0;
1531 ref->generation = leaf_gen;
1532 ref->nritems = 0;
1533
1534 ret = btrfs_add_leaf_ref(root, ref, 0);
1535 WARN_ON(ret);
1536 btrfs_free_leaf_ref(root, ref);
1537 } else {
1538 WARN_ON(1);
1539 }
1540 }
Chris Mason323ac952008-10-01 19:05:46 -04001541next_key:
1542 btrfs_release_path(root, path);
1543
1544 if (other_key.objectid == inode->i_ino &&
1545 other_key.type == key.type && other_key.offset > key.offset) {
1546 key.offset = other_key.offset;
1547 cond_resched();
1548 goto again;
1549 }
1550 ret = 0;
1551out:
1552 /* fixup any changes we've made to the path */
1553 path->lowest_level = 0;
1554 path->keep_locks = 0;
1555 btrfs_release_path(root, path);
1556 return ret;
1557}
1558
1559/*
Chris Mason39279cc2007-06-12 06:35:45 -04001560 * this can truncate away extent items, csum items and directory items.
1561 * It starts at a high offset and removes keys until it can't find
Chris Masond352ac62008-09-29 15:18:18 -04001562 * any higher than new_size
Chris Mason39279cc2007-06-12 06:35:45 -04001563 *
1564 * csum items that cross the new i_size are truncated to the new size
1565 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001566 *
1567 * min_type is the minimum key type to truncate down to. If set to 0, this
1568 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001569 */
Chris Masone02119d2008-09-05 16:13:11 -04001570noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
1571 struct btrfs_root *root,
1572 struct inode *inode,
1573 u64 new_size, u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001574{
1575 int ret;
1576 struct btrfs_path *path;
1577 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001578 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001579 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001580 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001581 struct btrfs_file_extent_item *fi;
1582 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001583 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001584 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001585 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001586 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001587 int found_extent;
1588 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001589 int pending_del_nr = 0;
1590 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001591 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001592 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001593
Chris Masone02119d2008-09-05 16:13:11 -04001594 if (root->ref_cows)
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001595 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001596 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001597 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001598 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001599
Chris Mason39279cc2007-06-12 06:35:45 -04001600 /* FIXME, add redo link to tree so we don't leak on crash */
1601 key.objectid = inode->i_ino;
1602 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001603 key.type = (u8)-1;
1604
Chris Mason85e21ba2008-01-29 15:11:36 -05001605 btrfs_init_path(path);
Chris Mason323ac952008-10-01 19:05:46 -04001606
1607 ret = drop_csum_leaves(trans, root, path, inode, new_size);
1608 BUG_ON(ret);
1609
Chris Mason85e21ba2008-01-29 15:11:36 -05001610search_again:
1611 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1612 if (ret < 0) {
1613 goto error;
1614 }
1615 if (ret > 0) {
Chris Masone02119d2008-09-05 16:13:11 -04001616 /* there are no items in the tree for us to truncate, we're
1617 * done
1618 */
1619 if (path->slots[0] == 0) {
1620 ret = 0;
1621 goto error;
1622 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001623 path->slots[0]--;
1624 }
1625
Chris Mason39279cc2007-06-12 06:35:45 -04001626 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001627 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001628 leaf = path->nodes[0];
1629 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1630 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001631
Chris Mason5f39d392007-10-15 16:14:19 -04001632 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001633 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001634
Chris Mason85e21ba2008-01-29 15:11:36 -05001635 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001636 break;
1637
Chris Mason5f39d392007-10-15 16:14:19 -04001638 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001639 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001640 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001641 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001642 extent_type = btrfs_file_extent_type(leaf, fi);
1643 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001644 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001645 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001646 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1647 struct btrfs_item *item = btrfs_item_nr(leaf,
1648 path->slots[0]);
1649 item_end += btrfs_file_extent_inline_len(leaf,
1650 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001651 }
Yan008630c2007-11-07 13:31:09 -05001652 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001653 }
1654 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1655 ret = btrfs_csum_truncate(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04001656 new_size);
Chris Mason39279cc2007-06-12 06:35:45 -04001657 BUG_ON(ret);
1658 }
Chris Masone02119d2008-09-05 16:13:11 -04001659 if (item_end < new_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001660 if (found_type == BTRFS_DIR_ITEM_KEY) {
1661 found_type = BTRFS_INODE_ITEM_KEY;
1662 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1663 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001664 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1665 found_type = BTRFS_XATTR_ITEM_KEY;
1666 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1667 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001668 } else if (found_type) {
1669 found_type--;
1670 } else {
1671 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001672 }
Yana61721d2007-09-17 11:08:38 -04001673 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001674 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001675 }
Chris Masone02119d2008-09-05 16:13:11 -04001676 if (found_key.offset >= new_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001677 del_item = 1;
1678 else
1679 del_item = 0;
1680 found_extent = 0;
1681
1682 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001683 if (found_type != BTRFS_EXTENT_DATA_KEY)
1684 goto delete;
1685
1686 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001687 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001688 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001689 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001690 u64 orig_num_bytes =
1691 btrfs_file_extent_num_bytes(leaf, fi);
Chris Masone02119d2008-09-05 16:13:11 -04001692 extent_num_bytes = new_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001693 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001694 extent_num_bytes = extent_num_bytes &
1695 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001696 btrfs_set_file_extent_num_bytes(leaf, fi,
1697 extent_num_bytes);
1698 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001699 extent_num_bytes);
Chris Masone02119d2008-09-05 16:13:11 -04001700 if (root->ref_cows && extent_start != 0)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001701 inode_sub_bytes(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001702 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001703 } else {
Chris Masondb945352007-10-15 16:15:53 -04001704 extent_num_bytes =
1705 btrfs_file_extent_disk_num_bytes(leaf,
1706 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001707 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001708 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001709 if (extent_start != 0) {
1710 found_extent = 1;
Chris Masone02119d2008-09-05 16:13:11 -04001711 if (root->ref_cows)
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001712 inode_sub_bytes(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001713 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001714 root_gen = btrfs_header_generation(leaf);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001715 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001716 }
Chris Mason90692182008-02-08 13:49:28 -05001717 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1718 if (!del_item) {
Chris Masone02119d2008-09-05 16:13:11 -04001719 u32 size = new_size - found_key.offset;
1720
1721 if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001722 inode_sub_bytes(inode, item_end + 1 -
1723 new_size);
Chris Masone02119d2008-09-05 16:13:11 -04001724 }
1725 size =
1726 btrfs_file_extent_calc_inline_size(size);
Chris Mason90692182008-02-08 13:49:28 -05001727 ret = btrfs_truncate_item(trans, root, path,
Chris Masone02119d2008-09-05 16:13:11 -04001728 size, 1);
Chris Mason90692182008-02-08 13:49:28 -05001729 BUG_ON(ret);
Chris Masone02119d2008-09-05 16:13:11 -04001730 } else if (root->ref_cows) {
Yan Zhenga76a3cd2008-10-09 11:46:29 -04001731 inode_sub_bytes(inode, item_end + 1 -
1732 found_key.offset);
Chris Mason90692182008-02-08 13:49:28 -05001733 }
Chris Mason39279cc2007-06-12 06:35:45 -04001734 }
Chris Mason179e29e2007-11-01 11:28:41 -04001735delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001736 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001737 if (!pending_del_nr) {
1738 /* no pending yet, add ourselves */
1739 pending_del_slot = path->slots[0];
1740 pending_del_nr = 1;
1741 } else if (pending_del_nr &&
1742 path->slots[0] + 1 == pending_del_slot) {
1743 /* hop on the pending chunk */
1744 pending_del_nr++;
1745 pending_del_slot = path->slots[0];
1746 } else {
1747 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1748 }
Chris Mason39279cc2007-06-12 06:35:45 -04001749 } else {
1750 break;
1751 }
Chris Mason39279cc2007-06-12 06:35:45 -04001752 if (found_extent) {
1753 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001754 extent_num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -04001755 leaf->start, root_owner,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001756 root_gen, inode->i_ino, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001757 BUG_ON(ret);
1758 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001759next:
1760 if (path->slots[0] == 0) {
1761 if (pending_del_nr)
1762 goto del_pending;
1763 btrfs_release_path(root, path);
1764 goto search_again;
1765 }
1766
1767 path->slots[0]--;
1768 if (pending_del_nr &&
1769 path->slots[0] + 1 != pending_del_slot) {
1770 struct btrfs_key debug;
1771del_pending:
1772 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1773 pending_del_slot);
1774 ret = btrfs_del_items(trans, root, path,
1775 pending_del_slot,
1776 pending_del_nr);
1777 BUG_ON(ret);
1778 pending_del_nr = 0;
1779 btrfs_release_path(root, path);
1780 goto search_again;
1781 }
Chris Mason39279cc2007-06-12 06:35:45 -04001782 }
1783 ret = 0;
1784error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001785 if (pending_del_nr) {
1786 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1787 pending_del_nr);
1788 }
Chris Mason39279cc2007-06-12 06:35:45 -04001789 btrfs_free_path(path);
1790 inode->i_sb->s_dirt = 1;
1791 return ret;
1792}
1793
Chris Masona52d9a82007-08-27 16:49:44 -04001794/*
1795 * taken from block_truncate_page, but does cow as it zeros out
1796 * any bytes left in the last page in the file.
1797 */
1798static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1799{
1800 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001801 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001802 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1803 struct btrfs_ordered_extent *ordered;
1804 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001805 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001806 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1807 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1808 struct page *page;
1809 int ret = 0;
1810 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001811 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001812
1813 if ((offset & (blocksize - 1)) == 0)
1814 goto out;
1815
1816 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001817again:
Chris Masona52d9a82007-08-27 16:49:44 -04001818 page = grab_cache_page(mapping, index);
1819 if (!page)
1820 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001821
1822 page_start = page_offset(page);
1823 page_end = page_start + PAGE_CACHE_SIZE - 1;
1824
Chris Masona52d9a82007-08-27 16:49:44 -04001825 if (!PageUptodate(page)) {
1826 ret = btrfs_readpage(NULL, page);
1827 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001828 if (page->mapping != mapping) {
1829 unlock_page(page);
1830 page_cache_release(page);
1831 goto again;
1832 }
Chris Masona52d9a82007-08-27 16:49:44 -04001833 if (!PageUptodate(page)) {
1834 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001835 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001836 }
1837 }
Chris Mason211c17f2008-05-15 09:13:45 -04001838 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001839
1840 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1841 set_page_extent_mapped(page);
1842
1843 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1844 if (ordered) {
1845 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1846 unlock_page(page);
1847 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001848 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001849 btrfs_put_ordered_extent(ordered);
1850 goto again;
1851 }
1852
Chris Masonea8c2812008-08-04 23:17:27 -04001853 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001854 ret = 0;
1855 if (offset != PAGE_CACHE_SIZE) {
1856 kaddr = kmap(page);
1857 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1858 flush_dcache_page(page);
1859 kunmap(page);
1860 }
Chris Mason247e7432008-07-17 12:53:51 -04001861 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001862 set_page_dirty(page);
1863 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001864
Chris Mason89642222008-07-24 09:41:53 -04001865out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001866 unlock_page(page);
1867 page_cache_release(page);
1868out:
1869 return ret;
1870}
1871
1872static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1873{
1874 struct inode *inode = dentry->d_inode;
1875 int err;
1876
1877 err = inode_change_ok(inode, attr);
1878 if (err)
1879 return err;
1880
1881 if (S_ISREG(inode->i_mode) &&
1882 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1883 struct btrfs_trans_handle *trans;
1884 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001885 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001886
Chris Mason5f39d392007-10-15 16:14:19 -04001887 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001888 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001889 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001890 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001891 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001892
Chris Mason1b0f7c22008-01-30 14:33:02 -05001893 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001894 goto out;
1895
Chris Mason1832a6d2007-12-21 16:27:21 -05001896 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001897 if (err)
1898 goto fail;
1899
Chris Mason39279cc2007-06-12 06:35:45 -04001900 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1901
Chris Mason5f564062008-01-22 16:47:59 -05001902 hole_size = block_end - hole_start;
Chris Mason7c2fe322008-08-20 08:51:50 -04001903 while(1) {
1904 struct btrfs_ordered_extent *ordered;
1905 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1906
1907 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1908 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
1909 if (ordered) {
1910 unlock_extent(io_tree, hole_start,
1911 block_end - 1, GFP_NOFS);
1912 btrfs_put_ordered_extent(ordered);
1913 } else {
1914 break;
1915 }
1916 }
Chris Mason39279cc2007-06-12 06:35:45 -04001917
Chris Mason39279cc2007-06-12 06:35:45 -04001918 trans = btrfs_start_transaction(root, 1);
1919 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001920 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001921 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001922 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001923 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001924
Chris Mason179e29e2007-11-01 11:28:41 -04001925 if (alloc_hint != EXTENT_MAP_INLINE) {
1926 err = btrfs_insert_file_extent(trans, root,
1927 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001928 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001929 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001930 btrfs_drop_extent_cache(inode, hole_start,
Zheng Yan5b21f2e2008-09-26 10:05:38 -04001931 (u64)-1, 0);
Chris Mason5f564062008-01-22 16:47:59 -05001932 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001933 }
Chris Masonee6e6502008-07-17 12:54:40 -04001934 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001935 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001936 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001937 if (err)
1938 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001939 }
1940out:
1941 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001942
1943 if (!err && ((attr->ia_valid & ATTR_MODE)))
1944 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001945fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001946 return err;
1947}
Chris Mason61295eb2008-01-14 16:24:38 -05001948
Chris Mason39279cc2007-06-12 06:35:45 -04001949void btrfs_delete_inode(struct inode *inode)
1950{
1951 struct btrfs_trans_handle *trans;
1952 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001953 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001954 int ret;
1955
1956 truncate_inode_pages(&inode->i_data, 0);
1957 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001958 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001959 goto no_delete;
1960 }
Chris Mason4a096752008-07-21 10:29:44 -04001961 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001962
Chris Masondbe674a2008-07-17 12:54:05 -04001963 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001964 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001965
Chris Mason39279cc2007-06-12 06:35:45 -04001966 btrfs_set_trans_block_group(trans, inode);
Chris Masone02119d2008-09-05 16:13:11 -04001967 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001968 if (ret) {
1969 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001970 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001971 }
1972
1973 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001974
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001975 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001976 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001977
Chris Mason39279cc2007-06-12 06:35:45 -04001978 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001979 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001980 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001981
1982no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001983 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001984 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001985 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001986no_delete:
1987 clear_inode(inode);
1988}
1989
1990/*
1991 * this returns the key found in the dir entry in the location pointer.
1992 * If no dir entries were found, location->objectid is 0.
1993 */
1994static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1995 struct btrfs_key *location)
1996{
1997 const char *name = dentry->d_name.name;
1998 int namelen = dentry->d_name.len;
1999 struct btrfs_dir_item *di;
2000 struct btrfs_path *path;
2001 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04002002 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002003
2004 path = btrfs_alloc_path();
2005 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05002006
Chris Mason39279cc2007-06-12 06:35:45 -04002007 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
2008 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04002009 if (IS_ERR(di))
2010 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04002011 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05002012 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04002013 }
Chris Mason5f39d392007-10-15 16:14:19 -04002014 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04002015out:
Chris Mason39279cc2007-06-12 06:35:45 -04002016 btrfs_free_path(path);
2017 return ret;
Chris Mason39544012007-12-12 14:38:19 -05002018out_err:
2019 location->objectid = 0;
2020 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04002021}
2022
2023/*
2024 * when we hit a tree root in a directory, the btrfs part of the inode
2025 * needs to be changed to reflect the root directory of the tree root. This
2026 * is kind of like crossing a mount point.
2027 */
2028static int fixup_tree_root_location(struct btrfs_root *root,
2029 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04002030 struct btrfs_root **sub_root,
2031 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04002032{
Chris Mason39279cc2007-06-12 06:35:45 -04002033 struct btrfs_root_item *ri;
2034
2035 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
2036 return 0;
2037 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
2038 return 0;
2039
Josef Bacik58176a92007-08-29 15:47:34 -04002040 *sub_root = btrfs_read_fs_root(root->fs_info, location,
2041 dentry->d_name.name,
2042 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04002043 if (IS_ERR(*sub_root))
2044 return PTR_ERR(*sub_root);
2045
2046 ri = &(*sub_root)->root_item;
2047 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04002048 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2049 location->offset = 0;
2050
Chris Mason39279cc2007-06-12 06:35:45 -04002051 return 0;
2052}
2053
Chris Masone02119d2008-09-05 16:13:11 -04002054static noinline void init_btrfs_i(struct inode *inode)
Chris Mason39279cc2007-06-12 06:35:45 -04002055{
Chris Masone02119d2008-09-05 16:13:11 -04002056 struct btrfs_inode *bi = BTRFS_I(inode);
2057
2058 bi->i_acl = NULL;
2059 bi->i_default_acl = NULL;
2060
2061 bi->generation = 0;
2062 bi->last_trans = 0;
2063 bi->logged_trans = 0;
2064 bi->delalloc_bytes = 0;
2065 bi->disk_i_size = 0;
2066 bi->flags = 0;
2067 bi->index_cnt = (u64)-1;
Chris Mason49eb7e42008-09-11 15:53:12 -04002068 bi->log_dirty_trans = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002069 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2070 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002071 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002072 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2073 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04002074 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04002075 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04002076 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002077 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002078 mutex_init(&BTRFS_I(inode)->log_mutex);
2079}
2080
2081static int btrfs_init_locked_inode(struct inode *inode, void *p)
2082{
2083 struct btrfs_iget_args *args = p;
2084 inode->i_ino = args->ino;
2085 init_btrfs_i(inode);
2086 BTRFS_I(inode)->root = args->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002087 return 0;
2088}
2089
2090static int btrfs_find_actor(struct inode *inode, void *opaque)
2091{
2092 struct btrfs_iget_args *args = opaque;
2093 return (args->ino == inode->i_ino &&
2094 args->root == BTRFS_I(inode)->root);
2095}
2096
Zheng Yan5b21f2e2008-09-26 10:05:38 -04002097struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
2098 struct btrfs_root *root, int wait)
2099{
2100 struct inode *inode;
2101 struct btrfs_iget_args args;
2102 args.ino = objectid;
2103 args.root = root;
2104
2105 if (wait) {
2106 inode = ilookup5(s, objectid, btrfs_find_actor,
2107 (void *)&args);
2108 } else {
2109 inode = ilookup5_nowait(s, objectid, btrfs_find_actor,
2110 (void *)&args);
2111 }
2112 return inode;
2113}
2114
Chris Mason39279cc2007-06-12 06:35:45 -04002115struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
2116 struct btrfs_root *root)
2117{
2118 struct inode *inode;
2119 struct btrfs_iget_args args;
2120 args.ino = objectid;
2121 args.root = root;
2122
2123 inode = iget5_locked(s, objectid, btrfs_find_actor,
2124 btrfs_init_locked_inode,
2125 (void *)&args);
2126 return inode;
2127}
2128
Balaji Rao1a54ef82008-07-21 02:01:04 +05302129/* Get an inode object given its location and corresponding root.
2130 * Returns in *is_new if the inode was read from disk
2131 */
2132struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
2133 struct btrfs_root *root, int *is_new)
2134{
2135 struct inode *inode;
2136
2137 inode = btrfs_iget_locked(s, location->objectid, root);
2138 if (!inode)
2139 return ERR_PTR(-EACCES);
2140
2141 if (inode->i_state & I_NEW) {
2142 BTRFS_I(inode)->root = root;
2143 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
2144 btrfs_read_locked_inode(inode);
2145 unlock_new_inode(inode);
2146 if (is_new)
2147 *is_new = 1;
2148 } else {
2149 if (is_new)
2150 *is_new = 0;
2151 }
2152
2153 return inode;
2154}
2155
Chris Mason39279cc2007-06-12 06:35:45 -04002156static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
2157 struct nameidata *nd)
2158{
2159 struct inode * inode;
2160 struct btrfs_inode *bi = BTRFS_I(dir);
2161 struct btrfs_root *root = bi->root;
2162 struct btrfs_root *sub_root = root;
2163 struct btrfs_key location;
Balaji Rao1a54ef82008-07-21 02:01:04 +05302164 int ret, new, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002165
2166 if (dentry->d_name.len > BTRFS_NAME_LEN)
2167 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04002168
Chris Mason39279cc2007-06-12 06:35:45 -04002169 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002170
Chris Mason39279cc2007-06-12 06:35:45 -04002171 if (ret < 0)
2172 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002173
Chris Mason39279cc2007-06-12 06:35:45 -04002174 inode = NULL;
2175 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04002176 ret = fixup_tree_root_location(root, &location, &sub_root,
2177 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04002178 if (ret < 0)
2179 return ERR_PTR(ret);
2180 if (ret > 0)
2181 return ERR_PTR(-ENOENT);
Balaji Rao1a54ef82008-07-21 02:01:04 +05302182 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
2183 if (IS_ERR(inode))
2184 return ERR_CAST(inode);
Josef Bacik7b128762008-07-24 12:17:14 -04002185
Balaji Rao1a54ef82008-07-21 02:01:04 +05302186 /* the inode and parent dir are two different roots */
2187 if (new && root != sub_root) {
2188 igrab(inode);
2189 sub_root->inode = inode;
2190 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002191 }
2192 }
Josef Bacik7b128762008-07-24 12:17:14 -04002193
2194 if (unlikely(do_orphan))
2195 btrfs_orphan_cleanup(sub_root);
2196
Chris Mason39279cc2007-06-12 06:35:45 -04002197 return d_splice_alias(inode, dentry);
2198}
2199
Chris Mason39279cc2007-06-12 06:35:45 -04002200static unsigned char btrfs_filetype_table[] = {
2201 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
2202};
2203
David Woodhousecbdf5a22008-08-06 19:42:33 +01002204static int btrfs_real_readdir(struct file *filp, void *dirent,
2205 filldir_t filldir)
Chris Mason39279cc2007-06-12 06:35:45 -04002206{
Chris Mason6da6aba2007-12-18 16:15:09 -05002207 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002208 struct btrfs_root *root = BTRFS_I(inode)->root;
2209 struct btrfs_item *item;
2210 struct btrfs_dir_item *di;
2211 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002212 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04002213 struct btrfs_path *path;
2214 int ret;
2215 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04002216 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04002217 int slot;
2218 int advance;
2219 unsigned char d_type;
2220 int over = 0;
2221 u32 di_cur;
2222 u32 di_total;
2223 u32 di_len;
2224 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04002225 char tmp_name[32];
2226 char *name_ptr;
2227 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04002228
2229 /* FIXME, use a real flag for deciding about the key type */
2230 if (root->fs_info->tree_root == root)
2231 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04002232
Chris Mason39544012007-12-12 14:38:19 -05002233 /* special case for "." */
2234 if (filp->f_pos == 0) {
2235 over = filldir(dirent, ".", 1,
2236 1, inode->i_ino,
2237 DT_DIR);
2238 if (over)
2239 return 0;
2240 filp->f_pos = 1;
2241 }
Chris Mason39544012007-12-12 14:38:19 -05002242 /* special case for .., just use the back ref */
2243 if (filp->f_pos == 1) {
David Woodhouse5ecc7e52008-08-17 15:14:48 +01002244 u64 pino = parent_ino(filp->f_path.dentry);
Chris Mason39544012007-12-12 14:38:19 -05002245 over = filldir(dirent, "..", 2,
David Woodhouse5ecc7e52008-08-17 15:14:48 +01002246 2, pino, DT_DIR);
Chris Mason39544012007-12-12 14:38:19 -05002247 if (over)
David Woodhouse49593bf2008-08-17 17:08:36 +01002248 return 0;
Chris Mason39544012007-12-12 14:38:19 -05002249 filp->f_pos = 2;
2250 }
2251
David Woodhouse49593bf2008-08-17 17:08:36 +01002252 path = btrfs_alloc_path();
2253 path->reada = 2;
2254
Chris Mason39279cc2007-06-12 06:35:45 -04002255 btrfs_set_key_type(&key, key_type);
2256 key.offset = filp->f_pos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002257 key.objectid = inode->i_ino;
Chris Mason5f39d392007-10-15 16:14:19 -04002258
Chris Mason39279cc2007-06-12 06:35:45 -04002259 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2260 if (ret < 0)
2261 goto err;
2262 advance = 0;
David Woodhouse49593bf2008-08-17 17:08:36 +01002263
2264 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -04002265 leaf = path->nodes[0];
2266 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002267 slot = path->slots[0];
2268 if (advance || slot >= nritems) {
David Woodhouse49593bf2008-08-17 17:08:36 +01002269 if (slot >= nritems - 1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002270 ret = btrfs_next_leaf(root, path);
2271 if (ret)
2272 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002273 leaf = path->nodes[0];
2274 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002275 slot = path->slots[0];
2276 } else {
2277 slot++;
2278 path->slots[0]++;
2279 }
2280 }
2281 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002282 item = btrfs_item_nr(leaf, slot);
2283 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2284
2285 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04002286 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002287 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002288 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002289 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04002290 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002291
2292 filp->f_pos = found_key.offset;
David Woodhouse49593bf2008-08-17 17:08:36 +01002293
Chris Mason39279cc2007-06-12 06:35:45 -04002294 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2295 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002296 di_total = btrfs_item_size(leaf, item);
David Woodhouse49593bf2008-08-17 17:08:36 +01002297
2298 while (di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04002299 struct btrfs_key location;
2300
2301 name_len = btrfs_dir_name_len(leaf, di);
David Woodhouse49593bf2008-08-17 17:08:36 +01002302 if (name_len <= sizeof(tmp_name)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002303 name_ptr = tmp_name;
2304 } else {
2305 name_ptr = kmalloc(name_len, GFP_NOFS);
David Woodhouse49593bf2008-08-17 17:08:36 +01002306 if (!name_ptr) {
2307 ret = -ENOMEM;
2308 goto err;
2309 }
Chris Mason5f39d392007-10-15 16:14:19 -04002310 }
2311 read_extent_buffer(leaf, name_ptr,
2312 (unsigned long)(di + 1), name_len);
2313
2314 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2315 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002316 over = filldir(dirent, name_ptr, name_len,
David Woodhouse49593bf2008-08-17 17:08:36 +01002317 found_key.offset, location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002318 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002319
2320 if (name_ptr != tmp_name)
2321 kfree(name_ptr);
2322
Chris Mason39279cc2007-06-12 06:35:45 -04002323 if (over)
2324 goto nopos;
David Woodhouse49593bf2008-08-17 17:08:36 +01002325
Josef Bacik5103e942007-11-16 11:45:54 -05002326 di_len = btrfs_dir_name_len(leaf, di) +
David Woodhouse49593bf2008-08-17 17:08:36 +01002327 btrfs_dir_data_len(leaf, di) + sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002328 di_cur += di_len;
2329 di = (struct btrfs_dir_item *)((char *)di + di_len);
2330 }
2331 }
David Woodhouse49593bf2008-08-17 17:08:36 +01002332
2333 /* Reached end of directory/root. Bump pos past the last item. */
Yan Zheng5e591a02008-02-19 11:41:02 -05002334 if (key_type == BTRFS_DIR_INDEX_KEY)
2335 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2336 else
2337 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002338nopos:
2339 ret = 0;
2340err:
Chris Mason39279cc2007-06-12 06:35:45 -04002341 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002342 return ret;
2343}
2344
2345int btrfs_write_inode(struct inode *inode, int wait)
2346{
2347 struct btrfs_root *root = BTRFS_I(inode)->root;
2348 struct btrfs_trans_handle *trans;
2349 int ret = 0;
2350
Chris Mason4ca8b412008-08-05 13:30:48 -04002351 if (root->fs_info->closing > 1)
2352 return 0;
2353
Chris Mason39279cc2007-06-12 06:35:45 -04002354 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002355 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002356 btrfs_set_trans_block_group(trans, inode);
2357 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002358 }
2359 return ret;
2360}
2361
2362/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002363 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002364 * inode changes. But, it is most likely to find the inode in cache.
2365 * FIXME, needs more benchmarking...there are no reasons other than performance
2366 * to keep or drop this code.
2367 */
2368void btrfs_dirty_inode(struct inode *inode)
2369{
2370 struct btrfs_root *root = BTRFS_I(inode)->root;
2371 struct btrfs_trans_handle *trans;
2372
Chris Masonf9295742008-07-17 12:54:14 -04002373 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002374 btrfs_set_trans_block_group(trans, inode);
2375 btrfs_update_inode(trans, root, inode);
2376 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002377}
2378
Chris Masond352ac62008-09-29 15:18:18 -04002379/*
2380 * find the highest existing sequence number in a directory
2381 * and then set the in-memory index_cnt variable to reflect
2382 * free sequence numbers
2383 */
Josef Bacikaec74772008-07-24 12:12:38 -04002384static int btrfs_set_inode_index_count(struct inode *inode)
2385{
2386 struct btrfs_root *root = BTRFS_I(inode)->root;
2387 struct btrfs_key key, found_key;
2388 struct btrfs_path *path;
2389 struct extent_buffer *leaf;
2390 int ret;
2391
2392 key.objectid = inode->i_ino;
2393 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2394 key.offset = (u64)-1;
2395
2396 path = btrfs_alloc_path();
2397 if (!path)
2398 return -ENOMEM;
2399
2400 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2401 if (ret < 0)
2402 goto out;
2403 /* FIXME: we should be able to handle this */
2404 if (ret == 0)
2405 goto out;
2406 ret = 0;
2407
2408 /*
2409 * MAGIC NUMBER EXPLANATION:
2410 * since we search a directory based on f_pos we have to start at 2
2411 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2412 * else has to start at 2
2413 */
2414 if (path->slots[0] == 0) {
2415 BTRFS_I(inode)->index_cnt = 2;
2416 goto out;
2417 }
2418
2419 path->slots[0]--;
2420
2421 leaf = path->nodes[0];
2422 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2423
2424 if (found_key.objectid != inode->i_ino ||
2425 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2426 BTRFS_I(inode)->index_cnt = 2;
2427 goto out;
2428 }
2429
2430 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2431out:
2432 btrfs_free_path(path);
2433 return ret;
2434}
2435
Chris Masond352ac62008-09-29 15:18:18 -04002436/*
2437 * helper to find a free sequence number in a given directory. This current
2438 * code is very simple, later versions will do smarter things in the btree
2439 */
Chris Mason00e4e6b2008-08-05 11:18:09 -04002440static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2441 u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04002442{
2443 int ret = 0;
2444
2445 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2446 ret = btrfs_set_inode_index_count(dir);
Chris Mason8d5bf1c2008-09-11 15:51:21 -04002447 if (ret) {
Josef Bacikaec74772008-07-24 12:12:38 -04002448 return ret;
Chris Mason8d5bf1c2008-09-11 15:51:21 -04002449 }
Josef Bacikaec74772008-07-24 12:12:38 -04002450 }
2451
Chris Mason00e4e6b2008-08-05 11:18:09 -04002452 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04002453 BTRFS_I(dir)->index_cnt++;
2454
2455 return ret;
2456}
2457
Chris Mason39279cc2007-06-12 06:35:45 -04002458static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2459 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002460 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002461 const char *name, int name_len,
2462 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002463 u64 objectid,
2464 struct btrfs_block_group_cache *group,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002465 int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04002466{
2467 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002468 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002469 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002470 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002471 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002472 struct btrfs_inode_ref *ref;
2473 struct btrfs_key key[2];
2474 u32 sizes[2];
2475 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002476 int ret;
2477 int owner;
2478
Chris Mason5f39d392007-10-15 16:14:19 -04002479 path = btrfs_alloc_path();
2480 BUG_ON(!path);
2481
Chris Mason39279cc2007-06-12 06:35:45 -04002482 inode = new_inode(root->fs_info->sb);
2483 if (!inode)
2484 return ERR_PTR(-ENOMEM);
2485
Josef Bacikaec74772008-07-24 12:12:38 -04002486 if (dir) {
Chris Mason00e4e6b2008-08-05 11:18:09 -04002487 ret = btrfs_set_inode_index(dir, inode, index);
Josef Bacikaec74772008-07-24 12:12:38 -04002488 if (ret)
2489 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002490 }
2491 /*
2492 * index_cnt is ignored for everything but a dir,
2493 * btrfs_get_inode_index_count has an explanation for the magic
2494 * number
2495 */
Chris Masone02119d2008-09-05 16:13:11 -04002496 init_btrfs_i(inode);
Josef Bacikaec74772008-07-24 12:12:38 -04002497 BTRFS_I(inode)->index_cnt = 2;
Chris Mason39279cc2007-06-12 06:35:45 -04002498 BTRFS_I(inode)->root = root;
Chris Masone02119d2008-09-05 16:13:11 -04002499 BTRFS_I(inode)->generation = trans->transid;
Chris Masonb888db22007-08-27 16:49:44 -04002500
Chris Mason39279cc2007-06-12 06:35:45 -04002501 if (mode & S_IFDIR)
2502 owner = 0;
2503 else
2504 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002505 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002506 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002507 if (!new_inode_group) {
2508 printk("find_block group failed\n");
2509 new_inode_group = group;
2510 }
2511 BTRFS_I(inode)->block_group = new_inode_group;
Chris Mason9c583092008-01-29 15:15:18 -05002512
2513 key[0].objectid = objectid;
2514 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2515 key[0].offset = 0;
2516
2517 key[1].objectid = objectid;
2518 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2519 key[1].offset = ref_objectid;
2520
2521 sizes[0] = sizeof(struct btrfs_inode_item);
2522 sizes[1] = name_len + sizeof(*ref);
2523
2524 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2525 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002526 goto fail;
2527
Chris Mason9c583092008-01-29 15:15:18 -05002528 if (objectid > root->highest_inode)
2529 root->highest_inode = objectid;
2530
Chris Mason39279cc2007-06-12 06:35:45 -04002531 inode->i_uid = current->fsuid;
2532 inode->i_gid = current->fsgid;
2533 inode->i_mode = mode;
2534 inode->i_ino = objectid;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04002535 inode_set_bytes(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002536 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002537 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2538 struct btrfs_inode_item);
Chris Masone02119d2008-09-05 16:13:11 -04002539 fill_inode_item(trans, path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002540
2541 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2542 struct btrfs_inode_ref);
2543 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002544 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05002545 ptr = (unsigned long)(ref + 1);
2546 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2547
Chris Mason5f39d392007-10-15 16:14:19 -04002548 btrfs_mark_buffer_dirty(path->nodes[0]);
2549 btrfs_free_path(path);
2550
Chris Mason39279cc2007-06-12 06:35:45 -04002551 location = &BTRFS_I(inode)->location;
2552 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002553 location->offset = 0;
2554 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2555
Chris Mason39279cc2007-06-12 06:35:45 -04002556 insert_inode_hash(inode);
2557 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002558fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002559 if (dir)
2560 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002561 btrfs_free_path(path);
2562 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002563}
2564
2565static inline u8 btrfs_inode_type(struct inode *inode)
2566{
2567 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2568}
2569
Chris Masond352ac62008-09-29 15:18:18 -04002570/*
2571 * utility function to add 'inode' into 'parent_inode' with
2572 * a give name and a given sequence number.
2573 * if 'add_backref' is true, also insert a backref from the
2574 * inode to the parent directory.
2575 */
Chris Masone02119d2008-09-05 16:13:11 -04002576int btrfs_add_link(struct btrfs_trans_handle *trans,
2577 struct inode *parent_inode, struct inode *inode,
2578 const char *name, int name_len, int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002579{
2580 int ret;
2581 struct btrfs_key key;
Chris Masone02119d2008-09-05 16:13:11 -04002582 struct btrfs_root *root = BTRFS_I(parent_inode)->root;
Chris Mason5f39d392007-10-15 16:14:19 -04002583
Chris Mason39279cc2007-06-12 06:35:45 -04002584 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002585 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2586 key.offset = 0;
2587
Chris Masone02119d2008-09-05 16:13:11 -04002588 ret = btrfs_insert_dir_item(trans, root, name, name_len,
2589 parent_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002590 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04002591 index);
Chris Mason39279cc2007-06-12 06:35:45 -04002592 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002593 if (add_backref) {
2594 ret = btrfs_insert_inode_ref(trans, root,
Chris Masone02119d2008-09-05 16:13:11 -04002595 name, name_len,
2596 inode->i_ino,
2597 parent_inode->i_ino,
2598 index);
Chris Mason9c583092008-01-29 15:15:18 -05002599 }
Chris Masondbe674a2008-07-17 12:54:05 -04002600 btrfs_i_size_write(parent_inode, parent_inode->i_size +
Chris Masone02119d2008-09-05 16:13:11 -04002601 name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002602 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04002603 ret = btrfs_update_inode(trans, root, parent_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002604 }
2605 return ret;
2606}
2607
2608static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002609 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002610 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002611{
Chris Masone02119d2008-09-05 16:13:11 -04002612 int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2613 inode, dentry->d_name.name,
2614 dentry->d_name.len, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002615 if (!err) {
2616 d_instantiate(dentry, inode);
2617 return 0;
2618 }
2619 if (err > 0)
2620 err = -EEXIST;
2621 return err;
2622}
2623
Josef Bacik618e21d2007-07-11 10:18:17 -04002624static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2625 int mode, dev_t rdev)
2626{
2627 struct btrfs_trans_handle *trans;
2628 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002629 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002630 int err;
2631 int drop_inode = 0;
2632 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002633 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002634 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002635
2636 if (!new_valid_dev(rdev))
2637 return -EINVAL;
2638
Chris Mason1832a6d2007-12-21 16:27:21 -05002639 err = btrfs_check_free_space(root, 1, 0);
2640 if (err)
2641 goto fail;
2642
Josef Bacik618e21d2007-07-11 10:18:17 -04002643 trans = btrfs_start_transaction(root, 1);
2644 btrfs_set_trans_block_group(trans, dir);
2645
2646 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2647 if (err) {
2648 err = -ENOSPC;
2649 goto out_unlock;
2650 }
2651
Josef Bacikaec74772008-07-24 12:12:38 -04002652 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002653 dentry->d_name.len,
2654 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002655 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002656 err = PTR_ERR(inode);
2657 if (IS_ERR(inode))
2658 goto out_unlock;
2659
Josef Bacik33268ea2008-07-24 12:16:36 -04002660 err = btrfs_init_acl(inode, dir);
2661 if (err) {
2662 drop_inode = 1;
2663 goto out_unlock;
2664 }
2665
Josef Bacik618e21d2007-07-11 10:18:17 -04002666 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002667 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002668 if (err)
2669 drop_inode = 1;
2670 else {
2671 inode->i_op = &btrfs_special_inode_operations;
2672 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002673 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002674 }
2675 dir->i_sb->s_dirt = 1;
2676 btrfs_update_inode_block_group(trans, inode);
2677 btrfs_update_inode_block_group(trans, dir);
2678out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002679 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002680 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002681fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002682 if (drop_inode) {
2683 inode_dec_link_count(inode);
2684 iput(inode);
2685 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002686 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002687 return err;
2688}
2689
Chris Mason39279cc2007-06-12 06:35:45 -04002690static int btrfs_create(struct inode *dir, struct dentry *dentry,
2691 int mode, struct nameidata *nd)
2692{
2693 struct btrfs_trans_handle *trans;
2694 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002695 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002696 int err;
2697 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002698 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002699 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002700 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002701
Chris Mason1832a6d2007-12-21 16:27:21 -05002702 err = btrfs_check_free_space(root, 1, 0);
2703 if (err)
2704 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002705 trans = btrfs_start_transaction(root, 1);
2706 btrfs_set_trans_block_group(trans, dir);
2707
2708 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2709 if (err) {
2710 err = -ENOSPC;
2711 goto out_unlock;
2712 }
2713
Josef Bacikaec74772008-07-24 12:12:38 -04002714 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002715 dentry->d_name.len,
2716 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002717 objectid, BTRFS_I(dir)->block_group, mode,
2718 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002719 err = PTR_ERR(inode);
2720 if (IS_ERR(inode))
2721 goto out_unlock;
2722
Josef Bacik33268ea2008-07-24 12:16:36 -04002723 err = btrfs_init_acl(inode, dir);
2724 if (err) {
2725 drop_inode = 1;
2726 goto out_unlock;
2727 }
2728
Chris Mason39279cc2007-06-12 06:35:45 -04002729 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002730 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002731 if (err)
2732 drop_inode = 1;
2733 else {
2734 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002735 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002736 inode->i_fop = &btrfs_file_operations;
2737 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002738 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002739 }
2740 dir->i_sb->s_dirt = 1;
2741 btrfs_update_inode_block_group(trans, inode);
2742 btrfs_update_inode_block_group(trans, dir);
2743out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002744 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002745 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002746fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002747 if (drop_inode) {
2748 inode_dec_link_count(inode);
2749 iput(inode);
2750 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002751 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002752 return err;
2753}
2754
2755static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2756 struct dentry *dentry)
2757{
2758 struct btrfs_trans_handle *trans;
2759 struct btrfs_root *root = BTRFS_I(dir)->root;
2760 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002761 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05002762 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002763 int err;
2764 int drop_inode = 0;
2765
2766 if (inode->i_nlink == 0)
2767 return -ENOENT;
2768
Chris Masone02119d2008-09-05 16:13:11 -04002769 btrfs_inc_nlink(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05002770 err = btrfs_check_free_space(root, 1, 0);
2771 if (err)
2772 goto fail;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002773 err = btrfs_set_inode_index(dir, inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002774 if (err)
2775 goto fail;
2776
Chris Mason39279cc2007-06-12 06:35:45 -04002777 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002778
Chris Mason39279cc2007-06-12 06:35:45 -04002779 btrfs_set_trans_block_group(trans, dir);
2780 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002781
Chris Mason00e4e6b2008-08-05 11:18:09 -04002782 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04002783
Chris Mason39279cc2007-06-12 06:35:45 -04002784 if (err)
2785 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002786
Chris Mason39279cc2007-06-12 06:35:45 -04002787 dir->i_sb->s_dirt = 1;
2788 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002789 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002790
Chris Mason54aa1f42007-06-22 14:16:25 -04002791 if (err)
2792 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002793
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002794 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002795 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002796fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002797 if (drop_inode) {
2798 inode_dec_link_count(inode);
2799 iput(inode);
2800 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002801 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002802 return err;
2803}
2804
Chris Mason39279cc2007-06-12 06:35:45 -04002805static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2806{
Chris Masonb9d86662008-05-02 16:13:49 -04002807 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002808 struct btrfs_trans_handle *trans;
2809 struct btrfs_root *root = BTRFS_I(dir)->root;
2810 int err = 0;
2811 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002812 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002813 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002814 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002815
Chris Mason1832a6d2007-12-21 16:27:21 -05002816 err = btrfs_check_free_space(root, 1, 0);
2817 if (err)
2818 goto out_unlock;
2819
Chris Mason39279cc2007-06-12 06:35:45 -04002820 trans = btrfs_start_transaction(root, 1);
2821 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002822
Chris Mason39279cc2007-06-12 06:35:45 -04002823 if (IS_ERR(trans)) {
2824 err = PTR_ERR(trans);
2825 goto out_unlock;
2826 }
2827
2828 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2829 if (err) {
2830 err = -ENOSPC;
2831 goto out_unlock;
2832 }
2833
Josef Bacikaec74772008-07-24 12:12:38 -04002834 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002835 dentry->d_name.len,
2836 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002837 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2838 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002839 if (IS_ERR(inode)) {
2840 err = PTR_ERR(inode);
2841 goto out_fail;
2842 }
Chris Mason5f39d392007-10-15 16:14:19 -04002843
Chris Mason39279cc2007-06-12 06:35:45 -04002844 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002845
2846 err = btrfs_init_acl(inode, dir);
2847 if (err)
2848 goto out_fail;
2849
Chris Mason39279cc2007-06-12 06:35:45 -04002850 inode->i_op = &btrfs_dir_inode_operations;
2851 inode->i_fop = &btrfs_dir_file_operations;
2852 btrfs_set_trans_block_group(trans, inode);
2853
Chris Masondbe674a2008-07-17 12:54:05 -04002854 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002855 err = btrfs_update_inode(trans, root, inode);
2856 if (err)
2857 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002858
Chris Masone02119d2008-09-05 16:13:11 -04002859 err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2860 inode, dentry->d_name.name,
2861 dentry->d_name.len, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002862 if (err)
2863 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002864
Chris Mason39279cc2007-06-12 06:35:45 -04002865 d_instantiate(dentry, inode);
2866 drop_on_err = 0;
2867 dir->i_sb->s_dirt = 1;
2868 btrfs_update_inode_block_group(trans, inode);
2869 btrfs_update_inode_block_group(trans, dir);
2870
2871out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002872 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002873 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002874
Chris Mason39279cc2007-06-12 06:35:45 -04002875out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002876 if (drop_on_err)
2877 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002878 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002879 return err;
2880}
2881
Chris Masond352ac62008-09-29 15:18:18 -04002882/* helper for btfs_get_extent. Given an existing extent in the tree,
2883 * and an extent that you want to insert, deal with overlap and insert
2884 * the new extent into the tree.
2885 */
Chris Mason3b951512008-04-17 11:29:12 -04002886static int merge_extent_mapping(struct extent_map_tree *em_tree,
2887 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002888 struct extent_map *em,
2889 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002890{
2891 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002892
Chris Masone6dcd2d2008-07-17 12:53:50 -04002893 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2894 start_diff = map_start - em->start;
2895 em->start = map_start;
2896 em->len = map_len;
2897 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2898 em->block_start += start_diff;
2899 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002900}
2901
Chris Masond352ac62008-09-29 15:18:18 -04002902/*
2903 * a bit scary, this does extent mapping from logical file offset to the disk.
2904 * the ugly parts come from merging extents from the disk with the
2905 * in-ram representation. This gets more complex because of the data=ordered code,
2906 * where the in-ram extents might be locked pending data=ordered completion.
2907 *
2908 * This also copies inline extents directly into the page.
2909 */
Chris Masona52d9a82007-08-27 16:49:44 -04002910struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002911 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002912 int create)
2913{
2914 int ret;
2915 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002916 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002917 u64 extent_start = 0;
2918 u64 extent_end = 0;
2919 u64 objectid = inode->i_ino;
2920 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002921 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002922 struct btrfs_root *root = BTRFS_I(inode)->root;
2923 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002924 struct extent_buffer *leaf;
2925 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002926 struct extent_map *em = NULL;
2927 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002928 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002929 struct btrfs_trans_handle *trans = NULL;
2930
Chris Masona52d9a82007-08-27 16:49:44 -04002931again:
Chris Masond1310b22008-01-24 16:13:08 -05002932 spin_lock(&em_tree->lock);
2933 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002934 if (em)
2935 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002936 spin_unlock(&em_tree->lock);
2937
Chris Masona52d9a82007-08-27 16:49:44 -04002938 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002939 if (em->start > start || em->start + em->len <= start)
2940 free_extent_map(em);
2941 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002942 free_extent_map(em);
2943 else
2944 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002945 }
Chris Masond1310b22008-01-24 16:13:08 -05002946 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002947 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002948 err = -ENOMEM;
2949 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002950 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002951 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002952 em->start = EXTENT_MAP_HOLE;
2953 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002954
2955 if (!path) {
2956 path = btrfs_alloc_path();
2957 BUG_ON(!path);
2958 }
2959
Chris Mason179e29e2007-11-01 11:28:41 -04002960 ret = btrfs_lookup_file_extent(trans, root, path,
2961 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002962 if (ret < 0) {
2963 err = ret;
2964 goto out;
2965 }
2966
2967 if (ret != 0) {
2968 if (path->slots[0] == 0)
2969 goto not_found;
2970 path->slots[0]--;
2971 }
2972
Chris Mason5f39d392007-10-15 16:14:19 -04002973 leaf = path->nodes[0];
2974 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002975 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002976 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002977 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2978 found_type = btrfs_key_type(&found_key);
2979 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002980 found_type != BTRFS_EXTENT_DATA_KEY) {
2981 goto not_found;
2982 }
2983
Chris Mason5f39d392007-10-15 16:14:19 -04002984 found_type = btrfs_file_extent_type(leaf, item);
2985 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002986 if (found_type == BTRFS_FILE_EXTENT_REG) {
2987 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002988 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002989 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002990 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002991 em->start = start;
2992 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002993 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002994 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002995 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002996 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002997 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002998 }
2999 goto not_found_em;
3000 }
Chris Masondb945352007-10-15 16:15:53 -04003001 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
3002 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04003003 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05003004 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04003005 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04003006 goto insert;
3007 }
Chris Masondb945352007-10-15 16:15:53 -04003008 bytenr += btrfs_file_extent_offset(leaf, item);
3009 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04003010 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05003011 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04003012 goto insert;
3013 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05003014 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04003015 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04003016 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04003017 size_t size;
3018 size_t extent_offset;
3019 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04003020
Chris Mason5f39d392007-10-15 16:14:19 -04003021 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
3022 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05003023 extent_end = (extent_start + size + root->sectorsize - 1) &
3024 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04003025 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04003026 em->start = start;
3027 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05003028 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04003029 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05003030 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04003031 } else {
Chris Masond1310b22008-01-24 16:13:08 -05003032 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04003033 }
3034 goto not_found_em;
3035 }
3036 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04003037
3038 if (!page) {
3039 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05003040 em->len = size;
Yan689f9342007-10-29 11:41:07 -04003041 goto out;
3042 }
3043
Chris Mason70dec802008-01-29 09:59:12 -05003044 page_start = page_offset(page) + pg_offset;
3045 extent_offset = page_start - extent_start;
3046 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04003047 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04003048 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05003049 em->len = (copy_size + root->sectorsize - 1) &
3050 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04003051 map = kmap(page);
3052 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04003053 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05003054 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04003055 copy_size);
3056 flush_dcache_page(page);
3057 } else if (create && PageUptodate(page)) {
3058 if (!trans) {
3059 kunmap(page);
3060 free_extent_map(em);
3061 em = NULL;
3062 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04003063 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04003064 goto again;
3065 }
Chris Mason70dec802008-01-29 09:59:12 -05003066 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04003067 copy_size);
3068 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04003069 }
Chris Masona52d9a82007-08-27 16:49:44 -04003070 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05003071 set_extent_uptodate(io_tree, em->start,
3072 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04003073 goto insert;
3074 } else {
3075 printk("unkknown found_type %d\n", found_type);
3076 WARN_ON(1);
3077 }
3078not_found:
3079 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05003080 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04003081not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04003082 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04003083insert:
3084 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05003085 if (em->start > start || extent_map_end(em) <= start) {
3086 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04003087 err = -EIO;
3088 goto out;
3089 }
Chris Masond1310b22008-01-24 16:13:08 -05003090
3091 err = 0;
3092 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04003093 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04003094 /* it is possible that someone inserted the extent into the tree
3095 * while we had the lock dropped. It is also possible that
3096 * an overlapping map exists in the tree
3097 */
Chris Masona52d9a82007-08-27 16:49:44 -04003098 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04003099 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003100
3101 ret = 0;
3102
Chris Mason3b951512008-04-17 11:29:12 -04003103 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04003104 if (existing && (existing->start > start ||
3105 existing->start + existing->len <= start)) {
3106 free_extent_map(existing);
3107 existing = NULL;
3108 }
Chris Mason3b951512008-04-17 11:29:12 -04003109 if (!existing) {
3110 existing = lookup_extent_mapping(em_tree, em->start,
3111 em->len);
3112 if (existing) {
3113 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003114 em, start,
3115 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04003116 free_extent_map(existing);
3117 if (err) {
3118 free_extent_map(em);
3119 em = NULL;
3120 }
3121 } else {
3122 err = -EIO;
3123 printk("failing to insert %Lu %Lu\n",
3124 start, len);
3125 free_extent_map(em);
3126 em = NULL;
3127 }
3128 } else {
3129 free_extent_map(em);
3130 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003131 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04003132 }
Chris Masona52d9a82007-08-27 16:49:44 -04003133 }
Chris Masond1310b22008-01-24 16:13:08 -05003134 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04003135out:
Chris Masonf4219502008-07-22 11:18:09 -04003136 if (path)
3137 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04003138 if (trans) {
3139 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003140 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04003141 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003142 }
Chris Masona52d9a82007-08-27 16:49:44 -04003143 }
Chris Masona52d9a82007-08-27 16:49:44 -04003144 if (err) {
3145 free_extent_map(em);
3146 WARN_ON(1);
3147 return ERR_PTR(err);
3148 }
3149 return em;
3150}
3151
Chris Mason16432982008-04-10 10:23:21 -04003152static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
3153 const struct iovec *iov, loff_t offset,
3154 unsigned long nr_segs)
3155{
Chris Masone1c4b742008-04-22 13:26:46 -04003156 return -EINVAL;
Chris Mason16432982008-04-10 10:23:21 -04003157}
3158
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04003159static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04003160{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04003161 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04003162}
3163
Chris Mason9ebefb182007-06-15 13:50:00 -04003164int btrfs_readpage(struct file *file, struct page *page)
3165{
Chris Masond1310b22008-01-24 16:13:08 -05003166 struct extent_io_tree *tree;
3167 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003168 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04003169}
Chris Mason1832a6d2007-12-21 16:27:21 -05003170
Chris Mason39279cc2007-06-12 06:35:45 -04003171static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3172{
Chris Masond1310b22008-01-24 16:13:08 -05003173 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04003174
3175
3176 if (current->flags & PF_MEMALLOC) {
3177 redirty_page_for_writepage(wbc, page);
3178 unlock_page(page);
3179 return 0;
3180 }
Chris Masond1310b22008-01-24 16:13:08 -05003181 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04003182 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3183}
Chris Mason39279cc2007-06-12 06:35:45 -04003184
Chris Masonf4219502008-07-22 11:18:09 -04003185int btrfs_writepages(struct address_space *mapping,
3186 struct writeback_control *wbc)
Chris Masonb293f02e2007-11-01 19:45:34 -04003187{
Chris Masond1310b22008-01-24 16:13:08 -05003188 struct extent_io_tree *tree;
3189 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04003190 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3191}
3192
Chris Mason3ab2fb52007-11-08 10:59:22 -05003193static int
3194btrfs_readpages(struct file *file, struct address_space *mapping,
3195 struct list_head *pages, unsigned nr_pages)
3196{
Chris Masond1310b22008-01-24 16:13:08 -05003197 struct extent_io_tree *tree;
3198 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05003199 return extent_readpages(tree, mapping, pages, nr_pages,
3200 btrfs_get_extent);
3201}
Chris Masone6dcd2d2008-07-17 12:53:50 -04003202static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04003203{
Chris Masond1310b22008-01-24 16:13:08 -05003204 struct extent_io_tree *tree;
3205 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04003206 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04003207
Chris Masond1310b22008-01-24 16:13:08 -05003208 tree = &BTRFS_I(page->mapping->host)->io_tree;
3209 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05003210 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04003211 if (ret == 1) {
3212 ClearPagePrivate(page);
3213 set_page_private(page, 0);
3214 page_cache_release(page);
3215 }
3216 return ret;
3217}
Chris Mason39279cc2007-06-12 06:35:45 -04003218
Chris Masone6dcd2d2008-07-17 12:53:50 -04003219static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3220{
Chris Mason98509cf2008-09-11 15:51:43 -04003221 if (PageWriteback(page) || PageDirty(page))
3222 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003223 return __btrfs_releasepage(page, gfp_flags);
3224}
3225
Chris Masona52d9a82007-08-27 16:49:44 -04003226static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3227{
Chris Masond1310b22008-01-24 16:13:08 -05003228 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003229 struct btrfs_ordered_extent *ordered;
3230 u64 page_start = page_offset(page);
3231 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003232
Chris Masone6dcd2d2008-07-17 12:53:50 -04003233 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003234 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003235 if (offset) {
3236 btrfs_releasepage(page, GFP_NOFS);
3237 return;
3238 }
3239
3240 lock_extent(tree, page_start, page_end, GFP_NOFS);
3241 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3242 page_offset(page));
3243 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003244 /*
3245 * IO on this page will never be started, so we need
3246 * to account for any ordered extents now
3247 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003248 clear_extent_bit(tree, page_start, page_end,
3249 EXTENT_DIRTY | EXTENT_DELALLOC |
3250 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003251 btrfs_finish_ordered_io(page->mapping->host,
3252 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003253 btrfs_put_ordered_extent(ordered);
3254 lock_extent(tree, page_start, page_end, GFP_NOFS);
3255 }
3256 clear_extent_bit(tree, page_start, page_end,
3257 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3258 EXTENT_ORDERED,
3259 1, 1, GFP_NOFS);
3260 __btrfs_releasepage(page, GFP_NOFS);
3261
Chris Mason4a096752008-07-21 10:29:44 -04003262 ClearPageChecked(page);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04003263 if (PagePrivate(page)) {
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04003264 ClearPagePrivate(page);
3265 set_page_private(page, 0);
3266 page_cache_release(page);
3267 }
Chris Mason39279cc2007-06-12 06:35:45 -04003268}
3269
Chris Mason9ebefb182007-06-15 13:50:00 -04003270/*
3271 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3272 * called from a page fault handler when a page is first dirtied. Hence we must
3273 * be careful to check for EOF conditions here. We set the page up correctly
3274 * for a written page which means we get ENOSPC checking when writing into
3275 * holes and correct delalloc and unwritten extent mapping on filesystems that
3276 * support these features.
3277 *
3278 * We are not allowed to take the i_mutex here so we have to play games to
3279 * protect against truncate races as the page could now be beyond EOF. Because
3280 * vmtruncate() writes the inode size before removing pages, once we have the
3281 * page lock we can determine safely if the page is beyond EOF. If it is not
3282 * beyond EOF, then the page is guaranteed safe against truncation until we
3283 * unlock the page.
3284 */
3285int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3286{
Chris Mason6da6aba2007-12-18 16:15:09 -05003287 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003288 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003289 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3290 struct btrfs_ordered_extent *ordered;
3291 char *kaddr;
3292 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003293 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003294 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003295 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003296 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003297
Chris Mason1832a6d2007-12-21 16:27:21 -05003298 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003299 if (ret)
3300 goto out;
3301
3302 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003303again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003304 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003305 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003306 page_start = page_offset(page);
3307 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003308
Chris Mason9ebefb182007-06-15 13:50:00 -04003309 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003310 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003311 /* page got truncated out from underneath us */
3312 goto out_unlock;
3313 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003314 wait_on_page_writeback(page);
3315
3316 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3317 set_page_extent_mapped(page);
3318
Chris Masoneb84ae02008-07-17 13:53:27 -04003319 /*
3320 * we can't set the delalloc bits if there are pending ordered
3321 * extents. Drop our locks and wait for them to finish
3322 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003323 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3324 if (ordered) {
3325 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3326 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003327 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003328 btrfs_put_ordered_extent(ordered);
3329 goto again;
3330 }
3331
Chris Masonea8c2812008-08-04 23:17:27 -04003332 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003333 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003334
3335 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003336 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003337 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003338 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003339 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003340
Chris Masone6dcd2d2008-07-17 12:53:50 -04003341 if (zero_start != PAGE_CACHE_SIZE) {
3342 kaddr = kmap(page);
3343 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3344 flush_dcache_page(page);
3345 kunmap(page);
3346 }
Chris Mason247e7432008-07-17 12:53:51 -04003347 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003348 set_page_dirty(page);
3349 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003350
3351out_unlock:
3352 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003353out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003354 return ret;
3355}
3356
Chris Mason39279cc2007-06-12 06:35:45 -04003357static void btrfs_truncate(struct inode *inode)
3358{
3359 struct btrfs_root *root = BTRFS_I(inode)->root;
3360 int ret;
3361 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003362 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003363 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003364
3365 if (!S_ISREG(inode->i_mode))
3366 return;
3367 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3368 return;
3369
3370 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003371 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003372
Chris Mason39279cc2007-06-12 06:35:45 -04003373 trans = btrfs_start_transaction(root, 1);
3374 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003375 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003376
Josef Bacik7b128762008-07-24 12:17:14 -04003377 ret = btrfs_orphan_add(trans, inode);
3378 if (ret)
3379 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003380 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Masone02119d2008-09-05 16:13:11 -04003381 ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
Chris Mason85e21ba2008-01-29 15:11:36 -05003382 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003383 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003384
Josef Bacik7b128762008-07-24 12:17:14 -04003385 ret = btrfs_orphan_del(trans, inode);
3386 BUG_ON(ret);
3387
3388out:
3389 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003390 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003391 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003392 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003393}
3394
Sven Wegener3b963622008-06-09 21:57:42 -04003395/*
3396 * Invalidate a single dcache entry at the root of the filesystem.
3397 * Needed after creation of snapshot or subvolume.
3398 */
3399void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3400 int namelen)
3401{
3402 struct dentry *alias, *entry;
3403 struct qstr qstr;
3404
3405 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3406 if (alias) {
3407 qstr.name = name;
3408 qstr.len = namelen;
3409 /* change me if btrfs ever gets a d_hash operation */
3410 qstr.hash = full_name_hash(qstr.name, qstr.len);
3411 entry = d_lookup(alias, &qstr);
3412 dput(alias);
3413 if (entry) {
3414 d_invalidate(entry);
3415 dput(entry);
3416 }
3417 }
3418}
3419
Chris Masond352ac62008-09-29 15:18:18 -04003420/*
3421 * create a new subvolume directory/inode (helper for the ioctl).
3422 */
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04003423int btrfs_create_subvol_root(struct btrfs_root *new_root, struct dentry *dentry,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003424 struct btrfs_trans_handle *trans, u64 new_dirid,
3425 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003426{
Chris Mason39279cc2007-06-12 06:35:45 -04003427 struct inode *inode;
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04003428 int error;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003429 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003430
Josef Bacikaec74772008-07-24 12:12:38 -04003431 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003432 new_dirid, block_group, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04003433 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003434 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003435 inode->i_op = &btrfs_dir_inode_operations;
3436 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003437 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003438
Chris Mason39279cc2007-06-12 06:35:45 -04003439 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003440 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003441
Christoph Hellwigcb8e7092008-10-09 13:39:39 -04003442 error = btrfs_update_inode(trans, new_root, inode);
3443 if (error)
3444 return error;
3445
3446 d_instantiate(dentry, inode);
3447 return 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003448}
3449
Chris Masond352ac62008-09-29 15:18:18 -04003450/* helper function for file defrag and space balancing. This
3451 * forces readahead on a given range of bytes in an inode
3452 */
Chris Masonedbd8d42007-12-21 16:27:24 -05003453unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003454 struct file_ra_state *ra, struct file *file,
3455 pgoff_t offset, pgoff_t last_index)
3456{
Chris Mason8e7bf942008-04-28 09:02:36 -04003457 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003458
Chris Mason86479a02007-09-10 19:58:16 -04003459 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3460 return offset + req_size;
Chris Mason86479a02007-09-10 19:58:16 -04003461}
3462
Chris Mason39279cc2007-06-12 06:35:45 -04003463struct inode *btrfs_alloc_inode(struct super_block *sb)
3464{
3465 struct btrfs_inode *ei;
3466
3467 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3468 if (!ei)
3469 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003470 ei->last_trans = 0;
Chris Masone02119d2008-09-05 16:13:11 -04003471 ei->logged_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003472 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003473 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3474 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003475 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003476 return &ei->vfs_inode;
3477}
3478
3479void btrfs_destroy_inode(struct inode *inode)
3480{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003481 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003482 WARN_ON(!list_empty(&inode->i_dentry));
3483 WARN_ON(inode->i_data.nrpages);
3484
Josef Bacik33268ea2008-07-24 12:16:36 -04003485 if (BTRFS_I(inode)->i_acl &&
3486 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3487 posix_acl_release(BTRFS_I(inode)->i_acl);
3488 if (BTRFS_I(inode)->i_default_acl &&
3489 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3490 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3491
Yanbcc63ab2008-07-30 16:29:20 -04003492 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003493 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3494 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3495 " list\n", inode->i_ino);
3496 dump_stack();
3497 }
Yanbcc63ab2008-07-30 16:29:20 -04003498 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003499
Chris Masone6dcd2d2008-07-17 12:53:50 -04003500 while(1) {
3501 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3502 if (!ordered)
3503 break;
3504 else {
3505 printk("found ordered extent %Lu %Lu\n",
3506 ordered->file_offset, ordered->len);
3507 btrfs_remove_ordered_extent(inode, ordered);
3508 btrfs_put_ordered_extent(ordered);
3509 btrfs_put_ordered_extent(ordered);
3510 }
3511 }
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003512 btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003513 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3514}
3515
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003516static void init_once(void *foo)
Chris Mason39279cc2007-06-12 06:35:45 -04003517{
3518 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3519
3520 inode_init_once(&ei->vfs_inode);
3521}
3522
3523void btrfs_destroy_cachep(void)
3524{
3525 if (btrfs_inode_cachep)
3526 kmem_cache_destroy(btrfs_inode_cachep);
3527 if (btrfs_trans_handle_cachep)
3528 kmem_cache_destroy(btrfs_trans_handle_cachep);
3529 if (btrfs_transaction_cachep)
3530 kmem_cache_destroy(btrfs_transaction_cachep);
3531 if (btrfs_bit_radix_cachep)
3532 kmem_cache_destroy(btrfs_bit_radix_cachep);
3533 if (btrfs_path_cachep)
3534 kmem_cache_destroy(btrfs_path_cachep);
3535}
3536
Chris Mason86479a02007-09-10 19:58:16 -04003537struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003538 unsigned long extra_flags,
Chris Mason2b1f55b2008-09-24 11:48:04 -04003539 void (*ctor)(void *))
Chris Mason92fee662007-07-25 12:31:35 -04003540{
3541 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
Chris Mason2b1f55b2008-09-24 11:48:04 -04003542 SLAB_MEM_SPREAD | extra_flags), ctor);
Chris Mason92fee662007-07-25 12:31:35 -04003543}
3544
Chris Mason39279cc2007-06-12 06:35:45 -04003545int btrfs_init_cachep(void)
3546{
Chris Mason86479a02007-09-10 19:58:16 -04003547 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003548 sizeof(struct btrfs_inode),
3549 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003550 if (!btrfs_inode_cachep)
3551 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003552 btrfs_trans_handle_cachep =
3553 btrfs_cache_create("btrfs_trans_handle_cache",
3554 sizeof(struct btrfs_trans_handle),
3555 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003556 if (!btrfs_trans_handle_cachep)
3557 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003558 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003559 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003560 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003561 if (!btrfs_transaction_cachep)
3562 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003563 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003564 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003565 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003566 if (!btrfs_path_cachep)
3567 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003568 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003569 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003570 if (!btrfs_bit_radix_cachep)
3571 goto fail;
3572 return 0;
3573fail:
3574 btrfs_destroy_cachep();
3575 return -ENOMEM;
3576}
3577
3578static int btrfs_getattr(struct vfsmount *mnt,
3579 struct dentry *dentry, struct kstat *stat)
3580{
3581 struct inode *inode = dentry->d_inode;
3582 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003583 stat->blksize = PAGE_CACHE_SIZE;
Yan Zhenga76a3cd2008-10-09 11:46:29 -04003584 stat->blocks = (inode_get_bytes(inode) +
3585 BTRFS_I(inode)->delalloc_bytes) >> 9;
Chris Mason39279cc2007-06-12 06:35:45 -04003586 return 0;
3587}
3588
3589static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3590 struct inode * new_dir,struct dentry *new_dentry)
3591{
3592 struct btrfs_trans_handle *trans;
3593 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3594 struct inode *new_inode = new_dentry->d_inode;
3595 struct inode *old_inode = old_dentry->d_inode;
3596 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003597 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003598 int ret;
3599
3600 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3601 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3602 return -ENOTEMPTY;
3603 }
Chris Mason5f39d392007-10-15 16:14:19 -04003604
Chris Mason1832a6d2007-12-21 16:27:21 -05003605 ret = btrfs_check_free_space(root, 1, 0);
3606 if (ret)
3607 goto out_unlock;
3608
Chris Mason39279cc2007-06-12 06:35:45 -04003609 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003610
Chris Mason39279cc2007-06-12 06:35:45 -04003611 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003612
Chris Masone02119d2008-09-05 16:13:11 -04003613 btrfs_inc_nlink(old_dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003614 old_dir->i_ctime = old_dir->i_mtime = ctime;
3615 new_dir->i_ctime = new_dir->i_mtime = ctime;
3616 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003617
Chris Masone02119d2008-09-05 16:13:11 -04003618 ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
3619 old_dentry->d_name.name,
3620 old_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04003621 if (ret)
3622 goto out_fail;
3623
3624 if (new_inode) {
3625 new_inode->i_ctime = CURRENT_TIME;
Chris Masone02119d2008-09-05 16:13:11 -04003626 ret = btrfs_unlink_inode(trans, root, new_dir,
3627 new_dentry->d_inode,
3628 new_dentry->d_name.name,
3629 new_dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04003630 if (ret)
3631 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003632 if (new_inode->i_nlink == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003633 ret = btrfs_orphan_add(trans, new_dentry->d_inode);
Josef Bacik7b128762008-07-24 12:17:14 -04003634 if (ret)
3635 goto out_fail;
3636 }
Chris Masone02119d2008-09-05 16:13:11 -04003637
Chris Mason39279cc2007-06-12 06:35:45 -04003638 }
Chris Mason00e4e6b2008-08-05 11:18:09 -04003639 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003640 if (ret)
3641 goto out_fail;
3642
Chris Masone02119d2008-09-05 16:13:11 -04003643 ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
3644 old_inode, new_dentry->d_name.name,
3645 new_dentry->d_name.len, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003646 if (ret)
3647 goto out_fail;
3648
3649out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003650 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003651out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003652 return ret;
3653}
3654
Chris Masond352ac62008-09-29 15:18:18 -04003655/*
3656 * some fairly slow code that needs optimization. This walks the list
3657 * of all the inodes with pending delalloc and forces them to disk.
3658 */
Chris Masonea8c2812008-08-04 23:17:27 -04003659int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3660{
3661 struct list_head *head = &root->fs_info->delalloc_inodes;
3662 struct btrfs_inode *binode;
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003663 struct inode *inode;
Chris Masonea8c2812008-08-04 23:17:27 -04003664 unsigned long flags;
3665
3666 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3667 while(!list_empty(head)) {
3668 binode = list_entry(head->next, struct btrfs_inode,
3669 delalloc_inodes);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003670 inode = igrab(&binode->vfs_inode);
3671 if (!inode)
3672 list_del_init(&binode->delalloc_inodes);
Chris Masonea8c2812008-08-04 23:17:27 -04003673 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003674 if (inode) {
Chris Mason8c8bee12008-09-29 11:19:10 -04003675 filemap_flush(inode->i_mapping);
Zheng Yan5b21f2e2008-09-26 10:05:38 -04003676 iput(inode);
3677 }
3678 cond_resched();
Chris Masonea8c2812008-08-04 23:17:27 -04003679 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3680 }
3681 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason8c8bee12008-09-29 11:19:10 -04003682
3683 /* the filemap_flush will queue IO into the worker threads, but
3684 * we have to make sure the IO is actually started and that
3685 * ordered extents get created before we return
3686 */
3687 atomic_inc(&root->fs_info->async_submit_draining);
3688 while(atomic_read(&root->fs_info->nr_async_submits)) {
3689 wait_event(root->fs_info->async_submit_wait,
3690 (atomic_read(&root->fs_info->nr_async_submits) == 0));
3691 }
3692 atomic_dec(&root->fs_info->async_submit_draining);
Chris Masonea8c2812008-08-04 23:17:27 -04003693 return 0;
3694}
3695
Chris Mason39279cc2007-06-12 06:35:45 -04003696static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3697 const char *symname)
3698{
3699 struct btrfs_trans_handle *trans;
3700 struct btrfs_root *root = BTRFS_I(dir)->root;
3701 struct btrfs_path *path;
3702 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003703 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003704 int err;
3705 int drop_inode = 0;
3706 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003707 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04003708 int name_len;
3709 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003710 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003711 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003712 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003713 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003714
3715 name_len = strlen(symname) + 1;
3716 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3717 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003718
Chris Mason1832a6d2007-12-21 16:27:21 -05003719 err = btrfs_check_free_space(root, 1, 0);
3720 if (err)
3721 goto out_fail;
3722
Chris Mason39279cc2007-06-12 06:35:45 -04003723 trans = btrfs_start_transaction(root, 1);
3724 btrfs_set_trans_block_group(trans, dir);
3725
3726 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3727 if (err) {
3728 err = -ENOSPC;
3729 goto out_unlock;
3730 }
3731
Josef Bacikaec74772008-07-24 12:12:38 -04003732 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003733 dentry->d_name.len,
3734 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003735 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3736 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003737 err = PTR_ERR(inode);
3738 if (IS_ERR(inode))
3739 goto out_unlock;
3740
Josef Bacik33268ea2008-07-24 12:16:36 -04003741 err = btrfs_init_acl(inode, dir);
3742 if (err) {
3743 drop_inode = 1;
3744 goto out_unlock;
3745 }
3746
Chris Mason39279cc2007-06-12 06:35:45 -04003747 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003748 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003749 if (err)
3750 drop_inode = 1;
3751 else {
3752 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003753 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003754 inode->i_fop = &btrfs_file_operations;
3755 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003756 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003757 }
3758 dir->i_sb->s_dirt = 1;
3759 btrfs_update_inode_block_group(trans, inode);
3760 btrfs_update_inode_block_group(trans, dir);
3761 if (drop_inode)
3762 goto out_unlock;
3763
3764 path = btrfs_alloc_path();
3765 BUG_ON(!path);
3766 key.objectid = inode->i_ino;
3767 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003768 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3769 datasize = btrfs_file_extent_calc_inline_size(name_len);
3770 err = btrfs_insert_empty_item(trans, root, path, &key,
3771 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003772 if (err) {
3773 drop_inode = 1;
3774 goto out_unlock;
3775 }
Chris Mason5f39d392007-10-15 16:14:19 -04003776 leaf = path->nodes[0];
3777 ei = btrfs_item_ptr(leaf, path->slots[0],
3778 struct btrfs_file_extent_item);
3779 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3780 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003781 BTRFS_FILE_EXTENT_INLINE);
3782 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003783 write_extent_buffer(leaf, symname, ptr, name_len);
3784 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003785 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003786
Chris Mason39279cc2007-06-12 06:35:45 -04003787 inode->i_op = &btrfs_symlink_inode_operations;
3788 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003789 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003790 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003791 err = btrfs_update_inode(trans, root, inode);
3792 if (err)
3793 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003794
3795out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003796 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003797 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003798out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003799 if (drop_inode) {
3800 inode_dec_link_count(inode);
3801 iput(inode);
3802 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003803 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003804 return err;
3805}
Chris Mason16432982008-04-10 10:23:21 -04003806
Chris Masone6dcd2d2008-07-17 12:53:50 -04003807static int btrfs_set_page_dirty(struct page *page)
3808{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003809 return __set_page_dirty_nobuffers(page);
3810}
3811
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003812static int btrfs_permission(struct inode *inode, int mask)
Yanfdebe2b2008-01-14 13:26:08 -05003813{
3814 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3815 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003816 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003817}
Chris Mason39279cc2007-06-12 06:35:45 -04003818
3819static struct inode_operations btrfs_dir_inode_operations = {
3820 .lookup = btrfs_lookup,
3821 .create = btrfs_create,
3822 .unlink = btrfs_unlink,
3823 .link = btrfs_link,
3824 .mkdir = btrfs_mkdir,
3825 .rmdir = btrfs_rmdir,
3826 .rename = btrfs_rename,
3827 .symlink = btrfs_symlink,
3828 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003829 .mknod = btrfs_mknod,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003830 .setxattr = btrfs_setxattr,
3831 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003832 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003833 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003834 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003835};
Chris Mason39279cc2007-06-12 06:35:45 -04003836static struct inode_operations btrfs_dir_ro_inode_operations = {
3837 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003838 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003839};
Chris Mason39279cc2007-06-12 06:35:45 -04003840static struct file_operations btrfs_dir_file_operations = {
3841 .llseek = generic_file_llseek,
3842 .read = generic_read_dir,
David Woodhousecbdf5a22008-08-06 19:42:33 +01003843 .readdir = btrfs_real_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003844 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003845#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003846 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003847#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003848 .release = btrfs_release_file,
Chris Masone02119d2008-09-05 16:13:11 -04003849 .fsync = btrfs_sync_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003850};
3851
Chris Masond1310b22008-01-24 16:13:08 -05003852static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003853 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003854 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003855 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003856 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003857 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003858 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003859 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003860 .set_bit_hook = btrfs_set_bit_hook,
3861 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003862};
3863
Chris Mason39279cc2007-06-12 06:35:45 -04003864static struct address_space_operations btrfs_aops = {
3865 .readpage = btrfs_readpage,
3866 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04003867 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003868 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003869 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003870 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003871 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003872 .invalidatepage = btrfs_invalidatepage,
3873 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003874 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003875};
3876
3877static struct address_space_operations btrfs_symlink_aops = {
3878 .readpage = btrfs_readpage,
3879 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003880 .invalidatepage = btrfs_invalidatepage,
3881 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003882};
3883
3884static struct inode_operations btrfs_file_inode_operations = {
3885 .truncate = btrfs_truncate,
3886 .getattr = btrfs_getattr,
3887 .setattr = btrfs_setattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003888 .setxattr = btrfs_setxattr,
3889 .getxattr = btrfs_getxattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003890 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003891 .removexattr = btrfs_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003892 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003893};
Josef Bacik618e21d2007-07-11 10:18:17 -04003894static struct inode_operations btrfs_special_inode_operations = {
3895 .getattr = btrfs_getattr,
3896 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003897 .permission = btrfs_permission,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003898 .setxattr = btrfs_setxattr,
3899 .getxattr = btrfs_getxattr,
Josef Bacik33268ea2008-07-24 12:16:36 -04003900 .listxattr = btrfs_listxattr,
Christoph Hellwig95819c02008-08-28 06:21:17 -04003901 .removexattr = btrfs_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003902};
Chris Mason39279cc2007-06-12 06:35:45 -04003903static struct inode_operations btrfs_symlink_inode_operations = {
3904 .readlink = generic_readlink,
3905 .follow_link = page_follow_link_light,
3906 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003907 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003908};