blob: 293355c92a4ff02361249ea7f889b4acd371ab7d [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>
Chris Mason39279cc2007-06-12 06:35:45 -040039#include "ctree.h"
40#include "disk-io.h"
41#include "transaction.h"
42#include "btrfs_inode.h"
43#include "ioctl.h"
44#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040045#include "volumes.h"
Chris Masone6dcd2d2008-07-17 12:53:50 -040046#include "ordered-data.h"
Chris Mason39279cc2007-06-12 06:35:45 -040047
48struct btrfs_iget_args {
49 u64 ino;
50 struct btrfs_root *root;
51};
52
53static struct inode_operations btrfs_dir_inode_operations;
54static struct inode_operations btrfs_symlink_inode_operations;
55static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040056static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040057static struct inode_operations btrfs_file_inode_operations;
58static struct address_space_operations btrfs_aops;
59static struct address_space_operations btrfs_symlink_aops;
60static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050061static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040062
63static struct kmem_cache *btrfs_inode_cachep;
64struct kmem_cache *btrfs_trans_handle_cachep;
65struct kmem_cache *btrfs_transaction_cachep;
66struct kmem_cache *btrfs_bit_radix_cachep;
67struct kmem_cache *btrfs_path_cachep;
68
69#define S_SHIFT 12
70static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
71 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
72 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
73 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
74 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
75 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
76 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
77 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
78};
79
Chris Mason1832a6d2007-12-21 16:27:21 -050080int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
81 int for_del)
82{
Chris Masona2135012008-06-25 16:01:30 -040083 u64 total;
84 u64 used;
Chris Mason1832a6d2007-12-21 16:27:21 -050085 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040086 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050087 int ret = 0;
88
Chris Masona2135012008-06-25 16:01:30 -040089 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
90 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
91 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
Chris Mason1832a6d2007-12-21 16:27:21 -050092 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050093 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050094 else
Chris Masonf9ef6602008-01-03 09:22:38 -050095 thresh = total * 85;
96
97 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050098
Chris Mason1832a6d2007-12-21 16:27:21 -050099 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
100 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -0400101 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -0500102 return ret;
103}
104
Chris Masonbe20aa92007-12-17 20:14:01 -0500105static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400106{
107 struct btrfs_root *root = BTRFS_I(inode)->root;
108 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400109 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400110 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500111 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400112 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500113 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500114 struct btrfs_key ins;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400115 struct extent_map *em;
116 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
117 int ret = 0;
Chris Masonb888db22007-08-27 16:49:44 -0400118
Chris Masonf9295742008-07-17 12:54:14 -0400119 trans = btrfs_join_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400120 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500121 btrfs_set_trans_block_group(trans, inode);
122
Chris Masondb945352007-10-15 16:15:53 -0400123 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500124 num_bytes = max(blocksize, num_bytes);
Chris Masond1310b22008-01-24 16:13:08 -0500125 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400126
Chris Mason179e29e2007-11-01 11:28:41 -0400127 if (alloc_hint == EXTENT_MAP_INLINE)
128 goto out;
129
Chris Mason3b951512008-04-17 11:29:12 -0400130 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
Chris Masone6dcd2d2008-07-17 12:53:50 -0400131 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Mason3b951512008-04-17 11:29:12 -0400132
Chris Masonc59f8952007-12-17 20:14:04 -0500133 while(num_bytes > 0) {
134 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400135 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
136 root->sectorsize, 0, 0,
137 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500138 if (ret) {
139 WARN_ON(1);
140 goto out;
141 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400142 em = alloc_extent_map(GFP_NOFS);
143 em->start = start;
144 em->len = ins.offset;
145 em->block_start = ins.objectid;
146 em->bdev = root->fs_info->fs_devices->latest_bdev;
147 while(1) {
148 spin_lock(&em_tree->lock);
149 ret = add_extent_mapping(em_tree, em);
150 spin_unlock(&em_tree->lock);
151 if (ret != -EEXIST) {
152 free_extent_map(em);
153 break;
154 }
155 btrfs_drop_extent_cache(inode, start,
156 start + ins.offset - 1);
157 }
158
Chris Mason98d20f62008-04-14 09:46:10 -0400159 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400160 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
161 ins.offset);
162 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400163 if (num_bytes < cur_alloc_size) {
164 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
165 cur_alloc_size);
166 break;
167 }
Chris Masonc59f8952007-12-17 20:14:04 -0500168 num_bytes -= cur_alloc_size;
169 alloc_hint = ins.objectid + ins.offset;
170 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400171 }
Chris Masonb888db22007-08-27 16:49:44 -0400172out:
173 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500174 return ret;
175}
176
177static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
178{
179 u64 extent_start;
180 u64 extent_end;
181 u64 bytenr;
182 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500183 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500184 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500185 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400186 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500187 struct extent_buffer *leaf;
188 int found_type;
189 struct btrfs_path *path;
190 struct btrfs_file_extent_item *item;
191 int ret;
192 int err;
193 struct btrfs_key found_key;
194
Chris Masonc31f8832008-01-08 15:46:31 -0500195 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500196 path = btrfs_alloc_path();
197 BUG_ON(!path);
198again:
199 ret = btrfs_lookup_file_extent(NULL, root, path,
200 inode->i_ino, start, 0);
201 if (ret < 0) {
202 btrfs_free_path(path);
203 return ret;
204 }
205
206 cow_end = end;
207 if (ret != 0) {
208 if (path->slots[0] == 0)
209 goto not_found;
210 path->slots[0]--;
211 }
212
213 leaf = path->nodes[0];
214 item = btrfs_item_ptr(leaf, path->slots[0],
215 struct btrfs_file_extent_item);
216
217 /* are we inside the extent that was found? */
218 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
219 found_type = btrfs_key_type(&found_key);
220 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400221 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500222 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500223
224 found_type = btrfs_file_extent_type(leaf, item);
225 extent_start = found_key.offset;
226 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500227 u64 extent_num_bytes;
228
229 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
230 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500231 err = 0;
232
Chris Mason1832a6d2007-12-21 16:27:21 -0500233 if (loops && start != extent_start)
234 goto not_found;
235
Chris Masonbe20aa92007-12-17 20:14:01 -0500236 if (start < extent_start || start >= extent_end)
237 goto not_found;
238
239 cow_end = min(end, extent_end - 1);
240 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
241 if (bytenr == 0)
242 goto not_found;
243
Chris Masona68d5932008-05-08 14:11:56 -0400244 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
245 bytenr) != 1) {
246 goto not_found;
247 }
248
Chris Masonc31f8832008-01-08 15:46:31 -0500249 /*
250 * we may be called by the resizer, make sure we're inside
251 * the limits of the FS
252 */
Chris Masona68d5932008-05-08 14:11:56 -0400253 block_group = btrfs_lookup_block_group(root->fs_info,
254 bytenr);
255 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500256 goto not_found;
257
Chris Masonbe20aa92007-12-17 20:14:01 -0500258 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500259 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500260 goto not_found;
261 }
262loop:
263 if (start > end) {
264 btrfs_free_path(path);
265 return 0;
266 }
267 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500268 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500269 goto again;
270
271not_found:
Chris Masonbbaf5492008-05-08 16:31:21 -0400272 cow_file_range(inode, start, end);
273 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500274 goto loop;
275}
276
277static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
278{
279 struct btrfs_root *root = BTRFS_I(inode)->root;
280 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400281
Yanb98b6762008-01-08 15:54:37 -0500282 if (btrfs_test_opt(root, NODATACOW) ||
283 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500284 ret = run_delalloc_nocow(inode, start, end);
285 else
286 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500287
Chris Masonb888db22007-08-27 16:49:44 -0400288 return ret;
289}
290
Chris Mason291d6732008-01-29 15:55:23 -0500291int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500292 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500293{
Chris Masonbcbfce82008-04-22 13:26:47 -0400294 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500295 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500296 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400297 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500298 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500299 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400300 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500301 }
302 return 0;
303}
304
305int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500306 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500307{
Chris Masonb0c68f82008-01-31 11:05:37 -0500308 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500309 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400310 unsigned long flags;
311
312 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500313 if (end - start + 1 > root->fs_info->delalloc_bytes) {
314 printk("warning: delalloc account %Lu %Lu\n",
315 end - start + 1, root->fs_info->delalloc_bytes);
316 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500317 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500318 } else {
319 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500320 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500321 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400322 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500323 }
324 return 0;
325}
326
Chris Mason239b14b2008-03-24 15:02:07 -0400327int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
328 size_t size, struct bio *bio)
329{
330 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
331 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400332 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400333 u64 length = 0;
334 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400335 int ret;
336
Chris Masonf2d8d742008-04-21 10:03:05 -0400337 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400338 map_tree = &root->fs_info->mapping_tree;
339 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400340 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400341 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400342
Chris Mason239b14b2008-03-24 15:02:07 -0400343 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400344 return 1;
345 }
346 return 0;
347}
348
Chris Mason44b8bd72008-04-16 11:14:51 -0400349int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400350 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500351{
Chris Mason065631f2008-02-20 12:07:25 -0500352 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500353 int ret = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400354 struct btrfs_ordered_sum *sums;
Chris Masone0156402008-04-16 11:15:20 -0400355
356 ret = btrfs_csum_one_bio(root, bio, &sums);
357 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500358
Chris Masone6dcd2d2008-07-17 12:53:50 -0400359 ret = btrfs_add_ordered_sum(inode, sums);
Chris Mason44b8bd72008-04-16 11:14:51 -0400360 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400361
Chris Mason8b712842008-06-11 16:50:36 -0400362 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400363}
364
365int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
366 int mirror_num)
367{
368 struct btrfs_root *root = BTRFS_I(inode)->root;
369 int ret = 0;
370
Chris Masone6dcd2d2008-07-17 12:53:50 -0400371 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
372 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500373
Chris Masone6dcd2d2008-07-17 12:53:50 -0400374 if (!(rw & (1 << BIO_RW))) {
Chris Mason0b86a832008-03-24 15:01:56 -0400375 goto mapit;
376 }
Chris Mason065631f2008-02-20 12:07:25 -0500377
Chris Mason44b8bd72008-04-16 11:14:51 -0400378 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
379 inode, rw, bio, mirror_num,
380 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400381mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400382 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500383}
Chris Mason6885f302008-02-20 16:11:05 -0500384
Chris Masonba1da2f2008-07-17 12:54:15 -0400385static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400386 struct inode *inode, u64 file_offset,
387 struct list_head *list)
388{
389 struct list_head *cur;
390 struct btrfs_ordered_sum *sum;
391
392 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400393 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400394 sum = list_entry(cur, struct btrfs_ordered_sum, list);
395 mutex_lock(&BTRFS_I(inode)->csum_mutex);
396 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
397 inode, sum);
398 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400399 }
400 return 0;
401}
402
Chris Mason247e7432008-07-17 12:53:51 -0400403struct btrfs_writepage_fixup {
404 struct page *page;
405 struct btrfs_work work;
406};
407
408/* see btrfs_writepage_start_hook for details on why this is required */
409void btrfs_writepage_fixup_worker(struct btrfs_work *work)
410{
411 struct btrfs_writepage_fixup *fixup;
412 struct btrfs_ordered_extent *ordered;
413 struct page *page;
414 struct inode *inode;
415 u64 page_start;
416 u64 page_end;
417
418 fixup = container_of(work, struct btrfs_writepage_fixup, work);
419 page = fixup->page;
420
421 lock_page(page);
422 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
423 ClearPageChecked(page);
424 goto out_page;
425 }
426
427 inode = page->mapping->host;
428 page_start = page_offset(page);
429 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
430
431 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
432 ordered = btrfs_lookup_ordered_extent(inode, page_start);
433 if (ordered)
434 goto out;
435
436 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
437 GFP_NOFS);
438 ClearPageChecked(page);
439out:
440 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
441out_page:
442 unlock_page(page);
443 page_cache_release(page);
444}
445
446/*
447 * There are a few paths in the higher layers of the kernel that directly
448 * set the page dirty bit without asking the filesystem if it is a
449 * good idea. This causes problems because we want to make sure COW
450 * properly happens and the data=ordered rules are followed.
451 *
452 * In our case any range that doesn't have the EXTENT_ORDERED bit set
453 * hasn't been properly setup for IO. We kick off an async process
454 * to fix it up. The async helper will wait for ordered extents, set
455 * the delalloc bit and make it safe to write the page.
456 */
457int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
458{
459 struct inode *inode = page->mapping->host;
460 struct btrfs_writepage_fixup *fixup;
461 struct btrfs_root *root = BTRFS_I(inode)->root;
462 int ret;
463
464 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
465 EXTENT_ORDERED, 0);
466 if (ret)
467 return 0;
468
469 if (PageChecked(page))
470 return -EAGAIN;
471
472 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
473 if (!fixup)
474 return -EAGAIN;
475printk("queueing worker to fixup page %lu %Lu\n", inode->i_ino, page_offset(page));
476 SetPageChecked(page);
477 page_cache_get(page);
478 fixup->work.func = btrfs_writepage_fixup_worker;
479 fixup->page = page;
480 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
481 return -EAGAIN;
482}
483
Chris Masone6dcd2d2008-07-17 12:53:50 -0400484int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
485 struct extent_state *state, int uptodate)
486{
487 struct inode *inode = page->mapping->host;
488 struct btrfs_root *root = BTRFS_I(inode)->root;
489 struct btrfs_trans_handle *trans;
490 struct btrfs_ordered_extent *ordered_extent;
491 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
492 u64 alloc_hint = 0;
493 struct list_head list;
494 struct btrfs_key ins;
495 int ret;
496
497 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400498 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400499 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400500
Chris Masonf9295742008-07-17 12:54:14 -0400501 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400502
503 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
504 BUG_ON(!ordered_extent);
505
506 lock_extent(io_tree, ordered_extent->file_offset,
507 ordered_extent->file_offset + ordered_extent->len - 1,
508 GFP_NOFS);
509
510 INIT_LIST_HEAD(&list);
511
512 ins.objectid = ordered_extent->start;
513 ins.offset = ordered_extent->len;
514 ins.type = BTRFS_EXTENT_ITEM_KEY;
515 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
516 trans->transid, inode->i_ino,
517 ordered_extent->file_offset, &ins);
518 BUG_ON(ret);
519 ret = btrfs_drop_extents(trans, root, inode,
520 ordered_extent->file_offset,
521 ordered_extent->file_offset +
522 ordered_extent->len,
523 ordered_extent->file_offset, &alloc_hint);
524 BUG_ON(ret);
525 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
526 ordered_extent->file_offset,
527 ordered_extent->start,
528 ordered_extent->len,
529 ordered_extent->len, 0);
530 BUG_ON(ret);
531 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
532 ordered_extent->file_offset +
533 ordered_extent->len - 1);
534 inode->i_blocks += ordered_extent->len >> 9;
535 unlock_extent(io_tree, ordered_extent->file_offset,
536 ordered_extent->file_offset + ordered_extent->len - 1,
537 GFP_NOFS);
538 add_pending_csums(trans, inode, ordered_extent->file_offset,
539 &ordered_extent->list);
540
Chris Masondbe674a2008-07-17 12:54:05 -0400541 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400542 btrfs_remove_ordered_extent(inode, ordered_extent);
543 /* once for us */
544 btrfs_put_ordered_extent(ordered_extent);
545 /* once for the tree */
546 btrfs_put_ordered_extent(ordered_extent);
547
548 btrfs_update_inode(trans, root, inode);
549 btrfs_end_transaction(trans, root);
550 return 0;
551}
552
Chris Mason07157aa2007-08-30 08:50:51 -0400553int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
554{
555 int ret = 0;
556 struct inode *inode = page->mapping->host;
557 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500558 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400559 struct btrfs_csum_item *item;
560 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400561 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400562
Yanb98b6762008-01-08 15:54:37 -0500563 if (btrfs_test_opt(root, NODATASUM) ||
564 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500565 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400566
Chris Mason07157aa2007-08-30 08:50:51 -0400567 path = btrfs_alloc_path();
568 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
569 if (IS_ERR(item)) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400570 /*
571 * It is possible there is an ordered extent that has
572 * not yet finished for this range in the file. If so,
573 * that extent will have a csum cached, and it will insert
574 * the sum after all the blocks in the extent are fully
575 * on disk. So, look for an ordered extent and use the
576 * sum if found.
577 */
578 ret = btrfs_find_ordered_sum(inode, start, &csum);
579 if (ret == 0)
580 goto found;
581
Chris Mason07157aa2007-08-30 08:50:51 -0400582 ret = PTR_ERR(item);
583 /* a csum that isn't present is a preallocated region. */
584 if (ret == -ENOENT || ret == -EFBIG)
585 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400586 csum = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400587 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
588 start);
Chris Mason07157aa2007-08-30 08:50:51 -0400589 goto out;
590 }
Chris Masonff79f812007-10-15 16:22:25 -0400591 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
592 BTRFS_CRC32_SIZE);
Chris Masonba1da2f2008-07-17 12:54:15 -0400593found:
Chris Masond1310b22008-01-24 16:13:08 -0500594 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400595out:
596 if (path)
597 btrfs_free_path(path);
Chris Mason07157aa2007-08-30 08:50:51 -0400598 return ret;
599}
600
Chris Mason7e383262008-04-09 16:28:12 -0400601struct io_failure_record {
602 struct page *page;
603 u64 start;
604 u64 len;
605 u64 logical;
606 int last_mirror;
607};
608
Chris Mason1259ab72008-05-12 13:39:03 -0400609int btrfs_io_failed_hook(struct bio *failed_bio,
610 struct page *page, u64 start, u64 end,
611 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400612{
613 struct io_failure_record *failrec = NULL;
614 u64 private;
615 struct extent_map *em;
616 struct inode *inode = page->mapping->host;
617 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400618 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400619 struct bio *bio;
620 int num_copies;
621 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400622 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400623 u64 logical;
624
625 ret = get_state_private(failure_tree, start, &private);
626 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400627 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
628 if (!failrec)
629 return -ENOMEM;
630 failrec->start = start;
631 failrec->len = end - start + 1;
632 failrec->last_mirror = 0;
633
Chris Mason3b951512008-04-17 11:29:12 -0400634 spin_lock(&em_tree->lock);
635 em = lookup_extent_mapping(em_tree, start, failrec->len);
636 if (em->start > start || em->start + em->len < start) {
637 free_extent_map(em);
638 em = NULL;
639 }
640 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400641
642 if (!em || IS_ERR(em)) {
643 kfree(failrec);
644 return -EIO;
645 }
646 logical = start - em->start;
647 logical = em->block_start + logical;
648 failrec->logical = logical;
649 free_extent_map(em);
650 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
651 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400652 set_state_private(failure_tree, start,
653 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400654 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400655 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400656 }
657 num_copies = btrfs_num_copies(
658 &BTRFS_I(inode)->root->fs_info->mapping_tree,
659 failrec->logical, failrec->len);
660 failrec->last_mirror++;
661 if (!state) {
662 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
663 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
664 failrec->start,
665 EXTENT_LOCKED);
666 if (state && state->start != failrec->start)
667 state = NULL;
668 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
669 }
670 if (!state || failrec->last_mirror > num_copies) {
671 set_state_private(failure_tree, failrec->start, 0);
672 clear_extent_bits(failure_tree, failrec->start,
673 failrec->start + failrec->len - 1,
674 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
675 kfree(failrec);
676 return -EIO;
677 }
678 bio = bio_alloc(GFP_NOFS, 1);
679 bio->bi_private = state;
680 bio->bi_end_io = failed_bio->bi_end_io;
681 bio->bi_sector = failrec->logical >> 9;
682 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400683 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400684 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400685 if (failed_bio->bi_rw & (1 << BIO_RW))
686 rw = WRITE;
687 else
688 rw = READ;
689
690 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
691 failrec->last_mirror);
692 return 0;
693}
694
695int btrfs_clean_io_failures(struct inode *inode, u64 start)
696{
697 u64 private;
698 u64 private_failure;
699 struct io_failure_record *failure;
700 int ret;
701
702 private = 0;
703 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
704 (u64)-1, 1, EXTENT_DIRTY)) {
705 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
706 start, &private_failure);
707 if (ret == 0) {
708 failure = (struct io_failure_record *)(unsigned long)
709 private_failure;
710 set_state_private(&BTRFS_I(inode)->io_failure_tree,
711 failure->start, 0);
712 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
713 failure->start,
714 failure->start + failure->len - 1,
715 EXTENT_DIRTY | EXTENT_LOCKED,
716 GFP_NOFS);
717 kfree(failure);
718 }
719 }
Chris Mason7e383262008-04-09 16:28:12 -0400720 return 0;
721}
722
Chris Mason70dec802008-01-29 09:59:12 -0500723int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
724 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400725{
Chris Mason35ebb932007-10-30 16:56:53 -0400726 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400727 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500728 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400729 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500730 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400731 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400732 struct btrfs_root *root = BTRFS_I(inode)->root;
733 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400734 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500735
Yanb98b6762008-01-08 15:54:37 -0500736 if (btrfs_test_opt(root, NODATASUM) ||
737 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500738 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500739 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500740 private = state->private;
741 ret = 0;
742 } else {
743 ret = get_state_private(io_tree, start, &private);
744 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400745 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400746 kaddr = kmap_atomic(page, KM_IRQ0);
747 if (ret) {
748 goto zeroit;
749 }
Chris Masonff79f812007-10-15 16:22:25 -0400750 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
751 btrfs_csum_final(csum, (char *)&csum);
752 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400753 goto zeroit;
754 }
755 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400756 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400757
758 /* if the io failure tree for this inode is non-empty,
759 * check to see if we've recovered from a failed IO
760 */
Chris Mason1259ab72008-05-12 13:39:03 -0400761 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400762 return 0;
763
764zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500765 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
766 page->mapping->host->i_ino, (unsigned long long)start, csum,
767 private);
Chris Masondb945352007-10-15 16:15:53 -0400768 memset(kaddr + offset, 1, end - start + 1);
769 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400770 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400771 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400772 if (private == 0)
773 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400774 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400775}
Chris Masonb888db22007-08-27 16:49:44 -0400776
Chris Mason39279cc2007-06-12 06:35:45 -0400777void btrfs_read_locked_inode(struct inode *inode)
778{
779 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400780 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400781 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400782 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400783 struct btrfs_root *root = BTRFS_I(inode)->root;
784 struct btrfs_key location;
785 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400786 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400787 int ret;
788
789 path = btrfs_alloc_path();
790 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400791 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500792
Chris Mason39279cc2007-06-12 06:35:45 -0400793 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400794 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400795 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400796
Chris Mason5f39d392007-10-15 16:14:19 -0400797 leaf = path->nodes[0];
798 inode_item = btrfs_item_ptr(leaf, path->slots[0],
799 struct btrfs_inode_item);
800
801 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
802 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
803 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
804 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -0400805 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400806
807 tspec = btrfs_inode_atime(inode_item);
808 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
809 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
810
811 tspec = btrfs_inode_mtime(inode_item);
812 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
813 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
814
815 tspec = btrfs_inode_ctime(inode_item);
816 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
817 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
818
819 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
820 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400821 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400822 rdev = btrfs_inode_rdev(leaf, inode_item);
823
824 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400825 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
826 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500827 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500828 if (!BTRFS_I(inode)->block_group) {
829 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400830 NULL, 0,
831 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500832 }
Chris Mason39279cc2007-06-12 06:35:45 -0400833 btrfs_free_path(path);
834 inode_item = NULL;
835
Chris Mason39279cc2007-06-12 06:35:45 -0400836 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400837 case S_IFREG:
838 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400839 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500840 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400841 inode->i_fop = &btrfs_file_operations;
842 inode->i_op = &btrfs_file_inode_operations;
843 break;
844 case S_IFDIR:
845 inode->i_fop = &btrfs_dir_file_operations;
846 if (root == root->fs_info->tree_root)
847 inode->i_op = &btrfs_dir_ro_inode_operations;
848 else
849 inode->i_op = &btrfs_dir_inode_operations;
850 break;
851 case S_IFLNK:
852 inode->i_op = &btrfs_symlink_inode_operations;
853 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400854 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400855 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400856 default:
857 init_special_inode(inode, inode->i_mode, rdev);
858 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400859 }
860 return;
861
862make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -0400863 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -0400864 make_bad_inode(inode);
865}
866
Chris Mason5f39d392007-10-15 16:14:19 -0400867static void fill_inode_item(struct extent_buffer *leaf,
868 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400869 struct inode *inode)
870{
Chris Mason5f39d392007-10-15 16:14:19 -0400871 btrfs_set_inode_uid(leaf, item, inode->i_uid);
872 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -0400873 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -0400874 btrfs_set_inode_mode(leaf, item, inode->i_mode);
875 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
876
877 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
878 inode->i_atime.tv_sec);
879 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
880 inode->i_atime.tv_nsec);
881
882 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
883 inode->i_mtime.tv_sec);
884 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
885 inode->i_mtime.tv_nsec);
886
887 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
888 inode->i_ctime.tv_sec);
889 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
890 inode->i_ctime.tv_nsec);
891
892 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
893 btrfs_set_inode_generation(leaf, item, inode->i_generation);
894 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500895 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400896 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400897 BTRFS_I(inode)->block_group->key.objectid);
898}
899
Chris Masonba1da2f2008-07-17 12:54:15 -0400900int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400901 struct btrfs_root *root,
902 struct inode *inode)
903{
904 struct btrfs_inode_item *inode_item;
905 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400906 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400907 int ret;
908
909 path = btrfs_alloc_path();
910 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400911 ret = btrfs_lookup_inode(trans, root, path,
912 &BTRFS_I(inode)->location, 1);
913 if (ret) {
914 if (ret > 0)
915 ret = -ENOENT;
916 goto failed;
917 }
918
Chris Mason5f39d392007-10-15 16:14:19 -0400919 leaf = path->nodes[0];
920 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400921 struct btrfs_inode_item);
922
Chris Mason5f39d392007-10-15 16:14:19 -0400923 fill_inode_item(leaf, inode_item, inode);
924 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400925 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400926 ret = 0;
927failed:
Chris Mason39279cc2007-06-12 06:35:45 -0400928 btrfs_free_path(path);
929 return ret;
930}
931
932
933static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
934 struct btrfs_root *root,
935 struct inode *dir,
936 struct dentry *dentry)
937{
938 struct btrfs_path *path;
939 const char *name = dentry->d_name.name;
940 int name_len = dentry->d_name.len;
941 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400942 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400943 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400944 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400945
946 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400947 if (!path) {
948 ret = -ENOMEM;
949 goto err;
950 }
951
Chris Mason39279cc2007-06-12 06:35:45 -0400952 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
953 name, name_len, -1);
954 if (IS_ERR(di)) {
955 ret = PTR_ERR(di);
956 goto err;
957 }
958 if (!di) {
959 ret = -ENOENT;
960 goto err;
961 }
Chris Mason5f39d392007-10-15 16:14:19 -0400962 leaf = path->nodes[0];
963 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400964 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400965 if (ret)
966 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400967 btrfs_release_path(root, path);
968
969 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400970 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400971 if (IS_ERR(di)) {
972 ret = PTR_ERR(di);
973 goto err;
974 }
975 if (!di) {
976 ret = -ENOENT;
977 goto err;
978 }
979 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -0400980 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400981
982 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500983 ret = btrfs_del_inode_ref(trans, root, name, name_len,
984 dentry->d_inode->i_ino,
985 dentry->d_parent->d_inode->i_ino);
986 if (ret) {
987 printk("failed to delete reference to %.*s, "
988 "inode %lu parent %lu\n", name_len, name,
989 dentry->d_inode->i_ino,
990 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500991 }
Chris Mason39279cc2007-06-12 06:35:45 -0400992err:
993 btrfs_free_path(path);
994 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -0400995 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -0400996 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400997 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500998#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
999 dentry->d_inode->i_nlink--;
1000#else
Chris Mason39279cc2007-06-12 06:35:45 -04001001 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001002#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001003 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001004 dir->i_sb->s_dirt = 1;
1005 }
1006 return ret;
1007}
1008
1009static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1010{
1011 struct btrfs_root *root;
1012 struct btrfs_trans_handle *trans;
1013 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001014 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001015
1016 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001017
1018 ret = btrfs_check_free_space(root, 1, 1);
1019 if (ret)
1020 goto fail;
1021
Chris Mason39279cc2007-06-12 06:35:45 -04001022 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001023
Chris Mason39279cc2007-06-12 06:35:45 -04001024 btrfs_set_trans_block_group(trans, dir);
1025 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001026 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001027
Chris Mason89ce8a62008-06-25 16:01:31 -04001028 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001029fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001030 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001031 return ret;
1032}
1033
1034static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1035{
1036 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001037 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001038 int ret;
1039 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001040 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001041 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001042
Chris Mason925baed2008-06-25 16:01:30 -04001043 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001044 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001045 }
Yan134d4512007-10-25 15:49:25 -04001046
Chris Mason1832a6d2007-12-21 16:27:21 -05001047 ret = btrfs_check_free_space(root, 1, 1);
1048 if (ret)
1049 goto fail;
1050
Chris Mason39279cc2007-06-12 06:35:45 -04001051 trans = btrfs_start_transaction(root, 1);
1052 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001053
1054 /* now the directory is empty */
1055 err = btrfs_unlink_trans(trans, root, dir, dentry);
1056 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001057 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001058 }
Chris Mason39544012007-12-12 14:38:19 -05001059
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001060 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001061 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001062fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001063 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001064
Chris Mason39279cc2007-06-12 06:35:45 -04001065 if (ret && !err)
1066 err = ret;
1067 return err;
1068}
1069
Chris Mason39279cc2007-06-12 06:35:45 -04001070/*
Chris Mason39279cc2007-06-12 06:35:45 -04001071 * this can truncate away extent items, csum items and directory items.
1072 * It starts at a high offset and removes keys until it can't find
1073 * any higher than i_size.
1074 *
1075 * csum items that cross the new i_size are truncated to the new size
1076 * as well.
1077 */
1078static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1079 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001080 struct inode *inode,
1081 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001082{
1083 int ret;
1084 struct btrfs_path *path;
1085 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001086 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001087 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001088 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001089 struct btrfs_file_extent_item *fi;
1090 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001091 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001092 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001093 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001094 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001095 int found_extent;
1096 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001097 int pending_del_nr = 0;
1098 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001099 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001100 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001101
Chris Mason3b951512008-04-17 11:29:12 -04001102 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001103 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001104 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001105 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001106
Chris Mason39279cc2007-06-12 06:35:45 -04001107 /* FIXME, add redo link to tree so we don't leak on crash */
1108 key.objectid = inode->i_ino;
1109 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001110 key.type = (u8)-1;
1111
Chris Mason85e21ba2008-01-29 15:11:36 -05001112 btrfs_init_path(path);
1113search_again:
1114 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1115 if (ret < 0) {
1116 goto error;
1117 }
1118 if (ret > 0) {
1119 BUG_ON(path->slots[0] == 0);
1120 path->slots[0]--;
1121 }
1122
Chris Mason39279cc2007-06-12 06:35:45 -04001123 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001124 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001125 leaf = path->nodes[0];
1126 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1127 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001128
Chris Mason5f39d392007-10-15 16:14:19 -04001129 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001130 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001131
Chris Mason85e21ba2008-01-29 15:11:36 -05001132 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001133 break;
1134
Chris Mason5f39d392007-10-15 16:14:19 -04001135 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001136 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001137 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001138 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001139 extent_type = btrfs_file_extent_type(leaf, fi);
1140 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001141 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001142 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001143 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1144 struct btrfs_item *item = btrfs_item_nr(leaf,
1145 path->slots[0]);
1146 item_end += btrfs_file_extent_inline_len(leaf,
1147 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001148 }
Yan008630c2007-11-07 13:31:09 -05001149 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001150 }
1151 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1152 ret = btrfs_csum_truncate(trans, root, path,
1153 inode->i_size);
1154 BUG_ON(ret);
1155 }
Yan008630c2007-11-07 13:31:09 -05001156 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001157 if (found_type == BTRFS_DIR_ITEM_KEY) {
1158 found_type = BTRFS_INODE_ITEM_KEY;
1159 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1160 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001161 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1162 found_type = BTRFS_XATTR_ITEM_KEY;
1163 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1164 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001165 } else if (found_type) {
1166 found_type--;
1167 } else {
1168 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001169 }
Yana61721d2007-09-17 11:08:38 -04001170 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001171 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001172 }
Chris Mason5f39d392007-10-15 16:14:19 -04001173 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001174 del_item = 1;
1175 else
1176 del_item = 0;
1177 found_extent = 0;
1178
1179 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001180 if (found_type != BTRFS_EXTENT_DATA_KEY)
1181 goto delete;
1182
1183 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001184 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001185 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001186 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001187 u64 orig_num_bytes =
1188 btrfs_file_extent_num_bytes(leaf, fi);
1189 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001190 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001191 extent_num_bytes = extent_num_bytes &
1192 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001193 btrfs_set_file_extent_num_bytes(leaf, fi,
1194 extent_num_bytes);
1195 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001196 extent_num_bytes);
1197 if (extent_start != 0)
1198 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001199 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001200 } else {
Chris Masondb945352007-10-15 16:15:53 -04001201 extent_num_bytes =
1202 btrfs_file_extent_disk_num_bytes(leaf,
1203 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001204 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001205 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001206 if (extent_start != 0) {
1207 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001208 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001209 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001210 root_gen = btrfs_header_generation(leaf);
1211 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001212 }
Chris Mason90692182008-02-08 13:49:28 -05001213 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1214 if (!del_item) {
1215 u32 newsize = inode->i_size - found_key.offset;
1216 dec_i_blocks(inode, item_end + 1 -
1217 found_key.offset - newsize);
1218 newsize =
1219 btrfs_file_extent_calc_inline_size(newsize);
1220 ret = btrfs_truncate_item(trans, root, path,
1221 newsize, 1);
1222 BUG_ON(ret);
1223 } else {
1224 dec_i_blocks(inode, item_end + 1 -
1225 found_key.offset);
1226 }
Chris Mason39279cc2007-06-12 06:35:45 -04001227 }
Chris Mason179e29e2007-11-01 11:28:41 -04001228delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001229 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001230 if (!pending_del_nr) {
1231 /* no pending yet, add ourselves */
1232 pending_del_slot = path->slots[0];
1233 pending_del_nr = 1;
1234 } else if (pending_del_nr &&
1235 path->slots[0] + 1 == pending_del_slot) {
1236 /* hop on the pending chunk */
1237 pending_del_nr++;
1238 pending_del_slot = path->slots[0];
1239 } else {
1240 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1241 }
Chris Mason39279cc2007-06-12 06:35:45 -04001242 } else {
1243 break;
1244 }
Chris Mason39279cc2007-06-12 06:35:45 -04001245 if (found_extent) {
1246 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001247 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001248 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001249 root_gen, inode->i_ino,
1250 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001251 BUG_ON(ret);
1252 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001253next:
1254 if (path->slots[0] == 0) {
1255 if (pending_del_nr)
1256 goto del_pending;
1257 btrfs_release_path(root, path);
1258 goto search_again;
1259 }
1260
1261 path->slots[0]--;
1262 if (pending_del_nr &&
1263 path->slots[0] + 1 != pending_del_slot) {
1264 struct btrfs_key debug;
1265del_pending:
1266 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1267 pending_del_slot);
1268 ret = btrfs_del_items(trans, root, path,
1269 pending_del_slot,
1270 pending_del_nr);
1271 BUG_ON(ret);
1272 pending_del_nr = 0;
1273 btrfs_release_path(root, path);
1274 goto search_again;
1275 }
Chris Mason39279cc2007-06-12 06:35:45 -04001276 }
1277 ret = 0;
1278error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001279 if (pending_del_nr) {
1280 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1281 pending_del_nr);
1282 }
Chris Mason39279cc2007-06-12 06:35:45 -04001283 btrfs_free_path(path);
1284 inode->i_sb->s_dirt = 1;
1285 return ret;
1286}
1287
Chris Masona52d9a82007-08-27 16:49:44 -04001288/*
1289 * taken from block_truncate_page, but does cow as it zeros out
1290 * any bytes left in the last page in the file.
1291 */
1292static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1293{
1294 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001295 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001296 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1297 struct btrfs_ordered_extent *ordered;
1298 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001299 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001300 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1301 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1302 struct page *page;
1303 int ret = 0;
1304 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001305 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001306
1307 if ((offset & (blocksize - 1)) == 0)
1308 goto out;
1309
1310 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001311again:
Chris Masona52d9a82007-08-27 16:49:44 -04001312 page = grab_cache_page(mapping, index);
1313 if (!page)
1314 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001315
1316 page_start = page_offset(page);
1317 page_end = page_start + PAGE_CACHE_SIZE - 1;
1318
Chris Masona52d9a82007-08-27 16:49:44 -04001319 if (!PageUptodate(page)) {
1320 ret = btrfs_readpage(NULL, page);
1321 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001322 if (page->mapping != mapping) {
1323 unlock_page(page);
1324 page_cache_release(page);
1325 goto again;
1326 }
Chris Masona52d9a82007-08-27 16:49:44 -04001327 if (!PageUptodate(page)) {
1328 ret = -EIO;
1329 goto out;
1330 }
1331 }
Chris Mason211c17f2008-05-15 09:13:45 -04001332 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001333
1334 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1335 set_page_extent_mapped(page);
1336
1337 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1338 if (ordered) {
1339 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1340 unlock_page(page);
1341 page_cache_release(page);
1342 btrfs_wait_ordered_extent(inode, ordered);
1343 btrfs_put_ordered_extent(ordered);
1344 goto again;
1345 }
1346
1347 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1348 page_end, GFP_NOFS);
1349 ret = 0;
1350 if (offset != PAGE_CACHE_SIZE) {
1351 kaddr = kmap(page);
1352 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1353 flush_dcache_page(page);
1354 kunmap(page);
1355 }
Chris Mason247e7432008-07-17 12:53:51 -04001356 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001357 set_page_dirty(page);
1358 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001359
Chris Mason39279cc2007-06-12 06:35:45 -04001360 unlock_page(page);
1361 page_cache_release(page);
1362out:
1363 return ret;
1364}
1365
1366static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1367{
1368 struct inode *inode = dentry->d_inode;
1369 int err;
1370
1371 err = inode_change_ok(inode, attr);
1372 if (err)
1373 return err;
1374
1375 if (S_ISREG(inode->i_mode) &&
1376 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1377 struct btrfs_trans_handle *trans;
1378 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001379 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001380
Chris Mason5f39d392007-10-15 16:14:19 -04001381 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001382 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001383 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001384 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001385 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001386
Chris Mason1b0f7c22008-01-30 14:33:02 -05001387 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001388 goto out;
1389
Chris Mason1832a6d2007-12-21 16:27:21 -05001390 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001391 if (err)
1392 goto fail;
1393
Chris Mason39279cc2007-06-12 06:35:45 -04001394 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1395
Chris Mason5f564062008-01-22 16:47:59 -05001396 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001397 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1398 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001399
Chris Mason39279cc2007-06-12 06:35:45 -04001400 trans = btrfs_start_transaction(root, 1);
1401 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001402 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001403 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001404 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001405
Chris Mason179e29e2007-11-01 11:28:41 -04001406 if (alloc_hint != EXTENT_MAP_INLINE) {
1407 err = btrfs_insert_file_extent(trans, root,
1408 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001409 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001410 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001411 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001412 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001413 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001414 }
Chris Mason39279cc2007-06-12 06:35:45 -04001415 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001416 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001417 if (err)
1418 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001419 }
1420out:
1421 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001422fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001423 return err;
1424}
Chris Mason61295eb2008-01-14 16:24:38 -05001425
Chris Mason39279cc2007-06-12 06:35:45 -04001426void btrfs_delete_inode(struct inode *inode)
1427{
1428 struct btrfs_trans_handle *trans;
1429 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001430 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001431 int ret;
1432
Chris Masone6dcd2d2008-07-17 12:53:50 -04001433 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001434 truncate_inode_pages(&inode->i_data, 0);
1435 if (is_bad_inode(inode)) {
1436 goto no_delete;
1437 }
Chris Mason5f39d392007-10-15 16:14:19 -04001438
Chris Masondbe674a2008-07-17 12:54:05 -04001439 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001440 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001441
Chris Mason39279cc2007-06-12 06:35:45 -04001442 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001443 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001444 if (ret)
1445 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001446
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001447 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001448 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001449
Chris Mason39279cc2007-06-12 06:35:45 -04001450 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001451 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001452 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001453
1454no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001455 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001456 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001457 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001458no_delete:
1459 clear_inode(inode);
1460}
1461
1462/*
1463 * this returns the key found in the dir entry in the location pointer.
1464 * If no dir entries were found, location->objectid is 0.
1465 */
1466static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1467 struct btrfs_key *location)
1468{
1469 const char *name = dentry->d_name.name;
1470 int namelen = dentry->d_name.len;
1471 struct btrfs_dir_item *di;
1472 struct btrfs_path *path;
1473 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001474 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001475
Chris Mason39544012007-12-12 14:38:19 -05001476 if (namelen == 1 && strcmp(name, ".") == 0) {
1477 location->objectid = dir->i_ino;
1478 location->type = BTRFS_INODE_ITEM_KEY;
1479 location->offset = 0;
1480 return 0;
1481 }
Chris Mason39279cc2007-06-12 06:35:45 -04001482 path = btrfs_alloc_path();
1483 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001484
Chris Mason7a720532007-12-13 09:06:59 -05001485 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001486 struct btrfs_key key;
1487 struct extent_buffer *leaf;
1488 u32 nritems;
1489 int slot;
1490
1491 key.objectid = dir->i_ino;
1492 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1493 key.offset = 0;
1494 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1495 BUG_ON(ret == 0);
1496 ret = 0;
1497
1498 leaf = path->nodes[0];
1499 slot = path->slots[0];
1500 nritems = btrfs_header_nritems(leaf);
1501 if (slot >= nritems)
1502 goto out_err;
1503
1504 btrfs_item_key_to_cpu(leaf, &key, slot);
1505 if (key.objectid != dir->i_ino ||
1506 key.type != BTRFS_INODE_REF_KEY) {
1507 goto out_err;
1508 }
1509 location->objectid = key.offset;
1510 location->type = BTRFS_INODE_ITEM_KEY;
1511 location->offset = 0;
1512 goto out;
1513 }
1514
Chris Mason39279cc2007-06-12 06:35:45 -04001515 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1516 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001517 if (IS_ERR(di))
1518 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001519 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001520 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001521 }
Chris Mason5f39d392007-10-15 16:14:19 -04001522 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001523out:
Chris Mason39279cc2007-06-12 06:35:45 -04001524 btrfs_free_path(path);
1525 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001526out_err:
1527 location->objectid = 0;
1528 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001529}
1530
1531/*
1532 * when we hit a tree root in a directory, the btrfs part of the inode
1533 * needs to be changed to reflect the root directory of the tree root. This
1534 * is kind of like crossing a mount point.
1535 */
1536static int fixup_tree_root_location(struct btrfs_root *root,
1537 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001538 struct btrfs_root **sub_root,
1539 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001540{
1541 struct btrfs_path *path;
1542 struct btrfs_root_item *ri;
1543
1544 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1545 return 0;
1546 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1547 return 0;
1548
1549 path = btrfs_alloc_path();
1550 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001551
Josef Bacik58176a92007-08-29 15:47:34 -04001552 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1553 dentry->d_name.name,
1554 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001555 if (IS_ERR(*sub_root))
1556 return PTR_ERR(*sub_root);
1557
1558 ri = &(*sub_root)->root_item;
1559 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001560 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1561 location->offset = 0;
1562
1563 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001564 return 0;
1565}
1566
1567static int btrfs_init_locked_inode(struct inode *inode, void *p)
1568{
1569 struct btrfs_iget_args *args = p;
1570 inode->i_ino = args->ino;
1571 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001572 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001573 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001574 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1575 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001576 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001577 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1578 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001579 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001580 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001581 return 0;
1582}
1583
1584static int btrfs_find_actor(struct inode *inode, void *opaque)
1585{
1586 struct btrfs_iget_args *args = opaque;
1587 return (args->ino == inode->i_ino &&
1588 args->root == BTRFS_I(inode)->root);
1589}
1590
Chris Masondc17ff82008-01-08 15:46:30 -05001591struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1592 u64 root_objectid)
1593{
1594 struct btrfs_iget_args args;
1595 args.ino = objectid;
1596 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1597
1598 if (!args.root)
1599 return NULL;
1600
1601 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1602}
1603
Chris Mason39279cc2007-06-12 06:35:45 -04001604struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1605 struct btrfs_root *root)
1606{
1607 struct inode *inode;
1608 struct btrfs_iget_args args;
1609 args.ino = objectid;
1610 args.root = root;
1611
1612 inode = iget5_locked(s, objectid, btrfs_find_actor,
1613 btrfs_init_locked_inode,
1614 (void *)&args);
1615 return inode;
1616}
1617
1618static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1619 struct nameidata *nd)
1620{
1621 struct inode * inode;
1622 struct btrfs_inode *bi = BTRFS_I(dir);
1623 struct btrfs_root *root = bi->root;
1624 struct btrfs_root *sub_root = root;
1625 struct btrfs_key location;
1626 int ret;
1627
1628 if (dentry->d_name.len > BTRFS_NAME_LEN)
1629 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001630
Chris Mason39279cc2007-06-12 06:35:45 -04001631 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001632
Chris Mason39279cc2007-06-12 06:35:45 -04001633 if (ret < 0)
1634 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001635
Chris Mason39279cc2007-06-12 06:35:45 -04001636 inode = NULL;
1637 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001638 ret = fixup_tree_root_location(root, &location, &sub_root,
1639 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001640 if (ret < 0)
1641 return ERR_PTR(ret);
1642 if (ret > 0)
1643 return ERR_PTR(-ENOENT);
1644 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1645 sub_root);
1646 if (!inode)
1647 return ERR_PTR(-EACCES);
1648 if (inode->i_state & I_NEW) {
1649 /* the inode and parent dir are two different roots */
1650 if (sub_root != root) {
1651 igrab(inode);
1652 sub_root->inode = inode;
1653 }
1654 BTRFS_I(inode)->root = sub_root;
1655 memcpy(&BTRFS_I(inode)->location, &location,
1656 sizeof(location));
1657 btrfs_read_locked_inode(inode);
1658 unlock_new_inode(inode);
1659 }
1660 }
1661 return d_splice_alias(inode, dentry);
1662}
1663
Chris Mason39279cc2007-06-12 06:35:45 -04001664static unsigned char btrfs_filetype_table[] = {
1665 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1666};
1667
1668static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1669{
Chris Mason6da6aba2007-12-18 16:15:09 -05001670 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001671 struct btrfs_root *root = BTRFS_I(inode)->root;
1672 struct btrfs_item *item;
1673 struct btrfs_dir_item *di;
1674 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001675 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001676 struct btrfs_path *path;
1677 int ret;
1678 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001679 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001680 int slot;
1681 int advance;
1682 unsigned char d_type;
1683 int over = 0;
1684 u32 di_cur;
1685 u32 di_total;
1686 u32 di_len;
1687 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001688 char tmp_name[32];
1689 char *name_ptr;
1690 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001691
1692 /* FIXME, use a real flag for deciding about the key type */
1693 if (root->fs_info->tree_root == root)
1694 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001695
Chris Mason39544012007-12-12 14:38:19 -05001696 /* special case for "." */
1697 if (filp->f_pos == 0) {
1698 over = filldir(dirent, ".", 1,
1699 1, inode->i_ino,
1700 DT_DIR);
1701 if (over)
1702 return 0;
1703 filp->f_pos = 1;
1704 }
1705
Chris Mason39279cc2007-06-12 06:35:45 -04001706 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001707 path = btrfs_alloc_path();
1708 path->reada = 2;
1709
1710 /* special case for .., just use the back ref */
1711 if (filp->f_pos == 1) {
1712 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1713 key.offset = 0;
1714 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1715 BUG_ON(ret == 0);
1716 leaf = path->nodes[0];
1717 slot = path->slots[0];
1718 nritems = btrfs_header_nritems(leaf);
1719 if (slot >= nritems) {
1720 btrfs_release_path(root, path);
1721 goto read_dir_items;
1722 }
1723 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1724 btrfs_release_path(root, path);
1725 if (found_key.objectid != key.objectid ||
1726 found_key.type != BTRFS_INODE_REF_KEY)
1727 goto read_dir_items;
1728 over = filldir(dirent, "..", 2,
1729 2, found_key.offset, DT_DIR);
1730 if (over)
1731 goto nopos;
1732 filp->f_pos = 2;
1733 }
1734
1735read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001736 btrfs_set_key_type(&key, key_type);
1737 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001738
Chris Mason39279cc2007-06-12 06:35:45 -04001739 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1740 if (ret < 0)
1741 goto err;
1742 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001743 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001744 leaf = path->nodes[0];
1745 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001746 slot = path->slots[0];
1747 if (advance || slot >= nritems) {
1748 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001749 ret = btrfs_next_leaf(root, path);
1750 if (ret)
1751 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001752 leaf = path->nodes[0];
1753 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001754 slot = path->slots[0];
1755 } else {
1756 slot++;
1757 path->slots[0]++;
1758 }
1759 }
1760 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001761 item = btrfs_item_nr(leaf, slot);
1762 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1763
1764 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001765 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001766 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001767 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001768 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001769 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001770
1771 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001772 advance = 1;
1773 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1774 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001775 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001776 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001777 struct btrfs_key location;
1778
1779 name_len = btrfs_dir_name_len(leaf, di);
1780 if (name_len < 32) {
1781 name_ptr = tmp_name;
1782 } else {
1783 name_ptr = kmalloc(name_len, GFP_NOFS);
1784 BUG_ON(!name_ptr);
1785 }
1786 read_extent_buffer(leaf, name_ptr,
1787 (unsigned long)(di + 1), name_len);
1788
1789 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1790 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001791 over = filldir(dirent, name_ptr, name_len,
1792 found_key.offset,
1793 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001794 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001795
1796 if (name_ptr != tmp_name)
1797 kfree(name_ptr);
1798
Chris Mason39279cc2007-06-12 06:35:45 -04001799 if (over)
1800 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001801 di_len = btrfs_dir_name_len(leaf, di) +
1802 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001803 di_cur += di_len;
1804 di = (struct btrfs_dir_item *)((char *)di + di_len);
1805 }
1806 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001807 if (key_type == BTRFS_DIR_INDEX_KEY)
1808 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1809 else
1810 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001811nopos:
1812 ret = 0;
1813err:
Chris Mason39279cc2007-06-12 06:35:45 -04001814 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001815 return ret;
1816}
1817
1818int btrfs_write_inode(struct inode *inode, int wait)
1819{
1820 struct btrfs_root *root = BTRFS_I(inode)->root;
1821 struct btrfs_trans_handle *trans;
1822 int ret = 0;
1823
1824 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04001825 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001826 btrfs_set_trans_block_group(trans, inode);
1827 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001828 }
1829 return ret;
1830}
1831
1832/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001833 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001834 * inode changes. But, it is most likely to find the inode in cache.
1835 * FIXME, needs more benchmarking...there are no reasons other than performance
1836 * to keep or drop this code.
1837 */
1838void btrfs_dirty_inode(struct inode *inode)
1839{
1840 struct btrfs_root *root = BTRFS_I(inode)->root;
1841 struct btrfs_trans_handle *trans;
1842
Chris Masonf9295742008-07-17 12:54:14 -04001843 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001844 btrfs_set_trans_block_group(trans, inode);
1845 btrfs_update_inode(trans, root, inode);
1846 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001847}
1848
1849static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1850 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001851 const char *name, int name_len,
1852 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001853 u64 objectid,
1854 struct btrfs_block_group_cache *group,
1855 int mode)
1856{
1857 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001858 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001859 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001860 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001861 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001862 struct btrfs_inode_ref *ref;
1863 struct btrfs_key key[2];
1864 u32 sizes[2];
1865 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001866 int ret;
1867 int owner;
1868
Chris Mason5f39d392007-10-15 16:14:19 -04001869 path = btrfs_alloc_path();
1870 BUG_ON(!path);
1871
Chris Mason39279cc2007-06-12 06:35:45 -04001872 inode = new_inode(root->fs_info->sb);
1873 if (!inode)
1874 return ERR_PTR(-ENOMEM);
1875
Chris Masond1310b22008-01-24 16:13:08 -05001876 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1877 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001878 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001879 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1880 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001881 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001882 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Mason90692182008-02-08 13:49:28 -05001883 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001884 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001885 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001886
Chris Mason39279cc2007-06-12 06:35:45 -04001887 if (mode & S_IFDIR)
1888 owner = 0;
1889 else
1890 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001891 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001892 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001893 if (!new_inode_group) {
1894 printk("find_block group failed\n");
1895 new_inode_group = group;
1896 }
1897 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001898 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001899
1900 key[0].objectid = objectid;
1901 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1902 key[0].offset = 0;
1903
1904 key[1].objectid = objectid;
1905 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1906 key[1].offset = ref_objectid;
1907
1908 sizes[0] = sizeof(struct btrfs_inode_item);
1909 sizes[1] = name_len + sizeof(*ref);
1910
1911 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1912 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001913 goto fail;
1914
Chris Mason9c583092008-01-29 15:15:18 -05001915 if (objectid > root->highest_inode)
1916 root->highest_inode = objectid;
1917
Chris Mason39279cc2007-06-12 06:35:45 -04001918 inode->i_uid = current->fsuid;
1919 inode->i_gid = current->fsgid;
1920 inode->i_mode = mode;
1921 inode->i_ino = objectid;
1922 inode->i_blocks = 0;
1923 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001924 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1925 struct btrfs_inode_item);
1926 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001927
1928 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1929 struct btrfs_inode_ref);
1930 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1931 ptr = (unsigned long)(ref + 1);
1932 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1933
Chris Mason5f39d392007-10-15 16:14:19 -04001934 btrfs_mark_buffer_dirty(path->nodes[0]);
1935 btrfs_free_path(path);
1936
Chris Mason39279cc2007-06-12 06:35:45 -04001937 location = &BTRFS_I(inode)->location;
1938 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001939 location->offset = 0;
1940 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1941
Chris Mason39279cc2007-06-12 06:35:45 -04001942 insert_inode_hash(inode);
1943 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001944fail:
1945 btrfs_free_path(path);
1946 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001947}
1948
1949static inline u8 btrfs_inode_type(struct inode *inode)
1950{
1951 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1952}
1953
1954static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001955 struct dentry *dentry, struct inode *inode,
1956 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001957{
1958 int ret;
1959 struct btrfs_key key;
1960 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001961 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001962
Chris Mason39279cc2007-06-12 06:35:45 -04001963 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001964 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1965 key.offset = 0;
1966
1967 ret = btrfs_insert_dir_item(trans, root,
1968 dentry->d_name.name, dentry->d_name.len,
1969 dentry->d_parent->d_inode->i_ino,
1970 &key, btrfs_inode_type(inode));
1971 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001972 if (add_backref) {
1973 ret = btrfs_insert_inode_ref(trans, root,
1974 dentry->d_name.name,
1975 dentry->d_name.len,
1976 inode->i_ino,
1977 dentry->d_parent->d_inode->i_ino);
1978 }
Chris Mason79c44582007-06-25 10:09:33 -04001979 parent_inode = dentry->d_parent->d_inode;
Chris Masondbe674a2008-07-17 12:54:05 -04001980 btrfs_i_size_write(parent_inode, parent_inode->i_size +
1981 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001982 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001983 ret = btrfs_update_inode(trans, root,
1984 dentry->d_parent->d_inode);
1985 }
1986 return ret;
1987}
1988
1989static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001990 struct dentry *dentry, struct inode *inode,
1991 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001992{
Chris Mason9c583092008-01-29 15:15:18 -05001993 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001994 if (!err) {
1995 d_instantiate(dentry, inode);
1996 return 0;
1997 }
1998 if (err > 0)
1999 err = -EEXIST;
2000 return err;
2001}
2002
Josef Bacik618e21d2007-07-11 10:18:17 -04002003static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2004 int mode, dev_t rdev)
2005{
2006 struct btrfs_trans_handle *trans;
2007 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002008 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002009 int err;
2010 int drop_inode = 0;
2011 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002012 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002013
2014 if (!new_valid_dev(rdev))
2015 return -EINVAL;
2016
Chris Mason1832a6d2007-12-21 16:27:21 -05002017 err = btrfs_check_free_space(root, 1, 0);
2018 if (err)
2019 goto fail;
2020
Josef Bacik618e21d2007-07-11 10:18:17 -04002021 trans = btrfs_start_transaction(root, 1);
2022 btrfs_set_trans_block_group(trans, dir);
2023
2024 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2025 if (err) {
2026 err = -ENOSPC;
2027 goto out_unlock;
2028 }
2029
Chris Mason9c583092008-01-29 15:15:18 -05002030 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2031 dentry->d_name.len,
2032 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002033 BTRFS_I(dir)->block_group, mode);
2034 err = PTR_ERR(inode);
2035 if (IS_ERR(inode))
2036 goto out_unlock;
2037
2038 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002039 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002040 if (err)
2041 drop_inode = 1;
2042 else {
2043 inode->i_op = &btrfs_special_inode_operations;
2044 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002045 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002046 }
2047 dir->i_sb->s_dirt = 1;
2048 btrfs_update_inode_block_group(trans, inode);
2049 btrfs_update_inode_block_group(trans, dir);
2050out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002051 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002052 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002053fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002054 if (drop_inode) {
2055 inode_dec_link_count(inode);
2056 iput(inode);
2057 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002058 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002059 return err;
2060}
2061
Chris Mason39279cc2007-06-12 06:35:45 -04002062static int btrfs_create(struct inode *dir, struct dentry *dentry,
2063 int mode, struct nameidata *nd)
2064{
2065 struct btrfs_trans_handle *trans;
2066 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002067 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002068 int err;
2069 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002070 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002071 u64 objectid;
2072
Chris Mason1832a6d2007-12-21 16:27:21 -05002073 err = btrfs_check_free_space(root, 1, 0);
2074 if (err)
2075 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002076 trans = btrfs_start_transaction(root, 1);
2077 btrfs_set_trans_block_group(trans, dir);
2078
2079 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2080 if (err) {
2081 err = -ENOSPC;
2082 goto out_unlock;
2083 }
2084
Chris Mason9c583092008-01-29 15:15:18 -05002085 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2086 dentry->d_name.len,
2087 dentry->d_parent->d_inode->i_ino,
2088 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002089 err = PTR_ERR(inode);
2090 if (IS_ERR(inode))
2091 goto out_unlock;
2092
2093 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002094 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002095 if (err)
2096 drop_inode = 1;
2097 else {
2098 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002099 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002100 inode->i_fop = &btrfs_file_operations;
2101 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002102 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2103 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002104 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002105 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2106 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002107 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002108 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002109 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002110 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002111 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002112 }
2113 dir->i_sb->s_dirt = 1;
2114 btrfs_update_inode_block_group(trans, inode);
2115 btrfs_update_inode_block_group(trans, dir);
2116out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002117 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002118 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002119fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002120 if (drop_inode) {
2121 inode_dec_link_count(inode);
2122 iput(inode);
2123 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002124 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002125 return err;
2126}
2127
2128static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2129 struct dentry *dentry)
2130{
2131 struct btrfs_trans_handle *trans;
2132 struct btrfs_root *root = BTRFS_I(dir)->root;
2133 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002134 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002135 int err;
2136 int drop_inode = 0;
2137
2138 if (inode->i_nlink == 0)
2139 return -ENOENT;
2140
Chris Mason6da6aba2007-12-18 16:15:09 -05002141#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2142 inode->i_nlink++;
2143#else
Chris Mason39279cc2007-06-12 06:35:45 -04002144 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002145#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002146 err = btrfs_check_free_space(root, 1, 0);
2147 if (err)
2148 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002149 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002150
Chris Mason39279cc2007-06-12 06:35:45 -04002151 btrfs_set_trans_block_group(trans, dir);
2152 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05002153 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002154
Chris Mason39279cc2007-06-12 06:35:45 -04002155 if (err)
2156 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002157
Chris Mason39279cc2007-06-12 06:35:45 -04002158 dir->i_sb->s_dirt = 1;
2159 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002160 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002161
Chris Mason54aa1f42007-06-22 14:16:25 -04002162 if (err)
2163 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002164
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002165 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002166 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002167fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002168 if (drop_inode) {
2169 inode_dec_link_count(inode);
2170 iput(inode);
2171 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002172 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002173 return err;
2174}
2175
Chris Mason39279cc2007-06-12 06:35:45 -04002176static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2177{
Chris Masonb9d86662008-05-02 16:13:49 -04002178 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002179 struct btrfs_trans_handle *trans;
2180 struct btrfs_root *root = BTRFS_I(dir)->root;
2181 int err = 0;
2182 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002183 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002184 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002185
Chris Mason1832a6d2007-12-21 16:27:21 -05002186 err = btrfs_check_free_space(root, 1, 0);
2187 if (err)
2188 goto out_unlock;
2189
Chris Mason39279cc2007-06-12 06:35:45 -04002190 trans = btrfs_start_transaction(root, 1);
2191 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002192
Chris Mason39279cc2007-06-12 06:35:45 -04002193 if (IS_ERR(trans)) {
2194 err = PTR_ERR(trans);
2195 goto out_unlock;
2196 }
2197
2198 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2199 if (err) {
2200 err = -ENOSPC;
2201 goto out_unlock;
2202 }
2203
Chris Mason9c583092008-01-29 15:15:18 -05002204 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2205 dentry->d_name.len,
2206 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002207 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2208 if (IS_ERR(inode)) {
2209 err = PTR_ERR(inode);
2210 goto out_fail;
2211 }
Chris Mason5f39d392007-10-15 16:14:19 -04002212
Chris Mason39279cc2007-06-12 06:35:45 -04002213 drop_on_err = 1;
2214 inode->i_op = &btrfs_dir_inode_operations;
2215 inode->i_fop = &btrfs_dir_file_operations;
2216 btrfs_set_trans_block_group(trans, inode);
2217
Chris Masondbe674a2008-07-17 12:54:05 -04002218 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002219 err = btrfs_update_inode(trans, root, inode);
2220 if (err)
2221 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002222
Chris Mason9c583092008-01-29 15:15:18 -05002223 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002224 if (err)
2225 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002226
Chris Mason39279cc2007-06-12 06:35:45 -04002227 d_instantiate(dentry, inode);
2228 drop_on_err = 0;
2229 dir->i_sb->s_dirt = 1;
2230 btrfs_update_inode_block_group(trans, inode);
2231 btrfs_update_inode_block_group(trans, dir);
2232
2233out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002234 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002235 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002236
Chris Mason39279cc2007-06-12 06:35:45 -04002237out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002238 if (drop_on_err)
2239 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002240 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002241 return err;
2242}
2243
Chris Mason3b951512008-04-17 11:29:12 -04002244static int merge_extent_mapping(struct extent_map_tree *em_tree,
2245 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002246 struct extent_map *em,
2247 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002248{
2249 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002250
Chris Masone6dcd2d2008-07-17 12:53:50 -04002251 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2252 start_diff = map_start - em->start;
2253 em->start = map_start;
2254 em->len = map_len;
2255 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2256 em->block_start += start_diff;
2257 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002258}
2259
Chris Masona52d9a82007-08-27 16:49:44 -04002260struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002261 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002262 int create)
2263{
2264 int ret;
2265 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002266 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002267 u64 extent_start = 0;
2268 u64 extent_end = 0;
2269 u64 objectid = inode->i_ino;
2270 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002271 struct btrfs_path *path;
2272 struct btrfs_root *root = BTRFS_I(inode)->root;
2273 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002274 struct extent_buffer *leaf;
2275 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002276 struct extent_map *em = NULL;
2277 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002278 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002279 struct btrfs_trans_handle *trans = NULL;
2280
2281 path = btrfs_alloc_path();
2282 BUG_ON(!path);
Chris Masona52d9a82007-08-27 16:49:44 -04002283
2284again:
Chris Masond1310b22008-01-24 16:13:08 -05002285 spin_lock(&em_tree->lock);
2286 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002287 if (em)
2288 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002289 spin_unlock(&em_tree->lock);
2290
Chris Masona52d9a82007-08-27 16:49:44 -04002291 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002292 if (em->start > start || em->start + em->len <= start)
2293 free_extent_map(em);
2294 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002295 free_extent_map(em);
2296 else
2297 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002298 }
Chris Masond1310b22008-01-24 16:13:08 -05002299 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002300 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002301 err = -ENOMEM;
2302 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002303 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002304 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002305 em->start = EXTENT_MAP_HOLE;
2306 em->len = (u64)-1;
Chris Mason179e29e2007-11-01 11:28:41 -04002307 ret = btrfs_lookup_file_extent(trans, root, path,
2308 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002309 if (ret < 0) {
2310 err = ret;
2311 goto out;
2312 }
2313
2314 if (ret != 0) {
2315 if (path->slots[0] == 0)
2316 goto not_found;
2317 path->slots[0]--;
2318 }
2319
Chris Mason5f39d392007-10-15 16:14:19 -04002320 leaf = path->nodes[0];
2321 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002322 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002323 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002324 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2325 found_type = btrfs_key_type(&found_key);
2326 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002327 found_type != BTRFS_EXTENT_DATA_KEY) {
2328 goto not_found;
2329 }
2330
Chris Mason5f39d392007-10-15 16:14:19 -04002331 found_type = btrfs_file_extent_type(leaf, item);
2332 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002333 if (found_type == BTRFS_FILE_EXTENT_REG) {
2334 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002335 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002336 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002337 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002338 em->start = start;
2339 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002340 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002341 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002342 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002343 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002344 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002345 }
2346 goto not_found_em;
2347 }
Chris Masondb945352007-10-15 16:15:53 -04002348 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2349 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002350 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002351 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002352 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002353 goto insert;
2354 }
Chris Masondb945352007-10-15 16:15:53 -04002355 bytenr += btrfs_file_extent_offset(leaf, item);
2356 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002357 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002358 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002359 goto insert;
2360 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002361 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002362 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002363 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002364 size_t size;
2365 size_t extent_offset;
2366 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002367
Chris Mason5f39d392007-10-15 16:14:19 -04002368 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2369 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002370 extent_end = (extent_start + size + root->sectorsize - 1) &
2371 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002372 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002373 em->start = start;
2374 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002375 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002376 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002377 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002378 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002379 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002380 }
2381 goto not_found_em;
2382 }
2383 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002384
2385 if (!page) {
2386 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002387 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002388 goto out;
2389 }
2390
Chris Mason70dec802008-01-29 09:59:12 -05002391 page_start = page_offset(page) + pg_offset;
2392 extent_offset = page_start - extent_start;
2393 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002394 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002395 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002396 em->len = (copy_size + root->sectorsize - 1) &
2397 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002398 map = kmap(page);
2399 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002400 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002401 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002402 copy_size);
2403 flush_dcache_page(page);
2404 } else if (create && PageUptodate(page)) {
2405 if (!trans) {
2406 kunmap(page);
2407 free_extent_map(em);
2408 em = NULL;
2409 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002410 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002411 goto again;
2412 }
Chris Mason70dec802008-01-29 09:59:12 -05002413 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002414 copy_size);
2415 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002416 }
Chris Masona52d9a82007-08-27 16:49:44 -04002417 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002418 set_extent_uptodate(io_tree, em->start,
2419 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002420 goto insert;
2421 } else {
2422 printk("unkknown found_type %d\n", found_type);
2423 WARN_ON(1);
2424 }
2425not_found:
2426 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002427 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002428not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002429 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002430insert:
2431 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002432 if (em->start > start || extent_map_end(em) <= start) {
2433 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002434 err = -EIO;
2435 goto out;
2436 }
Chris Masond1310b22008-01-24 16:13:08 -05002437
2438 err = 0;
2439 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002440 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002441 /* it is possible that someone inserted the extent into the tree
2442 * while we had the lock dropped. It is also possible that
2443 * an overlapping map exists in the tree
2444 */
Chris Masona52d9a82007-08-27 16:49:44 -04002445 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002446 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002447
2448 ret = 0;
2449
Chris Mason3b951512008-04-17 11:29:12 -04002450 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002451 if (existing && (existing->start > start ||
2452 existing->start + existing->len <= start)) {
2453 free_extent_map(existing);
2454 existing = NULL;
2455 }
Chris Mason3b951512008-04-17 11:29:12 -04002456 if (!existing) {
2457 existing = lookup_extent_mapping(em_tree, em->start,
2458 em->len);
2459 if (existing) {
2460 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002461 em, start,
2462 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002463 free_extent_map(existing);
2464 if (err) {
2465 free_extent_map(em);
2466 em = NULL;
2467 }
2468 } else {
2469 err = -EIO;
2470 printk("failing to insert %Lu %Lu\n",
2471 start, len);
2472 free_extent_map(em);
2473 em = NULL;
2474 }
2475 } else {
2476 free_extent_map(em);
2477 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002478 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002479 }
Chris Masona52d9a82007-08-27 16:49:44 -04002480 }
Chris Masond1310b22008-01-24 16:13:08 -05002481 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002482out:
2483 btrfs_free_path(path);
2484 if (trans) {
2485 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002486 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002487 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002488 }
Chris Masona52d9a82007-08-27 16:49:44 -04002489 }
Chris Masona52d9a82007-08-27 16:49:44 -04002490 if (err) {
2491 free_extent_map(em);
2492 WARN_ON(1);
2493 return ERR_PTR(err);
2494 }
2495 return em;
2496}
2497
Chris Masone1c4b742008-04-22 13:26:46 -04002498#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002499static int btrfs_get_block(struct inode *inode, sector_t iblock,
2500 struct buffer_head *bh_result, int create)
2501{
2502 struct extent_map *em;
2503 u64 start = (u64)iblock << inode->i_blkbits;
2504 struct btrfs_multi_bio *multi = NULL;
2505 struct btrfs_root *root = BTRFS_I(inode)->root;
2506 u64 len;
2507 u64 logical;
2508 u64 map_length;
2509 int ret = 0;
2510
2511 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2512
2513 if (!em || IS_ERR(em))
2514 goto out;
2515
Chris Masone1c4b742008-04-22 13:26:46 -04002516 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002517 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002518 }
Chris Mason16432982008-04-10 10:23:21 -04002519
2520 if (em->block_start == EXTENT_MAP_INLINE) {
2521 ret = -EINVAL;
2522 goto out;
2523 }
2524
Chris Mason16432982008-04-10 10:23:21 -04002525 len = em->start + em->len - start;
2526 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2527
Chris Masone1c4b742008-04-22 13:26:46 -04002528 if (em->block_start == EXTENT_MAP_HOLE ||
2529 em->block_start == EXTENT_MAP_DELALLOC) {
2530 bh_result->b_size = len;
2531 goto out;
2532 }
2533
Chris Mason16432982008-04-10 10:23:21 -04002534 logical = start - em->start;
2535 logical = em->block_start + logical;
2536
2537 map_length = len;
2538 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2539 logical, &map_length, &multi, 0);
2540 BUG_ON(ret);
2541 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2542 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002543
Chris Mason16432982008-04-10 10:23:21 -04002544 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2545 set_buffer_mapped(bh_result);
2546 kfree(multi);
2547out:
2548 free_extent_map(em);
2549 return ret;
2550}
Chris Masone1c4b742008-04-22 13:26:46 -04002551#endif
Chris Mason16432982008-04-10 10:23:21 -04002552
2553static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2554 const struct iovec *iov, loff_t offset,
2555 unsigned long nr_segs)
2556{
Chris Masone1c4b742008-04-22 13:26:46 -04002557 return -EINVAL;
2558#if 0
Chris Mason16432982008-04-10 10:23:21 -04002559 struct file *file = iocb->ki_filp;
2560 struct inode *inode = file->f_mapping->host;
2561
2562 if (rw == WRITE)
2563 return -EINVAL;
2564
2565 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2566 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002567#endif
Chris Mason16432982008-04-10 10:23:21 -04002568}
2569
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002570static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002571{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002572 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002573}
2574
Chris Mason9ebefb182007-06-15 13:50:00 -04002575int btrfs_readpage(struct file *file, struct page *page)
2576{
Chris Masond1310b22008-01-24 16:13:08 -05002577 struct extent_io_tree *tree;
2578 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002579 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002580}
Chris Mason1832a6d2007-12-21 16:27:21 -05002581
Chris Mason39279cc2007-06-12 06:35:45 -04002582static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2583{
Chris Masond1310b22008-01-24 16:13:08 -05002584 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002585
2586
2587 if (current->flags & PF_MEMALLOC) {
2588 redirty_page_for_writepage(wbc, page);
2589 unlock_page(page);
2590 return 0;
2591 }
Chris Masond1310b22008-01-24 16:13:08 -05002592 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002593 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2594}
Chris Mason39279cc2007-06-12 06:35:45 -04002595
Chris Masonb293f022007-11-01 19:45:34 -04002596static int btrfs_writepages(struct address_space *mapping,
2597 struct writeback_control *wbc)
2598{
Chris Masond1310b22008-01-24 16:13:08 -05002599 struct extent_io_tree *tree;
2600 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002601 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2602}
2603
Chris Mason3ab2fb52007-11-08 10:59:22 -05002604static int
2605btrfs_readpages(struct file *file, struct address_space *mapping,
2606 struct list_head *pages, unsigned nr_pages)
2607{
Chris Masond1310b22008-01-24 16:13:08 -05002608 struct extent_io_tree *tree;
2609 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002610 return extent_readpages(tree, mapping, pages, nr_pages,
2611 btrfs_get_extent);
2612}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002613static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002614{
Chris Masond1310b22008-01-24 16:13:08 -05002615 struct extent_io_tree *tree;
2616 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002617 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002618
Chris Masond1310b22008-01-24 16:13:08 -05002619 tree = &BTRFS_I(page->mapping->host)->io_tree;
2620 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002621 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002622 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002623 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002624 ClearPagePrivate(page);
2625 set_page_private(page, 0);
2626 page_cache_release(page);
2627 }
2628 return ret;
2629}
Chris Mason39279cc2007-06-12 06:35:45 -04002630
Chris Masone6dcd2d2008-07-17 12:53:50 -04002631static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2632{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002633 return __btrfs_releasepage(page, gfp_flags);
2634}
2635
Chris Masona52d9a82007-08-27 16:49:44 -04002636static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2637{
Chris Masond1310b22008-01-24 16:13:08 -05002638 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002639 struct btrfs_ordered_extent *ordered;
2640 u64 page_start = page_offset(page);
2641 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002642
Chris Masone6dcd2d2008-07-17 12:53:50 -04002643 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002644 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645 if (offset) {
2646 btrfs_releasepage(page, GFP_NOFS);
2647 return;
2648 }
2649
2650 lock_extent(tree, page_start, page_end, GFP_NOFS);
2651 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2652 page_offset(page));
2653 if (ordered) {
2654 clear_extent_bit(tree, page_start, page_end,
2655 EXTENT_DIRTY | EXTENT_DELALLOC |
2656 EXTENT_LOCKED, 1, 0, GFP_NOFS);
2657 btrfs_writepage_end_io_hook(page, page_start,
2658 page_end, NULL, 1);
2659 btrfs_put_ordered_extent(ordered);
2660 lock_extent(tree, page_start, page_end, GFP_NOFS);
2661 }
2662 clear_extent_bit(tree, page_start, page_end,
2663 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2664 EXTENT_ORDERED,
2665 1, 1, GFP_NOFS);
2666 __btrfs_releasepage(page, GFP_NOFS);
2667
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002668 if (PagePrivate(page)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002669 invalidate_extent_lru(tree, page_offset(page),
2670 PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002671 ClearPagePrivate(page);
2672 set_page_private(page, 0);
2673 page_cache_release(page);
2674 }
Chris Mason39279cc2007-06-12 06:35:45 -04002675}
2676
Chris Mason9ebefb182007-06-15 13:50:00 -04002677/*
2678 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2679 * called from a page fault handler when a page is first dirtied. Hence we must
2680 * be careful to check for EOF conditions here. We set the page up correctly
2681 * for a written page which means we get ENOSPC checking when writing into
2682 * holes and correct delalloc and unwritten extent mapping on filesystems that
2683 * support these features.
2684 *
2685 * We are not allowed to take the i_mutex here so we have to play games to
2686 * protect against truncate races as the page could now be beyond EOF. Because
2687 * vmtruncate() writes the inode size before removing pages, once we have the
2688 * page lock we can determine safely if the page is beyond EOF. If it is not
2689 * beyond EOF, then the page is guaranteed safe against truncation until we
2690 * unlock the page.
2691 */
2692int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2693{
Chris Mason6da6aba2007-12-18 16:15:09 -05002694 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002695 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002696 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2697 struct btrfs_ordered_extent *ordered;
2698 char *kaddr;
2699 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002700 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002701 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002702 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002703 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04002704
Chris Mason1832a6d2007-12-21 16:27:21 -05002705 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05002706 if (ret)
2707 goto out;
2708
2709 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002710again:
Chris Mason9ebefb182007-06-15 13:50:00 -04002711 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002712 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002713 page_start = page_offset(page);
2714 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002715
Chris Mason9ebefb182007-06-15 13:50:00 -04002716 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04002717 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002718 /* page got truncated out from underneath us */
2719 goto out_unlock;
2720 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002721 wait_on_page_writeback(page);
2722
2723 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2724 set_page_extent_mapped(page);
2725
2726 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2727 if (ordered) {
2728 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2729 unlock_page(page);
2730 btrfs_wait_ordered_extent(inode, ordered);
2731 btrfs_put_ordered_extent(ordered);
2732 goto again;
2733 }
2734
2735 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
2736 page_end, GFP_NOFS);
2737 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04002738
2739 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002740 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002741 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04002742 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04002743 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04002744
Chris Masone6dcd2d2008-07-17 12:53:50 -04002745 if (zero_start != PAGE_CACHE_SIZE) {
2746 kaddr = kmap(page);
2747 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
2748 flush_dcache_page(page);
2749 kunmap(page);
2750 }
Chris Mason247e7432008-07-17 12:53:51 -04002751 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002752 set_page_dirty(page);
2753 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04002754
2755out_unlock:
2756 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002757out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002758 return ret;
2759}
2760
Chris Mason39279cc2007-06-12 06:35:45 -04002761static void btrfs_truncate(struct inode *inode)
2762{
2763 struct btrfs_root *root = BTRFS_I(inode)->root;
2764 int ret;
2765 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002766 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04002767 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002768
2769 if (!S_ISREG(inode->i_mode))
2770 return;
2771 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2772 return;
2773
2774 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2775
Chris Mason39279cc2007-06-12 06:35:45 -04002776 trans = btrfs_start_transaction(root, 1);
2777 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04002778 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
2779 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04002780
2781 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002782 ret = btrfs_truncate_in_trans(trans, root, inode,
2783 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002784 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002785 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002786
Chris Mason89ce8a62008-06-25 16:01:31 -04002787 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002788 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002789 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002790}
2791
Sven Wegener3b963622008-06-09 21:57:42 -04002792/*
2793 * Invalidate a single dcache entry at the root of the filesystem.
2794 * Needed after creation of snapshot or subvolume.
2795 */
2796void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2797 int namelen)
2798{
2799 struct dentry *alias, *entry;
2800 struct qstr qstr;
2801
2802 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2803 if (alias) {
2804 qstr.name = name;
2805 qstr.len = namelen;
2806 /* change me if btrfs ever gets a d_hash operation */
2807 qstr.hash = full_name_hash(qstr.name, qstr.len);
2808 entry = d_lookup(alias, &qstr);
2809 dput(alias);
2810 if (entry) {
2811 d_invalidate(entry);
2812 dput(entry);
2813 }
2814 }
2815}
2816
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002817int btrfs_create_subvol_root(struct btrfs_root *new_root,
2818 struct btrfs_trans_handle *trans, u64 new_dirid,
2819 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04002820{
Chris Mason39279cc2007-06-12 06:35:45 -04002821 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002822 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002823
Chris Mason9c583092008-01-29 15:15:18 -05002824 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002825 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002826 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002827 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002828 inode->i_op = &btrfs_dir_inode_operations;
2829 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002830 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002831
Chris Mason39544012007-12-12 14:38:19 -05002832 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2833 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002834 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04002835 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04002836
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002837 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002838}
2839
Chris Masonedbd8d42007-12-21 16:27:24 -05002840unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002841 struct file_ra_state *ra, struct file *file,
2842 pgoff_t offset, pgoff_t last_index)
2843{
Chris Mason8e7bf942008-04-28 09:02:36 -04002844 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04002845
2846#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04002847 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2848 return offset;
2849#else
Chris Mason86479a02007-09-10 19:58:16 -04002850 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2851 return offset + req_size;
2852#endif
2853}
2854
Chris Mason39279cc2007-06-12 06:35:45 -04002855struct inode *btrfs_alloc_inode(struct super_block *sb)
2856{
2857 struct btrfs_inode *ei;
2858
2859 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2860 if (!ei)
2861 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002862 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002863 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002864 return &ei->vfs_inode;
2865}
2866
2867void btrfs_destroy_inode(struct inode *inode)
2868{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002869 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04002870 WARN_ON(!list_empty(&inode->i_dentry));
2871 WARN_ON(inode->i_data.nrpages);
2872
Chris Masone6dcd2d2008-07-17 12:53:50 -04002873 while(1) {
2874 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
2875 if (!ordered)
2876 break;
2877 else {
2878 printk("found ordered extent %Lu %Lu\n",
2879 ordered->file_offset, ordered->len);
2880 btrfs_remove_ordered_extent(inode, ordered);
2881 btrfs_put_ordered_extent(ordered);
2882 btrfs_put_ordered_extent(ordered);
2883 }
2884 }
Chris Mason8c416c92008-01-14 15:10:26 -05002885 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002886 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2887}
2888
Chris Mason44ec0b72007-10-29 10:55:05 -04002889#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2890static void init_once(struct kmem_cache * cachep, void *foo)
2891#else
Chris Mason39279cc2007-06-12 06:35:45 -04002892static void init_once(void * foo, struct kmem_cache * cachep,
2893 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002894#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002895{
2896 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2897
2898 inode_init_once(&ei->vfs_inode);
2899}
2900
2901void btrfs_destroy_cachep(void)
2902{
2903 if (btrfs_inode_cachep)
2904 kmem_cache_destroy(btrfs_inode_cachep);
2905 if (btrfs_trans_handle_cachep)
2906 kmem_cache_destroy(btrfs_trans_handle_cachep);
2907 if (btrfs_transaction_cachep)
2908 kmem_cache_destroy(btrfs_transaction_cachep);
2909 if (btrfs_bit_radix_cachep)
2910 kmem_cache_destroy(btrfs_bit_radix_cachep);
2911 if (btrfs_path_cachep)
2912 kmem_cache_destroy(btrfs_path_cachep);
2913}
2914
Chris Mason86479a02007-09-10 19:58:16 -04002915struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002916 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002917#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2918 void (*ctor)(struct kmem_cache *, void *)
2919#else
Chris Mason92fee662007-07-25 12:31:35 -04002920 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002921 unsigned long)
2922#endif
2923 )
Chris Mason92fee662007-07-25 12:31:35 -04002924{
2925 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2926 SLAB_MEM_SPREAD | extra_flags), ctor
2927#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2928 ,NULL
2929#endif
2930 );
2931}
2932
Chris Mason39279cc2007-06-12 06:35:45 -04002933int btrfs_init_cachep(void)
2934{
Chris Mason86479a02007-09-10 19:58:16 -04002935 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002936 sizeof(struct btrfs_inode),
2937 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002938 if (!btrfs_inode_cachep)
2939 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002940 btrfs_trans_handle_cachep =
2941 btrfs_cache_create("btrfs_trans_handle_cache",
2942 sizeof(struct btrfs_trans_handle),
2943 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002944 if (!btrfs_trans_handle_cachep)
2945 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002946 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002947 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002948 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002949 if (!btrfs_transaction_cachep)
2950 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002951 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002952 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002953 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002954 if (!btrfs_path_cachep)
2955 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002956 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002957 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002958 if (!btrfs_bit_radix_cachep)
2959 goto fail;
2960 return 0;
2961fail:
2962 btrfs_destroy_cachep();
2963 return -ENOMEM;
2964}
2965
2966static int btrfs_getattr(struct vfsmount *mnt,
2967 struct dentry *dentry, struct kstat *stat)
2968{
2969 struct inode *inode = dentry->d_inode;
2970 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002971 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05002972 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04002973 return 0;
2974}
2975
2976static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2977 struct inode * new_dir,struct dentry *new_dentry)
2978{
2979 struct btrfs_trans_handle *trans;
2980 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2981 struct inode *new_inode = new_dentry->d_inode;
2982 struct inode *old_inode = old_dentry->d_inode;
2983 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002984 int ret;
2985
2986 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2987 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2988 return -ENOTEMPTY;
2989 }
Chris Mason5f39d392007-10-15 16:14:19 -04002990
Chris Mason1832a6d2007-12-21 16:27:21 -05002991 ret = btrfs_check_free_space(root, 1, 0);
2992 if (ret)
2993 goto out_unlock;
2994
Chris Mason39279cc2007-06-12 06:35:45 -04002995 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002996
Chris Mason39279cc2007-06-12 06:35:45 -04002997 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04002998
2999 old_dentry->d_inode->i_nlink++;
3000 old_dir->i_ctime = old_dir->i_mtime = ctime;
3001 new_dir->i_ctime = new_dir->i_mtime = ctime;
3002 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003003
Chris Mason39279cc2007-06-12 06:35:45 -04003004 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3005 if (ret)
3006 goto out_fail;
3007
3008 if (new_inode) {
3009 new_inode->i_ctime = CURRENT_TIME;
3010 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3011 if (ret)
3012 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003013 }
Chris Mason9c583092008-01-29 15:15:18 -05003014 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003015 if (ret)
3016 goto out_fail;
3017
3018out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003019 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003020out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003021 return ret;
3022}
3023
3024static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3025 const char *symname)
3026{
3027 struct btrfs_trans_handle *trans;
3028 struct btrfs_root *root = BTRFS_I(dir)->root;
3029 struct btrfs_path *path;
3030 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003031 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003032 int err;
3033 int drop_inode = 0;
3034 u64 objectid;
3035 int name_len;
3036 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003037 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003038 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003039 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003040 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003041
3042 name_len = strlen(symname) + 1;
3043 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3044 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003045
Chris Mason1832a6d2007-12-21 16:27:21 -05003046 err = btrfs_check_free_space(root, 1, 0);
3047 if (err)
3048 goto out_fail;
3049
Chris Mason39279cc2007-06-12 06:35:45 -04003050 trans = btrfs_start_transaction(root, 1);
3051 btrfs_set_trans_block_group(trans, dir);
3052
3053 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3054 if (err) {
3055 err = -ENOSPC;
3056 goto out_unlock;
3057 }
3058
Chris Mason9c583092008-01-29 15:15:18 -05003059 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3060 dentry->d_name.len,
3061 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003062 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3063 err = PTR_ERR(inode);
3064 if (IS_ERR(inode))
3065 goto out_unlock;
3066
3067 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003068 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003069 if (err)
3070 drop_inode = 1;
3071 else {
3072 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003073 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003074 inode->i_fop = &btrfs_file_operations;
3075 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003076 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3077 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003078 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003079 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3080 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04003081 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003082 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003083 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003084 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003085 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003086 }
3087 dir->i_sb->s_dirt = 1;
3088 btrfs_update_inode_block_group(trans, inode);
3089 btrfs_update_inode_block_group(trans, dir);
3090 if (drop_inode)
3091 goto out_unlock;
3092
3093 path = btrfs_alloc_path();
3094 BUG_ON(!path);
3095 key.objectid = inode->i_ino;
3096 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003097 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3098 datasize = btrfs_file_extent_calc_inline_size(name_len);
3099 err = btrfs_insert_empty_item(trans, root, path, &key,
3100 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003101 if (err) {
3102 drop_inode = 1;
3103 goto out_unlock;
3104 }
Chris Mason5f39d392007-10-15 16:14:19 -04003105 leaf = path->nodes[0];
3106 ei = btrfs_item_ptr(leaf, path->slots[0],
3107 struct btrfs_file_extent_item);
3108 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3109 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003110 BTRFS_FILE_EXTENT_INLINE);
3111 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003112 write_extent_buffer(leaf, symname, ptr, name_len);
3113 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003114 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003115
Chris Mason39279cc2007-06-12 06:35:45 -04003116 inode->i_op = &btrfs_symlink_inode_operations;
3117 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003118 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003119 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003120 err = btrfs_update_inode(trans, root, inode);
3121 if (err)
3122 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003123
3124out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003125 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003126 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003127out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003128 if (drop_inode) {
3129 inode_dec_link_count(inode);
3130 iput(inode);
3131 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003132 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003133 return err;
3134}
Chris Mason16432982008-04-10 10:23:21 -04003135
Chris Masone6dcd2d2008-07-17 12:53:50 -04003136static int btrfs_set_page_dirty(struct page *page)
3137{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003138 return __set_page_dirty_nobuffers(page);
3139}
3140
Yanfdebe2b2008-01-14 13:26:08 -05003141static int btrfs_permission(struct inode *inode, int mask,
3142 struct nameidata *nd)
3143{
3144 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3145 return -EACCES;
3146 return generic_permission(inode, mask, NULL);
3147}
Chris Mason39279cc2007-06-12 06:35:45 -04003148
3149static struct inode_operations btrfs_dir_inode_operations = {
3150 .lookup = btrfs_lookup,
3151 .create = btrfs_create,
3152 .unlink = btrfs_unlink,
3153 .link = btrfs_link,
3154 .mkdir = btrfs_mkdir,
3155 .rmdir = btrfs_rmdir,
3156 .rename = btrfs_rename,
3157 .symlink = btrfs_symlink,
3158 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003159 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003160 .setxattr = generic_setxattr,
3161 .getxattr = generic_getxattr,
3162 .listxattr = btrfs_listxattr,
3163 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003164 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003165};
Chris Mason39279cc2007-06-12 06:35:45 -04003166static struct inode_operations btrfs_dir_ro_inode_operations = {
3167 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003168 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003169};
Chris Mason39279cc2007-06-12 06:35:45 -04003170static struct file_operations btrfs_dir_file_operations = {
3171 .llseek = generic_file_llseek,
3172 .read = generic_read_dir,
3173 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003174 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003175#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003176 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003177#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003178 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003179};
3180
Chris Masond1310b22008-01-24 16:13:08 -05003181static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003182 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003183 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003184 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003185 .readpage_io_hook = btrfs_readpage_io_hook,
3186 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003187 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003188 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003189 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003190 .set_bit_hook = btrfs_set_bit_hook,
3191 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003192};
3193
Chris Mason39279cc2007-06-12 06:35:45 -04003194static struct address_space_operations btrfs_aops = {
3195 .readpage = btrfs_readpage,
3196 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003197 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003198 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003199 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003200 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003201 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003202 .invalidatepage = btrfs_invalidatepage,
3203 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003204 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003205};
3206
3207static struct address_space_operations btrfs_symlink_aops = {
3208 .readpage = btrfs_readpage,
3209 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003210 .invalidatepage = btrfs_invalidatepage,
3211 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003212};
3213
3214static struct inode_operations btrfs_file_inode_operations = {
3215 .truncate = btrfs_truncate,
3216 .getattr = btrfs_getattr,
3217 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003218 .setxattr = generic_setxattr,
3219 .getxattr = generic_getxattr,
3220 .listxattr = btrfs_listxattr,
3221 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003222 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003223};
Josef Bacik618e21d2007-07-11 10:18:17 -04003224static struct inode_operations btrfs_special_inode_operations = {
3225 .getattr = btrfs_getattr,
3226 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003227 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003228};
Chris Mason39279cc2007-06-12 06:35:45 -04003229static struct inode_operations btrfs_symlink_inode_operations = {
3230 .readlink = generic_readlink,
3231 .follow_link = page_follow_link_light,
3232 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003233 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003234};