blob: 8803abc89bb85ff892836196bb5ed994c6c3d987 [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 Masone0156402008-04-16 11:15:20 -0400354
Chris Mason3edf7d32008-07-18 06:17:13 -0400355 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400356 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400357
Chris Mason8b712842008-06-11 16:50:36 -0400358 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400359}
360
361int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
362 int mirror_num)
363{
364 struct btrfs_root *root = BTRFS_I(inode)->root;
365 int ret = 0;
366
Chris Masone6dcd2d2008-07-17 12:53:50 -0400367 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
368 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500369
Chris Masone6dcd2d2008-07-17 12:53:50 -0400370 if (!(rw & (1 << BIO_RW))) {
Chris Mason0b86a832008-03-24 15:01:56 -0400371 goto mapit;
372 }
Chris Mason065631f2008-02-20 12:07:25 -0500373
Chris Mason44b8bd72008-04-16 11:14:51 -0400374 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
375 inode, rw, bio, mirror_num,
376 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400377mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400378 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500379}
Chris Mason6885f302008-02-20 16:11:05 -0500380
Chris Masonba1da2f2008-07-17 12:54:15 -0400381static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400382 struct inode *inode, u64 file_offset,
383 struct list_head *list)
384{
385 struct list_head *cur;
386 struct btrfs_ordered_sum *sum;
387
388 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400389 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400390 sum = list_entry(cur, struct btrfs_ordered_sum, list);
391 mutex_lock(&BTRFS_I(inode)->csum_mutex);
392 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
393 inode, sum);
394 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400395 }
396 return 0;
397}
398
Chris Mason247e7432008-07-17 12:53:51 -0400399struct btrfs_writepage_fixup {
400 struct page *page;
401 struct btrfs_work work;
402};
403
404/* see btrfs_writepage_start_hook for details on why this is required */
405void btrfs_writepage_fixup_worker(struct btrfs_work *work)
406{
407 struct btrfs_writepage_fixup *fixup;
408 struct btrfs_ordered_extent *ordered;
409 struct page *page;
410 struct inode *inode;
411 u64 page_start;
412 u64 page_end;
413
414 fixup = container_of(work, struct btrfs_writepage_fixup, work);
415 page = fixup->page;
416
417 lock_page(page);
418 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
419 ClearPageChecked(page);
420 goto out_page;
421 }
422
423 inode = page->mapping->host;
424 page_start = page_offset(page);
425 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
426
427 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
428 ordered = btrfs_lookup_ordered_extent(inode, page_start);
429 if (ordered)
430 goto out;
431
432 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
433 GFP_NOFS);
434 ClearPageChecked(page);
435out:
436 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
437out_page:
438 unlock_page(page);
439 page_cache_release(page);
440}
441
442/*
443 * There are a few paths in the higher layers of the kernel that directly
444 * set the page dirty bit without asking the filesystem if it is a
445 * good idea. This causes problems because we want to make sure COW
446 * properly happens and the data=ordered rules are followed.
447 *
448 * In our case any range that doesn't have the EXTENT_ORDERED bit set
449 * hasn't been properly setup for IO. We kick off an async process
450 * to fix it up. The async helper will wait for ordered extents, set
451 * the delalloc bit and make it safe to write the page.
452 */
453int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
454{
455 struct inode *inode = page->mapping->host;
456 struct btrfs_writepage_fixup *fixup;
457 struct btrfs_root *root = BTRFS_I(inode)->root;
458 int ret;
459
460 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
461 EXTENT_ORDERED, 0);
462 if (ret)
463 return 0;
464
465 if (PageChecked(page))
466 return -EAGAIN;
467
468 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
469 if (!fixup)
470 return -EAGAIN;
471printk("queueing worker to fixup page %lu %Lu\n", inode->i_ino, page_offset(page));
472 SetPageChecked(page);
473 page_cache_get(page);
474 fixup->work.func = btrfs_writepage_fixup_worker;
475 fixup->page = page;
476 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
477 return -EAGAIN;
478}
479
Chris Mason211f90e2008-07-18 11:56:15 -0400480static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400481{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400482 struct btrfs_root *root = BTRFS_I(inode)->root;
483 struct btrfs_trans_handle *trans;
484 struct btrfs_ordered_extent *ordered_extent;
485 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
486 u64 alloc_hint = 0;
487 struct list_head list;
488 struct btrfs_key ins;
489 int ret;
490
491 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400492 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400493 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400494
Chris Masonf9295742008-07-17 12:54:14 -0400495 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400496
497 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
498 BUG_ON(!ordered_extent);
499
500 lock_extent(io_tree, ordered_extent->file_offset,
501 ordered_extent->file_offset + ordered_extent->len - 1,
502 GFP_NOFS);
503
504 INIT_LIST_HEAD(&list);
505
506 ins.objectid = ordered_extent->start;
507 ins.offset = ordered_extent->len;
508 ins.type = BTRFS_EXTENT_ITEM_KEY;
509 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
510 trans->transid, inode->i_ino,
511 ordered_extent->file_offset, &ins);
512 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400513
514 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400515 ret = btrfs_drop_extents(trans, root, inode,
516 ordered_extent->file_offset,
517 ordered_extent->file_offset +
518 ordered_extent->len,
519 ordered_extent->file_offset, &alloc_hint);
520 BUG_ON(ret);
521 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
522 ordered_extent->file_offset,
523 ordered_extent->start,
524 ordered_extent->len,
525 ordered_extent->len, 0);
526 BUG_ON(ret);
527 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
528 ordered_extent->file_offset +
529 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400530 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
531
Chris Masone6dcd2d2008-07-17 12:53:50 -0400532 inode->i_blocks += ordered_extent->len >> 9;
533 unlock_extent(io_tree, ordered_extent->file_offset,
534 ordered_extent->file_offset + ordered_extent->len - 1,
535 GFP_NOFS);
536 add_pending_csums(trans, inode, ordered_extent->file_offset,
537 &ordered_extent->list);
538
Chris Masondbe674a2008-07-17 12:54:05 -0400539 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400540 btrfs_remove_ordered_extent(inode, ordered_extent);
541 /* once for us */
542 btrfs_put_ordered_extent(ordered_extent);
543 /* once for the tree */
544 btrfs_put_ordered_extent(ordered_extent);
545
546 btrfs_update_inode(trans, root, inode);
547 btrfs_end_transaction(trans, root);
548 return 0;
549}
550
Chris Mason211f90e2008-07-18 11:56:15 -0400551int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
552 struct extent_state *state, int uptodate)
553{
554 return btrfs_finish_ordered_io(page->mapping->host, start, end);
555}
556
Chris Mason07157aa2007-08-30 08:50:51 -0400557int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
558{
559 int ret = 0;
560 struct inode *inode = page->mapping->host;
561 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500562 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400563 struct btrfs_csum_item *item;
564 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400565 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400566
Yanb98b6762008-01-08 15:54:37 -0500567 if (btrfs_test_opt(root, NODATASUM) ||
568 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500569 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400570
Chris Mason07157aa2007-08-30 08:50:51 -0400571 path = btrfs_alloc_path();
572 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
573 if (IS_ERR(item)) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400574 /*
575 * It is possible there is an ordered extent that has
576 * not yet finished for this range in the file. If so,
577 * that extent will have a csum cached, and it will insert
578 * the sum after all the blocks in the extent are fully
579 * on disk. So, look for an ordered extent and use the
580 * sum if found.
581 */
582 ret = btrfs_find_ordered_sum(inode, start, &csum);
583 if (ret == 0)
584 goto found;
585
Chris Mason07157aa2007-08-30 08:50:51 -0400586 ret = PTR_ERR(item);
587 /* a csum that isn't present is a preallocated region. */
588 if (ret == -ENOENT || ret == -EFBIG)
589 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400590 csum = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400591 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
592 start);
Chris Mason07157aa2007-08-30 08:50:51 -0400593 goto out;
594 }
Chris Masonff79f812007-10-15 16:22:25 -0400595 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
596 BTRFS_CRC32_SIZE);
Chris Masonba1da2f2008-07-17 12:54:15 -0400597found:
Chris Masond1310b22008-01-24 16:13:08 -0500598 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400599out:
600 if (path)
601 btrfs_free_path(path);
Chris Mason07157aa2007-08-30 08:50:51 -0400602 return ret;
603}
604
Chris Mason7e383262008-04-09 16:28:12 -0400605struct io_failure_record {
606 struct page *page;
607 u64 start;
608 u64 len;
609 u64 logical;
610 int last_mirror;
611};
612
Chris Mason1259ab72008-05-12 13:39:03 -0400613int btrfs_io_failed_hook(struct bio *failed_bio,
614 struct page *page, u64 start, u64 end,
615 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400616{
617 struct io_failure_record *failrec = NULL;
618 u64 private;
619 struct extent_map *em;
620 struct inode *inode = page->mapping->host;
621 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400622 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400623 struct bio *bio;
624 int num_copies;
625 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400626 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400627 u64 logical;
628
629 ret = get_state_private(failure_tree, start, &private);
630 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400631 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
632 if (!failrec)
633 return -ENOMEM;
634 failrec->start = start;
635 failrec->len = end - start + 1;
636 failrec->last_mirror = 0;
637
Chris Mason3b951512008-04-17 11:29:12 -0400638 spin_lock(&em_tree->lock);
639 em = lookup_extent_mapping(em_tree, start, failrec->len);
640 if (em->start > start || em->start + em->len < start) {
641 free_extent_map(em);
642 em = NULL;
643 }
644 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400645
646 if (!em || IS_ERR(em)) {
647 kfree(failrec);
648 return -EIO;
649 }
650 logical = start - em->start;
651 logical = em->block_start + logical;
652 failrec->logical = logical;
653 free_extent_map(em);
654 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
655 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400656 set_state_private(failure_tree, start,
657 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400658 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400659 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400660 }
661 num_copies = btrfs_num_copies(
662 &BTRFS_I(inode)->root->fs_info->mapping_tree,
663 failrec->logical, failrec->len);
664 failrec->last_mirror++;
665 if (!state) {
666 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
667 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
668 failrec->start,
669 EXTENT_LOCKED);
670 if (state && state->start != failrec->start)
671 state = NULL;
672 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
673 }
674 if (!state || failrec->last_mirror > num_copies) {
675 set_state_private(failure_tree, failrec->start, 0);
676 clear_extent_bits(failure_tree, failrec->start,
677 failrec->start + failrec->len - 1,
678 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
679 kfree(failrec);
680 return -EIO;
681 }
682 bio = bio_alloc(GFP_NOFS, 1);
683 bio->bi_private = state;
684 bio->bi_end_io = failed_bio->bi_end_io;
685 bio->bi_sector = failrec->logical >> 9;
686 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400687 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400688 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400689 if (failed_bio->bi_rw & (1 << BIO_RW))
690 rw = WRITE;
691 else
692 rw = READ;
693
694 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
695 failrec->last_mirror);
696 return 0;
697}
698
699int btrfs_clean_io_failures(struct inode *inode, u64 start)
700{
701 u64 private;
702 u64 private_failure;
703 struct io_failure_record *failure;
704 int ret;
705
706 private = 0;
707 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
708 (u64)-1, 1, EXTENT_DIRTY)) {
709 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
710 start, &private_failure);
711 if (ret == 0) {
712 failure = (struct io_failure_record *)(unsigned long)
713 private_failure;
714 set_state_private(&BTRFS_I(inode)->io_failure_tree,
715 failure->start, 0);
716 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
717 failure->start,
718 failure->start + failure->len - 1,
719 EXTENT_DIRTY | EXTENT_LOCKED,
720 GFP_NOFS);
721 kfree(failure);
722 }
723 }
Chris Mason7e383262008-04-09 16:28:12 -0400724 return 0;
725}
726
Chris Mason70dec802008-01-29 09:59:12 -0500727int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
728 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400729{
Chris Mason35ebb932007-10-30 16:56:53 -0400730 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400731 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500732 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400733 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500734 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400735 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400736 struct btrfs_root *root = BTRFS_I(inode)->root;
737 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400738 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500739
Yanb98b6762008-01-08 15:54:37 -0500740 if (btrfs_test_opt(root, NODATASUM) ||
741 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500742 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500743 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500744 private = state->private;
745 ret = 0;
746 } else {
747 ret = get_state_private(io_tree, start, &private);
748 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400749 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400750 kaddr = kmap_atomic(page, KM_IRQ0);
751 if (ret) {
752 goto zeroit;
753 }
Chris Masonff79f812007-10-15 16:22:25 -0400754 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
755 btrfs_csum_final(csum, (char *)&csum);
756 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400757 goto zeroit;
758 }
759 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400760 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400761
762 /* if the io failure tree for this inode is non-empty,
763 * check to see if we've recovered from a failed IO
764 */
Chris Mason1259ab72008-05-12 13:39:03 -0400765 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400766 return 0;
767
768zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500769 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
770 page->mapping->host->i_ino, (unsigned long long)start, csum,
771 private);
Chris Masondb945352007-10-15 16:15:53 -0400772 memset(kaddr + offset, 1, end - start + 1);
773 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400774 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400775 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400776 if (private == 0)
777 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400778 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400779}
Chris Masonb888db22007-08-27 16:49:44 -0400780
Chris Mason39279cc2007-06-12 06:35:45 -0400781void btrfs_read_locked_inode(struct inode *inode)
782{
783 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400784 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400785 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400786 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400787 struct btrfs_root *root = BTRFS_I(inode)->root;
788 struct btrfs_key location;
789 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400790 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400791 int ret;
792
793 path = btrfs_alloc_path();
794 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400795 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500796
Chris Mason39279cc2007-06-12 06:35:45 -0400797 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400798 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400799 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400800
Chris Mason5f39d392007-10-15 16:14:19 -0400801 leaf = path->nodes[0];
802 inode_item = btrfs_item_ptr(leaf, path->slots[0],
803 struct btrfs_inode_item);
804
805 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
806 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
807 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
808 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -0400809 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400810
811 tspec = btrfs_inode_atime(inode_item);
812 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
813 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
814
815 tspec = btrfs_inode_mtime(inode_item);
816 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
817 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
818
819 tspec = btrfs_inode_ctime(inode_item);
820 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
821 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
822
823 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
824 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400825 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400826 rdev = btrfs_inode_rdev(leaf, inode_item);
827
828 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400829 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
830 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500831 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500832 if (!BTRFS_I(inode)->block_group) {
833 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400834 NULL, 0,
835 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500836 }
Chris Mason39279cc2007-06-12 06:35:45 -0400837 btrfs_free_path(path);
838 inode_item = NULL;
839
Chris Mason39279cc2007-06-12 06:35:45 -0400840 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400841 case S_IFREG:
842 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400843 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500844 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400845 inode->i_fop = &btrfs_file_operations;
846 inode->i_op = &btrfs_file_inode_operations;
847 break;
848 case S_IFDIR:
849 inode->i_fop = &btrfs_dir_file_operations;
850 if (root == root->fs_info->tree_root)
851 inode->i_op = &btrfs_dir_ro_inode_operations;
852 else
853 inode->i_op = &btrfs_dir_inode_operations;
854 break;
855 case S_IFLNK:
856 inode->i_op = &btrfs_symlink_inode_operations;
857 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400858 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400859 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400860 default:
861 init_special_inode(inode, inode->i_mode, rdev);
862 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400863 }
864 return;
865
866make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -0400867 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -0400868 make_bad_inode(inode);
869}
870
Chris Mason5f39d392007-10-15 16:14:19 -0400871static void fill_inode_item(struct extent_buffer *leaf,
872 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400873 struct inode *inode)
874{
Chris Mason5f39d392007-10-15 16:14:19 -0400875 btrfs_set_inode_uid(leaf, item, inode->i_uid);
876 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -0400877 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -0400878 btrfs_set_inode_mode(leaf, item, inode->i_mode);
879 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
880
881 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
882 inode->i_atime.tv_sec);
883 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
884 inode->i_atime.tv_nsec);
885
886 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
887 inode->i_mtime.tv_sec);
888 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
889 inode->i_mtime.tv_nsec);
890
891 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
892 inode->i_ctime.tv_sec);
893 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
894 inode->i_ctime.tv_nsec);
895
896 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
897 btrfs_set_inode_generation(leaf, item, inode->i_generation);
898 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500899 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400900 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400901 BTRFS_I(inode)->block_group->key.objectid);
902}
903
Chris Masonba1da2f2008-07-17 12:54:15 -0400904int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400905 struct btrfs_root *root,
906 struct inode *inode)
907{
908 struct btrfs_inode_item *inode_item;
909 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400910 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400911 int ret;
912
913 path = btrfs_alloc_path();
914 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400915 ret = btrfs_lookup_inode(trans, root, path,
916 &BTRFS_I(inode)->location, 1);
917 if (ret) {
918 if (ret > 0)
919 ret = -ENOENT;
920 goto failed;
921 }
922
Chris Mason5f39d392007-10-15 16:14:19 -0400923 leaf = path->nodes[0];
924 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400925 struct btrfs_inode_item);
926
Chris Mason5f39d392007-10-15 16:14:19 -0400927 fill_inode_item(leaf, inode_item, inode);
928 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400929 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400930 ret = 0;
931failed:
Chris Mason39279cc2007-06-12 06:35:45 -0400932 btrfs_free_path(path);
933 return ret;
934}
935
936
937static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
938 struct btrfs_root *root,
939 struct inode *dir,
940 struct dentry *dentry)
941{
942 struct btrfs_path *path;
943 const char *name = dentry->d_name.name;
944 int name_len = dentry->d_name.len;
945 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400946 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400947 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400948 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400949
950 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400951 if (!path) {
952 ret = -ENOMEM;
953 goto err;
954 }
955
Chris Mason39279cc2007-06-12 06:35:45 -0400956 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
957 name, name_len, -1);
958 if (IS_ERR(di)) {
959 ret = PTR_ERR(di);
960 goto err;
961 }
962 if (!di) {
963 ret = -ENOENT;
964 goto err;
965 }
Chris Mason5f39d392007-10-15 16:14:19 -0400966 leaf = path->nodes[0];
967 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400968 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400969 if (ret)
970 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400971 btrfs_release_path(root, path);
972
973 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400974 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400975 if (IS_ERR(di)) {
976 ret = PTR_ERR(di);
977 goto err;
978 }
979 if (!di) {
980 ret = -ENOENT;
981 goto err;
982 }
983 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -0400984 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -0400985
986 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500987 ret = btrfs_del_inode_ref(trans, root, name, name_len,
988 dentry->d_inode->i_ino,
989 dentry->d_parent->d_inode->i_ino);
990 if (ret) {
991 printk("failed to delete reference to %.*s, "
992 "inode %lu parent %lu\n", name_len, name,
993 dentry->d_inode->i_ino,
994 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500995 }
Chris Mason39279cc2007-06-12 06:35:45 -0400996err:
997 btrfs_free_path(path);
998 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -0400999 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001000 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001001 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001002#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1003 dentry->d_inode->i_nlink--;
1004#else
Chris Mason39279cc2007-06-12 06:35:45 -04001005 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001006#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001007 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001008 dir->i_sb->s_dirt = 1;
1009 }
1010 return ret;
1011}
1012
1013static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1014{
1015 struct btrfs_root *root;
1016 struct btrfs_trans_handle *trans;
1017 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001018 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001019
1020 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001021
1022 ret = btrfs_check_free_space(root, 1, 1);
1023 if (ret)
1024 goto fail;
1025
Chris Mason39279cc2007-06-12 06:35:45 -04001026 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001027
Chris Mason39279cc2007-06-12 06:35:45 -04001028 btrfs_set_trans_block_group(trans, dir);
1029 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001030 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001031
Chris Mason89ce8a62008-06-25 16:01:31 -04001032 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001033fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001034 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001035 return ret;
1036}
1037
1038static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1039{
1040 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001041 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001042 int ret;
1043 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001044 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001045 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001046
Chris Mason925baed2008-06-25 16:01:30 -04001047 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001048 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001049 }
Yan134d4512007-10-25 15:49:25 -04001050
Chris Mason1832a6d2007-12-21 16:27:21 -05001051 ret = btrfs_check_free_space(root, 1, 1);
1052 if (ret)
1053 goto fail;
1054
Chris Mason39279cc2007-06-12 06:35:45 -04001055 trans = btrfs_start_transaction(root, 1);
1056 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001057
1058 /* now the directory is empty */
1059 err = btrfs_unlink_trans(trans, root, dir, dentry);
1060 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001061 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001062 }
Chris Mason39544012007-12-12 14:38:19 -05001063
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001064 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001065 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001066fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001067 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001068
Chris Mason39279cc2007-06-12 06:35:45 -04001069 if (ret && !err)
1070 err = ret;
1071 return err;
1072}
1073
Chris Mason39279cc2007-06-12 06:35:45 -04001074/*
Chris Mason39279cc2007-06-12 06:35:45 -04001075 * this can truncate away extent items, csum items and directory items.
1076 * It starts at a high offset and removes keys until it can't find
1077 * any higher than i_size.
1078 *
1079 * csum items that cross the new i_size are truncated to the new size
1080 * as well.
1081 */
1082static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1083 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001084 struct inode *inode,
1085 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001086{
1087 int ret;
1088 struct btrfs_path *path;
1089 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001090 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001091 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001092 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001093 struct btrfs_file_extent_item *fi;
1094 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001095 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001096 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001097 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001098 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001099 int found_extent;
1100 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001101 int pending_del_nr = 0;
1102 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001103 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001104 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001105
Chris Mason3b951512008-04-17 11:29:12 -04001106 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001107 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001108 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001109 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001110
Chris Mason39279cc2007-06-12 06:35:45 -04001111 /* FIXME, add redo link to tree so we don't leak on crash */
1112 key.objectid = inode->i_ino;
1113 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001114 key.type = (u8)-1;
1115
Chris Mason85e21ba2008-01-29 15:11:36 -05001116 btrfs_init_path(path);
1117search_again:
1118 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1119 if (ret < 0) {
1120 goto error;
1121 }
1122 if (ret > 0) {
1123 BUG_ON(path->slots[0] == 0);
1124 path->slots[0]--;
1125 }
1126
Chris Mason39279cc2007-06-12 06:35:45 -04001127 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001128 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001129 leaf = path->nodes[0];
1130 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1131 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001132
Chris Mason5f39d392007-10-15 16:14:19 -04001133 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001134 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001135
Chris Mason85e21ba2008-01-29 15:11:36 -05001136 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001137 break;
1138
Chris Mason5f39d392007-10-15 16:14:19 -04001139 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001140 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001141 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001142 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001143 extent_type = btrfs_file_extent_type(leaf, fi);
1144 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001145 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001146 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001147 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1148 struct btrfs_item *item = btrfs_item_nr(leaf,
1149 path->slots[0]);
1150 item_end += btrfs_file_extent_inline_len(leaf,
1151 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001152 }
Yan008630c2007-11-07 13:31:09 -05001153 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001154 }
1155 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1156 ret = btrfs_csum_truncate(trans, root, path,
1157 inode->i_size);
1158 BUG_ON(ret);
1159 }
Yan008630c2007-11-07 13:31:09 -05001160 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001161 if (found_type == BTRFS_DIR_ITEM_KEY) {
1162 found_type = BTRFS_INODE_ITEM_KEY;
1163 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1164 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001165 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1166 found_type = BTRFS_XATTR_ITEM_KEY;
1167 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1168 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001169 } else if (found_type) {
1170 found_type--;
1171 } else {
1172 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001173 }
Yana61721d2007-09-17 11:08:38 -04001174 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001175 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001176 }
Chris Mason5f39d392007-10-15 16:14:19 -04001177 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001178 del_item = 1;
1179 else
1180 del_item = 0;
1181 found_extent = 0;
1182
1183 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001184 if (found_type != BTRFS_EXTENT_DATA_KEY)
1185 goto delete;
1186
1187 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001188 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001189 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001190 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001191 u64 orig_num_bytes =
1192 btrfs_file_extent_num_bytes(leaf, fi);
1193 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001194 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001195 extent_num_bytes = extent_num_bytes &
1196 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001197 btrfs_set_file_extent_num_bytes(leaf, fi,
1198 extent_num_bytes);
1199 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001200 extent_num_bytes);
1201 if (extent_start != 0)
1202 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001203 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001204 } else {
Chris Masondb945352007-10-15 16:15:53 -04001205 extent_num_bytes =
1206 btrfs_file_extent_disk_num_bytes(leaf,
1207 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001208 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001209 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001210 if (extent_start != 0) {
1211 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001212 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001213 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001214 root_gen = btrfs_header_generation(leaf);
1215 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001216 }
Chris Mason90692182008-02-08 13:49:28 -05001217 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1218 if (!del_item) {
1219 u32 newsize = inode->i_size - found_key.offset;
1220 dec_i_blocks(inode, item_end + 1 -
1221 found_key.offset - newsize);
1222 newsize =
1223 btrfs_file_extent_calc_inline_size(newsize);
1224 ret = btrfs_truncate_item(trans, root, path,
1225 newsize, 1);
1226 BUG_ON(ret);
1227 } else {
1228 dec_i_blocks(inode, item_end + 1 -
1229 found_key.offset);
1230 }
Chris Mason39279cc2007-06-12 06:35:45 -04001231 }
Chris Mason179e29e2007-11-01 11:28:41 -04001232delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001233 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001234 if (!pending_del_nr) {
1235 /* no pending yet, add ourselves */
1236 pending_del_slot = path->slots[0];
1237 pending_del_nr = 1;
1238 } else if (pending_del_nr &&
1239 path->slots[0] + 1 == pending_del_slot) {
1240 /* hop on the pending chunk */
1241 pending_del_nr++;
1242 pending_del_slot = path->slots[0];
1243 } else {
1244 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1245 }
Chris Mason39279cc2007-06-12 06:35:45 -04001246 } else {
1247 break;
1248 }
Chris Mason39279cc2007-06-12 06:35:45 -04001249 if (found_extent) {
1250 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001251 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001252 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001253 root_gen, inode->i_ino,
1254 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001255 BUG_ON(ret);
1256 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001257next:
1258 if (path->slots[0] == 0) {
1259 if (pending_del_nr)
1260 goto del_pending;
1261 btrfs_release_path(root, path);
1262 goto search_again;
1263 }
1264
1265 path->slots[0]--;
1266 if (pending_del_nr &&
1267 path->slots[0] + 1 != pending_del_slot) {
1268 struct btrfs_key debug;
1269del_pending:
1270 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1271 pending_del_slot);
1272 ret = btrfs_del_items(trans, root, path,
1273 pending_del_slot,
1274 pending_del_nr);
1275 BUG_ON(ret);
1276 pending_del_nr = 0;
1277 btrfs_release_path(root, path);
1278 goto search_again;
1279 }
Chris Mason39279cc2007-06-12 06:35:45 -04001280 }
1281 ret = 0;
1282error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001283 if (pending_del_nr) {
1284 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1285 pending_del_nr);
1286 }
Chris Mason39279cc2007-06-12 06:35:45 -04001287 btrfs_free_path(path);
1288 inode->i_sb->s_dirt = 1;
1289 return ret;
1290}
1291
Chris Masona52d9a82007-08-27 16:49:44 -04001292/*
1293 * taken from block_truncate_page, but does cow as it zeros out
1294 * any bytes left in the last page in the file.
1295 */
1296static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1297{
1298 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001299 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001300 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1301 struct btrfs_ordered_extent *ordered;
1302 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001303 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001304 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1305 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1306 struct page *page;
1307 int ret = 0;
1308 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001309 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001310
1311 if ((offset & (blocksize - 1)) == 0)
1312 goto out;
1313
1314 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001315again:
Chris Masona52d9a82007-08-27 16:49:44 -04001316 page = grab_cache_page(mapping, index);
1317 if (!page)
1318 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001319
1320 page_start = page_offset(page);
1321 page_end = page_start + PAGE_CACHE_SIZE - 1;
1322
Chris Masona52d9a82007-08-27 16:49:44 -04001323 if (!PageUptodate(page)) {
1324 ret = btrfs_readpage(NULL, page);
1325 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001326 if (page->mapping != mapping) {
1327 unlock_page(page);
1328 page_cache_release(page);
1329 goto again;
1330 }
Chris Masona52d9a82007-08-27 16:49:44 -04001331 if (!PageUptodate(page)) {
1332 ret = -EIO;
1333 goto out;
1334 }
1335 }
Chris Mason211c17f2008-05-15 09:13:45 -04001336 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001337
1338 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1339 set_page_extent_mapped(page);
1340
1341 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1342 if (ordered) {
1343 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1344 unlock_page(page);
1345 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001346 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001347 btrfs_put_ordered_extent(ordered);
1348 goto again;
1349 }
1350
1351 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1352 page_end, GFP_NOFS);
1353 ret = 0;
1354 if (offset != PAGE_CACHE_SIZE) {
1355 kaddr = kmap(page);
1356 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1357 flush_dcache_page(page);
1358 kunmap(page);
1359 }
Chris Mason247e7432008-07-17 12:53:51 -04001360 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001361 set_page_dirty(page);
1362 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001363
Chris Mason39279cc2007-06-12 06:35:45 -04001364 unlock_page(page);
1365 page_cache_release(page);
1366out:
1367 return ret;
1368}
1369
1370static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1371{
1372 struct inode *inode = dentry->d_inode;
1373 int err;
1374
1375 err = inode_change_ok(inode, attr);
1376 if (err)
1377 return err;
1378
1379 if (S_ISREG(inode->i_mode) &&
1380 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1381 struct btrfs_trans_handle *trans;
1382 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001383 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001384
Chris Mason5f39d392007-10-15 16:14:19 -04001385 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001386 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001387 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001388 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001389 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001390
Chris Mason1b0f7c22008-01-30 14:33:02 -05001391 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001392 goto out;
1393
Chris Mason1832a6d2007-12-21 16:27:21 -05001394 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001395 if (err)
1396 goto fail;
1397
Chris Mason39279cc2007-06-12 06:35:45 -04001398 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1399
Chris Mason5f564062008-01-22 16:47:59 -05001400 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001401 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1402 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001403
Chris Mason39279cc2007-06-12 06:35:45 -04001404 trans = btrfs_start_transaction(root, 1);
1405 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001406 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001407 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001408 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001409 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001410
Chris Mason179e29e2007-11-01 11:28:41 -04001411 if (alloc_hint != EXTENT_MAP_INLINE) {
1412 err = btrfs_insert_file_extent(trans, root,
1413 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001414 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001415 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001416 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001417 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001418 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001419 }
Chris Masonee6e6502008-07-17 12:54:40 -04001420 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001421 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001422 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001423 if (err)
1424 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001425 }
1426out:
1427 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001428fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001429 return err;
1430}
Chris Mason61295eb2008-01-14 16:24:38 -05001431
Chris Mason39279cc2007-06-12 06:35:45 -04001432void btrfs_delete_inode(struct inode *inode)
1433{
1434 struct btrfs_trans_handle *trans;
1435 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001436 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001437 int ret;
1438
Chris Masone6dcd2d2008-07-17 12:53:50 -04001439 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001440 truncate_inode_pages(&inode->i_data, 0);
1441 if (is_bad_inode(inode)) {
1442 goto no_delete;
1443 }
Chris Mason5f39d392007-10-15 16:14:19 -04001444
Chris Masondbe674a2008-07-17 12:54:05 -04001445 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001446 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001447
Chris Mason39279cc2007-06-12 06:35:45 -04001448 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001449 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001450 if (ret)
1451 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001452
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001453 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001454 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001455
Chris Mason39279cc2007-06-12 06:35:45 -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 -04001458 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001459
1460no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001461 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001462 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001463 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001464no_delete:
1465 clear_inode(inode);
1466}
1467
1468/*
1469 * this returns the key found in the dir entry in the location pointer.
1470 * If no dir entries were found, location->objectid is 0.
1471 */
1472static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1473 struct btrfs_key *location)
1474{
1475 const char *name = dentry->d_name.name;
1476 int namelen = dentry->d_name.len;
1477 struct btrfs_dir_item *di;
1478 struct btrfs_path *path;
1479 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001480 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001481
Chris Mason39544012007-12-12 14:38:19 -05001482 if (namelen == 1 && strcmp(name, ".") == 0) {
1483 location->objectid = dir->i_ino;
1484 location->type = BTRFS_INODE_ITEM_KEY;
1485 location->offset = 0;
1486 return 0;
1487 }
Chris Mason39279cc2007-06-12 06:35:45 -04001488 path = btrfs_alloc_path();
1489 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001490
Chris Mason7a720532007-12-13 09:06:59 -05001491 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001492 struct btrfs_key key;
1493 struct extent_buffer *leaf;
1494 u32 nritems;
1495 int slot;
1496
1497 key.objectid = dir->i_ino;
1498 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1499 key.offset = 0;
1500 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1501 BUG_ON(ret == 0);
1502 ret = 0;
1503
1504 leaf = path->nodes[0];
1505 slot = path->slots[0];
1506 nritems = btrfs_header_nritems(leaf);
1507 if (slot >= nritems)
1508 goto out_err;
1509
1510 btrfs_item_key_to_cpu(leaf, &key, slot);
1511 if (key.objectid != dir->i_ino ||
1512 key.type != BTRFS_INODE_REF_KEY) {
1513 goto out_err;
1514 }
1515 location->objectid = key.offset;
1516 location->type = BTRFS_INODE_ITEM_KEY;
1517 location->offset = 0;
1518 goto out;
1519 }
1520
Chris Mason39279cc2007-06-12 06:35:45 -04001521 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1522 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001523 if (IS_ERR(di))
1524 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001525 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001526 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001527 }
Chris Mason5f39d392007-10-15 16:14:19 -04001528 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001529out:
Chris Mason39279cc2007-06-12 06:35:45 -04001530 btrfs_free_path(path);
1531 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001532out_err:
1533 location->objectid = 0;
1534 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001535}
1536
1537/*
1538 * when we hit a tree root in a directory, the btrfs part of the inode
1539 * needs to be changed to reflect the root directory of the tree root. This
1540 * is kind of like crossing a mount point.
1541 */
1542static int fixup_tree_root_location(struct btrfs_root *root,
1543 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001544 struct btrfs_root **sub_root,
1545 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001546{
1547 struct btrfs_path *path;
1548 struct btrfs_root_item *ri;
1549
1550 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1551 return 0;
1552 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1553 return 0;
1554
1555 path = btrfs_alloc_path();
1556 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001557
Josef Bacik58176a92007-08-29 15:47:34 -04001558 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1559 dentry->d_name.name,
1560 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001561 if (IS_ERR(*sub_root))
1562 return PTR_ERR(*sub_root);
1563
1564 ri = &(*sub_root)->root_item;
1565 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001566 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1567 location->offset = 0;
1568
1569 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001570 return 0;
1571}
1572
1573static int btrfs_init_locked_inode(struct inode *inode, void *p)
1574{
1575 struct btrfs_iget_args *args = p;
1576 inode->i_ino = args->ino;
1577 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001578 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001579 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001580 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1581 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001582 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001583 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1584 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001585 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001586 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001587 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001588 return 0;
1589}
1590
1591static int btrfs_find_actor(struct inode *inode, void *opaque)
1592{
1593 struct btrfs_iget_args *args = opaque;
1594 return (args->ino == inode->i_ino &&
1595 args->root == BTRFS_I(inode)->root);
1596}
1597
Chris Masondc17ff82008-01-08 15:46:30 -05001598struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1599 u64 root_objectid)
1600{
1601 struct btrfs_iget_args args;
1602 args.ino = objectid;
1603 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1604
1605 if (!args.root)
1606 return NULL;
1607
1608 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1609}
1610
Chris Mason39279cc2007-06-12 06:35:45 -04001611struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1612 struct btrfs_root *root)
1613{
1614 struct inode *inode;
1615 struct btrfs_iget_args args;
1616 args.ino = objectid;
1617 args.root = root;
1618
1619 inode = iget5_locked(s, objectid, btrfs_find_actor,
1620 btrfs_init_locked_inode,
1621 (void *)&args);
1622 return inode;
1623}
1624
1625static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1626 struct nameidata *nd)
1627{
1628 struct inode * inode;
1629 struct btrfs_inode *bi = BTRFS_I(dir);
1630 struct btrfs_root *root = bi->root;
1631 struct btrfs_root *sub_root = root;
1632 struct btrfs_key location;
1633 int ret;
1634
1635 if (dentry->d_name.len > BTRFS_NAME_LEN)
1636 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001637
Chris Mason39279cc2007-06-12 06:35:45 -04001638 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001639
Chris Mason39279cc2007-06-12 06:35:45 -04001640 if (ret < 0)
1641 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001642
Chris Mason39279cc2007-06-12 06:35:45 -04001643 inode = NULL;
1644 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001645 ret = fixup_tree_root_location(root, &location, &sub_root,
1646 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001647 if (ret < 0)
1648 return ERR_PTR(ret);
1649 if (ret > 0)
1650 return ERR_PTR(-ENOENT);
1651 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1652 sub_root);
1653 if (!inode)
1654 return ERR_PTR(-EACCES);
1655 if (inode->i_state & I_NEW) {
1656 /* the inode and parent dir are two different roots */
1657 if (sub_root != root) {
1658 igrab(inode);
1659 sub_root->inode = inode;
1660 }
1661 BTRFS_I(inode)->root = sub_root;
1662 memcpy(&BTRFS_I(inode)->location, &location,
1663 sizeof(location));
1664 btrfs_read_locked_inode(inode);
1665 unlock_new_inode(inode);
1666 }
1667 }
1668 return d_splice_alias(inode, dentry);
1669}
1670
Chris Mason39279cc2007-06-12 06:35:45 -04001671static unsigned char btrfs_filetype_table[] = {
1672 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1673};
1674
1675static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1676{
Chris Mason6da6aba2007-12-18 16:15:09 -05001677 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001678 struct btrfs_root *root = BTRFS_I(inode)->root;
1679 struct btrfs_item *item;
1680 struct btrfs_dir_item *di;
1681 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001682 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001683 struct btrfs_path *path;
1684 int ret;
1685 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001686 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001687 int slot;
1688 int advance;
1689 unsigned char d_type;
1690 int over = 0;
1691 u32 di_cur;
1692 u32 di_total;
1693 u32 di_len;
1694 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001695 char tmp_name[32];
1696 char *name_ptr;
1697 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001698
1699 /* FIXME, use a real flag for deciding about the key type */
1700 if (root->fs_info->tree_root == root)
1701 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001702
Chris Mason39544012007-12-12 14:38:19 -05001703 /* special case for "." */
1704 if (filp->f_pos == 0) {
1705 over = filldir(dirent, ".", 1,
1706 1, inode->i_ino,
1707 DT_DIR);
1708 if (over)
1709 return 0;
1710 filp->f_pos = 1;
1711 }
1712
Chris Mason39279cc2007-06-12 06:35:45 -04001713 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001714 path = btrfs_alloc_path();
1715 path->reada = 2;
1716
1717 /* special case for .., just use the back ref */
1718 if (filp->f_pos == 1) {
1719 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1720 key.offset = 0;
1721 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1722 BUG_ON(ret == 0);
1723 leaf = path->nodes[0];
1724 slot = path->slots[0];
1725 nritems = btrfs_header_nritems(leaf);
1726 if (slot >= nritems) {
1727 btrfs_release_path(root, path);
1728 goto read_dir_items;
1729 }
1730 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1731 btrfs_release_path(root, path);
1732 if (found_key.objectid != key.objectid ||
1733 found_key.type != BTRFS_INODE_REF_KEY)
1734 goto read_dir_items;
1735 over = filldir(dirent, "..", 2,
1736 2, found_key.offset, DT_DIR);
1737 if (over)
1738 goto nopos;
1739 filp->f_pos = 2;
1740 }
1741
1742read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001743 btrfs_set_key_type(&key, key_type);
1744 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001745
Chris Mason39279cc2007-06-12 06:35:45 -04001746 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1747 if (ret < 0)
1748 goto err;
1749 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001750 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001751 leaf = path->nodes[0];
1752 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001753 slot = path->slots[0];
1754 if (advance || slot >= nritems) {
1755 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001756 ret = btrfs_next_leaf(root, path);
1757 if (ret)
1758 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001759 leaf = path->nodes[0];
1760 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001761 slot = path->slots[0];
1762 } else {
1763 slot++;
1764 path->slots[0]++;
1765 }
1766 }
1767 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001768 item = btrfs_item_nr(leaf, slot);
1769 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1770
1771 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001772 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001773 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001774 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001775 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001776 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001777
1778 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001779 advance = 1;
1780 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1781 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001782 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001783 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001784 struct btrfs_key location;
1785
1786 name_len = btrfs_dir_name_len(leaf, di);
1787 if (name_len < 32) {
1788 name_ptr = tmp_name;
1789 } else {
1790 name_ptr = kmalloc(name_len, GFP_NOFS);
1791 BUG_ON(!name_ptr);
1792 }
1793 read_extent_buffer(leaf, name_ptr,
1794 (unsigned long)(di + 1), name_len);
1795
1796 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1797 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001798 over = filldir(dirent, name_ptr, name_len,
1799 found_key.offset,
1800 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001801 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001802
1803 if (name_ptr != tmp_name)
1804 kfree(name_ptr);
1805
Chris Mason39279cc2007-06-12 06:35:45 -04001806 if (over)
1807 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001808 di_len = btrfs_dir_name_len(leaf, di) +
1809 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001810 di_cur += di_len;
1811 di = (struct btrfs_dir_item *)((char *)di + di_len);
1812 }
1813 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001814 if (key_type == BTRFS_DIR_INDEX_KEY)
1815 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1816 else
1817 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001818nopos:
1819 ret = 0;
1820err:
Chris Mason39279cc2007-06-12 06:35:45 -04001821 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001822 return ret;
1823}
1824
1825int btrfs_write_inode(struct inode *inode, int wait)
1826{
1827 struct btrfs_root *root = BTRFS_I(inode)->root;
1828 struct btrfs_trans_handle *trans;
1829 int ret = 0;
1830
1831 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04001832 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001833 btrfs_set_trans_block_group(trans, inode);
1834 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001835 }
1836 return ret;
1837}
1838
1839/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001840 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001841 * inode changes. But, it is most likely to find the inode in cache.
1842 * FIXME, needs more benchmarking...there are no reasons other than performance
1843 * to keep or drop this code.
1844 */
1845void btrfs_dirty_inode(struct inode *inode)
1846{
1847 struct btrfs_root *root = BTRFS_I(inode)->root;
1848 struct btrfs_trans_handle *trans;
1849
Chris Masonf9295742008-07-17 12:54:14 -04001850 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001851 btrfs_set_trans_block_group(trans, inode);
1852 btrfs_update_inode(trans, root, inode);
1853 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001854}
1855
1856static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1857 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001858 const char *name, int name_len,
1859 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001860 u64 objectid,
1861 struct btrfs_block_group_cache *group,
1862 int mode)
1863{
1864 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001865 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001866 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001867 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001868 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001869 struct btrfs_inode_ref *ref;
1870 struct btrfs_key key[2];
1871 u32 sizes[2];
1872 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001873 int ret;
1874 int owner;
1875
Chris Mason5f39d392007-10-15 16:14:19 -04001876 path = btrfs_alloc_path();
1877 BUG_ON(!path);
1878
Chris Mason39279cc2007-06-12 06:35:45 -04001879 inode = new_inode(root->fs_info->sb);
1880 if (!inode)
1881 return ERR_PTR(-ENOMEM);
1882
Chris Masond1310b22008-01-24 16:13:08 -05001883 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1884 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001885 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001886 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1887 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001888 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001889 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001890 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05001891 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001892 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001893 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001894
Chris Mason39279cc2007-06-12 06:35:45 -04001895 if (mode & S_IFDIR)
1896 owner = 0;
1897 else
1898 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001899 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001900 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001901 if (!new_inode_group) {
1902 printk("find_block group failed\n");
1903 new_inode_group = group;
1904 }
1905 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001906 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001907
1908 key[0].objectid = objectid;
1909 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1910 key[0].offset = 0;
1911
1912 key[1].objectid = objectid;
1913 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1914 key[1].offset = ref_objectid;
1915
1916 sizes[0] = sizeof(struct btrfs_inode_item);
1917 sizes[1] = name_len + sizeof(*ref);
1918
1919 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1920 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001921 goto fail;
1922
Chris Mason9c583092008-01-29 15:15:18 -05001923 if (objectid > root->highest_inode)
1924 root->highest_inode = objectid;
1925
Chris Mason39279cc2007-06-12 06:35:45 -04001926 inode->i_uid = current->fsuid;
1927 inode->i_gid = current->fsgid;
1928 inode->i_mode = mode;
1929 inode->i_ino = objectid;
1930 inode->i_blocks = 0;
1931 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001932 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1933 struct btrfs_inode_item);
1934 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001935
1936 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1937 struct btrfs_inode_ref);
1938 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1939 ptr = (unsigned long)(ref + 1);
1940 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1941
Chris Mason5f39d392007-10-15 16:14:19 -04001942 btrfs_mark_buffer_dirty(path->nodes[0]);
1943 btrfs_free_path(path);
1944
Chris Mason39279cc2007-06-12 06:35:45 -04001945 location = &BTRFS_I(inode)->location;
1946 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001947 location->offset = 0;
1948 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1949
Chris Mason39279cc2007-06-12 06:35:45 -04001950 insert_inode_hash(inode);
1951 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001952fail:
1953 btrfs_free_path(path);
1954 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001955}
1956
1957static inline u8 btrfs_inode_type(struct inode *inode)
1958{
1959 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1960}
1961
1962static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001963 struct dentry *dentry, struct inode *inode,
1964 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001965{
1966 int ret;
1967 struct btrfs_key key;
1968 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001969 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001970
Chris Mason39279cc2007-06-12 06:35:45 -04001971 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001972 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1973 key.offset = 0;
1974
1975 ret = btrfs_insert_dir_item(trans, root,
1976 dentry->d_name.name, dentry->d_name.len,
1977 dentry->d_parent->d_inode->i_ino,
1978 &key, btrfs_inode_type(inode));
1979 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001980 if (add_backref) {
1981 ret = btrfs_insert_inode_ref(trans, root,
1982 dentry->d_name.name,
1983 dentry->d_name.len,
1984 inode->i_ino,
1985 dentry->d_parent->d_inode->i_ino);
1986 }
Chris Mason79c44582007-06-25 10:09:33 -04001987 parent_inode = dentry->d_parent->d_inode;
Chris Masondbe674a2008-07-17 12:54:05 -04001988 btrfs_i_size_write(parent_inode, parent_inode->i_size +
1989 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001990 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001991 ret = btrfs_update_inode(trans, root,
1992 dentry->d_parent->d_inode);
1993 }
1994 return ret;
1995}
1996
1997static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001998 struct dentry *dentry, struct inode *inode,
1999 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002000{
Chris Mason9c583092008-01-29 15:15:18 -05002001 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04002002 if (!err) {
2003 d_instantiate(dentry, inode);
2004 return 0;
2005 }
2006 if (err > 0)
2007 err = -EEXIST;
2008 return err;
2009}
2010
Josef Bacik618e21d2007-07-11 10:18:17 -04002011static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2012 int mode, dev_t rdev)
2013{
2014 struct btrfs_trans_handle *trans;
2015 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002016 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002017 int err;
2018 int drop_inode = 0;
2019 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002020 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002021
2022 if (!new_valid_dev(rdev))
2023 return -EINVAL;
2024
Chris Mason1832a6d2007-12-21 16:27:21 -05002025 err = btrfs_check_free_space(root, 1, 0);
2026 if (err)
2027 goto fail;
2028
Josef Bacik618e21d2007-07-11 10:18:17 -04002029 trans = btrfs_start_transaction(root, 1);
2030 btrfs_set_trans_block_group(trans, dir);
2031
2032 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2033 if (err) {
2034 err = -ENOSPC;
2035 goto out_unlock;
2036 }
2037
Chris Mason9c583092008-01-29 15:15:18 -05002038 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2039 dentry->d_name.len,
2040 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002041 BTRFS_I(dir)->block_group, mode);
2042 err = PTR_ERR(inode);
2043 if (IS_ERR(inode))
2044 goto out_unlock;
2045
2046 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002047 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002048 if (err)
2049 drop_inode = 1;
2050 else {
2051 inode->i_op = &btrfs_special_inode_operations;
2052 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002053 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002054 }
2055 dir->i_sb->s_dirt = 1;
2056 btrfs_update_inode_block_group(trans, inode);
2057 btrfs_update_inode_block_group(trans, dir);
2058out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002059 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002060 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002061fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002062 if (drop_inode) {
2063 inode_dec_link_count(inode);
2064 iput(inode);
2065 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002066 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002067 return err;
2068}
2069
Chris Mason39279cc2007-06-12 06:35:45 -04002070static int btrfs_create(struct inode *dir, struct dentry *dentry,
2071 int mode, struct nameidata *nd)
2072{
2073 struct btrfs_trans_handle *trans;
2074 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002075 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002076 int err;
2077 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002078 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002079 u64 objectid;
2080
Chris Mason1832a6d2007-12-21 16:27:21 -05002081 err = btrfs_check_free_space(root, 1, 0);
2082 if (err)
2083 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002084 trans = btrfs_start_transaction(root, 1);
2085 btrfs_set_trans_block_group(trans, dir);
2086
2087 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2088 if (err) {
2089 err = -ENOSPC;
2090 goto out_unlock;
2091 }
2092
Chris Mason9c583092008-01-29 15:15:18 -05002093 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2094 dentry->d_name.len,
2095 dentry->d_parent->d_inode->i_ino,
2096 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002097 err = PTR_ERR(inode);
2098 if (IS_ERR(inode))
2099 goto out_unlock;
2100
2101 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002102 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002103 if (err)
2104 drop_inode = 1;
2105 else {
2106 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002107 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002108 inode->i_fop = &btrfs_file_operations;
2109 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002110 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2111 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002112 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002113 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2114 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002115 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002116 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002117 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002118 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002119 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002120 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002121 }
2122 dir->i_sb->s_dirt = 1;
2123 btrfs_update_inode_block_group(trans, inode);
2124 btrfs_update_inode_block_group(trans, dir);
2125out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002126 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002127 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002128fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002129 if (drop_inode) {
2130 inode_dec_link_count(inode);
2131 iput(inode);
2132 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002133 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002134 return err;
2135}
2136
2137static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2138 struct dentry *dentry)
2139{
2140 struct btrfs_trans_handle *trans;
2141 struct btrfs_root *root = BTRFS_I(dir)->root;
2142 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002143 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002144 int err;
2145 int drop_inode = 0;
2146
2147 if (inode->i_nlink == 0)
2148 return -ENOENT;
2149
Chris Mason6da6aba2007-12-18 16:15:09 -05002150#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2151 inode->i_nlink++;
2152#else
Chris Mason39279cc2007-06-12 06:35:45 -04002153 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002154#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002155 err = btrfs_check_free_space(root, 1, 0);
2156 if (err)
2157 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002158 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002159
Chris Mason39279cc2007-06-12 06:35:45 -04002160 btrfs_set_trans_block_group(trans, dir);
2161 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05002162 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002163
Chris Mason39279cc2007-06-12 06:35:45 -04002164 if (err)
2165 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002166
Chris Mason39279cc2007-06-12 06:35:45 -04002167 dir->i_sb->s_dirt = 1;
2168 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002169 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002170
Chris Mason54aa1f42007-06-22 14:16:25 -04002171 if (err)
2172 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002173
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002174 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002175 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002176fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002177 if (drop_inode) {
2178 inode_dec_link_count(inode);
2179 iput(inode);
2180 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002181 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002182 return err;
2183}
2184
Chris Mason39279cc2007-06-12 06:35:45 -04002185static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2186{
Chris Masonb9d86662008-05-02 16:13:49 -04002187 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002188 struct btrfs_trans_handle *trans;
2189 struct btrfs_root *root = BTRFS_I(dir)->root;
2190 int err = 0;
2191 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002192 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002193 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002194
Chris Mason1832a6d2007-12-21 16:27:21 -05002195 err = btrfs_check_free_space(root, 1, 0);
2196 if (err)
2197 goto out_unlock;
2198
Chris Mason39279cc2007-06-12 06:35:45 -04002199 trans = btrfs_start_transaction(root, 1);
2200 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002201
Chris Mason39279cc2007-06-12 06:35:45 -04002202 if (IS_ERR(trans)) {
2203 err = PTR_ERR(trans);
2204 goto out_unlock;
2205 }
2206
2207 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2208 if (err) {
2209 err = -ENOSPC;
2210 goto out_unlock;
2211 }
2212
Chris Mason9c583092008-01-29 15:15:18 -05002213 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2214 dentry->d_name.len,
2215 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002216 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2217 if (IS_ERR(inode)) {
2218 err = PTR_ERR(inode);
2219 goto out_fail;
2220 }
Chris Mason5f39d392007-10-15 16:14:19 -04002221
Chris Mason39279cc2007-06-12 06:35:45 -04002222 drop_on_err = 1;
2223 inode->i_op = &btrfs_dir_inode_operations;
2224 inode->i_fop = &btrfs_dir_file_operations;
2225 btrfs_set_trans_block_group(trans, inode);
2226
Chris Masondbe674a2008-07-17 12:54:05 -04002227 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002228 err = btrfs_update_inode(trans, root, inode);
2229 if (err)
2230 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002231
Chris Mason9c583092008-01-29 15:15:18 -05002232 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002233 if (err)
2234 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002235
Chris Mason39279cc2007-06-12 06:35:45 -04002236 d_instantiate(dentry, inode);
2237 drop_on_err = 0;
2238 dir->i_sb->s_dirt = 1;
2239 btrfs_update_inode_block_group(trans, inode);
2240 btrfs_update_inode_block_group(trans, dir);
2241
2242out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002243 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002244 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002245
Chris Mason39279cc2007-06-12 06:35:45 -04002246out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002247 if (drop_on_err)
2248 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002249 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002250 return err;
2251}
2252
Chris Mason3b951512008-04-17 11:29:12 -04002253static int merge_extent_mapping(struct extent_map_tree *em_tree,
2254 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002255 struct extent_map *em,
2256 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002257{
2258 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002259
Chris Masone6dcd2d2008-07-17 12:53:50 -04002260 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2261 start_diff = map_start - em->start;
2262 em->start = map_start;
2263 em->len = map_len;
2264 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2265 em->block_start += start_diff;
2266 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002267}
2268
Chris Masona52d9a82007-08-27 16:49:44 -04002269struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002270 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002271 int create)
2272{
2273 int ret;
2274 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002275 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002276 u64 extent_start = 0;
2277 u64 extent_end = 0;
2278 u64 objectid = inode->i_ino;
2279 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002280 struct btrfs_path *path;
2281 struct btrfs_root *root = BTRFS_I(inode)->root;
2282 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002283 struct extent_buffer *leaf;
2284 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002285 struct extent_map *em = NULL;
2286 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002287 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002288 struct btrfs_trans_handle *trans = NULL;
2289
2290 path = btrfs_alloc_path();
2291 BUG_ON(!path);
Chris Masona52d9a82007-08-27 16:49:44 -04002292
2293again:
Chris Masond1310b22008-01-24 16:13:08 -05002294 spin_lock(&em_tree->lock);
2295 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002296 if (em)
2297 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002298 spin_unlock(&em_tree->lock);
2299
Chris Masona52d9a82007-08-27 16:49:44 -04002300 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002301 if (em->start > start || em->start + em->len <= start)
2302 free_extent_map(em);
2303 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002304 free_extent_map(em);
2305 else
2306 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002307 }
Chris Masond1310b22008-01-24 16:13:08 -05002308 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002309 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002310 err = -ENOMEM;
2311 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002312 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002313 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002314 em->start = EXTENT_MAP_HOLE;
2315 em->len = (u64)-1;
Chris Mason179e29e2007-11-01 11:28:41 -04002316 ret = btrfs_lookup_file_extent(trans, root, path,
2317 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002318 if (ret < 0) {
2319 err = ret;
2320 goto out;
2321 }
2322
2323 if (ret != 0) {
2324 if (path->slots[0] == 0)
2325 goto not_found;
2326 path->slots[0]--;
2327 }
2328
Chris Mason5f39d392007-10-15 16:14:19 -04002329 leaf = path->nodes[0];
2330 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002331 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002332 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002333 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2334 found_type = btrfs_key_type(&found_key);
2335 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002336 found_type != BTRFS_EXTENT_DATA_KEY) {
2337 goto not_found;
2338 }
2339
Chris Mason5f39d392007-10-15 16:14:19 -04002340 found_type = btrfs_file_extent_type(leaf, item);
2341 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002342 if (found_type == BTRFS_FILE_EXTENT_REG) {
2343 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002344 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002345 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002346 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002347 em->start = start;
2348 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002349 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002350 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002351 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002352 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002353 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002354 }
2355 goto not_found_em;
2356 }
Chris Masondb945352007-10-15 16:15:53 -04002357 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2358 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002359 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002360 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002361 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002362 goto insert;
2363 }
Chris Masondb945352007-10-15 16:15:53 -04002364 bytenr += btrfs_file_extent_offset(leaf, item);
2365 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002366 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002367 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002368 goto insert;
2369 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002370 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002371 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002372 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002373 size_t size;
2374 size_t extent_offset;
2375 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002376
Chris Mason5f39d392007-10-15 16:14:19 -04002377 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2378 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002379 extent_end = (extent_start + size + root->sectorsize - 1) &
2380 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002381 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002382 em->start = start;
2383 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002384 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002385 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002386 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002387 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002388 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002389 }
2390 goto not_found_em;
2391 }
2392 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002393
2394 if (!page) {
2395 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002396 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002397 goto out;
2398 }
2399
Chris Mason70dec802008-01-29 09:59:12 -05002400 page_start = page_offset(page) + pg_offset;
2401 extent_offset = page_start - extent_start;
2402 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002403 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002404 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002405 em->len = (copy_size + root->sectorsize - 1) &
2406 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002407 map = kmap(page);
2408 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002409 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002410 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002411 copy_size);
2412 flush_dcache_page(page);
2413 } else if (create && PageUptodate(page)) {
2414 if (!trans) {
2415 kunmap(page);
2416 free_extent_map(em);
2417 em = NULL;
2418 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002419 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002420 goto again;
2421 }
Chris Mason70dec802008-01-29 09:59:12 -05002422 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002423 copy_size);
2424 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002425 }
Chris Masona52d9a82007-08-27 16:49:44 -04002426 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002427 set_extent_uptodate(io_tree, em->start,
2428 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002429 goto insert;
2430 } else {
2431 printk("unkknown found_type %d\n", found_type);
2432 WARN_ON(1);
2433 }
2434not_found:
2435 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002436 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002437not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002438 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002439insert:
2440 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002441 if (em->start > start || extent_map_end(em) <= start) {
2442 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002443 err = -EIO;
2444 goto out;
2445 }
Chris Masond1310b22008-01-24 16:13:08 -05002446
2447 err = 0;
2448 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002449 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002450 /* it is possible that someone inserted the extent into the tree
2451 * while we had the lock dropped. It is also possible that
2452 * an overlapping map exists in the tree
2453 */
Chris Masona52d9a82007-08-27 16:49:44 -04002454 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002455 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002456
2457 ret = 0;
2458
Chris Mason3b951512008-04-17 11:29:12 -04002459 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002460 if (existing && (existing->start > start ||
2461 existing->start + existing->len <= start)) {
2462 free_extent_map(existing);
2463 existing = NULL;
2464 }
Chris Mason3b951512008-04-17 11:29:12 -04002465 if (!existing) {
2466 existing = lookup_extent_mapping(em_tree, em->start,
2467 em->len);
2468 if (existing) {
2469 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002470 em, start,
2471 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002472 free_extent_map(existing);
2473 if (err) {
2474 free_extent_map(em);
2475 em = NULL;
2476 }
2477 } else {
2478 err = -EIO;
2479 printk("failing to insert %Lu %Lu\n",
2480 start, len);
2481 free_extent_map(em);
2482 em = NULL;
2483 }
2484 } else {
2485 free_extent_map(em);
2486 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002487 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002488 }
Chris Masona52d9a82007-08-27 16:49:44 -04002489 }
Chris Masond1310b22008-01-24 16:13:08 -05002490 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002491out:
2492 btrfs_free_path(path);
2493 if (trans) {
2494 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002495 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002496 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002497 }
Chris Masona52d9a82007-08-27 16:49:44 -04002498 }
Chris Masona52d9a82007-08-27 16:49:44 -04002499 if (err) {
2500 free_extent_map(em);
2501 WARN_ON(1);
2502 return ERR_PTR(err);
2503 }
2504 return em;
2505}
2506
Chris Masone1c4b742008-04-22 13:26:46 -04002507#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002508static int btrfs_get_block(struct inode *inode, sector_t iblock,
2509 struct buffer_head *bh_result, int create)
2510{
2511 struct extent_map *em;
2512 u64 start = (u64)iblock << inode->i_blkbits;
2513 struct btrfs_multi_bio *multi = NULL;
2514 struct btrfs_root *root = BTRFS_I(inode)->root;
2515 u64 len;
2516 u64 logical;
2517 u64 map_length;
2518 int ret = 0;
2519
2520 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2521
2522 if (!em || IS_ERR(em))
2523 goto out;
2524
Chris Masone1c4b742008-04-22 13:26:46 -04002525 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002526 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002527 }
Chris Mason16432982008-04-10 10:23:21 -04002528
2529 if (em->block_start == EXTENT_MAP_INLINE) {
2530 ret = -EINVAL;
2531 goto out;
2532 }
2533
Chris Mason16432982008-04-10 10:23:21 -04002534 len = em->start + em->len - start;
2535 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2536
Chris Masone1c4b742008-04-22 13:26:46 -04002537 if (em->block_start == EXTENT_MAP_HOLE ||
2538 em->block_start == EXTENT_MAP_DELALLOC) {
2539 bh_result->b_size = len;
2540 goto out;
2541 }
2542
Chris Mason16432982008-04-10 10:23:21 -04002543 logical = start - em->start;
2544 logical = em->block_start + logical;
2545
2546 map_length = len;
2547 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2548 logical, &map_length, &multi, 0);
2549 BUG_ON(ret);
2550 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2551 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002552
Chris Mason16432982008-04-10 10:23:21 -04002553 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2554 set_buffer_mapped(bh_result);
2555 kfree(multi);
2556out:
2557 free_extent_map(em);
2558 return ret;
2559}
Chris Masone1c4b742008-04-22 13:26:46 -04002560#endif
Chris Mason16432982008-04-10 10:23:21 -04002561
2562static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2563 const struct iovec *iov, loff_t offset,
2564 unsigned long nr_segs)
2565{
Chris Masone1c4b742008-04-22 13:26:46 -04002566 return -EINVAL;
2567#if 0
Chris Mason16432982008-04-10 10:23:21 -04002568 struct file *file = iocb->ki_filp;
2569 struct inode *inode = file->f_mapping->host;
2570
2571 if (rw == WRITE)
2572 return -EINVAL;
2573
2574 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2575 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002576#endif
Chris Mason16432982008-04-10 10:23:21 -04002577}
2578
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002579static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002580{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002581 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002582}
2583
Chris Mason9ebefb182007-06-15 13:50:00 -04002584int btrfs_readpage(struct file *file, struct page *page)
2585{
Chris Masond1310b22008-01-24 16:13:08 -05002586 struct extent_io_tree *tree;
2587 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002588 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002589}
Chris Mason1832a6d2007-12-21 16:27:21 -05002590
Chris Mason39279cc2007-06-12 06:35:45 -04002591static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2592{
Chris Masond1310b22008-01-24 16:13:08 -05002593 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002594
2595
2596 if (current->flags & PF_MEMALLOC) {
2597 redirty_page_for_writepage(wbc, page);
2598 unlock_page(page);
2599 return 0;
2600 }
Chris Masond1310b22008-01-24 16:13:08 -05002601 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002602 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2603}
Chris Mason39279cc2007-06-12 06:35:45 -04002604
Chris Masonb293f02e2007-11-01 19:45:34 -04002605static int btrfs_writepages(struct address_space *mapping,
2606 struct writeback_control *wbc)
2607{
Chris Masond1310b22008-01-24 16:13:08 -05002608 struct extent_io_tree *tree;
2609 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f02e2007-11-01 19:45:34 -04002610 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2611}
2612
Chris Mason3ab2fb52007-11-08 10:59:22 -05002613static int
2614btrfs_readpages(struct file *file, struct address_space *mapping,
2615 struct list_head *pages, unsigned nr_pages)
2616{
Chris Masond1310b22008-01-24 16:13:08 -05002617 struct extent_io_tree *tree;
2618 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002619 return extent_readpages(tree, mapping, pages, nr_pages,
2620 btrfs_get_extent);
2621}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002622static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002623{
Chris Masond1310b22008-01-24 16:13:08 -05002624 struct extent_io_tree *tree;
2625 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002626 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002627
Chris Masond1310b22008-01-24 16:13:08 -05002628 tree = &BTRFS_I(page->mapping->host)->io_tree;
2629 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002630 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002631 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002632 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002633 ClearPagePrivate(page);
2634 set_page_private(page, 0);
2635 page_cache_release(page);
2636 }
2637 return ret;
2638}
Chris Mason39279cc2007-06-12 06:35:45 -04002639
Chris Masone6dcd2d2008-07-17 12:53:50 -04002640static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2641{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002642 return __btrfs_releasepage(page, gfp_flags);
2643}
2644
Chris Masona52d9a82007-08-27 16:49:44 -04002645static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2646{
Chris Masond1310b22008-01-24 16:13:08 -05002647 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002648 struct btrfs_ordered_extent *ordered;
2649 u64 page_start = page_offset(page);
2650 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002651
Chris Masone6dcd2d2008-07-17 12:53:50 -04002652 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002653 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002654 if (offset) {
2655 btrfs_releasepage(page, GFP_NOFS);
2656 return;
2657 }
2658
2659 lock_extent(tree, page_start, page_end, GFP_NOFS);
2660 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2661 page_offset(page));
2662 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04002663 /*
2664 * IO on this page will never be started, so we need
2665 * to account for any ordered extents now
2666 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002667 clear_extent_bit(tree, page_start, page_end,
2668 EXTENT_DIRTY | EXTENT_DELALLOC |
2669 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04002670 btrfs_finish_ordered_io(page->mapping->host,
2671 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002672 btrfs_put_ordered_extent(ordered);
2673 lock_extent(tree, page_start, page_end, GFP_NOFS);
2674 }
2675 clear_extent_bit(tree, page_start, page_end,
2676 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2677 EXTENT_ORDERED,
2678 1, 1, GFP_NOFS);
2679 __btrfs_releasepage(page, GFP_NOFS);
2680
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04002681 if (PagePrivate(page)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002682 invalidate_extent_lru(tree, page_offset(page),
2683 PAGE_CACHE_SIZE);
Chris Mason9ad6b7bc2008-04-18 16:11:30 -04002684 ClearPagePrivate(page);
2685 set_page_private(page, 0);
2686 page_cache_release(page);
2687 }
Chris Mason39279cc2007-06-12 06:35:45 -04002688}
2689
Chris Mason9ebefb182007-06-15 13:50:00 -04002690/*
2691 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2692 * called from a page fault handler when a page is first dirtied. Hence we must
2693 * be careful to check for EOF conditions here. We set the page up correctly
2694 * for a written page which means we get ENOSPC checking when writing into
2695 * holes and correct delalloc and unwritten extent mapping on filesystems that
2696 * support these features.
2697 *
2698 * We are not allowed to take the i_mutex here so we have to play games to
2699 * protect against truncate races as the page could now be beyond EOF. Because
2700 * vmtruncate() writes the inode size before removing pages, once we have the
2701 * page lock we can determine safely if the page is beyond EOF. If it is not
2702 * beyond EOF, then the page is guaranteed safe against truncation until we
2703 * unlock the page.
2704 */
2705int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2706{
Chris Mason6da6aba2007-12-18 16:15:09 -05002707 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002708 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002709 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2710 struct btrfs_ordered_extent *ordered;
2711 char *kaddr;
2712 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002713 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002714 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002715 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002716 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04002717
Chris Mason1832a6d2007-12-21 16:27:21 -05002718 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05002719 if (ret)
2720 goto out;
2721
2722 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002723again:
Chris Mason9ebefb182007-06-15 13:50:00 -04002724 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002725 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002726 page_start = page_offset(page);
2727 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002728
Chris Mason9ebefb182007-06-15 13:50:00 -04002729 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04002730 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002731 /* page got truncated out from underneath us */
2732 goto out_unlock;
2733 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002734 wait_on_page_writeback(page);
2735
2736 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2737 set_page_extent_mapped(page);
2738
Chris Masoneb84ae02008-07-17 13:53:27 -04002739 /*
2740 * we can't set the delalloc bits if there are pending ordered
2741 * extents. Drop our locks and wait for them to finish
2742 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002743 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2744 if (ordered) {
2745 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2746 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002747 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002748 btrfs_put_ordered_extent(ordered);
2749 goto again;
2750 }
2751
2752 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
2753 page_end, GFP_NOFS);
2754 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04002755
2756 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002757 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002758 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04002759 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04002760 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04002761
Chris Masone6dcd2d2008-07-17 12:53:50 -04002762 if (zero_start != PAGE_CACHE_SIZE) {
2763 kaddr = kmap(page);
2764 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
2765 flush_dcache_page(page);
2766 kunmap(page);
2767 }
Chris Mason247e7432008-07-17 12:53:51 -04002768 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002769 set_page_dirty(page);
2770 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04002771
2772out_unlock:
2773 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002774out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002775 return ret;
2776}
2777
Chris Mason39279cc2007-06-12 06:35:45 -04002778static void btrfs_truncate(struct inode *inode)
2779{
2780 struct btrfs_root *root = BTRFS_I(inode)->root;
2781 int ret;
2782 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002783 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04002784 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002785
2786 if (!S_ISREG(inode->i_mode))
2787 return;
2788 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2789 return;
2790
2791 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2792
Chris Mason39279cc2007-06-12 06:35:45 -04002793 trans = btrfs_start_transaction(root, 1);
2794 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04002795 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
2796 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04002797
2798 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002799 ret = btrfs_truncate_in_trans(trans, root, inode,
2800 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002801 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002802 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002803
Chris Mason89ce8a62008-06-25 16:01:31 -04002804 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002805 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002806 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002807}
2808
Sven Wegener3b963622008-06-09 21:57:42 -04002809/*
2810 * Invalidate a single dcache entry at the root of the filesystem.
2811 * Needed after creation of snapshot or subvolume.
2812 */
2813void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2814 int namelen)
2815{
2816 struct dentry *alias, *entry;
2817 struct qstr qstr;
2818
2819 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2820 if (alias) {
2821 qstr.name = name;
2822 qstr.len = namelen;
2823 /* change me if btrfs ever gets a d_hash operation */
2824 qstr.hash = full_name_hash(qstr.name, qstr.len);
2825 entry = d_lookup(alias, &qstr);
2826 dput(alias);
2827 if (entry) {
2828 d_invalidate(entry);
2829 dput(entry);
2830 }
2831 }
2832}
2833
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002834int btrfs_create_subvol_root(struct btrfs_root *new_root,
2835 struct btrfs_trans_handle *trans, u64 new_dirid,
2836 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04002837{
Chris Mason39279cc2007-06-12 06:35:45 -04002838 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002839 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002840
Chris Mason9c583092008-01-29 15:15:18 -05002841 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002842 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002843 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002844 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002845 inode->i_op = &btrfs_dir_inode_operations;
2846 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002847 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002848
Chris Mason39544012007-12-12 14:38:19 -05002849 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2850 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002851 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04002852 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04002853
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002854 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002855}
2856
Chris Masonedbd8d42007-12-21 16:27:24 -05002857unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002858 struct file_ra_state *ra, struct file *file,
2859 pgoff_t offset, pgoff_t last_index)
2860{
Chris Mason8e7bf942008-04-28 09:02:36 -04002861 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04002862
2863#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04002864 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2865 return offset;
2866#else
Chris Mason86479a02007-09-10 19:58:16 -04002867 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2868 return offset + req_size;
2869#endif
2870}
2871
Chris Mason39279cc2007-06-12 06:35:45 -04002872struct inode *btrfs_alloc_inode(struct super_block *sb)
2873{
2874 struct btrfs_inode *ei;
2875
2876 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2877 if (!ei)
2878 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002879 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002880 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002881 return &ei->vfs_inode;
2882}
2883
2884void btrfs_destroy_inode(struct inode *inode)
2885{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002886 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04002887 WARN_ON(!list_empty(&inode->i_dentry));
2888 WARN_ON(inode->i_data.nrpages);
2889
Chris Masone6dcd2d2008-07-17 12:53:50 -04002890 while(1) {
2891 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
2892 if (!ordered)
2893 break;
2894 else {
2895 printk("found ordered extent %Lu %Lu\n",
2896 ordered->file_offset, ordered->len);
2897 btrfs_remove_ordered_extent(inode, ordered);
2898 btrfs_put_ordered_extent(ordered);
2899 btrfs_put_ordered_extent(ordered);
2900 }
2901 }
Chris Mason8c416c92008-01-14 15:10:26 -05002902 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002903 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2904}
2905
Chris Mason44ec0b72007-10-29 10:55:05 -04002906#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2907static void init_once(struct kmem_cache * cachep, void *foo)
2908#else
Chris Mason39279cc2007-06-12 06:35:45 -04002909static void init_once(void * foo, struct kmem_cache * cachep,
2910 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002911#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002912{
2913 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2914
2915 inode_init_once(&ei->vfs_inode);
2916}
2917
2918void btrfs_destroy_cachep(void)
2919{
2920 if (btrfs_inode_cachep)
2921 kmem_cache_destroy(btrfs_inode_cachep);
2922 if (btrfs_trans_handle_cachep)
2923 kmem_cache_destroy(btrfs_trans_handle_cachep);
2924 if (btrfs_transaction_cachep)
2925 kmem_cache_destroy(btrfs_transaction_cachep);
2926 if (btrfs_bit_radix_cachep)
2927 kmem_cache_destroy(btrfs_bit_radix_cachep);
2928 if (btrfs_path_cachep)
2929 kmem_cache_destroy(btrfs_path_cachep);
2930}
2931
Chris Mason86479a02007-09-10 19:58:16 -04002932struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002933 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002934#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2935 void (*ctor)(struct kmem_cache *, void *)
2936#else
Chris Mason92fee662007-07-25 12:31:35 -04002937 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002938 unsigned long)
2939#endif
2940 )
Chris Mason92fee662007-07-25 12:31:35 -04002941{
2942 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2943 SLAB_MEM_SPREAD | extra_flags), ctor
2944#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2945 ,NULL
2946#endif
2947 );
2948}
2949
Chris Mason39279cc2007-06-12 06:35:45 -04002950int btrfs_init_cachep(void)
2951{
Chris Mason86479a02007-09-10 19:58:16 -04002952 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002953 sizeof(struct btrfs_inode),
2954 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002955 if (!btrfs_inode_cachep)
2956 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002957 btrfs_trans_handle_cachep =
2958 btrfs_cache_create("btrfs_trans_handle_cache",
2959 sizeof(struct btrfs_trans_handle),
2960 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002961 if (!btrfs_trans_handle_cachep)
2962 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002963 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002964 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002965 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002966 if (!btrfs_transaction_cachep)
2967 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002968 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002969 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002970 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002971 if (!btrfs_path_cachep)
2972 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002973 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002974 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002975 if (!btrfs_bit_radix_cachep)
2976 goto fail;
2977 return 0;
2978fail:
2979 btrfs_destroy_cachep();
2980 return -ENOMEM;
2981}
2982
2983static int btrfs_getattr(struct vfsmount *mnt,
2984 struct dentry *dentry, struct kstat *stat)
2985{
2986 struct inode *inode = dentry->d_inode;
2987 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002988 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05002989 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04002990 return 0;
2991}
2992
2993static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2994 struct inode * new_dir,struct dentry *new_dentry)
2995{
2996 struct btrfs_trans_handle *trans;
2997 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2998 struct inode *new_inode = new_dentry->d_inode;
2999 struct inode *old_inode = old_dentry->d_inode;
3000 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04003001 int ret;
3002
3003 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3004 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3005 return -ENOTEMPTY;
3006 }
Chris Mason5f39d392007-10-15 16:14:19 -04003007
Chris Mason1832a6d2007-12-21 16:27:21 -05003008 ret = btrfs_check_free_space(root, 1, 0);
3009 if (ret)
3010 goto out_unlock;
3011
Chris Mason39279cc2007-06-12 06:35:45 -04003012 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003013
Chris Mason39279cc2007-06-12 06:35:45 -04003014 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003015
3016 old_dentry->d_inode->i_nlink++;
3017 old_dir->i_ctime = old_dir->i_mtime = ctime;
3018 new_dir->i_ctime = new_dir->i_mtime = ctime;
3019 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003020
Chris Mason39279cc2007-06-12 06:35:45 -04003021 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3022 if (ret)
3023 goto out_fail;
3024
3025 if (new_inode) {
3026 new_inode->i_ctime = CURRENT_TIME;
3027 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3028 if (ret)
3029 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003030 }
Chris Mason9c583092008-01-29 15:15:18 -05003031 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003032 if (ret)
3033 goto out_fail;
3034
3035out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003036 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003037out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003038 return ret;
3039}
3040
3041static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3042 const char *symname)
3043{
3044 struct btrfs_trans_handle *trans;
3045 struct btrfs_root *root = BTRFS_I(dir)->root;
3046 struct btrfs_path *path;
3047 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003048 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003049 int err;
3050 int drop_inode = 0;
3051 u64 objectid;
3052 int name_len;
3053 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003054 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003055 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003056 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003057 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003058
3059 name_len = strlen(symname) + 1;
3060 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3061 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003062
Chris Mason1832a6d2007-12-21 16:27:21 -05003063 err = btrfs_check_free_space(root, 1, 0);
3064 if (err)
3065 goto out_fail;
3066
Chris Mason39279cc2007-06-12 06:35:45 -04003067 trans = btrfs_start_transaction(root, 1);
3068 btrfs_set_trans_block_group(trans, dir);
3069
3070 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3071 if (err) {
3072 err = -ENOSPC;
3073 goto out_unlock;
3074 }
3075
Chris Mason9c583092008-01-29 15:15:18 -05003076 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3077 dentry->d_name.len,
3078 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003079 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3080 err = PTR_ERR(inode);
3081 if (IS_ERR(inode))
3082 goto out_unlock;
3083
3084 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003085 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003086 if (err)
3087 drop_inode = 1;
3088 else {
3089 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003090 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003091 inode->i_fop = &btrfs_file_operations;
3092 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003093 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3094 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003095 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003096 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3097 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04003098 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003099 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003100 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003101 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003102 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003103 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003104 }
3105 dir->i_sb->s_dirt = 1;
3106 btrfs_update_inode_block_group(trans, inode);
3107 btrfs_update_inode_block_group(trans, dir);
3108 if (drop_inode)
3109 goto out_unlock;
3110
3111 path = btrfs_alloc_path();
3112 BUG_ON(!path);
3113 key.objectid = inode->i_ino;
3114 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003115 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3116 datasize = btrfs_file_extent_calc_inline_size(name_len);
3117 err = btrfs_insert_empty_item(trans, root, path, &key,
3118 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003119 if (err) {
3120 drop_inode = 1;
3121 goto out_unlock;
3122 }
Chris Mason5f39d392007-10-15 16:14:19 -04003123 leaf = path->nodes[0];
3124 ei = btrfs_item_ptr(leaf, path->slots[0],
3125 struct btrfs_file_extent_item);
3126 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3127 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003128 BTRFS_FILE_EXTENT_INLINE);
3129 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003130 write_extent_buffer(leaf, symname, ptr, name_len);
3131 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003132 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003133
Chris Mason39279cc2007-06-12 06:35:45 -04003134 inode->i_op = &btrfs_symlink_inode_operations;
3135 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003136 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003137 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003138 err = btrfs_update_inode(trans, root, inode);
3139 if (err)
3140 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003141
3142out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003143 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003144 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003145out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003146 if (drop_inode) {
3147 inode_dec_link_count(inode);
3148 iput(inode);
3149 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003150 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003151 return err;
3152}
Chris Mason16432982008-04-10 10:23:21 -04003153
Chris Masone6dcd2d2008-07-17 12:53:50 -04003154static int btrfs_set_page_dirty(struct page *page)
3155{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003156 return __set_page_dirty_nobuffers(page);
3157}
3158
Yanfdebe2b2008-01-14 13:26:08 -05003159static int btrfs_permission(struct inode *inode, int mask,
3160 struct nameidata *nd)
3161{
3162 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3163 return -EACCES;
3164 return generic_permission(inode, mask, NULL);
3165}
Chris Mason39279cc2007-06-12 06:35:45 -04003166
3167static struct inode_operations btrfs_dir_inode_operations = {
3168 .lookup = btrfs_lookup,
3169 .create = btrfs_create,
3170 .unlink = btrfs_unlink,
3171 .link = btrfs_link,
3172 .mkdir = btrfs_mkdir,
3173 .rmdir = btrfs_rmdir,
3174 .rename = btrfs_rename,
3175 .symlink = btrfs_symlink,
3176 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003177 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003178 .setxattr = generic_setxattr,
3179 .getxattr = generic_getxattr,
3180 .listxattr = btrfs_listxattr,
3181 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003182 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003183};
Chris Mason39279cc2007-06-12 06:35:45 -04003184static struct inode_operations btrfs_dir_ro_inode_operations = {
3185 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003186 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003187};
Chris Mason39279cc2007-06-12 06:35:45 -04003188static struct file_operations btrfs_dir_file_operations = {
3189 .llseek = generic_file_llseek,
3190 .read = generic_read_dir,
3191 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003192 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003193#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003194 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003195#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003196 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003197};
3198
Chris Masond1310b22008-01-24 16:13:08 -05003199static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003200 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003201 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003202 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003203 .readpage_io_hook = btrfs_readpage_io_hook,
3204 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003205 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003206 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003207 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003208 .set_bit_hook = btrfs_set_bit_hook,
3209 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003210};
3211
Chris Mason39279cc2007-06-12 06:35:45 -04003212static struct address_space_operations btrfs_aops = {
3213 .readpage = btrfs_readpage,
3214 .writepage = btrfs_writepage,
Chris Masonb293f02e2007-11-01 19:45:34 -04003215 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003216 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003217 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003218 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003219 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003220 .invalidatepage = btrfs_invalidatepage,
3221 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003222 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003223};
3224
3225static struct address_space_operations btrfs_symlink_aops = {
3226 .readpage = btrfs_readpage,
3227 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003228 .invalidatepage = btrfs_invalidatepage,
3229 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003230};
3231
3232static struct inode_operations btrfs_file_inode_operations = {
3233 .truncate = btrfs_truncate,
3234 .getattr = btrfs_getattr,
3235 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003236 .setxattr = generic_setxattr,
3237 .getxattr = generic_getxattr,
3238 .listxattr = btrfs_listxattr,
3239 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003240 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003241};
Josef Bacik618e21d2007-07-11 10:18:17 -04003242static struct inode_operations btrfs_special_inode_operations = {
3243 .getattr = btrfs_getattr,
3244 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003245 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003246};
Chris Mason39279cc2007-06-12 06:35:45 -04003247static struct inode_operations btrfs_symlink_inode_operations = {
3248 .readlink = generic_readlink,
3249 .follow_link = page_follow_link_light,
3250 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003251 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003252};