blob: 4d8ffc01931e4ddeb2e6463262ce0d534f2ee359 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Chris Mason8f18cf12008-04-25 16:53:30 -040019#include <linux/kernel.h>
Chris Mason065631f2008-02-20 12:07:25 -050020#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040021#include <linux/buffer_head.h>
Sage Weilf2eb0a22008-05-02 14:43:14 -040022#include <linux/file.h>
Chris Mason39279cc2007-06-12 06:35:45 -040023#include <linux/fs.h>
24#include <linux/pagemap.h>
25#include <linux/highmem.h>
26#include <linux/time.h>
27#include <linux/init.h>
28#include <linux/string.h>
29#include <linux/smp_lock.h>
30#include <linux/backing-dev.h>
31#include <linux/mpage.h>
32#include <linux/swap.h>
33#include <linux/writeback.h>
34#include <linux/statfs.h>
35#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040036#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040037#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050038#include <linux/xattr.h>
Josef Bacik33268ea2008-07-24 12:16:36 -040039#include <linux/posix_acl.h>
Chris Mason39279cc2007-06-12 06:35:45 -040040#include "ctree.h"
41#include "disk-io.h"
42#include "transaction.h"
43#include "btrfs_inode.h"
44#include "ioctl.h"
45#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040046#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040047#include "ordered-data.h"
Chris Mason39279cc2007-06-12 06:35:45 -040048
49struct btrfs_iget_args {
50 u64 ino;
51 struct btrfs_root *root;
52};
53
54static struct inode_operations btrfs_dir_inode_operations;
55static struct inode_operations btrfs_symlink_inode_operations;
56static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040057static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040058static struct inode_operations btrfs_file_inode_operations;
59static struct address_space_operations btrfs_aops;
60static struct address_space_operations btrfs_symlink_aops;
61static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050062static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040063
64static struct kmem_cache *btrfs_inode_cachep;
65struct kmem_cache *btrfs_trans_handle_cachep;
66struct kmem_cache *btrfs_transaction_cachep;
67struct kmem_cache *btrfs_bit_radix_cachep;
68struct kmem_cache *btrfs_path_cachep;
69
70#define S_SHIFT 12
71static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
72 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
73 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
74 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
75 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
76 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
77 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
78 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
79};
80
Josef Bacik7b128762008-07-24 12:17:14 -040081static void btrfs_truncate(struct inode *inode);
82
Chris Mason1832a6d2007-12-21 16:27:21 -050083int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
84 int for_del)
85{
Chris Masona2135012008-06-25 16:01:30 -040086 u64 total;
87 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050088 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040089 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050090 int ret = 0;
91
Chris Masona2135012008-06-25 16:01:30 -040092 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
93 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
94 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050095 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050096 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050097 else
Chris Masonf9ef6602008-01-03 09:22:38 -050098 thresh = total * 85;
99
100 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -0500101
Chris Mason1832a6d2007-12-21 16:27:21 -0500102 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
103 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400104 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500105 return ret;
106}
107
Chris Masonbe20aa92007-12-17 20:14:01 -0500108static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400109{
110 struct btrfs_root *root = BTRFS_I(inode)->root;
111 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400112 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400113 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500114 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400115 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500116 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500117 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400118 struct extent_map *em;
119 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
120 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400121
Chris Masonf9295742008-07-17 12:54:14 -0400122 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400123 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500124 btrfs_set_trans_block_group(trans, inode);
125
Chris Masondb945352007-10-15 16:15:53 -0400126 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500127 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500128 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400129
Chris Mason179e29e2007-11-01 11:28:41 -0400130 if (alloc_hint == EXTENT_MAP_INLINE)
131 goto out;
132
Chris Mason3b951512008-04-17 11:29:12 -0400133 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone5a22172008-07-18 20:42:20 -0400134 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400135 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Masone5a22172008-07-18 20:42:20 -0400136 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400137
Chris Masonc59f8952007-12-17 20:14:04 -0500138 while(num_bytes > 0) {
139 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400140 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
141 root->sectorsize, 0, 0,
142 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500143 if (ret) {
144 WARN_ON(1);
145 goto out;
146 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400147 em = alloc_extent_map(GFP_NOFS);
148 em->start = start;
149 em->len = ins.offset;
150 em->block_start = ins.objectid;
151 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masone5a22172008-07-18 20:42:20 -0400152 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400153 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400154 while(1) {
155 spin_lock(&em_tree->lock);
156 ret = add_extent_mapping(em_tree, em);
157 spin_unlock(&em_tree->lock);
158 if (ret != -EEXIST) {
159 free_extent_map(em);
160 break;
161 }
162 btrfs_drop_extent_cache(inode, start,
163 start + ins.offset - 1);
164 }
Chris Masone5a22172008-07-18 20:42:20 -0400165 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400166
Chris Mason98d20f62008-04-14 09:46:10 -0400167 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400168 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
169 ins.offset);
170 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400171 if (num_bytes < cur_alloc_size) {
172 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
173 cur_alloc_size);
174 break;
175 }
Chris Masonc59f8952007-12-17 20:14:04 -0500176 num_bytes -= cur_alloc_size;
177 alloc_hint = ins.objectid + ins.offset;
178 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400179 }
Chris Masonb888db22007-08-27 16:49:44 -0400180out:
181 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500182 return ret;
183}
184
185static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
186{
187 u64 extent_start;
188 u64 extent_end;
189 u64 bytenr;
190 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500191 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500192 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500193 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400194 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500195 struct extent_buffer *leaf;
196 int found_type;
197 struct btrfs_path *path;
198 struct btrfs_file_extent_item *item;
199 int ret;
200 int err;
201 struct btrfs_key found_key;
202
Chris Masonc31f8832008-01-08 15:46:31 -0500203 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500204 path = btrfs_alloc_path();
205 BUG_ON(!path);
206again:
207 ret = btrfs_lookup_file_extent(NULL, root, path,
208 inode->i_ino, start, 0);
209 if (ret < 0) {
210 btrfs_free_path(path);
211 return ret;
212 }
213
214 cow_end = end;
215 if (ret != 0) {
216 if (path->slots[0] == 0)
217 goto not_found;
218 path->slots[0]--;
219 }
220
221 leaf = path->nodes[0];
222 item = btrfs_item_ptr(leaf, path->slots[0],
223 struct btrfs_file_extent_item);
224
225 /* are we inside the extent that was found? */
226 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
227 found_type = btrfs_key_type(&found_key);
228 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400229 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500230 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500231
232 found_type = btrfs_file_extent_type(leaf, item);
233 extent_start = found_key.offset;
234 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500235 u64 extent_num_bytes;
236
237 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
238 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500239 err = 0;
240
Chris Mason1832a6d2007-12-21 16:27:21 -0500241 if (loops && start != extent_start)
242 goto not_found;
243
Chris Masonbe20aa92007-12-17 20:14:01 -0500244 if (start < extent_start || start >= extent_end)
245 goto not_found;
246
247 cow_end = min(end, extent_end - 1);
248 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
249 if (bytenr == 0)
250 goto not_found;
251
Yan Zhengf321e492008-07-30 09:26:11 -0400252 if (btrfs_cross_ref_exists(root, &found_key, bytenr))
Chris Masona68d5932008-05-08 14:11:56 -0400253 goto not_found;
Chris Masonc31f8832008-01-08 15:46:31 -0500254 /*
255 * we may be called by the resizer, make sure we're inside
256 * the limits of the FS
257 */
Chris Masona68d5932008-05-08 14:11:56 -0400258 block_group = btrfs_lookup_block_group(root->fs_info,
259 bytenr);
260 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500261 goto not_found;
262
Chris Masonbe20aa92007-12-17 20:14:01 -0500263 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500264 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500265 goto not_found;
266 }
267loop:
268 if (start > end) {
269 btrfs_free_path(path);
270 return 0;
271 }
272 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500273 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500274 goto again;
275
276not_found:
Yan Zhengf321e492008-07-30 09:26:11 -0400277 btrfs_release_path(root, path);
Chris Masonbbaf5492008-05-08 16:31:21 -0400278 cow_file_range(inode, start, end);
279 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500280 goto loop;
281}
282
283static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
284{
285 struct btrfs_root *root = BTRFS_I(inode)->root;
286 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400287
Yanb98b6762008-01-08 15:54:37 -0500288 if (btrfs_test_opt(root, NODATACOW) ||
289 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500290 ret = run_delalloc_nocow(inode, start, end);
291 else
292 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500293
Chris Masonb888db22007-08-27 16:49:44 -0400294 return ret;
295}
296
Chris Mason291d6732008-01-29 15:55:23 -0500297int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500298 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500299{
Chris Masonbcbfce82008-04-22 13:26:47 -0400300 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500301 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500302 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400303 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500304 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500305 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonea8c2812008-08-04 23:17:27 -0400306 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
307 list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
308 &root->fs_info->delalloc_inodes);
309 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400310 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500311 }
312 return 0;
313}
314
315int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500316 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500317{
Chris Masonb0c68f82008-01-31 11:05:37 -0500318 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500319 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400320 unsigned long flags;
321
322 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500323 if (end - start + 1 > root->fs_info->delalloc_bytes) {
324 printk("warning: delalloc account %Lu %Lu\n",
325 end - start + 1, root->fs_info->delalloc_bytes);
326 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500327 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500328 } else {
329 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500330 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500331 }
Chris Masonea8c2812008-08-04 23:17:27 -0400332 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
333 !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
334 list_del_init(&BTRFS_I(inode)->delalloc_inodes);
335 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400336 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500337 }
338 return 0;
339}
340
Chris Mason239b14b2008-03-24 15:02:07 -0400341int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
342 size_t size, struct bio *bio)
343{
344 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
345 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400346 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400347 u64 length = 0;
348 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400349 int ret;
350
Chris Masonf2d8d742008-04-21 10:03:05 -0400351 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400352 map_tree = &root->fs_info->mapping_tree;
353 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400354 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400355 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400356
Chris Mason239b14b2008-03-24 15:02:07 -0400357 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400358 return 1;
359 }
360 return 0;
361}
362
Chris Mason44b8bd72008-04-16 11:14:51 -0400363int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400364 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500365{
Chris Mason065631f2008-02-20 12:07:25 -0500366 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500367 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400368
Chris Mason3edf7d32008-07-18 06:17:13 -0400369 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400370 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400371
Chris Mason8b712842008-06-11 16:50:36 -0400372 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400373}
374
375int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
376 int mirror_num)
377{
378 struct btrfs_root *root = BTRFS_I(inode)->root;
379 int ret = 0;
380
Chris Masone6dcd2d2008-07-17 12:53:50 -0400381 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
382 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500383
Chris Masone6dcd2d2008-07-17 12:53:50 -0400384 if (!(rw & (1 << BIO_RW))) {
Chris Mason0b86a832008-03-24 15:01:56 -0400385 goto mapit;
386 }
Chris Mason065631f2008-02-20 12:07:25 -0500387
Chris Mason44b8bd72008-04-16 11:14:51 -0400388 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
389 inode, rw, bio, mirror_num,
390 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400391mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400392 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500393}
Chris Mason6885f302008-02-20 16:11:05 -0500394
Chris Masonba1da2f2008-07-17 12:54:15 -0400395static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400396 struct inode *inode, u64 file_offset,
397 struct list_head *list)
398{
399 struct list_head *cur;
400 struct btrfs_ordered_sum *sum;
401
402 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400403 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400404 sum = list_entry(cur, struct btrfs_ordered_sum, list);
405 mutex_lock(&BTRFS_I(inode)->csum_mutex);
406 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
407 inode, sum);
408 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400409 }
410 return 0;
411}
412
Chris Masonea8c2812008-08-04 23:17:27 -0400413int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
414{
415 return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
416 GFP_NOFS);
417}
418
Chris Mason247e7432008-07-17 12:53:51 -0400419struct btrfs_writepage_fixup {
420 struct page *page;
421 struct btrfs_work work;
422};
423
424/* see btrfs_writepage_start_hook for details on why this is required */
425void btrfs_writepage_fixup_worker(struct btrfs_work *work)
426{
427 struct btrfs_writepage_fixup *fixup;
428 struct btrfs_ordered_extent *ordered;
429 struct page *page;
430 struct inode *inode;
431 u64 page_start;
432 u64 page_end;
433
434 fixup = container_of(work, struct btrfs_writepage_fixup, work);
435 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400436again:
Chris Mason247e7432008-07-17 12:53:51 -0400437 lock_page(page);
438 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
439 ClearPageChecked(page);
440 goto out_page;
441 }
442
443 inode = page->mapping->host;
444 page_start = page_offset(page);
445 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
446
447 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400448
449 /* already ordered? We're done */
450 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
451 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400452 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400453 }
454
455 ordered = btrfs_lookup_ordered_extent(inode, page_start);
456 if (ordered) {
457 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
458 page_end, GFP_NOFS);
459 unlock_page(page);
460 btrfs_start_ordered_extent(inode, ordered, 1);
461 goto again;
462 }
Chris Mason247e7432008-07-17 12:53:51 -0400463
Chris Masonea8c2812008-08-04 23:17:27 -0400464 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Mason247e7432008-07-17 12:53:51 -0400465 ClearPageChecked(page);
466out:
467 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
468out_page:
469 unlock_page(page);
470 page_cache_release(page);
471}
472
473/*
474 * There are a few paths in the higher layers of the kernel that directly
475 * set the page dirty bit without asking the filesystem if it is a
476 * good idea. This causes problems because we want to make sure COW
477 * properly happens and the data=ordered rules are followed.
478 *
479 * In our case any range that doesn't have the EXTENT_ORDERED bit set
480 * hasn't been properly setup for IO. We kick off an async process
481 * to fix it up. The async helper will wait for ordered extents, set
482 * the delalloc bit and make it safe to write the page.
483 */
484int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
485{
486 struct inode *inode = page->mapping->host;
487 struct btrfs_writepage_fixup *fixup;
488 struct btrfs_root *root = BTRFS_I(inode)->root;
489 int ret;
490
491 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
492 EXTENT_ORDERED, 0);
493 if (ret)
494 return 0;
495
496 if (PageChecked(page))
497 return -EAGAIN;
498
499 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
500 if (!fixup)
501 return -EAGAIN;
Chris Masonf4219502008-07-22 11:18:09 -0400502
Chris Mason247e7432008-07-17 12:53:51 -0400503 SetPageChecked(page);
504 page_cache_get(page);
505 fixup->work.func = btrfs_writepage_fixup_worker;
506 fixup->page = page;
507 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
508 return -EAGAIN;
509}
510
Chris Mason211f90e2008-07-18 11:56:15 -0400511static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400512{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400513 struct btrfs_root *root = BTRFS_I(inode)->root;
514 struct btrfs_trans_handle *trans;
515 struct btrfs_ordered_extent *ordered_extent;
516 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
517 u64 alloc_hint = 0;
518 struct list_head list;
519 struct btrfs_key ins;
520 int ret;
521
522 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400523 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400524 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400525
Chris Masonf9295742008-07-17 12:54:14 -0400526 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400527
528 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
529 BUG_ON(!ordered_extent);
530
531 lock_extent(io_tree, ordered_extent->file_offset,
532 ordered_extent->file_offset + ordered_extent->len - 1,
533 GFP_NOFS);
534
535 INIT_LIST_HEAD(&list);
536
537 ins.objectid = ordered_extent->start;
538 ins.offset = ordered_extent->len;
539 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400540
Chris Masone6dcd2d2008-07-17 12:53:50 -0400541 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
542 trans->transid, inode->i_ino,
543 ordered_extent->file_offset, &ins);
544 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400545
546 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400547
Chris Masone6dcd2d2008-07-17 12:53:50 -0400548 ret = btrfs_drop_extents(trans, root, inode,
549 ordered_extent->file_offset,
550 ordered_extent->file_offset +
551 ordered_extent->len,
552 ordered_extent->file_offset, &alloc_hint);
553 BUG_ON(ret);
554 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
555 ordered_extent->file_offset,
556 ordered_extent->start,
557 ordered_extent->len,
558 ordered_extent->len, 0);
559 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400560
Chris Masone6dcd2d2008-07-17 12:53:50 -0400561 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
562 ordered_extent->file_offset +
563 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400564 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
565
Chris Masone6dcd2d2008-07-17 12:53:50 -0400566 inode->i_blocks += ordered_extent->len >> 9;
567 unlock_extent(io_tree, ordered_extent->file_offset,
568 ordered_extent->file_offset + ordered_extent->len - 1,
569 GFP_NOFS);
570 add_pending_csums(trans, inode, ordered_extent->file_offset,
571 &ordered_extent->list);
572
Chris Masondbe674a2008-07-17 12:54:05 -0400573 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400574 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400575
Chris Masone6dcd2d2008-07-17 12:53:50 -0400576 /* once for us */
577 btrfs_put_ordered_extent(ordered_extent);
578 /* once for the tree */
579 btrfs_put_ordered_extent(ordered_extent);
580
581 btrfs_update_inode(trans, root, inode);
582 btrfs_end_transaction(trans, root);
583 return 0;
584}
585
Chris Mason211f90e2008-07-18 11:56:15 -0400586int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
587 struct extent_state *state, int uptodate)
588{
589 return btrfs_finish_ordered_io(page->mapping->host, start, end);
590}
591
Chris Mason3de9d6b2008-08-04 23:17:27 -0400592int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
593{
594 int ret = 0;
595 struct inode *inode = page->mapping->host;
596 struct btrfs_root *root = BTRFS_I(inode)->root;
597 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
598 struct btrfs_csum_item *item;
599 struct btrfs_path *path = NULL;
600 u32 csum;
601
602 if (btrfs_test_opt(root, NODATASUM) ||
603 btrfs_test_flag(inode, NODATASUM))
604 return 0;
605
606 /*
607 * It is possible there is an ordered extent that has
608 * not yet finished for this range in the file. If so,
609 * that extent will have a csum cached, and it will insert
610 * the sum after all the blocks in the extent are fully
611 * on disk. So, look for an ordered extent and use the
612 * sum if found. We have to do this before looking in the
613 * btree because csum items are pre-inserted based on
614 * the file size. btrfs_lookup_csum might find an item
615 * that still hasn't been fully filled.
616 */
617 ret = btrfs_find_ordered_sum(inode, start, &csum);
618 if (ret == 0)
619 goto found;
620
621 ret = 0;
622 path = btrfs_alloc_path();
623 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
624 if (IS_ERR(item)) {
625 ret = PTR_ERR(item);
626 /* a csum that isn't present is a preallocated region. */
627 if (ret == -ENOENT || ret == -EFBIG)
628 ret = 0;
629 csum = 0;
630 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
631 start);
632 goto out;
633 }
634 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
635 BTRFS_CRC32_SIZE);
636found:
637 set_state_private(io_tree, start, csum);
638out:
639 if (path)
640 btrfs_free_path(path);
641 return ret;
642}
643
Chris Mason7e383262008-04-09 16:28:12 -0400644struct io_failure_record {
645 struct page *page;
646 u64 start;
647 u64 len;
648 u64 logical;
649 int last_mirror;
650};
651
Chris Mason1259ab72008-05-12 13:39:03 -0400652int btrfs_io_failed_hook(struct bio *failed_bio,
653 struct page *page, u64 start, u64 end,
654 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400655{
656 struct io_failure_record *failrec = NULL;
657 u64 private;
658 struct extent_map *em;
659 struct inode *inode = page->mapping->host;
660 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400661 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400662 struct bio *bio;
663 int num_copies;
664 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400665 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400666 u64 logical;
667
668 ret = get_state_private(failure_tree, start, &private);
669 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400670 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
671 if (!failrec)
672 return -ENOMEM;
673 failrec->start = start;
674 failrec->len = end - start + 1;
675 failrec->last_mirror = 0;
676
Chris Mason3b951512008-04-17 11:29:12 -0400677 spin_lock(&em_tree->lock);
678 em = lookup_extent_mapping(em_tree, start, failrec->len);
679 if (em->start > start || em->start + em->len < start) {
680 free_extent_map(em);
681 em = NULL;
682 }
683 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400684
685 if (!em || IS_ERR(em)) {
686 kfree(failrec);
687 return -EIO;
688 }
689 logical = start - em->start;
690 logical = em->block_start + logical;
691 failrec->logical = logical;
692 free_extent_map(em);
693 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
694 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400695 set_state_private(failure_tree, start,
696 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400697 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400698 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400699 }
700 num_copies = btrfs_num_copies(
701 &BTRFS_I(inode)->root->fs_info->mapping_tree,
702 failrec->logical, failrec->len);
703 failrec->last_mirror++;
704 if (!state) {
705 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
706 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
707 failrec->start,
708 EXTENT_LOCKED);
709 if (state && state->start != failrec->start)
710 state = NULL;
711 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
712 }
713 if (!state || failrec->last_mirror > num_copies) {
714 set_state_private(failure_tree, failrec->start, 0);
715 clear_extent_bits(failure_tree, failrec->start,
716 failrec->start + failrec->len - 1,
717 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
718 kfree(failrec);
719 return -EIO;
720 }
721 bio = bio_alloc(GFP_NOFS, 1);
722 bio->bi_private = state;
723 bio->bi_end_io = failed_bio->bi_end_io;
724 bio->bi_sector = failrec->logical >> 9;
725 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400726 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400727 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400728 if (failed_bio->bi_rw & (1 << BIO_RW))
729 rw = WRITE;
730 else
731 rw = READ;
732
733 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
734 failrec->last_mirror);
735 return 0;
736}
737
738int btrfs_clean_io_failures(struct inode *inode, u64 start)
739{
740 u64 private;
741 u64 private_failure;
742 struct io_failure_record *failure;
743 int ret;
744
745 private = 0;
746 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
747 (u64)-1, 1, EXTENT_DIRTY)) {
748 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
749 start, &private_failure);
750 if (ret == 0) {
751 failure = (struct io_failure_record *)(unsigned long)
752 private_failure;
753 set_state_private(&BTRFS_I(inode)->io_failure_tree,
754 failure->start, 0);
755 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
756 failure->start,
757 failure->start + failure->len - 1,
758 EXTENT_DIRTY | EXTENT_LOCKED,
759 GFP_NOFS);
760 kfree(failure);
761 }
762 }
Chris Mason7e383262008-04-09 16:28:12 -0400763 return 0;
764}
765
Chris Mason70dec802008-01-29 09:59:12 -0500766int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
767 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400768{
Chris Mason35ebb932007-10-30 16:56:53 -0400769 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400770 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500771 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400772 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500773 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400774 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400775 struct btrfs_root *root = BTRFS_I(inode)->root;
776 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400777 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500778
Yanb98b6762008-01-08 15:54:37 -0500779 if (btrfs_test_opt(root, NODATASUM) ||
780 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500781 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500782 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500783 private = state->private;
784 ret = 0;
785 } else {
786 ret = get_state_private(io_tree, start, &private);
787 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400788 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400789 kaddr = kmap_atomic(page, KM_IRQ0);
790 if (ret) {
791 goto zeroit;
792 }
Chris Masonff79f812007-10-15 16:22:25 -0400793 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
794 btrfs_csum_final(csum, (char *)&csum);
795 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400796 goto zeroit;
797 }
798 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400799 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400800
801 /* if the io failure tree for this inode is non-empty,
802 * check to see if we've recovered from a failed IO
803 */
Chris Mason1259ab72008-05-12 13:39:03 -0400804 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400805 return 0;
806
807zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500808 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
809 page->mapping->host->i_ino, (unsigned long long)start, csum,
810 private);
Chris Masondb945352007-10-15 16:15:53 -0400811 memset(kaddr + offset, 1, end - start + 1);
812 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400813 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400814 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400815 if (private == 0)
816 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400817 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400818}
Chris Masonb888db22007-08-27 16:49:44 -0400819
Josef Bacik7b128762008-07-24 12:17:14 -0400820/*
821 * This creates an orphan entry for the given inode in case something goes
822 * wrong in the middle of an unlink/truncate.
823 */
824int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
825{
826 struct btrfs_root *root = BTRFS_I(inode)->root;
827 int ret = 0;
828
Yanbcc63ab2008-07-30 16:29:20 -0400829 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400830
831 /* already on the orphan list, we're good */
832 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400833 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400834 return 0;
835 }
836
837 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
838
Yanbcc63ab2008-07-30 16:29:20 -0400839 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400840
841 /*
842 * insert an orphan item to track this unlinked/truncated file
843 */
844 ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
845
846 return ret;
847}
848
849/*
850 * We have done the truncate/delete so we can go ahead and remove the orphan
851 * item for this particular inode.
852 */
853int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
854{
855 struct btrfs_root *root = BTRFS_I(inode)->root;
856 int ret = 0;
857
Yanbcc63ab2008-07-30 16:29:20 -0400858 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400859
860 if (list_empty(&BTRFS_I(inode)->i_orphan)) {
Yanbcc63ab2008-07-30 16:29:20 -0400861 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400862 return 0;
863 }
864
865 list_del_init(&BTRFS_I(inode)->i_orphan);
866 if (!trans) {
Yanbcc63ab2008-07-30 16:29:20 -0400867 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400868 return 0;
869 }
870
Yanbcc63ab2008-07-30 16:29:20 -0400871 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400872
873 ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
874
875 return ret;
876}
877
878/*
879 * this cleans up any orphans that may be left on the list from the last use
880 * of this root.
881 */
882void btrfs_orphan_cleanup(struct btrfs_root *root)
883{
884 struct btrfs_path *path;
885 struct extent_buffer *leaf;
886 struct btrfs_item *item;
887 struct btrfs_key key, found_key;
888 struct btrfs_trans_handle *trans;
889 struct inode *inode;
890 int ret = 0, nr_unlink = 0, nr_truncate = 0;
891
892 /* don't do orphan cleanup if the fs is readonly. */
893 if (root->inode->i_sb->s_flags & MS_RDONLY)
894 return;
895
896 path = btrfs_alloc_path();
897 if (!path)
898 return;
899 path->reada = -1;
900
901 key.objectid = BTRFS_ORPHAN_OBJECTID;
902 btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
903 key.offset = (u64)-1;
904
905 trans = btrfs_start_transaction(root, 1);
906 btrfs_set_trans_block_group(trans, root->inode);
907
908 while (1) {
909 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
910 if (ret < 0) {
911 printk(KERN_ERR "Error searching slot for orphan: %d"
912 "\n", ret);
913 break;
914 }
915
916 /*
917 * if ret == 0 means we found what we were searching for, which
918 * is weird, but possible, so only screw with path if we didnt
919 * find the key and see if we have stuff that matches
920 */
921 if (ret > 0) {
922 if (path->slots[0] == 0)
923 break;
924 path->slots[0]--;
925 }
926
927 /* pull out the item */
928 leaf = path->nodes[0];
929 item = btrfs_item_nr(leaf, path->slots[0]);
930 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
931
932 /* make sure the item matches what we want */
933 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
934 break;
935 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
936 break;
937
938 /* release the path since we're done with it */
939 btrfs_release_path(root, path);
940
941 /*
942 * this is where we are basically btrfs_lookup, without the
943 * crossing root thing. we store the inode number in the
944 * offset of the orphan item.
945 */
946 inode = btrfs_iget_locked(root->inode->i_sb,
947 found_key.offset, root);
948 if (!inode)
949 break;
950
951 if (inode->i_state & I_NEW) {
952 BTRFS_I(inode)->root = root;
953
954 /* have to set the location manually */
955 BTRFS_I(inode)->location.objectid = inode->i_ino;
956 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
957 BTRFS_I(inode)->location.offset = 0;
958
959 btrfs_read_locked_inode(inode);
960 unlock_new_inode(inode);
961 }
962
963 /*
964 * add this inode to the orphan list so btrfs_orphan_del does
965 * the proper thing when we hit it
966 */
Yanbcc63ab2008-07-30 16:29:20 -0400967 spin_lock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400968 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
Yanbcc63ab2008-07-30 16:29:20 -0400969 spin_unlock(&root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -0400970
971 /*
972 * if this is a bad inode, means we actually succeeded in
973 * removing the inode, but not the orphan record, which means
974 * we need to manually delete the orphan since iput will just
975 * do a destroy_inode
976 */
977 if (is_bad_inode(inode)) {
978 btrfs_orphan_del(trans, inode);
979 iput(inode);
980 continue;
981 }
982
983 /* if we have links, this was a truncate, lets do that */
984 if (inode->i_nlink) {
985 nr_truncate++;
986 btrfs_truncate(inode);
987 } else {
988 nr_unlink++;
989 }
990
991 /* this will do delete_inode and everything for us */
992 iput(inode);
993 }
994
995 if (nr_unlink)
996 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
997 if (nr_truncate)
998 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
999
1000 btrfs_free_path(path);
1001 btrfs_end_transaction(trans, root);
1002}
1003
Chris Mason39279cc2007-06-12 06:35:45 -04001004void btrfs_read_locked_inode(struct inode *inode)
1005{
1006 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001007 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001008 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -04001009 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -04001010 struct btrfs_root *root = BTRFS_I(inode)->root;
1011 struct btrfs_key location;
1012 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -04001013 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -04001014 int ret;
1015
1016 path = btrfs_alloc_path();
1017 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001018 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -05001019
Chris Mason39279cc2007-06-12 06:35:45 -04001020 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04001021 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -04001022 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -04001023
Chris Mason5f39d392007-10-15 16:14:19 -04001024 leaf = path->nodes[0];
1025 inode_item = btrfs_item_ptr(leaf, path->slots[0],
1026 struct btrfs_inode_item);
1027
1028 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1029 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1030 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1031 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -04001032 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04001033
1034 tspec = btrfs_inode_atime(inode_item);
1035 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1036 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1037
1038 tspec = btrfs_inode_mtime(inode_item);
1039 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1040 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1041
1042 tspec = btrfs_inode_ctime(inode_item);
1043 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1044 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1045
1046 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1047 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -04001048 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001049 rdev = btrfs_inode_rdev(leaf, inode_item);
1050
Josef Bacikaec74772008-07-24 12:12:38 -04001051 BTRFS_I(inode)->index_cnt = (u64)-1;
1052
Chris Mason5f39d392007-10-15 16:14:19 -04001053 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -04001054 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1055 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -05001056 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -05001057 if (!BTRFS_I(inode)->block_group) {
1058 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -04001059 NULL, 0,
1060 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -05001061 }
Chris Mason39279cc2007-06-12 06:35:45 -04001062 btrfs_free_path(path);
1063 inode_item = NULL;
1064
Chris Mason39279cc2007-06-12 06:35:45 -04001065 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -04001066 case S_IFREG:
1067 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001068 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -05001069 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001070 inode->i_fop = &btrfs_file_operations;
1071 inode->i_op = &btrfs_file_inode_operations;
1072 break;
1073 case S_IFDIR:
1074 inode->i_fop = &btrfs_dir_file_operations;
1075 if (root == root->fs_info->tree_root)
1076 inode->i_op = &btrfs_dir_ro_inode_operations;
1077 else
1078 inode->i_op = &btrfs_dir_inode_operations;
1079 break;
1080 case S_IFLNK:
1081 inode->i_op = &btrfs_symlink_inode_operations;
1082 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04001083 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001084 break;
Josef Bacik618e21d2007-07-11 10:18:17 -04001085 default:
1086 init_special_inode(inode, inode->i_mode, rdev);
1087 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001088 }
1089 return;
1090
1091make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -04001092 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001093 make_bad_inode(inode);
1094}
1095
Chris Mason5f39d392007-10-15 16:14:19 -04001096static void fill_inode_item(struct extent_buffer *leaf,
1097 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -04001098 struct inode *inode)
1099{
Chris Mason5f39d392007-10-15 16:14:19 -04001100 btrfs_set_inode_uid(leaf, item, inode->i_uid);
1101 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -04001102 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -04001103 btrfs_set_inode_mode(leaf, item, inode->i_mode);
1104 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1105
1106 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1107 inode->i_atime.tv_sec);
1108 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1109 inode->i_atime.tv_nsec);
1110
1111 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1112 inode->i_mtime.tv_sec);
1113 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1114 inode->i_mtime.tv_nsec);
1115
1116 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1117 inode->i_ctime.tv_sec);
1118 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1119 inode->i_ctime.tv_nsec);
1120
1121 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1122 btrfs_set_inode_generation(leaf, item, inode->i_generation);
1123 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -05001124 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -04001125 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -04001126 BTRFS_I(inode)->block_group->key.objectid);
1127}
1128
Chris Masonba1da2f2008-07-17 12:54:15 -04001129int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -04001130 struct btrfs_root *root,
1131 struct inode *inode)
1132{
1133 struct btrfs_inode_item *inode_item;
1134 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -04001135 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001136 int ret;
1137
1138 path = btrfs_alloc_path();
1139 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001140 ret = btrfs_lookup_inode(trans, root, path,
1141 &BTRFS_I(inode)->location, 1);
1142 if (ret) {
1143 if (ret > 0)
1144 ret = -ENOENT;
1145 goto failed;
1146 }
1147
Chris Mason5f39d392007-10-15 16:14:19 -04001148 leaf = path->nodes[0];
1149 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001150 struct btrfs_inode_item);
1151
Chris Mason5f39d392007-10-15 16:14:19 -04001152 fill_inode_item(leaf, inode_item, inode);
1153 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -04001154 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001155 ret = 0;
1156failed:
Chris Mason39279cc2007-06-12 06:35:45 -04001157 btrfs_free_path(path);
1158 return ret;
1159}
1160
1161
1162static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
1163 struct btrfs_root *root,
1164 struct inode *dir,
1165 struct dentry *dentry)
1166{
1167 struct btrfs_path *path;
1168 const char *name = dentry->d_name.name;
1169 int name_len = dentry->d_name.len;
1170 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001171 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001172 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -04001173 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -04001174 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -04001175
1176 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001177 if (!path) {
1178 ret = -ENOMEM;
1179 goto err;
1180 }
1181
Chris Mason39279cc2007-06-12 06:35:45 -04001182 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1183 name, name_len, -1);
1184 if (IS_ERR(di)) {
1185 ret = PTR_ERR(di);
1186 goto err;
1187 }
1188 if (!di) {
1189 ret = -ENOENT;
1190 goto err;
1191 }
Chris Mason5f39d392007-10-15 16:14:19 -04001192 leaf = path->nodes[0];
1193 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001194 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001195 if (ret)
1196 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001197 btrfs_release_path(root, path);
1198
Josef Bacikaec74772008-07-24 12:12:38 -04001199 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1200 dentry->d_inode->i_ino,
1201 dentry->d_parent->d_inode->i_ino, &index);
1202 if (ret) {
1203 printk("failed to delete reference to %.*s, "
1204 "inode %lu parent %lu\n", name_len, name,
1205 dentry->d_inode->i_ino,
1206 dentry->d_parent->d_inode->i_ino);
1207 goto err;
1208 }
1209
Chris Mason39279cc2007-06-12 06:35:45 -04001210 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001211 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001212 if (IS_ERR(di)) {
1213 ret = PTR_ERR(di);
1214 goto err;
1215 }
1216 if (!di) {
1217 ret = -ENOENT;
1218 goto err;
1219 }
1220 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001221 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001222
1223 dentry->d_inode->i_ctime = dir->i_ctime;
1224err:
1225 btrfs_free_path(path);
1226 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001227 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001228 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001229 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001230#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1231 dentry->d_inode->i_nlink--;
1232#else
Chris Mason39279cc2007-06-12 06:35:45 -04001233 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001234#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001235 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001236 dir->i_sb->s_dirt = 1;
1237 }
1238 return ret;
1239}
1240
1241static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1242{
1243 struct btrfs_root *root;
1244 struct btrfs_trans_handle *trans;
Josef Bacik7b128762008-07-24 12:17:14 -04001245 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001246 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001247 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001248
1249 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001250
1251 ret = btrfs_check_free_space(root, 1, 1);
1252 if (ret)
1253 goto fail;
1254
Chris Mason39279cc2007-06-12 06:35:45 -04001255 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001256
Chris Mason39279cc2007-06-12 06:35:45 -04001257 btrfs_set_trans_block_group(trans, dir);
1258 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Josef Bacik7b128762008-07-24 12:17:14 -04001259
1260 if (inode->i_nlink == 0)
1261 ret = btrfs_orphan_add(trans, inode);
1262
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001263 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001264
Chris Mason89ce8a62008-06-25 16:01:31 -04001265 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001266fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001267 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001268 return ret;
1269}
1270
1271static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1272{
1273 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001274 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001275 int ret;
1276 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001277 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001278 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001279
Chris Mason925baed2008-06-25 16:01:30 -04001280 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001281 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001282 }
Yan134d4512007-10-25 15:49:25 -04001283
Chris Mason1832a6d2007-12-21 16:27:21 -05001284 ret = btrfs_check_free_space(root, 1, 1);
1285 if (ret)
1286 goto fail;
1287
Chris Mason39279cc2007-06-12 06:35:45 -04001288 trans = btrfs_start_transaction(root, 1);
1289 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001290
Josef Bacik7b128762008-07-24 12:17:14 -04001291 err = btrfs_orphan_add(trans, inode);
1292 if (err)
1293 goto fail_trans;
1294
Chris Mason39279cc2007-06-12 06:35:45 -04001295 /* now the directory is empty */
1296 err = btrfs_unlink_trans(trans, root, dir, dentry);
1297 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001298 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001299 }
Chris Mason39544012007-12-12 14:38:19 -05001300
Josef Bacik7b128762008-07-24 12:17:14 -04001301fail_trans:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001302 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001303 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001304fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001305 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001306
Chris Mason39279cc2007-06-12 06:35:45 -04001307 if (ret && !err)
1308 err = ret;
1309 return err;
1310}
1311
Chris Mason39279cc2007-06-12 06:35:45 -04001312/*
Chris Mason39279cc2007-06-12 06:35:45 -04001313 * this can truncate away extent items, csum items and directory items.
1314 * It starts at a high offset and removes keys until it can't find
1315 * any higher than i_size.
1316 *
1317 * csum items that cross the new i_size are truncated to the new size
1318 * as well.
Josef Bacik7b128762008-07-24 12:17:14 -04001319 *
1320 * min_type is the minimum key type to truncate down to. If set to 0, this
1321 * will kill all the items on this inode, including the INODE_ITEM_KEY.
Chris Mason39279cc2007-06-12 06:35:45 -04001322 */
1323static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1324 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001325 struct inode *inode,
1326 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001327{
1328 int ret;
1329 struct btrfs_path *path;
1330 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001331 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001332 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001333 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001334 struct btrfs_file_extent_item *fi;
1335 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001336 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001337 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001338 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001339 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001340 int found_extent;
1341 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001342 int pending_del_nr = 0;
1343 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001344 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001345 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001346
Chris Mason3b951512008-04-17 11:29:12 -04001347 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001348 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001349 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001350 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001351
Chris Mason39279cc2007-06-12 06:35:45 -04001352 /* FIXME, add redo link to tree so we don't leak on crash */
1353 key.objectid = inode->i_ino;
1354 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001355 key.type = (u8)-1;
1356
Chris Mason85e21ba2008-01-29 15:11:36 -05001357 btrfs_init_path(path);
1358search_again:
1359 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1360 if (ret < 0) {
1361 goto error;
1362 }
1363 if (ret > 0) {
1364 BUG_ON(path->slots[0] == 0);
1365 path->slots[0]--;
1366 }
1367
Chris Mason39279cc2007-06-12 06:35:45 -04001368 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001369 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001370 leaf = path->nodes[0];
1371 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1372 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001373
Chris Mason5f39d392007-10-15 16:14:19 -04001374 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001375 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001376
Chris Mason85e21ba2008-01-29 15:11:36 -05001377 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001378 break;
1379
Chris Mason5f39d392007-10-15 16:14:19 -04001380 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001381 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001382 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001383 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001384 extent_type = btrfs_file_extent_type(leaf, fi);
1385 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001386 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001387 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001388 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1389 struct btrfs_item *item = btrfs_item_nr(leaf,
1390 path->slots[0]);
1391 item_end += btrfs_file_extent_inline_len(leaf,
1392 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001393 }
Yan008630c2007-11-07 13:31:09 -05001394 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001395 }
1396 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1397 ret = btrfs_csum_truncate(trans, root, path,
1398 inode->i_size);
1399 BUG_ON(ret);
1400 }
Yan008630c2007-11-07 13:31:09 -05001401 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001402 if (found_type == BTRFS_DIR_ITEM_KEY) {
1403 found_type = BTRFS_INODE_ITEM_KEY;
1404 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1405 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001406 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1407 found_type = BTRFS_XATTR_ITEM_KEY;
1408 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1409 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001410 } else if (found_type) {
1411 found_type--;
1412 } else {
1413 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001414 }
Yana61721d2007-09-17 11:08:38 -04001415 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001416 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001417 }
Chris Mason5f39d392007-10-15 16:14:19 -04001418 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001419 del_item = 1;
1420 else
1421 del_item = 0;
1422 found_extent = 0;
1423
1424 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001425 if (found_type != BTRFS_EXTENT_DATA_KEY)
1426 goto delete;
1427
1428 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001429 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001430 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001431 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001432 u64 orig_num_bytes =
1433 btrfs_file_extent_num_bytes(leaf, fi);
1434 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001435 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001436 extent_num_bytes = extent_num_bytes &
1437 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001438 btrfs_set_file_extent_num_bytes(leaf, fi,
1439 extent_num_bytes);
1440 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001441 extent_num_bytes);
1442 if (extent_start != 0)
1443 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001444 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001445 } else {
Chris Masondb945352007-10-15 16:15:53 -04001446 extent_num_bytes =
1447 btrfs_file_extent_disk_num_bytes(leaf,
1448 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001449 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001450 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001451 if (extent_start != 0) {
1452 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001453 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001454 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001455 root_gen = btrfs_header_generation(leaf);
1456 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001457 }
Chris Mason90692182008-02-08 13:49:28 -05001458 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1459 if (!del_item) {
1460 u32 newsize = inode->i_size - found_key.offset;
1461 dec_i_blocks(inode, item_end + 1 -
1462 found_key.offset - newsize);
1463 newsize =
1464 btrfs_file_extent_calc_inline_size(newsize);
1465 ret = btrfs_truncate_item(trans, root, path,
1466 newsize, 1);
1467 BUG_ON(ret);
1468 } else {
1469 dec_i_blocks(inode, item_end + 1 -
1470 found_key.offset);
1471 }
Chris Mason39279cc2007-06-12 06:35:45 -04001472 }
Chris Mason179e29e2007-11-01 11:28:41 -04001473delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001474 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001475 if (!pending_del_nr) {
1476 /* no pending yet, add ourselves */
1477 pending_del_slot = path->slots[0];
1478 pending_del_nr = 1;
1479 } else if (pending_del_nr &&
1480 path->slots[0] + 1 == pending_del_slot) {
1481 /* hop on the pending chunk */
1482 pending_del_nr++;
1483 pending_del_slot = path->slots[0];
1484 } else {
1485 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1486 }
Chris Mason39279cc2007-06-12 06:35:45 -04001487 } else {
1488 break;
1489 }
Chris Mason39279cc2007-06-12 06:35:45 -04001490 if (found_extent) {
1491 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001492 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001493 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001494 root_gen, inode->i_ino,
1495 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001496 BUG_ON(ret);
1497 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001498next:
1499 if (path->slots[0] == 0) {
1500 if (pending_del_nr)
1501 goto del_pending;
1502 btrfs_release_path(root, path);
1503 goto search_again;
1504 }
1505
1506 path->slots[0]--;
1507 if (pending_del_nr &&
1508 path->slots[0] + 1 != pending_del_slot) {
1509 struct btrfs_key debug;
1510del_pending:
1511 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1512 pending_del_slot);
1513 ret = btrfs_del_items(trans, root, path,
1514 pending_del_slot,
1515 pending_del_nr);
1516 BUG_ON(ret);
1517 pending_del_nr = 0;
1518 btrfs_release_path(root, path);
1519 goto search_again;
1520 }
Chris Mason39279cc2007-06-12 06:35:45 -04001521 }
1522 ret = 0;
1523error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001524 if (pending_del_nr) {
1525 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1526 pending_del_nr);
1527 }
Chris Mason39279cc2007-06-12 06:35:45 -04001528 btrfs_free_path(path);
1529 inode->i_sb->s_dirt = 1;
1530 return ret;
1531}
1532
Chris Masona52d9a82007-08-27 16:49:44 -04001533/*
1534 * taken from block_truncate_page, but does cow as it zeros out
1535 * any bytes left in the last page in the file.
1536 */
1537static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1538{
1539 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001540 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001541 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1542 struct btrfs_ordered_extent *ordered;
1543 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001544 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001545 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1546 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1547 struct page *page;
1548 int ret = 0;
1549 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001550 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001551
1552 if ((offset & (blocksize - 1)) == 0)
1553 goto out;
1554
1555 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001556again:
Chris Masona52d9a82007-08-27 16:49:44 -04001557 page = grab_cache_page(mapping, index);
1558 if (!page)
1559 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001560
1561 page_start = page_offset(page);
1562 page_end = page_start + PAGE_CACHE_SIZE - 1;
1563
Chris Masona52d9a82007-08-27 16:49:44 -04001564 if (!PageUptodate(page)) {
1565 ret = btrfs_readpage(NULL, page);
1566 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001567 if (page->mapping != mapping) {
1568 unlock_page(page);
1569 page_cache_release(page);
1570 goto again;
1571 }
Chris Masona52d9a82007-08-27 16:49:44 -04001572 if (!PageUptodate(page)) {
1573 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001574 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001575 }
1576 }
Chris Mason211c17f2008-05-15 09:13:45 -04001577 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001578
1579 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1580 set_page_extent_mapped(page);
1581
1582 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1583 if (ordered) {
1584 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1585 unlock_page(page);
1586 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001587 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001588 btrfs_put_ordered_extent(ordered);
1589 goto again;
1590 }
1591
Chris Masonea8c2812008-08-04 23:17:27 -04001592 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001593 ret = 0;
1594 if (offset != PAGE_CACHE_SIZE) {
1595 kaddr = kmap(page);
1596 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1597 flush_dcache_page(page);
1598 kunmap(page);
1599 }
Chris Mason247e7432008-07-17 12:53:51 -04001600 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001601 set_page_dirty(page);
1602 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001603
Chris Mason89642222008-07-24 09:41:53 -04001604out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001605 unlock_page(page);
1606 page_cache_release(page);
1607out:
1608 return ret;
1609}
1610
1611static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1612{
1613 struct inode *inode = dentry->d_inode;
1614 int err;
1615
1616 err = inode_change_ok(inode, attr);
1617 if (err)
1618 return err;
1619
1620 if (S_ISREG(inode->i_mode) &&
1621 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1622 struct btrfs_trans_handle *trans;
1623 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001624 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001625
Chris Mason5f39d392007-10-15 16:14:19 -04001626 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001627 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001628 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001629 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001630 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001631
Chris Mason1b0f7c22008-01-30 14:33:02 -05001632 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001633 goto out;
1634
Chris Mason1832a6d2007-12-21 16:27:21 -05001635 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001636 if (err)
1637 goto fail;
1638
Chris Mason39279cc2007-06-12 06:35:45 -04001639 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1640
Chris Mason5f564062008-01-22 16:47:59 -05001641 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001642 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1643 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001644
Chris Mason39279cc2007-06-12 06:35:45 -04001645 trans = btrfs_start_transaction(root, 1);
1646 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001647 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001648 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001649 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001650 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001651
Chris Mason179e29e2007-11-01 11:28:41 -04001652 if (alloc_hint != EXTENT_MAP_INLINE) {
1653 err = btrfs_insert_file_extent(trans, root,
1654 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001655 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001656 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001657 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001658 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001659 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001660 }
Chris Masonee6e6502008-07-17 12:54:40 -04001661 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001662 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001663 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001664 if (err)
1665 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001666 }
1667out:
1668 err = inode_setattr(inode, attr);
Josef Bacik33268ea2008-07-24 12:16:36 -04001669
1670 if (!err && ((attr->ia_valid & ATTR_MODE)))
1671 err = btrfs_acl_chmod(inode);
Chris Mason1832a6d2007-12-21 16:27:21 -05001672fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001673 return err;
1674}
Chris Mason61295eb2008-01-14 16:24:38 -05001675
Chris Mason39279cc2007-06-12 06:35:45 -04001676void btrfs_delete_inode(struct inode *inode)
1677{
1678 struct btrfs_trans_handle *trans;
1679 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001680 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001681 int ret;
1682
1683 truncate_inode_pages(&inode->i_data, 0);
1684 if (is_bad_inode(inode)) {
Josef Bacik7b128762008-07-24 12:17:14 -04001685 btrfs_orphan_del(NULL, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001686 goto no_delete;
1687 }
Chris Mason4a096752008-07-21 10:29:44 -04001688 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001689
Chris Masondbe674a2008-07-17 12:54:05 -04001690 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001691 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001692
Chris Mason39279cc2007-06-12 06:35:45 -04001693 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001694 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Josef Bacik7b128762008-07-24 12:17:14 -04001695 if (ret) {
1696 btrfs_orphan_del(NULL, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04001697 goto no_delete_lock;
Josef Bacik7b128762008-07-24 12:17:14 -04001698 }
1699
1700 btrfs_orphan_del(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001701
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001702 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001703 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001704
Chris Mason39279cc2007-06-12 06:35:45 -04001705 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001706 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001707 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001708
1709no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001710 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001711 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001712 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001713no_delete:
1714 clear_inode(inode);
1715}
1716
1717/*
1718 * this returns the key found in the dir entry in the location pointer.
1719 * If no dir entries were found, location->objectid is 0.
1720 */
1721static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1722 struct btrfs_key *location)
1723{
1724 const char *name = dentry->d_name.name;
1725 int namelen = dentry->d_name.len;
1726 struct btrfs_dir_item *di;
1727 struct btrfs_path *path;
1728 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001729 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001730
Chris Mason39544012007-12-12 14:38:19 -05001731 if (namelen == 1 && strcmp(name, ".") == 0) {
1732 location->objectid = dir->i_ino;
1733 location->type = BTRFS_INODE_ITEM_KEY;
1734 location->offset = 0;
1735 return 0;
1736 }
Chris Mason39279cc2007-06-12 06:35:45 -04001737 path = btrfs_alloc_path();
1738 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001739
Chris Mason7a720532007-12-13 09:06:59 -05001740 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001741 struct btrfs_key key;
1742 struct extent_buffer *leaf;
Chris Mason39544012007-12-12 14:38:19 -05001743 int slot;
1744
1745 key.objectid = dir->i_ino;
Yan445dceb2008-07-24 12:19:32 -04001746 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001747 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001748 if (ret < 0 || path->slots[0] == 0)
1749 goto out_err;
Chris Mason39544012007-12-12 14:38:19 -05001750 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1751 BUG_ON(ret == 0);
1752 ret = 0;
Chris Mason39544012007-12-12 14:38:19 -05001753 leaf = path->nodes[0];
Yan445dceb2008-07-24 12:19:32 -04001754 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001755
1756 btrfs_item_key_to_cpu(leaf, &key, slot);
1757 if (key.objectid != dir->i_ino ||
1758 key.type != BTRFS_INODE_REF_KEY) {
1759 goto out_err;
1760 }
1761 location->objectid = key.offset;
1762 location->type = BTRFS_INODE_ITEM_KEY;
1763 location->offset = 0;
1764 goto out;
1765 }
1766
Chris Mason39279cc2007-06-12 06:35:45 -04001767 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1768 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001769 if (IS_ERR(di))
1770 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001771 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001772 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001773 }
Chris Mason5f39d392007-10-15 16:14:19 -04001774 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001775out:
Chris Mason39279cc2007-06-12 06:35:45 -04001776 btrfs_free_path(path);
1777 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001778out_err:
1779 location->objectid = 0;
1780 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001781}
1782
1783/*
1784 * when we hit a tree root in a directory, the btrfs part of the inode
1785 * needs to be changed to reflect the root directory of the tree root. This
1786 * is kind of like crossing a mount point.
1787 */
1788static int fixup_tree_root_location(struct btrfs_root *root,
1789 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001790 struct btrfs_root **sub_root,
1791 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001792{
Chris Mason39279cc2007-06-12 06:35:45 -04001793 struct btrfs_root_item *ri;
1794
1795 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1796 return 0;
1797 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1798 return 0;
1799
Josef Bacik58176a92007-08-29 15:47:34 -04001800 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1801 dentry->d_name.name,
1802 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001803 if (IS_ERR(*sub_root))
1804 return PTR_ERR(*sub_root);
1805
1806 ri = &(*sub_root)->root_item;
1807 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001808 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1809 location->offset = 0;
1810
Chris Mason39279cc2007-06-12 06:35:45 -04001811 return 0;
1812}
1813
1814static int btrfs_init_locked_inode(struct inode *inode, void *p)
1815{
1816 struct btrfs_iget_args *args = p;
1817 inode->i_ino = args->ino;
1818 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001819 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001820 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001821 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001822 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1823 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001824 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001825 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1826 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04001827 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Masonba1da2f2008-07-17 12:54:15 -04001828 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001829 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001830 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001831 return 0;
1832}
1833
1834static int btrfs_find_actor(struct inode *inode, void *opaque)
1835{
1836 struct btrfs_iget_args *args = opaque;
1837 return (args->ino == inode->i_ino &&
1838 args->root == BTRFS_I(inode)->root);
1839}
1840
Chris Masondc17ff82008-01-08 15:46:30 -05001841struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1842 u64 root_objectid)
1843{
1844 struct btrfs_iget_args args;
1845 args.ino = objectid;
1846 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1847
1848 if (!args.root)
1849 return NULL;
1850
1851 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1852}
1853
Chris Mason39279cc2007-06-12 06:35:45 -04001854struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1855 struct btrfs_root *root)
1856{
1857 struct inode *inode;
1858 struct btrfs_iget_args args;
1859 args.ino = objectid;
1860 args.root = root;
1861
1862 inode = iget5_locked(s, objectid, btrfs_find_actor,
1863 btrfs_init_locked_inode,
1864 (void *)&args);
1865 return inode;
1866}
1867
1868static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1869 struct nameidata *nd)
1870{
1871 struct inode * inode;
1872 struct btrfs_inode *bi = BTRFS_I(dir);
1873 struct btrfs_root *root = bi->root;
1874 struct btrfs_root *sub_root = root;
1875 struct btrfs_key location;
Josef Bacik7b128762008-07-24 12:17:14 -04001876 int ret, do_orphan = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001877
1878 if (dentry->d_name.len > BTRFS_NAME_LEN)
1879 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001880
Chris Mason39279cc2007-06-12 06:35:45 -04001881 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001882
Chris Mason39279cc2007-06-12 06:35:45 -04001883 if (ret < 0)
1884 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001885
Chris Mason39279cc2007-06-12 06:35:45 -04001886 inode = NULL;
1887 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001888 ret = fixup_tree_root_location(root, &location, &sub_root,
1889 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001890 if (ret < 0)
1891 return ERR_PTR(ret);
1892 if (ret > 0)
1893 return ERR_PTR(-ENOENT);
Josef Bacik7b128762008-07-24 12:17:14 -04001894
Chris Mason39279cc2007-06-12 06:35:45 -04001895 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1896 sub_root);
1897 if (!inode)
1898 return ERR_PTR(-EACCES);
1899 if (inode->i_state & I_NEW) {
1900 /* the inode and parent dir are two different roots */
1901 if (sub_root != root) {
1902 igrab(inode);
1903 sub_root->inode = inode;
Josef Bacik7b128762008-07-24 12:17:14 -04001904 do_orphan = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001905 }
1906 BTRFS_I(inode)->root = sub_root;
1907 memcpy(&BTRFS_I(inode)->location, &location,
1908 sizeof(location));
1909 btrfs_read_locked_inode(inode);
1910 unlock_new_inode(inode);
1911 }
1912 }
Josef Bacik7b128762008-07-24 12:17:14 -04001913
1914 if (unlikely(do_orphan))
1915 btrfs_orphan_cleanup(sub_root);
1916
Chris Mason39279cc2007-06-12 06:35:45 -04001917 return d_splice_alias(inode, dentry);
1918}
1919
Chris Mason39279cc2007-06-12 06:35:45 -04001920static unsigned char btrfs_filetype_table[] = {
1921 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1922};
1923
1924static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1925{
Chris Mason6da6aba2007-12-18 16:15:09 -05001926 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001927 struct btrfs_root *root = BTRFS_I(inode)->root;
1928 struct btrfs_item *item;
1929 struct btrfs_dir_item *di;
1930 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001931 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001932 struct btrfs_path *path;
1933 int ret;
1934 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001935 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001936 int slot;
1937 int advance;
1938 unsigned char d_type;
1939 int over = 0;
1940 u32 di_cur;
1941 u32 di_total;
1942 u32 di_len;
1943 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001944 char tmp_name[32];
1945 char *name_ptr;
1946 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001947
1948 /* FIXME, use a real flag for deciding about the key type */
1949 if (root->fs_info->tree_root == root)
1950 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001951
Chris Mason39544012007-12-12 14:38:19 -05001952 /* special case for "." */
1953 if (filp->f_pos == 0) {
1954 over = filldir(dirent, ".", 1,
1955 1, inode->i_ino,
1956 DT_DIR);
1957 if (over)
1958 return 0;
1959 filp->f_pos = 1;
1960 }
1961
Chris Mason39279cc2007-06-12 06:35:45 -04001962 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001963 path = btrfs_alloc_path();
1964 path->reada = 2;
1965
1966 /* special case for .., just use the back ref */
1967 if (filp->f_pos == 1) {
1968 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
Yan445dceb2008-07-24 12:19:32 -04001969 key.offset = (u64)-1;
Chris Mason39544012007-12-12 14:38:19 -05001970 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Yan445dceb2008-07-24 12:19:32 -04001971 if (ret < 0 || path->slots[0] == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001972 btrfs_release_path(root, path);
1973 goto read_dir_items;
1974 }
Yan445dceb2008-07-24 12:19:32 -04001975 BUG_ON(ret == 0);
1976 leaf = path->nodes[0];
1977 slot = path->slots[0] - 1;
Chris Mason39544012007-12-12 14:38:19 -05001978 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1979 btrfs_release_path(root, path);
1980 if (found_key.objectid != key.objectid ||
1981 found_key.type != BTRFS_INODE_REF_KEY)
1982 goto read_dir_items;
1983 over = filldir(dirent, "..", 2,
1984 2, found_key.offset, DT_DIR);
1985 if (over)
1986 goto nopos;
1987 filp->f_pos = 2;
1988 }
1989
1990read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001991 btrfs_set_key_type(&key, key_type);
1992 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001993
Chris Mason39279cc2007-06-12 06:35:45 -04001994 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1995 if (ret < 0)
1996 goto err;
1997 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001998 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001999 leaf = path->nodes[0];
2000 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002001 slot = path->slots[0];
2002 if (advance || slot >= nritems) {
2003 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04002004 ret = btrfs_next_leaf(root, path);
2005 if (ret)
2006 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002007 leaf = path->nodes[0];
2008 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002009 slot = path->slots[0];
2010 } else {
2011 slot++;
2012 path->slots[0]++;
2013 }
2014 }
2015 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002016 item = btrfs_item_nr(leaf, slot);
2017 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2018
2019 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04002020 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002021 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04002022 break;
Chris Mason5f39d392007-10-15 16:14:19 -04002023 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04002024 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04002025
2026 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04002027 advance = 1;
2028 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2029 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002030 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04002031 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04002032 struct btrfs_key location;
2033
2034 name_len = btrfs_dir_name_len(leaf, di);
2035 if (name_len < 32) {
2036 name_ptr = tmp_name;
2037 } else {
2038 name_ptr = kmalloc(name_len, GFP_NOFS);
2039 BUG_ON(!name_ptr);
2040 }
2041 read_extent_buffer(leaf, name_ptr,
2042 (unsigned long)(di + 1), name_len);
2043
2044 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2045 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04002046 over = filldir(dirent, name_ptr, name_len,
2047 found_key.offset,
2048 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002049 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04002050
2051 if (name_ptr != tmp_name)
2052 kfree(name_ptr);
2053
Chris Mason39279cc2007-06-12 06:35:45 -04002054 if (over)
2055 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05002056 di_len = btrfs_dir_name_len(leaf, di) +
2057 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04002058 di_cur += di_len;
2059 di = (struct btrfs_dir_item *)((char *)di + di_len);
2060 }
2061 }
Yan Zheng5e591a02008-02-19 11:41:02 -05002062 if (key_type == BTRFS_DIR_INDEX_KEY)
2063 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2064 else
2065 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04002066nopos:
2067 ret = 0;
2068err:
Chris Mason39279cc2007-06-12 06:35:45 -04002069 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04002070 return ret;
2071}
2072
2073int btrfs_write_inode(struct inode *inode, int wait)
2074{
2075 struct btrfs_root *root = BTRFS_I(inode)->root;
2076 struct btrfs_trans_handle *trans;
2077 int ret = 0;
2078
2079 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04002080 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002081 btrfs_set_trans_block_group(trans, inode);
2082 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002083 }
2084 return ret;
2085}
2086
2087/*
Chris Mason54aa1f42007-06-22 14:16:25 -04002088 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04002089 * inode changes. But, it is most likely to find the inode in cache.
2090 * FIXME, needs more benchmarking...there are no reasons other than performance
2091 * to keep or drop this code.
2092 */
2093void btrfs_dirty_inode(struct inode *inode)
2094{
2095 struct btrfs_root *root = BTRFS_I(inode)->root;
2096 struct btrfs_trans_handle *trans;
2097
Chris Masonf9295742008-07-17 12:54:14 -04002098 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002099 btrfs_set_trans_block_group(trans, inode);
2100 btrfs_update_inode(trans, root, inode);
2101 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002102}
2103
Josef Bacikaec74772008-07-24 12:12:38 -04002104static int btrfs_set_inode_index_count(struct inode *inode)
2105{
2106 struct btrfs_root *root = BTRFS_I(inode)->root;
2107 struct btrfs_key key, found_key;
2108 struct btrfs_path *path;
2109 struct extent_buffer *leaf;
2110 int ret;
2111
2112 key.objectid = inode->i_ino;
2113 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2114 key.offset = (u64)-1;
2115
2116 path = btrfs_alloc_path();
2117 if (!path)
2118 return -ENOMEM;
2119
2120 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2121 if (ret < 0)
2122 goto out;
2123 /* FIXME: we should be able to handle this */
2124 if (ret == 0)
2125 goto out;
2126 ret = 0;
2127
2128 /*
2129 * MAGIC NUMBER EXPLANATION:
2130 * since we search a directory based on f_pos we have to start at 2
2131 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2132 * else has to start at 2
2133 */
2134 if (path->slots[0] == 0) {
2135 BTRFS_I(inode)->index_cnt = 2;
2136 goto out;
2137 }
2138
2139 path->slots[0]--;
2140
2141 leaf = path->nodes[0];
2142 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2143
2144 if (found_key.objectid != inode->i_ino ||
2145 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2146 BTRFS_I(inode)->index_cnt = 2;
2147 goto out;
2148 }
2149
2150 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2151out:
2152 btrfs_free_path(path);
2153 return ret;
2154}
2155
Chris Mason00e4e6b2008-08-05 11:18:09 -04002156static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2157 u64 *index)
Josef Bacikaec74772008-07-24 12:12:38 -04002158{
2159 int ret = 0;
2160
2161 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2162 ret = btrfs_set_inode_index_count(dir);
2163 if (ret)
2164 return ret;
2165 }
2166
Chris Mason00e4e6b2008-08-05 11:18:09 -04002167 *index = BTRFS_I(dir)->index_cnt;
Josef Bacikaec74772008-07-24 12:12:38 -04002168 BTRFS_I(dir)->index_cnt++;
2169
2170 return ret;
2171}
2172
Chris Mason39279cc2007-06-12 06:35:45 -04002173static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2174 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04002175 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05002176 const char *name, int name_len,
2177 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002178 u64 objectid,
2179 struct btrfs_block_group_cache *group,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002180 int mode, u64 *index)
Chris Mason39279cc2007-06-12 06:35:45 -04002181{
2182 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002183 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04002184 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04002185 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04002186 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05002187 struct btrfs_inode_ref *ref;
2188 struct btrfs_key key[2];
2189 u32 sizes[2];
2190 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002191 int ret;
2192 int owner;
2193
Chris Mason5f39d392007-10-15 16:14:19 -04002194 path = btrfs_alloc_path();
2195 BUG_ON(!path);
2196
Chris Mason39279cc2007-06-12 06:35:45 -04002197 inode = new_inode(root->fs_info->sb);
2198 if (!inode)
2199 return ERR_PTR(-ENOMEM);
2200
Josef Bacikaec74772008-07-24 12:12:38 -04002201 if (dir) {
Chris Mason00e4e6b2008-08-05 11:18:09 -04002202 ret = btrfs_set_inode_index(dir, inode, index);
Josef Bacikaec74772008-07-24 12:12:38 -04002203 if (ret)
2204 return ERR_PTR(ret);
Josef Bacikaec74772008-07-24 12:12:38 -04002205 }
2206 /*
2207 * index_cnt is ignored for everything but a dir,
2208 * btrfs_get_inode_index_count has an explanation for the magic
2209 * number
2210 */
2211 BTRFS_I(inode)->index_cnt = 2;
2212
Chris Masond1310b22008-01-24 16:13:08 -05002213 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2214 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002215 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002216 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2217 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002218 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Masonea8c2812008-08-04 23:17:27 -04002219 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04002220 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002221 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002222 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002223 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002224 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002225
Chris Mason39279cc2007-06-12 06:35:45 -04002226 if (mode & S_IFDIR)
2227 owner = 0;
2228 else
2229 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002230 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002231 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002232 if (!new_inode_group) {
2233 printk("find_block group failed\n");
2234 new_inode_group = group;
2235 }
2236 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002237 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002238
2239 key[0].objectid = objectid;
2240 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2241 key[0].offset = 0;
2242
2243 key[1].objectid = objectid;
2244 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2245 key[1].offset = ref_objectid;
2246
2247 sizes[0] = sizeof(struct btrfs_inode_item);
2248 sizes[1] = name_len + sizeof(*ref);
2249
2250 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2251 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002252 goto fail;
2253
Chris Mason9c583092008-01-29 15:15:18 -05002254 if (objectid > root->highest_inode)
2255 root->highest_inode = objectid;
2256
Chris Mason39279cc2007-06-12 06:35:45 -04002257 inode->i_uid = current->fsuid;
2258 inode->i_gid = current->fsgid;
2259 inode->i_mode = mode;
2260 inode->i_ino = objectid;
2261 inode->i_blocks = 0;
2262 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002263 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2264 struct btrfs_inode_item);
2265 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002266
2267 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2268 struct btrfs_inode_ref);
2269 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002270 btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
Chris Mason9c583092008-01-29 15:15:18 -05002271 ptr = (unsigned long)(ref + 1);
2272 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2273
Chris Mason5f39d392007-10-15 16:14:19 -04002274 btrfs_mark_buffer_dirty(path->nodes[0]);
2275 btrfs_free_path(path);
2276
Chris Mason39279cc2007-06-12 06:35:45 -04002277 location = &BTRFS_I(inode)->location;
2278 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002279 location->offset = 0;
2280 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2281
Chris Mason39279cc2007-06-12 06:35:45 -04002282 insert_inode_hash(inode);
2283 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002284fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002285 if (dir)
2286 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002287 btrfs_free_path(path);
2288 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002289}
2290
2291static inline u8 btrfs_inode_type(struct inode *inode)
2292{
2293 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2294}
2295
2296static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002297 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002298 int add_backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002299{
2300 int ret;
2301 struct btrfs_key key;
2302 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002303 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002304
Chris Mason39279cc2007-06-12 06:35:45 -04002305 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002306 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2307 key.offset = 0;
2308
2309 ret = btrfs_insert_dir_item(trans, root,
2310 dentry->d_name.name, dentry->d_name.len,
2311 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002312 &key, btrfs_inode_type(inode),
Chris Mason00e4e6b2008-08-05 11:18:09 -04002313 index);
Chris Mason39279cc2007-06-12 06:35:45 -04002314 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002315 if (add_backref) {
2316 ret = btrfs_insert_inode_ref(trans, root,
2317 dentry->d_name.name,
2318 dentry->d_name.len,
2319 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002320 parent_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002321 index);
Chris Mason9c583092008-01-29 15:15:18 -05002322 }
Chris Masondbe674a2008-07-17 12:54:05 -04002323 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2324 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002325 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002326 ret = btrfs_update_inode(trans, root,
2327 dentry->d_parent->d_inode);
2328 }
2329 return ret;
2330}
2331
2332static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002333 struct dentry *dentry, struct inode *inode,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002334 int backref, u64 index)
Chris Mason39279cc2007-06-12 06:35:45 -04002335{
Chris Mason00e4e6b2008-08-05 11:18:09 -04002336 int err = btrfs_add_link(trans, dentry, inode, backref, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002337 if (!err) {
2338 d_instantiate(dentry, inode);
2339 return 0;
2340 }
2341 if (err > 0)
2342 err = -EEXIST;
2343 return err;
2344}
2345
Josef Bacik618e21d2007-07-11 10:18:17 -04002346static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2347 int mode, dev_t rdev)
2348{
2349 struct btrfs_trans_handle *trans;
2350 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002351 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002352 int err;
2353 int drop_inode = 0;
2354 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002355 unsigned long nr = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002356 u64 index = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002357
2358 if (!new_valid_dev(rdev))
2359 return -EINVAL;
2360
Chris Mason1832a6d2007-12-21 16:27:21 -05002361 err = btrfs_check_free_space(root, 1, 0);
2362 if (err)
2363 goto fail;
2364
Josef Bacik618e21d2007-07-11 10:18:17 -04002365 trans = btrfs_start_transaction(root, 1);
2366 btrfs_set_trans_block_group(trans, dir);
2367
2368 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2369 if (err) {
2370 err = -ENOSPC;
2371 goto out_unlock;
2372 }
2373
Josef Bacikaec74772008-07-24 12:12:38 -04002374 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002375 dentry->d_name.len,
2376 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002377 BTRFS_I(dir)->block_group, mode, &index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002378 err = PTR_ERR(inode);
2379 if (IS_ERR(inode))
2380 goto out_unlock;
2381
Josef Bacik33268ea2008-07-24 12:16:36 -04002382 err = btrfs_init_acl(inode, dir);
2383 if (err) {
2384 drop_inode = 1;
2385 goto out_unlock;
2386 }
2387
Josef Bacik618e21d2007-07-11 10:18:17 -04002388 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002389 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Josef Bacik618e21d2007-07-11 10:18:17 -04002390 if (err)
2391 drop_inode = 1;
2392 else {
2393 inode->i_op = &btrfs_special_inode_operations;
2394 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002395 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002396 }
2397 dir->i_sb->s_dirt = 1;
2398 btrfs_update_inode_block_group(trans, inode);
2399 btrfs_update_inode_block_group(trans, dir);
2400out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002401 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002402 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002403fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002404 if (drop_inode) {
2405 inode_dec_link_count(inode);
2406 iput(inode);
2407 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002408 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002409 return err;
2410}
2411
Chris Mason39279cc2007-06-12 06:35:45 -04002412static int btrfs_create(struct inode *dir, struct dentry *dentry,
2413 int mode, struct nameidata *nd)
2414{
2415 struct btrfs_trans_handle *trans;
2416 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002417 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002418 int err;
2419 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002420 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002421 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002422 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002423
Chris Mason1832a6d2007-12-21 16:27:21 -05002424 err = btrfs_check_free_space(root, 1, 0);
2425 if (err)
2426 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002427 trans = btrfs_start_transaction(root, 1);
2428 btrfs_set_trans_block_group(trans, dir);
2429
2430 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2431 if (err) {
2432 err = -ENOSPC;
2433 goto out_unlock;
2434 }
2435
Josef Bacikaec74772008-07-24 12:12:38 -04002436 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002437 dentry->d_name.len,
2438 dentry->d_parent->d_inode->i_ino,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002439 objectid, BTRFS_I(dir)->block_group, mode,
2440 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002441 err = PTR_ERR(inode);
2442 if (IS_ERR(inode))
2443 goto out_unlock;
2444
Josef Bacik33268ea2008-07-24 12:16:36 -04002445 err = btrfs_init_acl(inode, dir);
2446 if (err) {
2447 drop_inode = 1;
2448 goto out_unlock;
2449 }
2450
Chris Mason39279cc2007-06-12 06:35:45 -04002451 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04002452 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002453 if (err)
2454 drop_inode = 1;
2455 else {
2456 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002457 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002458 inode->i_fop = &btrfs_file_operations;
2459 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002460 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2461 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002462 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002463 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2464 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04002465 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04002466 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002467 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002468 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002469 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002470 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002471 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002472 }
2473 dir->i_sb->s_dirt = 1;
2474 btrfs_update_inode_block_group(trans, inode);
2475 btrfs_update_inode_block_group(trans, dir);
2476out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002477 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002478 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002479fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002480 if (drop_inode) {
2481 inode_dec_link_count(inode);
2482 iput(inode);
2483 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002484 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002485 return err;
2486}
2487
2488static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2489 struct dentry *dentry)
2490{
2491 struct btrfs_trans_handle *trans;
2492 struct btrfs_root *root = BTRFS_I(dir)->root;
2493 struct inode *inode = old_dentry->d_inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002494 u64 index;
Chris Mason1832a6d2007-12-21 16:27:21 -05002495 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002496 int err;
2497 int drop_inode = 0;
2498
2499 if (inode->i_nlink == 0)
2500 return -ENOENT;
2501
Chris Mason6da6aba2007-12-18 16:15:09 -05002502#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2503 inode->i_nlink++;
2504#else
Chris Mason39279cc2007-06-12 06:35:45 -04002505 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002506#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002507 err = btrfs_check_free_space(root, 1, 0);
2508 if (err)
2509 goto fail;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002510 err = btrfs_set_inode_index(dir, inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04002511 if (err)
2512 goto fail;
2513
Chris Mason39279cc2007-06-12 06:35:45 -04002514 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002515
Chris Mason39279cc2007-06-12 06:35:45 -04002516 btrfs_set_trans_block_group(trans, dir);
2517 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002518
Chris Mason00e4e6b2008-08-05 11:18:09 -04002519 err = btrfs_add_nondir(trans, dentry, inode, 1, index);
Chris Mason5f39d392007-10-15 16:14:19 -04002520
Chris Mason39279cc2007-06-12 06:35:45 -04002521 if (err)
2522 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002523
Chris Mason39279cc2007-06-12 06:35:45 -04002524 dir->i_sb->s_dirt = 1;
2525 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002526 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002527
Chris Mason54aa1f42007-06-22 14:16:25 -04002528 if (err)
2529 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002530
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002531 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002532 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002533fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002534 if (drop_inode) {
2535 inode_dec_link_count(inode);
2536 iput(inode);
2537 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002538 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002539 return err;
2540}
2541
Chris Mason39279cc2007-06-12 06:35:45 -04002542static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2543{
Chris Masonb9d86662008-05-02 16:13:49 -04002544 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002545 struct btrfs_trans_handle *trans;
2546 struct btrfs_root *root = BTRFS_I(dir)->root;
2547 int err = 0;
2548 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002549 u64 objectid = 0;
Chris Mason00e4e6b2008-08-05 11:18:09 -04002550 u64 index = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002551 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002552
Chris Mason1832a6d2007-12-21 16:27:21 -05002553 err = btrfs_check_free_space(root, 1, 0);
2554 if (err)
2555 goto out_unlock;
2556
Chris Mason39279cc2007-06-12 06:35:45 -04002557 trans = btrfs_start_transaction(root, 1);
2558 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002559
Chris Mason39279cc2007-06-12 06:35:45 -04002560 if (IS_ERR(trans)) {
2561 err = PTR_ERR(trans);
2562 goto out_unlock;
2563 }
2564
2565 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2566 if (err) {
2567 err = -ENOSPC;
2568 goto out_unlock;
2569 }
2570
Josef Bacikaec74772008-07-24 12:12:38 -04002571 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002572 dentry->d_name.len,
2573 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04002574 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2575 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04002576 if (IS_ERR(inode)) {
2577 err = PTR_ERR(inode);
2578 goto out_fail;
2579 }
Chris Mason5f39d392007-10-15 16:14:19 -04002580
Chris Mason39279cc2007-06-12 06:35:45 -04002581 drop_on_err = 1;
Josef Bacik33268ea2008-07-24 12:16:36 -04002582
2583 err = btrfs_init_acl(inode, dir);
2584 if (err)
2585 goto out_fail;
2586
Chris Mason39279cc2007-06-12 06:35:45 -04002587 inode->i_op = &btrfs_dir_inode_operations;
2588 inode->i_fop = &btrfs_dir_file_operations;
2589 btrfs_set_trans_block_group(trans, inode);
2590
Chris Masondbe674a2008-07-17 12:54:05 -04002591 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002592 err = btrfs_update_inode(trans, root, inode);
2593 if (err)
2594 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002595
Chris Mason00e4e6b2008-08-05 11:18:09 -04002596 err = btrfs_add_link(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04002597 if (err)
2598 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002599
Chris Mason39279cc2007-06-12 06:35:45 -04002600 d_instantiate(dentry, inode);
2601 drop_on_err = 0;
2602 dir->i_sb->s_dirt = 1;
2603 btrfs_update_inode_block_group(trans, inode);
2604 btrfs_update_inode_block_group(trans, dir);
2605
2606out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002607 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04002608 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002609
Chris Mason39279cc2007-06-12 06:35:45 -04002610out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002611 if (drop_on_err)
2612 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002613 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002614 return err;
2615}
2616
Chris Mason3b951512008-04-17 11:29:12 -04002617static int merge_extent_mapping(struct extent_map_tree *em_tree,
2618 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002619 struct extent_map *em,
2620 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002621{
2622 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002623
Chris Masone6dcd2d2008-07-17 12:53:50 -04002624 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2625 start_diff = map_start - em->start;
2626 em->start = map_start;
2627 em->len = map_len;
2628 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2629 em->block_start += start_diff;
2630 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002631}
2632
Chris Masona52d9a82007-08-27 16:49:44 -04002633struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002634 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002635 int create)
2636{
2637 int ret;
2638 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002639 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002640 u64 extent_start = 0;
2641 u64 extent_end = 0;
2642 u64 objectid = inode->i_ino;
2643 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002644 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002645 struct btrfs_root *root = BTRFS_I(inode)->root;
2646 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002647 struct extent_buffer *leaf;
2648 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002649 struct extent_map *em = NULL;
2650 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002651 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002652 struct btrfs_trans_handle *trans = NULL;
2653
Chris Masona52d9a82007-08-27 16:49:44 -04002654again:
Chris Masond1310b22008-01-24 16:13:08 -05002655 spin_lock(&em_tree->lock);
2656 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002657 if (em)
2658 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002659 spin_unlock(&em_tree->lock);
2660
Chris Masona52d9a82007-08-27 16:49:44 -04002661 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002662 if (em->start > start || em->start + em->len <= start)
2663 free_extent_map(em);
2664 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002665 free_extent_map(em);
2666 else
2667 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002668 }
Chris Masond1310b22008-01-24 16:13:08 -05002669 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002670 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002671 err = -ENOMEM;
2672 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002673 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002674 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002675 em->start = EXTENT_MAP_HOLE;
2676 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002677
2678 if (!path) {
2679 path = btrfs_alloc_path();
2680 BUG_ON(!path);
2681 }
2682
Chris Mason179e29e2007-11-01 11:28:41 -04002683 ret = btrfs_lookup_file_extent(trans, root, path,
2684 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002685 if (ret < 0) {
2686 err = ret;
2687 goto out;
2688 }
2689
2690 if (ret != 0) {
2691 if (path->slots[0] == 0)
2692 goto not_found;
2693 path->slots[0]--;
2694 }
2695
Chris Mason5f39d392007-10-15 16:14:19 -04002696 leaf = path->nodes[0];
2697 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002698 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002699 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002700 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2701 found_type = btrfs_key_type(&found_key);
2702 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002703 found_type != BTRFS_EXTENT_DATA_KEY) {
2704 goto not_found;
2705 }
2706
Chris Mason5f39d392007-10-15 16:14:19 -04002707 found_type = btrfs_file_extent_type(leaf, item);
2708 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002709 if (found_type == BTRFS_FILE_EXTENT_REG) {
2710 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002711 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002712 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002713 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002714 em->start = start;
2715 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002716 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002717 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002718 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002719 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002720 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002721 }
2722 goto not_found_em;
2723 }
Chris Masondb945352007-10-15 16:15:53 -04002724 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2725 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002726 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002727 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002728 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002729 goto insert;
2730 }
Chris Masondb945352007-10-15 16:15:53 -04002731 bytenr += btrfs_file_extent_offset(leaf, item);
2732 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002733 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002734 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002735 goto insert;
2736 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002737 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002738 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002739 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002740 size_t size;
2741 size_t extent_offset;
2742 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002743
Chris Mason5f39d392007-10-15 16:14:19 -04002744 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2745 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002746 extent_end = (extent_start + size + root->sectorsize - 1) &
2747 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002748 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002749 em->start = start;
2750 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002751 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002752 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002753 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002754 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002755 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002756 }
2757 goto not_found_em;
2758 }
2759 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002760
2761 if (!page) {
2762 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002763 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002764 goto out;
2765 }
2766
Chris Mason70dec802008-01-29 09:59:12 -05002767 page_start = page_offset(page) + pg_offset;
2768 extent_offset = page_start - extent_start;
2769 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002770 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002771 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002772 em->len = (copy_size + root->sectorsize - 1) &
2773 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002774 map = kmap(page);
2775 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002776 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002777 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002778 copy_size);
2779 flush_dcache_page(page);
2780 } else if (create && PageUptodate(page)) {
2781 if (!trans) {
2782 kunmap(page);
2783 free_extent_map(em);
2784 em = NULL;
2785 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002786 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002787 goto again;
2788 }
Chris Mason70dec802008-01-29 09:59:12 -05002789 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002790 copy_size);
2791 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002792 }
Chris Masona52d9a82007-08-27 16:49:44 -04002793 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002794 set_extent_uptodate(io_tree, em->start,
2795 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002796 goto insert;
2797 } else {
2798 printk("unkknown found_type %d\n", found_type);
2799 WARN_ON(1);
2800 }
2801not_found:
2802 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002803 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002804not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002805 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002806insert:
2807 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002808 if (em->start > start || extent_map_end(em) <= start) {
2809 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002810 err = -EIO;
2811 goto out;
2812 }
Chris Masond1310b22008-01-24 16:13:08 -05002813
2814 err = 0;
2815 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002816 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002817 /* it is possible that someone inserted the extent into the tree
2818 * while we had the lock dropped. It is also possible that
2819 * an overlapping map exists in the tree
2820 */
Chris Masona52d9a82007-08-27 16:49:44 -04002821 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002822 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002823
2824 ret = 0;
2825
Chris Mason3b951512008-04-17 11:29:12 -04002826 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002827 if (existing && (existing->start > start ||
2828 existing->start + existing->len <= start)) {
2829 free_extent_map(existing);
2830 existing = NULL;
2831 }
Chris Mason3b951512008-04-17 11:29:12 -04002832 if (!existing) {
2833 existing = lookup_extent_mapping(em_tree, em->start,
2834 em->len);
2835 if (existing) {
2836 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002837 em, start,
2838 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002839 free_extent_map(existing);
2840 if (err) {
2841 free_extent_map(em);
2842 em = NULL;
2843 }
2844 } else {
2845 err = -EIO;
2846 printk("failing to insert %Lu %Lu\n",
2847 start, len);
2848 free_extent_map(em);
2849 em = NULL;
2850 }
2851 } else {
2852 free_extent_map(em);
2853 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002854 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002855 }
Chris Masona52d9a82007-08-27 16:49:44 -04002856 }
Chris Masond1310b22008-01-24 16:13:08 -05002857 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002858out:
Chris Masonf4219502008-07-22 11:18:09 -04002859 if (path)
2860 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002861 if (trans) {
2862 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002863 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002864 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002865 }
Chris Masona52d9a82007-08-27 16:49:44 -04002866 }
Chris Masona52d9a82007-08-27 16:49:44 -04002867 if (err) {
2868 free_extent_map(em);
2869 WARN_ON(1);
2870 return ERR_PTR(err);
2871 }
2872 return em;
2873}
2874
Chris Masone1c4b742008-04-22 13:26:46 -04002875#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002876static int btrfs_get_block(struct inode *inode, sector_t iblock,
2877 struct buffer_head *bh_result, int create)
2878{
2879 struct extent_map *em;
2880 u64 start = (u64)iblock << inode->i_blkbits;
2881 struct btrfs_multi_bio *multi = NULL;
2882 struct btrfs_root *root = BTRFS_I(inode)->root;
2883 u64 len;
2884 u64 logical;
2885 u64 map_length;
2886 int ret = 0;
2887
2888 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2889
2890 if (!em || IS_ERR(em))
2891 goto out;
2892
Chris Masone1c4b742008-04-22 13:26:46 -04002893 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002894 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002895 }
Chris Mason16432982008-04-10 10:23:21 -04002896
2897 if (em->block_start == EXTENT_MAP_INLINE) {
2898 ret = -EINVAL;
2899 goto out;
2900 }
2901
Chris Mason16432982008-04-10 10:23:21 -04002902 len = em->start + em->len - start;
2903 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2904
Chris Masone1c4b742008-04-22 13:26:46 -04002905 if (em->block_start == EXTENT_MAP_HOLE ||
2906 em->block_start == EXTENT_MAP_DELALLOC) {
2907 bh_result->b_size = len;
2908 goto out;
2909 }
2910
Chris Mason16432982008-04-10 10:23:21 -04002911 logical = start - em->start;
2912 logical = em->block_start + logical;
2913
2914 map_length = len;
2915 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2916 logical, &map_length, &multi, 0);
2917 BUG_ON(ret);
2918 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2919 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002920
Chris Mason16432982008-04-10 10:23:21 -04002921 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2922 set_buffer_mapped(bh_result);
2923 kfree(multi);
2924out:
2925 free_extent_map(em);
2926 return ret;
2927}
Chris Masone1c4b742008-04-22 13:26:46 -04002928#endif
Chris Mason16432982008-04-10 10:23:21 -04002929
2930static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2931 const struct iovec *iov, loff_t offset,
2932 unsigned long nr_segs)
2933{
Chris Masone1c4b742008-04-22 13:26:46 -04002934 return -EINVAL;
2935#if 0
Chris Mason16432982008-04-10 10:23:21 -04002936 struct file *file = iocb->ki_filp;
2937 struct inode *inode = file->f_mapping->host;
2938
2939 if (rw == WRITE)
2940 return -EINVAL;
2941
2942 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2943 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002944#endif
Chris Mason16432982008-04-10 10:23:21 -04002945}
2946
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002947static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002948{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002949 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002950}
2951
Chris Mason9ebefb182007-06-15 13:50:00 -04002952int btrfs_readpage(struct file *file, struct page *page)
2953{
Chris Masond1310b22008-01-24 16:13:08 -05002954 struct extent_io_tree *tree;
2955 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002956 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002957}
Chris Mason1832a6d2007-12-21 16:27:21 -05002958
Chris Mason39279cc2007-06-12 06:35:45 -04002959static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2960{
Chris Masond1310b22008-01-24 16:13:08 -05002961 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002962
2963
2964 if (current->flags & PF_MEMALLOC) {
2965 redirty_page_for_writepage(wbc, page);
2966 unlock_page(page);
2967 return 0;
2968 }
Chris Masond1310b22008-01-24 16:13:08 -05002969 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002970 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2971}
Chris Mason39279cc2007-06-12 06:35:45 -04002972
Chris Masonf4219502008-07-22 11:18:09 -04002973int btrfs_writepages(struct address_space *mapping,
2974 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04002975{
Chris Masond1310b22008-01-24 16:13:08 -05002976 struct extent_io_tree *tree;
2977 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002978 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2979}
2980
Chris Mason3ab2fb52007-11-08 10:59:22 -05002981static int
2982btrfs_readpages(struct file *file, struct address_space *mapping,
2983 struct list_head *pages, unsigned nr_pages)
2984{
Chris Masond1310b22008-01-24 16:13:08 -05002985 struct extent_io_tree *tree;
2986 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002987 return extent_readpages(tree, mapping, pages, nr_pages,
2988 btrfs_get_extent);
2989}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002990static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002991{
Chris Masond1310b22008-01-24 16:13:08 -05002992 struct extent_io_tree *tree;
2993 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002994 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002995
Chris Masond1310b22008-01-24 16:13:08 -05002996 tree = &BTRFS_I(page->mapping->host)->io_tree;
2997 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002998 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002999 if (ret == 1) {
3000 ClearPagePrivate(page);
3001 set_page_private(page, 0);
3002 page_cache_release(page);
3003 }
3004 return ret;
3005}
Chris Mason39279cc2007-06-12 06:35:45 -04003006
Chris Masone6dcd2d2008-07-17 12:53:50 -04003007static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3008{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003009 return __btrfs_releasepage(page, gfp_flags);
3010}
3011
Chris Masona52d9a82007-08-27 16:49:44 -04003012static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3013{
Chris Masond1310b22008-01-24 16:13:08 -05003014 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003015 struct btrfs_ordered_extent *ordered;
3016 u64 page_start = page_offset(page);
3017 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003018
Chris Masone6dcd2d2008-07-17 12:53:50 -04003019 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05003020 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003021 if (offset) {
3022 btrfs_releasepage(page, GFP_NOFS);
3023 return;
3024 }
3025
3026 lock_extent(tree, page_start, page_end, GFP_NOFS);
3027 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3028 page_offset(page));
3029 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04003030 /*
3031 * IO on this page will never be started, so we need
3032 * to account for any ordered extents now
3033 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003034 clear_extent_bit(tree, page_start, page_end,
3035 EXTENT_DIRTY | EXTENT_DELALLOC |
3036 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04003037 btrfs_finish_ordered_io(page->mapping->host,
3038 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003039 btrfs_put_ordered_extent(ordered);
3040 lock_extent(tree, page_start, page_end, GFP_NOFS);
3041 }
3042 clear_extent_bit(tree, page_start, page_end,
3043 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3044 EXTENT_ORDERED,
3045 1, 1, GFP_NOFS);
3046 __btrfs_releasepage(page, GFP_NOFS);
3047
Chris Mason4a096752008-07-21 10:29:44 -04003048 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003049 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04003050 ClearPagePrivate(page);
3051 set_page_private(page, 0);
3052 page_cache_release(page);
3053 }
Chris Mason39279cc2007-06-12 06:35:45 -04003054}
3055
Chris Mason9ebefb182007-06-15 13:50:00 -04003056/*
3057 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3058 * called from a page fault handler when a page is first dirtied. Hence we must
3059 * be careful to check for EOF conditions here. We set the page up correctly
3060 * for a written page which means we get ENOSPC checking when writing into
3061 * holes and correct delalloc and unwritten extent mapping on filesystems that
3062 * support these features.
3063 *
3064 * We are not allowed to take the i_mutex here so we have to play games to
3065 * protect against truncate races as the page could now be beyond EOF. Because
3066 * vmtruncate() writes the inode size before removing pages, once we have the
3067 * page lock we can determine safely if the page is beyond EOF. If it is not
3068 * beyond EOF, then the page is guaranteed safe against truncation until we
3069 * unlock the page.
3070 */
3071int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3072{
Chris Mason6da6aba2007-12-18 16:15:09 -05003073 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05003074 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003075 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3076 struct btrfs_ordered_extent *ordered;
3077 char *kaddr;
3078 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04003079 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05003080 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04003081 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003082 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04003083
Chris Mason1832a6d2007-12-21 16:27:21 -05003084 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05003085 if (ret)
3086 goto out;
3087
3088 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003089again:
Chris Mason9ebefb182007-06-15 13:50:00 -04003090 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04003091 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003092 page_start = page_offset(page);
3093 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04003094
Chris Mason9ebefb182007-06-15 13:50:00 -04003095 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04003096 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04003097 /* page got truncated out from underneath us */
3098 goto out_unlock;
3099 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04003100 wait_on_page_writeback(page);
3101
3102 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3103 set_page_extent_mapped(page);
3104
Chris Masoneb84ae02008-07-17 13:53:27 -04003105 /*
3106 * we can't set the delalloc bits if there are pending ordered
3107 * extents. Drop our locks and wait for them to finish
3108 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003109 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3110 if (ordered) {
3111 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3112 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04003113 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003114 btrfs_put_ordered_extent(ordered);
3115 goto again;
3116 }
3117
Chris Masonea8c2812008-08-04 23:17:27 -04003118 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003119 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04003120
3121 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04003122 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003123 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04003124 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04003125 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04003126
Chris Masone6dcd2d2008-07-17 12:53:50 -04003127 if (zero_start != PAGE_CACHE_SIZE) {
3128 kaddr = kmap(page);
3129 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3130 flush_dcache_page(page);
3131 kunmap(page);
3132 }
Chris Mason247e7432008-07-17 12:53:51 -04003133 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003134 set_page_dirty(page);
3135 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04003136
3137out_unlock:
3138 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05003139out:
Chris Mason9ebefb182007-06-15 13:50:00 -04003140 return ret;
3141}
3142
Chris Mason39279cc2007-06-12 06:35:45 -04003143static void btrfs_truncate(struct inode *inode)
3144{
3145 struct btrfs_root *root = BTRFS_I(inode)->root;
3146 int ret;
3147 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003148 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04003149 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003150
3151 if (!S_ISREG(inode->i_mode))
3152 return;
3153 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3154 return;
3155
3156 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04003157 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003158
Chris Mason39279cc2007-06-12 06:35:45 -04003159 trans = btrfs_start_transaction(root, 1);
3160 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04003161 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04003162
Josef Bacik7b128762008-07-24 12:17:14 -04003163 ret = btrfs_orphan_add(trans, inode);
3164 if (ret)
3165 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04003166 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05003167 ret = btrfs_truncate_in_trans(trans, root, inode,
3168 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04003169 btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04003170
Josef Bacik7b128762008-07-24 12:17:14 -04003171 ret = btrfs_orphan_del(trans, inode);
3172 BUG_ON(ret);
3173
3174out:
3175 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003176 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04003177 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003178 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003179}
3180
Sven Wegener3b963622008-06-09 21:57:42 -04003181/*
3182 * Invalidate a single dcache entry at the root of the filesystem.
3183 * Needed after creation of snapshot or subvolume.
3184 */
3185void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3186 int namelen)
3187{
3188 struct dentry *alias, *entry;
3189 struct qstr qstr;
3190
3191 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3192 if (alias) {
3193 qstr.name = name;
3194 qstr.len = namelen;
3195 /* change me if btrfs ever gets a d_hash operation */
3196 qstr.hash = full_name_hash(qstr.name, qstr.len);
3197 entry = d_lookup(alias, &qstr);
3198 dput(alias);
3199 if (entry) {
3200 d_invalidate(entry);
3201 dput(entry);
3202 }
3203 }
3204}
3205
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003206int btrfs_create_subvol_root(struct btrfs_root *new_root,
3207 struct btrfs_trans_handle *trans, u64 new_dirid,
3208 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04003209{
Chris Mason39279cc2007-06-12 06:35:45 -04003210 struct inode *inode;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003211 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003212
Josef Bacikaec74772008-07-24 12:12:38 -04003213 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003214 new_dirid, block_group, S_IFDIR | 0700, &index);
Chris Mason54aa1f42007-06-22 14:16:25 -04003215 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003216 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003217 inode->i_op = &btrfs_dir_inode_operations;
3218 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04003219 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003220
Chris Mason39279cc2007-06-12 06:35:45 -04003221 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04003222 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04003223
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04003224 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003225}
3226
Chris Masonedbd8d42007-12-21 16:27:24 -05003227unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003228 struct file_ra_state *ra, struct file *file,
3229 pgoff_t offset, pgoff_t last_index)
3230{
Chris Mason8e7bf942008-04-28 09:02:36 -04003231 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003232
3233#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003234 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3235 return offset;
3236#else
Chris Mason86479a02007-09-10 19:58:16 -04003237 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3238 return offset + req_size;
3239#endif
3240}
3241
Chris Mason39279cc2007-06-12 06:35:45 -04003242struct inode *btrfs_alloc_inode(struct super_block *sb)
3243{
3244 struct btrfs_inode *ei;
3245
3246 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3247 if (!ei)
3248 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003249 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003250 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Josef Bacik33268ea2008-07-24 12:16:36 -04003251 ei->i_acl = BTRFS_ACL_NOT_CACHED;
3252 ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
Josef Bacik7b128762008-07-24 12:17:14 -04003253 INIT_LIST_HEAD(&ei->i_orphan);
Chris Mason39279cc2007-06-12 06:35:45 -04003254 return &ei->vfs_inode;
3255}
3256
3257void btrfs_destroy_inode(struct inode *inode)
3258{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003259 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003260 WARN_ON(!list_empty(&inode->i_dentry));
3261 WARN_ON(inode->i_data.nrpages);
3262
Josef Bacik33268ea2008-07-24 12:16:36 -04003263 if (BTRFS_I(inode)->i_acl &&
3264 BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3265 posix_acl_release(BTRFS_I(inode)->i_acl);
3266 if (BTRFS_I(inode)->i_default_acl &&
3267 BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3268 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3269
Yanbcc63ab2008-07-30 16:29:20 -04003270 spin_lock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003271 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3272 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3273 " list\n", inode->i_ino);
3274 dump_stack();
3275 }
Yanbcc63ab2008-07-30 16:29:20 -04003276 spin_unlock(&BTRFS_I(inode)->root->list_lock);
Josef Bacik7b128762008-07-24 12:17:14 -04003277
Chris Masone6dcd2d2008-07-17 12:53:50 -04003278 while(1) {
3279 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3280 if (!ordered)
3281 break;
3282 else {
3283 printk("found ordered extent %Lu %Lu\n",
3284 ordered->file_offset, ordered->len);
3285 btrfs_remove_ordered_extent(inode, ordered);
3286 btrfs_put_ordered_extent(ordered);
3287 btrfs_put_ordered_extent(ordered);
3288 }
3289 }
Chris Mason8c416c92008-01-14 15:10:26 -05003290 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003291 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3292}
3293
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003294#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3295static void init_once(void *foo)
3296#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003297static void init_once(struct kmem_cache * cachep, void *foo)
3298#else
Chris Mason39279cc2007-06-12 06:35:45 -04003299static void init_once(void * foo, struct kmem_cache * cachep,
3300 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003301#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003302{
3303 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3304
3305 inode_init_once(&ei->vfs_inode);
3306}
3307
3308void btrfs_destroy_cachep(void)
3309{
3310 if (btrfs_inode_cachep)
3311 kmem_cache_destroy(btrfs_inode_cachep);
3312 if (btrfs_trans_handle_cachep)
3313 kmem_cache_destroy(btrfs_trans_handle_cachep);
3314 if (btrfs_transaction_cachep)
3315 kmem_cache_destroy(btrfs_transaction_cachep);
3316 if (btrfs_bit_radix_cachep)
3317 kmem_cache_destroy(btrfs_bit_radix_cachep);
3318 if (btrfs_path_cachep)
3319 kmem_cache_destroy(btrfs_path_cachep);
3320}
3321
Chris Mason86479a02007-09-10 19:58:16 -04003322struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003323 unsigned long extra_flags,
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003324#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3325 void (*ctor)(void *)
3326#elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
Chris Mason44ec0b72007-10-29 10:55:05 -04003327 void (*ctor)(struct kmem_cache *, void *)
3328#else
Chris Mason92fee662007-07-25 12:31:35 -04003329 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003330 unsigned long)
3331#endif
3332 )
Chris Mason92fee662007-07-25 12:31:35 -04003333{
3334 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3335 SLAB_MEM_SPREAD | extra_flags), ctor
3336#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3337 ,NULL
3338#endif
3339 );
3340}
3341
Chris Mason39279cc2007-06-12 06:35:45 -04003342int btrfs_init_cachep(void)
3343{
Chris Mason86479a02007-09-10 19:58:16 -04003344 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003345 sizeof(struct btrfs_inode),
3346 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003347 if (!btrfs_inode_cachep)
3348 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003349 btrfs_trans_handle_cachep =
3350 btrfs_cache_create("btrfs_trans_handle_cache",
3351 sizeof(struct btrfs_trans_handle),
3352 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003353 if (!btrfs_trans_handle_cachep)
3354 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003355 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003356 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003357 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003358 if (!btrfs_transaction_cachep)
3359 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003360 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003361 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003362 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003363 if (!btrfs_path_cachep)
3364 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003365 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003366 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003367 if (!btrfs_bit_radix_cachep)
3368 goto fail;
3369 return 0;
3370fail:
3371 btrfs_destroy_cachep();
3372 return -ENOMEM;
3373}
3374
3375static int btrfs_getattr(struct vfsmount *mnt,
3376 struct dentry *dentry, struct kstat *stat)
3377{
3378 struct inode *inode = dentry->d_inode;
3379 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003380 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003381 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003382 return 0;
3383}
3384
3385static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3386 struct inode * new_dir,struct dentry *new_dentry)
3387{
3388 struct btrfs_trans_handle *trans;
3389 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3390 struct inode *new_inode = new_dentry->d_inode;
3391 struct inode *old_inode = old_dentry->d_inode;
3392 struct timespec ctime = CURRENT_TIME;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003393 u64 index = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003394 int ret;
3395
3396 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3397 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3398 return -ENOTEMPTY;
3399 }
Chris Mason5f39d392007-10-15 16:14:19 -04003400
Chris Mason1832a6d2007-12-21 16:27:21 -05003401 ret = btrfs_check_free_space(root, 1, 0);
3402 if (ret)
3403 goto out_unlock;
3404
Chris Mason39279cc2007-06-12 06:35:45 -04003405 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003406
Chris Mason39279cc2007-06-12 06:35:45 -04003407 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003408
3409 old_dentry->d_inode->i_nlink++;
3410 old_dir->i_ctime = old_dir->i_mtime = ctime;
3411 new_dir->i_ctime = new_dir->i_mtime = ctime;
3412 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003413
Chris Mason39279cc2007-06-12 06:35:45 -04003414 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3415 if (ret)
3416 goto out_fail;
3417
3418 if (new_inode) {
3419 new_inode->i_ctime = CURRENT_TIME;
3420 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3421 if (ret)
3422 goto out_fail;
Josef Bacik7b128762008-07-24 12:17:14 -04003423 if (new_inode->i_nlink == 0) {
3424 ret = btrfs_orphan_add(trans, new_inode);
3425 if (ret)
3426 goto out_fail;
3427 }
Chris Mason39279cc2007-06-12 06:35:45 -04003428 }
Chris Mason00e4e6b2008-08-05 11:18:09 -04003429 ret = btrfs_set_inode_index(new_dir, old_inode, &index);
Josef Bacikaec74772008-07-24 12:12:38 -04003430 if (ret)
3431 goto out_fail;
3432
Chris Mason00e4e6b2008-08-05 11:18:09 -04003433 ret = btrfs_add_link(trans, new_dentry, old_inode, 1, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003434 if (ret)
3435 goto out_fail;
3436
3437out_fail:
Chris Masonab78c842008-07-29 16:15:18 -04003438 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003439out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003440 return ret;
3441}
3442
Chris Masonea8c2812008-08-04 23:17:27 -04003443int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3444{
3445 struct list_head *head = &root->fs_info->delalloc_inodes;
3446 struct btrfs_inode *binode;
3447 unsigned long flags;
3448
3449 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3450 while(!list_empty(head)) {
3451 binode = list_entry(head->next, struct btrfs_inode,
3452 delalloc_inodes);
3453 atomic_inc(&binode->vfs_inode.i_count);
3454 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3455 filemap_write_and_wait(binode->vfs_inode.i_mapping);
3456 iput(&binode->vfs_inode);
3457 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3458 }
3459 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3460 return 0;
3461}
3462
Chris Mason39279cc2007-06-12 06:35:45 -04003463static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3464 const char *symname)
3465{
3466 struct btrfs_trans_handle *trans;
3467 struct btrfs_root *root = BTRFS_I(dir)->root;
3468 struct btrfs_path *path;
3469 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003470 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003471 int err;
3472 int drop_inode = 0;
3473 u64 objectid;
Chris Mason00e4e6b2008-08-05 11:18:09 -04003474 u64 index = 0 ;
Chris Mason39279cc2007-06-12 06:35:45 -04003475 int name_len;
3476 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003477 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003478 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003479 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003480 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003481
3482 name_len = strlen(symname) + 1;
3483 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3484 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003485
Chris Mason1832a6d2007-12-21 16:27:21 -05003486 err = btrfs_check_free_space(root, 1, 0);
3487 if (err)
3488 goto out_fail;
3489
Chris Mason39279cc2007-06-12 06:35:45 -04003490 trans = btrfs_start_transaction(root, 1);
3491 btrfs_set_trans_block_group(trans, dir);
3492
3493 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3494 if (err) {
3495 err = -ENOSPC;
3496 goto out_unlock;
3497 }
3498
Josef Bacikaec74772008-07-24 12:12:38 -04003499 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003500 dentry->d_name.len,
3501 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason00e4e6b2008-08-05 11:18:09 -04003502 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3503 &index);
Chris Mason39279cc2007-06-12 06:35:45 -04003504 err = PTR_ERR(inode);
3505 if (IS_ERR(inode))
3506 goto out_unlock;
3507
Josef Bacik33268ea2008-07-24 12:16:36 -04003508 err = btrfs_init_acl(inode, dir);
3509 if (err) {
3510 drop_inode = 1;
3511 goto out_unlock;
3512 }
3513
Chris Mason39279cc2007-06-12 06:35:45 -04003514 btrfs_set_trans_block_group(trans, inode);
Chris Mason00e4e6b2008-08-05 11:18:09 -04003515 err = btrfs_add_nondir(trans, dentry, inode, 0, index);
Chris Mason39279cc2007-06-12 06:35:45 -04003516 if (err)
3517 drop_inode = 1;
3518 else {
3519 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003520 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003521 inode->i_fop = &btrfs_file_operations;
3522 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003523 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3524 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003525 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003526 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3527 inode->i_mapping, GFP_NOFS);
Chris Masonea8c2812008-08-04 23:17:27 -04003528 INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
Chris Mason1b1e2132008-06-25 16:01:31 -04003529 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003530 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003531 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003532 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003533 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003534 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003535 }
3536 dir->i_sb->s_dirt = 1;
3537 btrfs_update_inode_block_group(trans, inode);
3538 btrfs_update_inode_block_group(trans, dir);
3539 if (drop_inode)
3540 goto out_unlock;
3541
3542 path = btrfs_alloc_path();
3543 BUG_ON(!path);
3544 key.objectid = inode->i_ino;
3545 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003546 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3547 datasize = btrfs_file_extent_calc_inline_size(name_len);
3548 err = btrfs_insert_empty_item(trans, root, path, &key,
3549 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003550 if (err) {
3551 drop_inode = 1;
3552 goto out_unlock;
3553 }
Chris Mason5f39d392007-10-15 16:14:19 -04003554 leaf = path->nodes[0];
3555 ei = btrfs_item_ptr(leaf, path->slots[0],
3556 struct btrfs_file_extent_item);
3557 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3558 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003559 BTRFS_FILE_EXTENT_INLINE);
3560 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003561 write_extent_buffer(leaf, symname, ptr, name_len);
3562 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003563 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003564
Chris Mason39279cc2007-06-12 06:35:45 -04003565 inode->i_op = &btrfs_symlink_inode_operations;
3566 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003567 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003568 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003569 err = btrfs_update_inode(trans, root, inode);
3570 if (err)
3571 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003572
3573out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003574 nr = trans->blocks_used;
Chris Masonab78c842008-07-29 16:15:18 -04003575 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003576out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003577 if (drop_inode) {
3578 inode_dec_link_count(inode);
3579 iput(inode);
3580 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003581 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003582 return err;
3583}
Chris Mason16432982008-04-10 10:23:21 -04003584
Chris Masone6dcd2d2008-07-17 12:53:50 -04003585static int btrfs_set_page_dirty(struct page *page)
3586{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003587 return __set_page_dirty_nobuffers(page);
3588}
3589
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003590#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3591static int btrfs_permission(struct inode *inode, int mask)
3592#else
Yanfdebe2b2008-01-14 13:26:08 -05003593static int btrfs_permission(struct inode *inode, int mask,
3594 struct nameidata *nd)
Sven Wegener0ee0fda2008-07-30 16:54:26 -04003595#endif
Yanfdebe2b2008-01-14 13:26:08 -05003596{
3597 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3598 return -EACCES;
Josef Bacik33268ea2008-07-24 12:16:36 -04003599 return generic_permission(inode, mask, btrfs_check_acl);
Yanfdebe2b2008-01-14 13:26:08 -05003600}
Chris Mason39279cc2007-06-12 06:35:45 -04003601
3602static struct inode_operations btrfs_dir_inode_operations = {
3603 .lookup = btrfs_lookup,
3604 .create = btrfs_create,
3605 .unlink = btrfs_unlink,
3606 .link = btrfs_link,
3607 .mkdir = btrfs_mkdir,
3608 .rmdir = btrfs_rmdir,
3609 .rename = btrfs_rename,
3610 .symlink = btrfs_symlink,
3611 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003612 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003613 .setxattr = generic_setxattr,
3614 .getxattr = generic_getxattr,
3615 .listxattr = btrfs_listxattr,
3616 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003617 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003618};
Chris Mason39279cc2007-06-12 06:35:45 -04003619static struct inode_operations btrfs_dir_ro_inode_operations = {
3620 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003621 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003622};
Chris Mason39279cc2007-06-12 06:35:45 -04003623static struct file_operations btrfs_dir_file_operations = {
3624 .llseek = generic_file_llseek,
3625 .read = generic_read_dir,
3626 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003627 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003628#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003629 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003630#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003631 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003632};
3633
Chris Masond1310b22008-01-24 16:13:08 -05003634static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003635 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003636 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003637 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason3de9d6b2008-08-04 23:17:27 -04003638 .readpage_io_hook = btrfs_readpage_io_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003639 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003640 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003641 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003642 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003643 .set_bit_hook = btrfs_set_bit_hook,
3644 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003645};
3646
Chris Mason39279cc2007-06-12 06:35:45 -04003647static struct address_space_operations btrfs_aops = {
3648 .readpage = btrfs_readpage,
3649 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003650 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003651 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003652 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003653 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003654 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003655 .invalidatepage = btrfs_invalidatepage,
3656 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003657 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003658};
3659
3660static struct address_space_operations btrfs_symlink_aops = {
3661 .readpage = btrfs_readpage,
3662 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003663 .invalidatepage = btrfs_invalidatepage,
3664 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003665};
3666
3667static struct inode_operations btrfs_file_inode_operations = {
3668 .truncate = btrfs_truncate,
3669 .getattr = btrfs_getattr,
3670 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003671 .setxattr = generic_setxattr,
3672 .getxattr = generic_getxattr,
3673 .listxattr = btrfs_listxattr,
3674 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003675 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003676};
Josef Bacik618e21d2007-07-11 10:18:17 -04003677static struct inode_operations btrfs_special_inode_operations = {
3678 .getattr = btrfs_getattr,
3679 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003680 .permission = btrfs_permission,
Josef Bacik33268ea2008-07-24 12:16:36 -04003681 .setxattr = generic_setxattr,
3682 .getxattr = generic_getxattr,
3683 .listxattr = btrfs_listxattr,
3684 .removexattr = generic_removexattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003685};
Chris Mason39279cc2007-06-12 06:35:45 -04003686static struct inode_operations btrfs_symlink_inode_operations = {
3687 .readlink = generic_readlink,
3688 .follow_link = page_follow_link_light,
3689 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003690 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003691};