blob: 8fb6dc25e7a53177436d8bd7ad6b9919f2cbad9b [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 Masone5a22172008-07-18 20:42:20 -0400131 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400132 btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
Chris Masone5a22172008-07-18 20:42:20 -0400133 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason3b951512008-04-17 11:29:12 -0400134
Chris Masonc59f8952007-12-17 20:14:04 -0500135 while(num_bytes > 0) {
136 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400137 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
138 root->sectorsize, 0, 0,
139 (u64)-1, &ins, 1);
Chris Masonc59f8952007-12-17 20:14:04 -0500140 if (ret) {
141 WARN_ON(1);
142 goto out;
143 }
Chris Masone6dcd2d2008-07-17 12:53:50 -0400144 em = alloc_extent_map(GFP_NOFS);
145 em->start = start;
146 em->len = ins.offset;
147 em->block_start = ins.objectid;
148 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masone5a22172008-07-18 20:42:20 -0400149 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400150 set_bit(EXTENT_FLAG_PINNED, &em->flags);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400151 while(1) {
152 spin_lock(&em_tree->lock);
153 ret = add_extent_mapping(em_tree, em);
154 spin_unlock(&em_tree->lock);
155 if (ret != -EEXIST) {
156 free_extent_map(em);
157 break;
158 }
159 btrfs_drop_extent_cache(inode, start,
160 start + ins.offset - 1);
161 }
Chris Masone5a22172008-07-18 20:42:20 -0400162 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400163
Chris Mason98d20f62008-04-14 09:46:10 -0400164 cur_alloc_size = ins.offset;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400165 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
166 ins.offset);
167 BUG_ON(ret);
Chris Mason3b951512008-04-17 11:29:12 -0400168 if (num_bytes < cur_alloc_size) {
169 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
170 cur_alloc_size);
171 break;
172 }
Chris Masonc59f8952007-12-17 20:14:04 -0500173 num_bytes -= cur_alloc_size;
174 alloc_hint = ins.objectid + ins.offset;
175 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400176 }
Chris Masonb888db22007-08-27 16:49:44 -0400177out:
178 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500179 return ret;
180}
181
182static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
183{
184 u64 extent_start;
185 u64 extent_end;
186 u64 bytenr;
187 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500188 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500189 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500190 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masona68d5932008-05-08 14:11:56 -0400191 struct btrfs_block_group_cache *block_group;
Chris Masonbe20aa92007-12-17 20:14:01 -0500192 struct extent_buffer *leaf;
193 int found_type;
194 struct btrfs_path *path;
195 struct btrfs_file_extent_item *item;
196 int ret;
197 int err;
198 struct btrfs_key found_key;
199
Chris Masonc31f8832008-01-08 15:46:31 -0500200 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500201 path = btrfs_alloc_path();
202 BUG_ON(!path);
203again:
204 ret = btrfs_lookup_file_extent(NULL, root, path,
205 inode->i_ino, start, 0);
206 if (ret < 0) {
207 btrfs_free_path(path);
208 return ret;
209 }
210
211 cow_end = end;
212 if (ret != 0) {
213 if (path->slots[0] == 0)
214 goto not_found;
215 path->slots[0]--;
216 }
217
218 leaf = path->nodes[0];
219 item = btrfs_item_ptr(leaf, path->slots[0],
220 struct btrfs_file_extent_item);
221
222 /* are we inside the extent that was found? */
223 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
224 found_type = btrfs_key_type(&found_key);
225 if (found_key.objectid != inode->i_ino ||
Chris Masonbbaf5492008-05-08 16:31:21 -0400226 found_type != BTRFS_EXTENT_DATA_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500227 goto not_found;
Chris Masonbe20aa92007-12-17 20:14:01 -0500228
229 found_type = btrfs_file_extent_type(leaf, item);
230 extent_start = found_key.offset;
231 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500232 u64 extent_num_bytes;
233
234 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
235 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500236 err = 0;
237
Chris Mason1832a6d2007-12-21 16:27:21 -0500238 if (loops && start != extent_start)
239 goto not_found;
240
Chris Masonbe20aa92007-12-17 20:14:01 -0500241 if (start < extent_start || start >= extent_end)
242 goto not_found;
243
244 cow_end = min(end, extent_end - 1);
245 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
246 if (bytenr == 0)
247 goto not_found;
248
Chris Masona68d5932008-05-08 14:11:56 -0400249 if (btrfs_count_snapshots_in_path(root, path, inode->i_ino,
250 bytenr) != 1) {
251 goto not_found;
252 }
253
Chris Masonc31f8832008-01-08 15:46:31 -0500254 /*
255 * we may be called by the resizer, make sure we're inside
256 * the limits of the FS
257 */
Chris Masona68d5932008-05-08 14:11:56 -0400258 block_group = btrfs_lookup_block_group(root->fs_info,
259 bytenr);
260 if (!block_group || block_group->ro)
Chris Masonc31f8832008-01-08 15:46:31 -0500261 goto not_found;
262
Chris Masonbe20aa92007-12-17 20:14:01 -0500263 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500264 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500265 goto not_found;
266 }
267loop:
268 if (start > end) {
269 btrfs_free_path(path);
270 return 0;
271 }
272 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500273 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500274 goto again;
275
276not_found:
Chris Masonbbaf5492008-05-08 16:31:21 -0400277 cow_file_range(inode, start, end);
278 start = end + 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500279 goto loop;
280}
281
282static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
283{
284 struct btrfs_root *root = BTRFS_I(inode)->root;
285 int ret;
Chris Masona2135012008-06-25 16:01:30 -0400286
Yanb98b6762008-01-08 15:54:37 -0500287 if (btrfs_test_opt(root, NODATACOW) ||
288 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500289 ret = run_delalloc_nocow(inode, start, end);
290 else
291 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500292
Chris Masonb888db22007-08-27 16:49:44 -0400293 return ret;
294}
295
Chris Mason291d6732008-01-29 15:55:23 -0500296int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500297 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500298{
Chris Masonbcbfce82008-04-22 13:26:47 -0400299 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500300 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500301 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400302 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500303 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500304 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400305 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500306 }
307 return 0;
308}
309
310int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500311 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500312{
Chris Masonb0c68f82008-01-31 11:05:37 -0500313 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500314 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400315 unsigned long flags;
316
317 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500318 if (end - start + 1 > root->fs_info->delalloc_bytes) {
319 printk("warning: delalloc account %Lu %Lu\n",
320 end - start + 1, root->fs_info->delalloc_bytes);
321 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500322 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500323 } else {
324 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500325 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500326 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400327 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500328 }
329 return 0;
330}
331
Chris Mason239b14b2008-03-24 15:02:07 -0400332int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
333 size_t size, struct bio *bio)
334{
335 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
336 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400337 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400338 u64 length = 0;
339 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400340 int ret;
341
Chris Masonf2d8d742008-04-21 10:03:05 -0400342 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400343 map_tree = &root->fs_info->mapping_tree;
344 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400345 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400346 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400347
Chris Mason239b14b2008-03-24 15:02:07 -0400348 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400349 return 1;
350 }
351 return 0;
352}
353
Chris Mason44b8bd72008-04-16 11:14:51 -0400354int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400355 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500356{
Chris Mason065631f2008-02-20 12:07:25 -0500357 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason065631f2008-02-20 12:07:25 -0500358 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400359
Chris Mason3edf7d32008-07-18 06:17:13 -0400360 ret = btrfs_csum_one_bio(root, inode, bio);
Chris Mason44b8bd72008-04-16 11:14:51 -0400361 BUG_ON(ret);
Chris Masone0156402008-04-16 11:15:20 -0400362
Chris Mason8b712842008-06-11 16:50:36 -0400363 return btrfs_map_bio(root, rw, bio, mirror_num, 1);
Chris Mason44b8bd72008-04-16 11:14:51 -0400364}
365
366int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
367 int mirror_num)
368{
369 struct btrfs_root *root = BTRFS_I(inode)->root;
370 int ret = 0;
371
Chris Masone6dcd2d2008-07-17 12:53:50 -0400372 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
373 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500374
Chris Masone6dcd2d2008-07-17 12:53:50 -0400375 if (!(rw & (1 << BIO_RW))) {
Chris Mason0b86a832008-03-24 15:01:56 -0400376 goto mapit;
377 }
Chris Mason065631f2008-02-20 12:07:25 -0500378
Chris Mason44b8bd72008-04-16 11:14:51 -0400379 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
380 inode, rw, bio, mirror_num,
381 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400382mapit:
Chris Mason8b712842008-06-11 16:50:36 -0400383 return btrfs_map_bio(root, rw, bio, mirror_num, 0);
Chris Mason065631f2008-02-20 12:07:25 -0500384}
Chris Mason6885f302008-02-20 16:11:05 -0500385
Chris Masonba1da2f2008-07-17 12:54:15 -0400386static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
Chris Masone6dcd2d2008-07-17 12:53:50 -0400387 struct inode *inode, u64 file_offset,
388 struct list_head *list)
389{
390 struct list_head *cur;
391 struct btrfs_ordered_sum *sum;
392
393 btrfs_set_trans_block_group(trans, inode);
Chris Masonba1da2f2008-07-17 12:54:15 -0400394 list_for_each(cur, list) {
Chris Masone6dcd2d2008-07-17 12:53:50 -0400395 sum = list_entry(cur, struct btrfs_ordered_sum, list);
396 mutex_lock(&BTRFS_I(inode)->csum_mutex);
397 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
398 inode, sum);
399 mutex_unlock(&BTRFS_I(inode)->csum_mutex);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400400 }
401 return 0;
402}
403
Chris Mason247e7432008-07-17 12:53:51 -0400404struct btrfs_writepage_fixup {
405 struct page *page;
406 struct btrfs_work work;
407};
408
409/* see btrfs_writepage_start_hook for details on why this is required */
410void btrfs_writepage_fixup_worker(struct btrfs_work *work)
411{
412 struct btrfs_writepage_fixup *fixup;
413 struct btrfs_ordered_extent *ordered;
414 struct page *page;
415 struct inode *inode;
416 u64 page_start;
417 u64 page_end;
418
419 fixup = container_of(work, struct btrfs_writepage_fixup, work);
420 page = fixup->page;
Chris Mason4a096752008-07-21 10:29:44 -0400421again:
Chris Mason247e7432008-07-17 12:53:51 -0400422 lock_page(page);
423 if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
424 ClearPageChecked(page);
425 goto out_page;
426 }
427
428 inode = page->mapping->host;
429 page_start = page_offset(page);
430 page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
431
432 lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
Chris Mason4a096752008-07-21 10:29:44 -0400433
434 /* already ordered? We're done */
435 if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
436 EXTENT_ORDERED, 0)) {
Chris Mason247e7432008-07-17 12:53:51 -0400437 goto out;
Chris Mason4a096752008-07-21 10:29:44 -0400438 }
439
440 ordered = btrfs_lookup_ordered_extent(inode, page_start);
441 if (ordered) {
442 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
443 page_end, GFP_NOFS);
444 unlock_page(page);
445 btrfs_start_ordered_extent(inode, ordered, 1);
446 goto again;
447 }
Chris Mason247e7432008-07-17 12:53:51 -0400448
449 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start, page_end,
450 GFP_NOFS);
451 ClearPageChecked(page);
452out:
453 unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
454out_page:
455 unlock_page(page);
456 page_cache_release(page);
457}
458
459/*
460 * There are a few paths in the higher layers of the kernel that directly
461 * set the page dirty bit without asking the filesystem if it is a
462 * good idea. This causes problems because we want to make sure COW
463 * properly happens and the data=ordered rules are followed.
464 *
465 * In our case any range that doesn't have the EXTENT_ORDERED bit set
466 * hasn't been properly setup for IO. We kick off an async process
467 * to fix it up. The async helper will wait for ordered extents, set
468 * the delalloc bit and make it safe to write the page.
469 */
470int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
471{
472 struct inode *inode = page->mapping->host;
473 struct btrfs_writepage_fixup *fixup;
474 struct btrfs_root *root = BTRFS_I(inode)->root;
475 int ret;
476
477 ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
478 EXTENT_ORDERED, 0);
479 if (ret)
480 return 0;
481
482 if (PageChecked(page))
483 return -EAGAIN;
484
485 fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
486 if (!fixup)
487 return -EAGAIN;
488printk("queueing worker to fixup page %lu %Lu\n", inode->i_ino, page_offset(page));
489 SetPageChecked(page);
490 page_cache_get(page);
491 fixup->work.func = btrfs_writepage_fixup_worker;
492 fixup->page = page;
493 btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
494 return -EAGAIN;
495}
496
Chris Mason211f90e2008-07-18 11:56:15 -0400497static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400498{
Chris Masone6dcd2d2008-07-17 12:53:50 -0400499 struct btrfs_root *root = BTRFS_I(inode)->root;
500 struct btrfs_trans_handle *trans;
501 struct btrfs_ordered_extent *ordered_extent;
502 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason7f3c74f2008-07-18 12:01:11 -0400503 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
504 struct extent_map *em;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400505 u64 alloc_hint = 0;
Chris Masone5a22172008-07-18 20:42:20 -0400506 u64 clear_start;
507 u64 clear_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400508 struct list_head list;
509 struct btrfs_key ins;
510 int ret;
511
512 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400513 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400514 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400515
Chris Masonf9295742008-07-17 12:54:14 -0400516 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400517
518 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
519 BUG_ON(!ordered_extent);
520
521 lock_extent(io_tree, ordered_extent->file_offset,
522 ordered_extent->file_offset + ordered_extent->len - 1,
523 GFP_NOFS);
524
525 INIT_LIST_HEAD(&list);
526
527 ins.objectid = ordered_extent->start;
528 ins.offset = ordered_extent->len;
529 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400530
Chris Masone6dcd2d2008-07-17 12:53:50 -0400531 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
532 trans->transid, inode->i_ino,
533 ordered_extent->file_offset, &ins);
534 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400535
536 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400537
Chris Masone6dcd2d2008-07-17 12:53:50 -0400538 ret = btrfs_drop_extents(trans, root, inode,
539 ordered_extent->file_offset,
540 ordered_extent->file_offset +
541 ordered_extent->len,
542 ordered_extent->file_offset, &alloc_hint);
543 BUG_ON(ret);
544 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
545 ordered_extent->file_offset,
546 ordered_extent->start,
547 ordered_extent->len,
548 ordered_extent->len, 0);
549 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400550
Chris Mason7f3c74f2008-07-18 12:01:11 -0400551 spin_lock(&em_tree->lock);
Chris Masone5a22172008-07-18 20:42:20 -0400552 clear_start = ordered_extent->file_offset;
553 clear_end = ordered_extent->file_offset + ordered_extent->len;
554 while(clear_start < clear_end) {
555 em = lookup_extent_mapping(em_tree, clear_start,
556 clear_end - clear_start);
557 if (em) {
558 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
559 clear_start = em->start + em->len;
560 free_extent_map(em);
561 } else {
562 break;
563 }
Chris Mason7f3c74f2008-07-18 12:01:11 -0400564 }
565 spin_unlock(&em_tree->lock);
566
Chris Masone6dcd2d2008-07-17 12:53:50 -0400567 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
568 ordered_extent->file_offset +
569 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400570 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
571
Chris Masone6dcd2d2008-07-17 12:53:50 -0400572 inode->i_blocks += ordered_extent->len >> 9;
573 unlock_extent(io_tree, ordered_extent->file_offset,
574 ordered_extent->file_offset + ordered_extent->len - 1,
575 GFP_NOFS);
576 add_pending_csums(trans, inode, ordered_extent->file_offset,
577 &ordered_extent->list);
578
Chris Masondbe674a2008-07-17 12:54:05 -0400579 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400580 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400581
Chris Masone6dcd2d2008-07-17 12:53:50 -0400582 /* once for us */
583 btrfs_put_ordered_extent(ordered_extent);
584 /* once for the tree */
585 btrfs_put_ordered_extent(ordered_extent);
586
587 btrfs_update_inode(trans, root, inode);
588 btrfs_end_transaction(trans, root);
589 return 0;
590}
591
Chris Mason211f90e2008-07-18 11:56:15 -0400592int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
593 struct extent_state *state, int uptodate)
594{
595 return btrfs_finish_ordered_io(page->mapping->host, start, end);
596}
597
Chris Mason07157aa2007-08-30 08:50:51 -0400598int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
599{
600 int ret = 0;
601 struct inode *inode = page->mapping->host;
602 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500603 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400604 struct btrfs_csum_item *item;
605 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400606 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400607
Yanb98b6762008-01-08 15:54:37 -0500608 if (btrfs_test_opt(root, NODATASUM) ||
609 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500610 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400611
Chris Mason07157aa2007-08-30 08:50:51 -0400612 path = btrfs_alloc_path();
613 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
614 if (IS_ERR(item)) {
Chris Masonba1da2f2008-07-17 12:54:15 -0400615 /*
616 * It is possible there is an ordered extent that has
617 * not yet finished for this range in the file. If so,
618 * that extent will have a csum cached, and it will insert
619 * the sum after all the blocks in the extent are fully
620 * on disk. So, look for an ordered extent and use the
621 * sum if found.
622 */
623 ret = btrfs_find_ordered_sum(inode, start, &csum);
624 if (ret == 0)
625 goto found;
626
Chris Mason07157aa2007-08-30 08:50:51 -0400627 ret = PTR_ERR(item);
628 /* a csum that isn't present is a preallocated region. */
629 if (ret == -ENOENT || ret == -EFBIG)
630 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400631 csum = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400632 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
633 start);
Chris Mason07157aa2007-08-30 08:50:51 -0400634 goto out;
635 }
Chris Masonff79f812007-10-15 16:22:25 -0400636 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
637 BTRFS_CRC32_SIZE);
Chris Masonba1da2f2008-07-17 12:54:15 -0400638found:
Chris Masond1310b22008-01-24 16:13:08 -0500639 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400640out:
641 if (path)
642 btrfs_free_path(path);
Chris Mason07157aa2007-08-30 08:50:51 -0400643 return ret;
644}
645
Chris Mason7e383262008-04-09 16:28:12 -0400646struct io_failure_record {
647 struct page *page;
648 u64 start;
649 u64 len;
650 u64 logical;
651 int last_mirror;
652};
653
Chris Mason1259ab72008-05-12 13:39:03 -0400654int btrfs_io_failed_hook(struct bio *failed_bio,
655 struct page *page, u64 start, u64 end,
656 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400657{
658 struct io_failure_record *failrec = NULL;
659 u64 private;
660 struct extent_map *em;
661 struct inode *inode = page->mapping->host;
662 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400663 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400664 struct bio *bio;
665 int num_copies;
666 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400667 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400668 u64 logical;
669
670 ret = get_state_private(failure_tree, start, &private);
671 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400672 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
673 if (!failrec)
674 return -ENOMEM;
675 failrec->start = start;
676 failrec->len = end - start + 1;
677 failrec->last_mirror = 0;
678
Chris Mason3b951512008-04-17 11:29:12 -0400679 spin_lock(&em_tree->lock);
680 em = lookup_extent_mapping(em_tree, start, failrec->len);
681 if (em->start > start || em->start + em->len < start) {
682 free_extent_map(em);
683 em = NULL;
684 }
685 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400686
687 if (!em || IS_ERR(em)) {
688 kfree(failrec);
689 return -EIO;
690 }
691 logical = start - em->start;
692 logical = em->block_start + logical;
693 failrec->logical = logical;
694 free_extent_map(em);
695 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
696 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400697 set_state_private(failure_tree, start,
698 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400699 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400700 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400701 }
702 num_copies = btrfs_num_copies(
703 &BTRFS_I(inode)->root->fs_info->mapping_tree,
704 failrec->logical, failrec->len);
705 failrec->last_mirror++;
706 if (!state) {
707 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
708 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
709 failrec->start,
710 EXTENT_LOCKED);
711 if (state && state->start != failrec->start)
712 state = NULL;
713 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
714 }
715 if (!state || failrec->last_mirror > num_copies) {
716 set_state_private(failure_tree, failrec->start, 0);
717 clear_extent_bits(failure_tree, failrec->start,
718 failrec->start + failrec->len - 1,
719 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
720 kfree(failrec);
721 return -EIO;
722 }
723 bio = bio_alloc(GFP_NOFS, 1);
724 bio->bi_private = state;
725 bio->bi_end_io = failed_bio->bi_end_io;
726 bio->bi_sector = failrec->logical >> 9;
727 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400728 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400729 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400730 if (failed_bio->bi_rw & (1 << BIO_RW))
731 rw = WRITE;
732 else
733 rw = READ;
734
735 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
736 failrec->last_mirror);
737 return 0;
738}
739
740int btrfs_clean_io_failures(struct inode *inode, u64 start)
741{
742 u64 private;
743 u64 private_failure;
744 struct io_failure_record *failure;
745 int ret;
746
747 private = 0;
748 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
749 (u64)-1, 1, EXTENT_DIRTY)) {
750 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
751 start, &private_failure);
752 if (ret == 0) {
753 failure = (struct io_failure_record *)(unsigned long)
754 private_failure;
755 set_state_private(&BTRFS_I(inode)->io_failure_tree,
756 failure->start, 0);
757 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
758 failure->start,
759 failure->start + failure->len - 1,
760 EXTENT_DIRTY | EXTENT_LOCKED,
761 GFP_NOFS);
762 kfree(failure);
763 }
764 }
Chris Mason7e383262008-04-09 16:28:12 -0400765 return 0;
766}
767
Chris Mason70dec802008-01-29 09:59:12 -0500768int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
769 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400770{
Chris Mason35ebb932007-10-30 16:56:53 -0400771 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400772 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500773 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400774 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500775 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400776 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400777 struct btrfs_root *root = BTRFS_I(inode)->root;
778 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400779 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500780
Yanb98b6762008-01-08 15:54:37 -0500781 if (btrfs_test_opt(root, NODATASUM) ||
782 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500783 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500784 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500785 private = state->private;
786 ret = 0;
787 } else {
788 ret = get_state_private(io_tree, start, &private);
789 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400790 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400791 kaddr = kmap_atomic(page, KM_IRQ0);
792 if (ret) {
793 goto zeroit;
794 }
Chris Masonff79f812007-10-15 16:22:25 -0400795 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
796 btrfs_csum_final(csum, (char *)&csum);
797 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400798 goto zeroit;
799 }
800 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400801 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400802
803 /* if the io failure tree for this inode is non-empty,
804 * check to see if we've recovered from a failed IO
805 */
Chris Mason1259ab72008-05-12 13:39:03 -0400806 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400807 return 0;
808
809zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500810 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
811 page->mapping->host->i_ino, (unsigned long long)start, csum,
812 private);
Chris Masondb945352007-10-15 16:15:53 -0400813 memset(kaddr + offset, 1, end - start + 1);
814 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400815 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400816 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400817 if (private == 0)
818 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400819 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400820}
Chris Masonb888db22007-08-27 16:49:44 -0400821
Chris Mason39279cc2007-06-12 06:35:45 -0400822void btrfs_read_locked_inode(struct inode *inode)
823{
824 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400825 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400826 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400827 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400828 struct btrfs_root *root = BTRFS_I(inode)->root;
829 struct btrfs_key location;
830 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400831 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400832 int ret;
833
834 path = btrfs_alloc_path();
835 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400836 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500837
Chris Mason39279cc2007-06-12 06:35:45 -0400838 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400839 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400840 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400841
Chris Mason5f39d392007-10-15 16:14:19 -0400842 leaf = path->nodes[0];
843 inode_item = btrfs_item_ptr(leaf, path->slots[0],
844 struct btrfs_inode_item);
845
846 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
847 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
848 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
849 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -0400850 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400851
852 tspec = btrfs_inode_atime(inode_item);
853 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
854 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
855
856 tspec = btrfs_inode_mtime(inode_item);
857 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
858 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
859
860 tspec = btrfs_inode_ctime(inode_item);
861 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
862 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
863
864 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
865 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400866 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400867 rdev = btrfs_inode_rdev(leaf, inode_item);
868
869 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400870 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
871 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500872 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500873 if (!BTRFS_I(inode)->block_group) {
874 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400875 NULL, 0,
876 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500877 }
Chris Mason39279cc2007-06-12 06:35:45 -0400878 btrfs_free_path(path);
879 inode_item = NULL;
880
Chris Mason39279cc2007-06-12 06:35:45 -0400881 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400882 case S_IFREG:
883 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400884 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500885 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400886 inode->i_fop = &btrfs_file_operations;
887 inode->i_op = &btrfs_file_inode_operations;
888 break;
889 case S_IFDIR:
890 inode->i_fop = &btrfs_dir_file_operations;
891 if (root == root->fs_info->tree_root)
892 inode->i_op = &btrfs_dir_ro_inode_operations;
893 else
894 inode->i_op = &btrfs_dir_inode_operations;
895 break;
896 case S_IFLNK:
897 inode->i_op = &btrfs_symlink_inode_operations;
898 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400899 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400900 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400901 default:
902 init_special_inode(inode, inode->i_mode, rdev);
903 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400904 }
905 return;
906
907make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -0400908 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -0400909 make_bad_inode(inode);
910}
911
Chris Mason5f39d392007-10-15 16:14:19 -0400912static void fill_inode_item(struct extent_buffer *leaf,
913 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400914 struct inode *inode)
915{
Chris Mason5f39d392007-10-15 16:14:19 -0400916 btrfs_set_inode_uid(leaf, item, inode->i_uid);
917 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -0400918 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -0400919 btrfs_set_inode_mode(leaf, item, inode->i_mode);
920 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
921
922 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
923 inode->i_atime.tv_sec);
924 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
925 inode->i_atime.tv_nsec);
926
927 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
928 inode->i_mtime.tv_sec);
929 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
930 inode->i_mtime.tv_nsec);
931
932 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
933 inode->i_ctime.tv_sec);
934 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
935 inode->i_ctime.tv_nsec);
936
937 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
938 btrfs_set_inode_generation(leaf, item, inode->i_generation);
939 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500940 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400941 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400942 BTRFS_I(inode)->block_group->key.objectid);
943}
944
Chris Masonba1da2f2008-07-17 12:54:15 -0400945int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400946 struct btrfs_root *root,
947 struct inode *inode)
948{
949 struct btrfs_inode_item *inode_item;
950 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400951 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400952 int ret;
953
954 path = btrfs_alloc_path();
955 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400956 ret = btrfs_lookup_inode(trans, root, path,
957 &BTRFS_I(inode)->location, 1);
958 if (ret) {
959 if (ret > 0)
960 ret = -ENOENT;
961 goto failed;
962 }
963
Chris Mason5f39d392007-10-15 16:14:19 -0400964 leaf = path->nodes[0];
965 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400966 struct btrfs_inode_item);
967
Chris Mason5f39d392007-10-15 16:14:19 -0400968 fill_inode_item(leaf, inode_item, inode);
969 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400970 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400971 ret = 0;
972failed:
Chris Mason39279cc2007-06-12 06:35:45 -0400973 btrfs_free_path(path);
974 return ret;
975}
976
977
978static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
979 struct btrfs_root *root,
980 struct inode *dir,
981 struct dentry *dentry)
982{
983 struct btrfs_path *path;
984 const char *name = dentry->d_name.name;
985 int name_len = dentry->d_name.len;
986 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400987 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400988 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400989 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400990
991 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400992 if (!path) {
993 ret = -ENOMEM;
994 goto err;
995 }
996
Chris Mason39279cc2007-06-12 06:35:45 -0400997 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
998 name, name_len, -1);
999 if (IS_ERR(di)) {
1000 ret = PTR_ERR(di);
1001 goto err;
1002 }
1003 if (!di) {
1004 ret = -ENOENT;
1005 goto err;
1006 }
Chris Mason5f39d392007-10-15 16:14:19 -04001007 leaf = path->nodes[0];
1008 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001009 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001010 if (ret)
1011 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001012 btrfs_release_path(root, path);
1013
1014 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -04001015 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001016 if (IS_ERR(di)) {
1017 ret = PTR_ERR(di);
1018 goto err;
1019 }
1020 if (!di) {
1021 ret = -ENOENT;
1022 goto err;
1023 }
1024 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001025 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001026
1027 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -05001028 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1029 dentry->d_inode->i_ino,
1030 dentry->d_parent->d_inode->i_ino);
1031 if (ret) {
1032 printk("failed to delete reference to %.*s, "
1033 "inode %lu parent %lu\n", name_len, name,
1034 dentry->d_inode->i_ino,
1035 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -05001036 }
Chris Mason39279cc2007-06-12 06:35:45 -04001037err:
1038 btrfs_free_path(path);
1039 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001040 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001041 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001042 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001043#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1044 dentry->d_inode->i_nlink--;
1045#else
Chris Mason39279cc2007-06-12 06:35:45 -04001046 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001047#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001048 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001049 dir->i_sb->s_dirt = 1;
1050 }
1051 return ret;
1052}
1053
1054static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1055{
1056 struct btrfs_root *root;
1057 struct btrfs_trans_handle *trans;
1058 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001059 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001060
1061 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001062
1063 ret = btrfs_check_free_space(root, 1, 1);
1064 if (ret)
1065 goto fail;
1066
Chris Mason39279cc2007-06-12 06:35:45 -04001067 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001068
Chris Mason39279cc2007-06-12 06:35:45 -04001069 btrfs_set_trans_block_group(trans, dir);
1070 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001071 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001072
Chris Mason89ce8a62008-06-25 16:01:31 -04001073 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001074fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001075 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001076 return ret;
1077}
1078
1079static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1080{
1081 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001082 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001083 int ret;
1084 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001085 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001086 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001087
Chris Mason925baed2008-06-25 16:01:30 -04001088 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001089 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001090 }
Yan134d4512007-10-25 15:49:25 -04001091
Chris Mason1832a6d2007-12-21 16:27:21 -05001092 ret = btrfs_check_free_space(root, 1, 1);
1093 if (ret)
1094 goto fail;
1095
Chris Mason39279cc2007-06-12 06:35:45 -04001096 trans = btrfs_start_transaction(root, 1);
1097 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001098
1099 /* now the directory is empty */
1100 err = btrfs_unlink_trans(trans, root, dir, dentry);
1101 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001102 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001103 }
Chris Mason39544012007-12-12 14:38:19 -05001104
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001105 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001106 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001107fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001108 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001109
Chris Mason39279cc2007-06-12 06:35:45 -04001110 if (ret && !err)
1111 err = ret;
1112 return err;
1113}
1114
Chris Mason39279cc2007-06-12 06:35:45 -04001115/*
Chris Mason39279cc2007-06-12 06:35:45 -04001116 * this can truncate away extent items, csum items and directory items.
1117 * It starts at a high offset and removes keys until it can't find
1118 * any higher than i_size.
1119 *
1120 * csum items that cross the new i_size are truncated to the new size
1121 * as well.
1122 */
1123static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1124 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001125 struct inode *inode,
1126 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001127{
1128 int ret;
1129 struct btrfs_path *path;
1130 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001131 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001132 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001133 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001134 struct btrfs_file_extent_item *fi;
1135 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001136 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001137 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001138 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001139 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001140 int found_extent;
1141 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001142 int pending_del_nr = 0;
1143 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001144 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001145 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001146
Chris Mason3b951512008-04-17 11:29:12 -04001147 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001148 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001149 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001150 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001151
Chris Mason39279cc2007-06-12 06:35:45 -04001152 /* FIXME, add redo link to tree so we don't leak on crash */
1153 key.objectid = inode->i_ino;
1154 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001155 key.type = (u8)-1;
1156
Chris Mason85e21ba2008-01-29 15:11:36 -05001157 btrfs_init_path(path);
1158search_again:
1159 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1160 if (ret < 0) {
1161 goto error;
1162 }
1163 if (ret > 0) {
1164 BUG_ON(path->slots[0] == 0);
1165 path->slots[0]--;
1166 }
1167
Chris Mason39279cc2007-06-12 06:35:45 -04001168 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001169 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001170 leaf = path->nodes[0];
1171 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1172 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001173
Chris Mason5f39d392007-10-15 16:14:19 -04001174 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001175 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001176
Chris Mason85e21ba2008-01-29 15:11:36 -05001177 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001178 break;
1179
Chris Mason5f39d392007-10-15 16:14:19 -04001180 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001181 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001182 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001183 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001184 extent_type = btrfs_file_extent_type(leaf, fi);
1185 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001186 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001187 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001188 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1189 struct btrfs_item *item = btrfs_item_nr(leaf,
1190 path->slots[0]);
1191 item_end += btrfs_file_extent_inline_len(leaf,
1192 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001193 }
Yan008630c2007-11-07 13:31:09 -05001194 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001195 }
1196 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1197 ret = btrfs_csum_truncate(trans, root, path,
1198 inode->i_size);
1199 BUG_ON(ret);
1200 }
Yan008630c2007-11-07 13:31:09 -05001201 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001202 if (found_type == BTRFS_DIR_ITEM_KEY) {
1203 found_type = BTRFS_INODE_ITEM_KEY;
1204 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1205 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001206 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1207 found_type = BTRFS_XATTR_ITEM_KEY;
1208 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1209 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001210 } else if (found_type) {
1211 found_type--;
1212 } else {
1213 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001214 }
Yana61721d2007-09-17 11:08:38 -04001215 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001216 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001217 }
Chris Mason5f39d392007-10-15 16:14:19 -04001218 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001219 del_item = 1;
1220 else
1221 del_item = 0;
1222 found_extent = 0;
1223
1224 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001225 if (found_type != BTRFS_EXTENT_DATA_KEY)
1226 goto delete;
1227
1228 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001229 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001230 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001231 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001232 u64 orig_num_bytes =
1233 btrfs_file_extent_num_bytes(leaf, fi);
1234 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001235 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001236 extent_num_bytes = extent_num_bytes &
1237 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001238 btrfs_set_file_extent_num_bytes(leaf, fi,
1239 extent_num_bytes);
1240 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001241 extent_num_bytes);
1242 if (extent_start != 0)
1243 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001244 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001245 } else {
Chris Masondb945352007-10-15 16:15:53 -04001246 extent_num_bytes =
1247 btrfs_file_extent_disk_num_bytes(leaf,
1248 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001249 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001250 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001251 if (extent_start != 0) {
1252 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001253 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001254 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001255 root_gen = btrfs_header_generation(leaf);
1256 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001257 }
Chris Mason90692182008-02-08 13:49:28 -05001258 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1259 if (!del_item) {
1260 u32 newsize = inode->i_size - found_key.offset;
1261 dec_i_blocks(inode, item_end + 1 -
1262 found_key.offset - newsize);
1263 newsize =
1264 btrfs_file_extent_calc_inline_size(newsize);
1265 ret = btrfs_truncate_item(trans, root, path,
1266 newsize, 1);
1267 BUG_ON(ret);
1268 } else {
1269 dec_i_blocks(inode, item_end + 1 -
1270 found_key.offset);
1271 }
Chris Mason39279cc2007-06-12 06:35:45 -04001272 }
Chris Mason179e29e2007-11-01 11:28:41 -04001273delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001274 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001275 if (!pending_del_nr) {
1276 /* no pending yet, add ourselves */
1277 pending_del_slot = path->slots[0];
1278 pending_del_nr = 1;
1279 } else if (pending_del_nr &&
1280 path->slots[0] + 1 == pending_del_slot) {
1281 /* hop on the pending chunk */
1282 pending_del_nr++;
1283 pending_del_slot = path->slots[0];
1284 } else {
1285 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1286 }
Chris Mason39279cc2007-06-12 06:35:45 -04001287 } else {
1288 break;
1289 }
Chris Mason39279cc2007-06-12 06:35:45 -04001290 if (found_extent) {
1291 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001292 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001293 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001294 root_gen, inode->i_ino,
1295 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001296 BUG_ON(ret);
1297 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001298next:
1299 if (path->slots[0] == 0) {
1300 if (pending_del_nr)
1301 goto del_pending;
1302 btrfs_release_path(root, path);
1303 goto search_again;
1304 }
1305
1306 path->slots[0]--;
1307 if (pending_del_nr &&
1308 path->slots[0] + 1 != pending_del_slot) {
1309 struct btrfs_key debug;
1310del_pending:
1311 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1312 pending_del_slot);
1313 ret = btrfs_del_items(trans, root, path,
1314 pending_del_slot,
1315 pending_del_nr);
1316 BUG_ON(ret);
1317 pending_del_nr = 0;
1318 btrfs_release_path(root, path);
1319 goto search_again;
1320 }
Chris Mason39279cc2007-06-12 06:35:45 -04001321 }
1322 ret = 0;
1323error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001324 if (pending_del_nr) {
1325 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1326 pending_del_nr);
1327 }
Chris Mason39279cc2007-06-12 06:35:45 -04001328 btrfs_free_path(path);
1329 inode->i_sb->s_dirt = 1;
1330 return ret;
1331}
1332
Chris Masona52d9a82007-08-27 16:49:44 -04001333/*
1334 * taken from block_truncate_page, but does cow as it zeros out
1335 * any bytes left in the last page in the file.
1336 */
1337static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1338{
1339 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001340 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001341 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1342 struct btrfs_ordered_extent *ordered;
1343 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001344 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001345 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1346 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1347 struct page *page;
1348 int ret = 0;
1349 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001350 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001351
1352 if ((offset & (blocksize - 1)) == 0)
1353 goto out;
1354
1355 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001356again:
Chris Masona52d9a82007-08-27 16:49:44 -04001357 page = grab_cache_page(mapping, index);
1358 if (!page)
1359 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001360
1361 page_start = page_offset(page);
1362 page_end = page_start + PAGE_CACHE_SIZE - 1;
1363
Chris Masona52d9a82007-08-27 16:49:44 -04001364 if (!PageUptodate(page)) {
1365 ret = btrfs_readpage(NULL, page);
1366 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001367 if (page->mapping != mapping) {
1368 unlock_page(page);
1369 page_cache_release(page);
1370 goto again;
1371 }
Chris Masona52d9a82007-08-27 16:49:44 -04001372 if (!PageUptodate(page)) {
1373 ret = -EIO;
1374 goto out;
1375 }
1376 }
Chris Mason211c17f2008-05-15 09:13:45 -04001377 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001378
1379 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1380 set_page_extent_mapped(page);
1381
1382 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1383 if (ordered) {
1384 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1385 unlock_page(page);
1386 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001387 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001388 btrfs_put_ordered_extent(ordered);
1389 goto again;
1390 }
1391
1392 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1393 page_end, GFP_NOFS);
1394 ret = 0;
1395 if (offset != PAGE_CACHE_SIZE) {
1396 kaddr = kmap(page);
1397 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1398 flush_dcache_page(page);
1399 kunmap(page);
1400 }
Chris Mason247e7432008-07-17 12:53:51 -04001401 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001402 set_page_dirty(page);
1403 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001404
Chris Mason39279cc2007-06-12 06:35:45 -04001405 unlock_page(page);
1406 page_cache_release(page);
1407out:
1408 return ret;
1409}
1410
1411static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1412{
1413 struct inode *inode = dentry->d_inode;
1414 int err;
1415
1416 err = inode_change_ok(inode, attr);
1417 if (err)
1418 return err;
1419
1420 if (S_ISREG(inode->i_mode) &&
1421 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1422 struct btrfs_trans_handle *trans;
1423 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001424 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001425
Chris Mason5f39d392007-10-15 16:14:19 -04001426 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001427 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001428 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001429 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001430 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001431
Chris Mason1b0f7c22008-01-30 14:33:02 -05001432 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001433 goto out;
1434
Chris Mason1832a6d2007-12-21 16:27:21 -05001435 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001436 if (err)
1437 goto fail;
1438
Chris Mason39279cc2007-06-12 06:35:45 -04001439 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1440
Chris Mason5f564062008-01-22 16:47:59 -05001441 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001442 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1443 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001444
Chris Mason39279cc2007-06-12 06:35:45 -04001445 trans = btrfs_start_transaction(root, 1);
1446 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001447 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001448 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001449 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001450 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001451
Chris Mason179e29e2007-11-01 11:28:41 -04001452 if (alloc_hint != EXTENT_MAP_INLINE) {
1453 err = btrfs_insert_file_extent(trans, root,
1454 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001455 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001456 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001457 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001458 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001459 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001460 }
Chris Masonee6e6502008-07-17 12:54:40 -04001461 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001462 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001463 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001464 if (err)
1465 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001466 }
1467out:
1468 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001469fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001470 return err;
1471}
Chris Mason61295eb2008-01-14 16:24:38 -05001472
Chris Mason39279cc2007-06-12 06:35:45 -04001473void btrfs_delete_inode(struct inode *inode)
1474{
1475 struct btrfs_trans_handle *trans;
1476 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001477 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001478 int ret;
1479
1480 truncate_inode_pages(&inode->i_data, 0);
1481 if (is_bad_inode(inode)) {
1482 goto no_delete;
1483 }
Chris Mason4a096752008-07-21 10:29:44 -04001484 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001485
Chris Masondbe674a2008-07-17 12:54:05 -04001486 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001487 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001488
Chris Mason39279cc2007-06-12 06:35:45 -04001489 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001490 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001491 if (ret)
1492 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001493
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001494 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001495 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001496
Chris Mason39279cc2007-06-12 06:35:45 -04001497 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001498 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001499 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001500
1501no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001502 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001503 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001504 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001505no_delete:
1506 clear_inode(inode);
1507}
1508
1509/*
1510 * this returns the key found in the dir entry in the location pointer.
1511 * If no dir entries were found, location->objectid is 0.
1512 */
1513static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1514 struct btrfs_key *location)
1515{
1516 const char *name = dentry->d_name.name;
1517 int namelen = dentry->d_name.len;
1518 struct btrfs_dir_item *di;
1519 struct btrfs_path *path;
1520 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001521 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001522
Chris Mason39544012007-12-12 14:38:19 -05001523 if (namelen == 1 && strcmp(name, ".") == 0) {
1524 location->objectid = dir->i_ino;
1525 location->type = BTRFS_INODE_ITEM_KEY;
1526 location->offset = 0;
1527 return 0;
1528 }
Chris Mason39279cc2007-06-12 06:35:45 -04001529 path = btrfs_alloc_path();
1530 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001531
Chris Mason7a720532007-12-13 09:06:59 -05001532 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001533 struct btrfs_key key;
1534 struct extent_buffer *leaf;
1535 u32 nritems;
1536 int slot;
1537
1538 key.objectid = dir->i_ino;
1539 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1540 key.offset = 0;
1541 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1542 BUG_ON(ret == 0);
1543 ret = 0;
1544
1545 leaf = path->nodes[0];
1546 slot = path->slots[0];
1547 nritems = btrfs_header_nritems(leaf);
1548 if (slot >= nritems)
1549 goto out_err;
1550
1551 btrfs_item_key_to_cpu(leaf, &key, slot);
1552 if (key.objectid != dir->i_ino ||
1553 key.type != BTRFS_INODE_REF_KEY) {
1554 goto out_err;
1555 }
1556 location->objectid = key.offset;
1557 location->type = BTRFS_INODE_ITEM_KEY;
1558 location->offset = 0;
1559 goto out;
1560 }
1561
Chris Mason39279cc2007-06-12 06:35:45 -04001562 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1563 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001564 if (IS_ERR(di))
1565 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001566 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001567 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001568 }
Chris Mason5f39d392007-10-15 16:14:19 -04001569 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001570out:
Chris Mason39279cc2007-06-12 06:35:45 -04001571 btrfs_free_path(path);
1572 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001573out_err:
1574 location->objectid = 0;
1575 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001576}
1577
1578/*
1579 * when we hit a tree root in a directory, the btrfs part of the inode
1580 * needs to be changed to reflect the root directory of the tree root. This
1581 * is kind of like crossing a mount point.
1582 */
1583static int fixup_tree_root_location(struct btrfs_root *root,
1584 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001585 struct btrfs_root **sub_root,
1586 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001587{
1588 struct btrfs_path *path;
1589 struct btrfs_root_item *ri;
1590
1591 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1592 return 0;
1593 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1594 return 0;
1595
1596 path = btrfs_alloc_path();
1597 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001598
Josef Bacik58176a92007-08-29 15:47:34 -04001599 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1600 dentry->d_name.name,
1601 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001602 if (IS_ERR(*sub_root))
1603 return PTR_ERR(*sub_root);
1604
1605 ri = &(*sub_root)->root_item;
1606 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001607 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1608 location->offset = 0;
1609
1610 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001611 return 0;
1612}
1613
1614static int btrfs_init_locked_inode(struct inode *inode, void *p)
1615{
1616 struct btrfs_iget_args *args = p;
1617 inode->i_ino = args->ino;
1618 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001619 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001620 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001621 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1622 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001623 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001624 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1625 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001626 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001627 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001628 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001629 return 0;
1630}
1631
1632static int btrfs_find_actor(struct inode *inode, void *opaque)
1633{
1634 struct btrfs_iget_args *args = opaque;
1635 return (args->ino == inode->i_ino &&
1636 args->root == BTRFS_I(inode)->root);
1637}
1638
Chris Masondc17ff82008-01-08 15:46:30 -05001639struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1640 u64 root_objectid)
1641{
1642 struct btrfs_iget_args args;
1643 args.ino = objectid;
1644 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1645
1646 if (!args.root)
1647 return NULL;
1648
1649 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1650}
1651
Chris Mason39279cc2007-06-12 06:35:45 -04001652struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1653 struct btrfs_root *root)
1654{
1655 struct inode *inode;
1656 struct btrfs_iget_args args;
1657 args.ino = objectid;
1658 args.root = root;
1659
1660 inode = iget5_locked(s, objectid, btrfs_find_actor,
1661 btrfs_init_locked_inode,
1662 (void *)&args);
1663 return inode;
1664}
1665
1666static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1667 struct nameidata *nd)
1668{
1669 struct inode * inode;
1670 struct btrfs_inode *bi = BTRFS_I(dir);
1671 struct btrfs_root *root = bi->root;
1672 struct btrfs_root *sub_root = root;
1673 struct btrfs_key location;
1674 int ret;
1675
1676 if (dentry->d_name.len > BTRFS_NAME_LEN)
1677 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001678
Chris Mason39279cc2007-06-12 06:35:45 -04001679 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001680
Chris Mason39279cc2007-06-12 06:35:45 -04001681 if (ret < 0)
1682 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001683
Chris Mason39279cc2007-06-12 06:35:45 -04001684 inode = NULL;
1685 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001686 ret = fixup_tree_root_location(root, &location, &sub_root,
1687 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001688 if (ret < 0)
1689 return ERR_PTR(ret);
1690 if (ret > 0)
1691 return ERR_PTR(-ENOENT);
1692 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1693 sub_root);
1694 if (!inode)
1695 return ERR_PTR(-EACCES);
1696 if (inode->i_state & I_NEW) {
1697 /* the inode and parent dir are two different roots */
1698 if (sub_root != root) {
1699 igrab(inode);
1700 sub_root->inode = inode;
1701 }
1702 BTRFS_I(inode)->root = sub_root;
1703 memcpy(&BTRFS_I(inode)->location, &location,
1704 sizeof(location));
1705 btrfs_read_locked_inode(inode);
1706 unlock_new_inode(inode);
1707 }
1708 }
1709 return d_splice_alias(inode, dentry);
1710}
1711
Chris Mason39279cc2007-06-12 06:35:45 -04001712static unsigned char btrfs_filetype_table[] = {
1713 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1714};
1715
1716static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1717{
Chris Mason6da6aba2007-12-18 16:15:09 -05001718 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001719 struct btrfs_root *root = BTRFS_I(inode)->root;
1720 struct btrfs_item *item;
1721 struct btrfs_dir_item *di;
1722 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001723 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001724 struct btrfs_path *path;
1725 int ret;
1726 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001727 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001728 int slot;
1729 int advance;
1730 unsigned char d_type;
1731 int over = 0;
1732 u32 di_cur;
1733 u32 di_total;
1734 u32 di_len;
1735 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001736 char tmp_name[32];
1737 char *name_ptr;
1738 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001739
1740 /* FIXME, use a real flag for deciding about the key type */
1741 if (root->fs_info->tree_root == root)
1742 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001743
Chris Mason39544012007-12-12 14:38:19 -05001744 /* special case for "." */
1745 if (filp->f_pos == 0) {
1746 over = filldir(dirent, ".", 1,
1747 1, inode->i_ino,
1748 DT_DIR);
1749 if (over)
1750 return 0;
1751 filp->f_pos = 1;
1752 }
1753
Chris Mason39279cc2007-06-12 06:35:45 -04001754 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001755 path = btrfs_alloc_path();
1756 path->reada = 2;
1757
1758 /* special case for .., just use the back ref */
1759 if (filp->f_pos == 1) {
1760 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1761 key.offset = 0;
1762 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1763 BUG_ON(ret == 0);
1764 leaf = path->nodes[0];
1765 slot = path->slots[0];
1766 nritems = btrfs_header_nritems(leaf);
1767 if (slot >= nritems) {
1768 btrfs_release_path(root, path);
1769 goto read_dir_items;
1770 }
1771 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1772 btrfs_release_path(root, path);
1773 if (found_key.objectid != key.objectid ||
1774 found_key.type != BTRFS_INODE_REF_KEY)
1775 goto read_dir_items;
1776 over = filldir(dirent, "..", 2,
1777 2, found_key.offset, DT_DIR);
1778 if (over)
1779 goto nopos;
1780 filp->f_pos = 2;
1781 }
1782
1783read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001784 btrfs_set_key_type(&key, key_type);
1785 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001786
Chris Mason39279cc2007-06-12 06:35:45 -04001787 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1788 if (ret < 0)
1789 goto err;
1790 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001791 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001792 leaf = path->nodes[0];
1793 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001794 slot = path->slots[0];
1795 if (advance || slot >= nritems) {
1796 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001797 ret = btrfs_next_leaf(root, path);
1798 if (ret)
1799 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001800 leaf = path->nodes[0];
1801 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001802 slot = path->slots[0];
1803 } else {
1804 slot++;
1805 path->slots[0]++;
1806 }
1807 }
1808 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001809 item = btrfs_item_nr(leaf, slot);
1810 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1811
1812 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001813 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001814 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001815 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001816 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001817 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001818
1819 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001820 advance = 1;
1821 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1822 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001823 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001824 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001825 struct btrfs_key location;
1826
1827 name_len = btrfs_dir_name_len(leaf, di);
1828 if (name_len < 32) {
1829 name_ptr = tmp_name;
1830 } else {
1831 name_ptr = kmalloc(name_len, GFP_NOFS);
1832 BUG_ON(!name_ptr);
1833 }
1834 read_extent_buffer(leaf, name_ptr,
1835 (unsigned long)(di + 1), name_len);
1836
1837 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1838 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001839 over = filldir(dirent, name_ptr, name_len,
1840 found_key.offset,
1841 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001842 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001843
1844 if (name_ptr != tmp_name)
1845 kfree(name_ptr);
1846
Chris Mason39279cc2007-06-12 06:35:45 -04001847 if (over)
1848 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001849 di_len = btrfs_dir_name_len(leaf, di) +
1850 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001851 di_cur += di_len;
1852 di = (struct btrfs_dir_item *)((char *)di + di_len);
1853 }
1854 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001855 if (key_type == BTRFS_DIR_INDEX_KEY)
1856 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1857 else
1858 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001859nopos:
1860 ret = 0;
1861err:
Chris Mason39279cc2007-06-12 06:35:45 -04001862 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001863 return ret;
1864}
1865
1866int btrfs_write_inode(struct inode *inode, int wait)
1867{
1868 struct btrfs_root *root = BTRFS_I(inode)->root;
1869 struct btrfs_trans_handle *trans;
1870 int ret = 0;
1871
1872 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04001873 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001874 btrfs_set_trans_block_group(trans, inode);
1875 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001876 }
1877 return ret;
1878}
1879
1880/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001881 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001882 * inode changes. But, it is most likely to find the inode in cache.
1883 * FIXME, needs more benchmarking...there are no reasons other than performance
1884 * to keep or drop this code.
1885 */
1886void btrfs_dirty_inode(struct inode *inode)
1887{
1888 struct btrfs_root *root = BTRFS_I(inode)->root;
1889 struct btrfs_trans_handle *trans;
1890
Chris Masonf9295742008-07-17 12:54:14 -04001891 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001892 btrfs_set_trans_block_group(trans, inode);
1893 btrfs_update_inode(trans, root, inode);
1894 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001895}
1896
1897static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1898 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001899 const char *name, int name_len,
1900 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001901 u64 objectid,
1902 struct btrfs_block_group_cache *group,
1903 int mode)
1904{
1905 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001906 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001907 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001908 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001909 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001910 struct btrfs_inode_ref *ref;
1911 struct btrfs_key key[2];
1912 u32 sizes[2];
1913 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001914 int ret;
1915 int owner;
1916
Chris Mason5f39d392007-10-15 16:14:19 -04001917 path = btrfs_alloc_path();
1918 BUG_ON(!path);
1919
Chris Mason39279cc2007-06-12 06:35:45 -04001920 inode = new_inode(root->fs_info->sb);
1921 if (!inode)
1922 return ERR_PTR(-ENOMEM);
1923
Chris Masond1310b22008-01-24 16:13:08 -05001924 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1925 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001926 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001927 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1928 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001929 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001930 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001931 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05001932 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001933 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001934 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001935
Chris Mason39279cc2007-06-12 06:35:45 -04001936 if (mode & S_IFDIR)
1937 owner = 0;
1938 else
1939 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001940 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001941 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001942 if (!new_inode_group) {
1943 printk("find_block group failed\n");
1944 new_inode_group = group;
1945 }
1946 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001947 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001948
1949 key[0].objectid = objectid;
1950 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1951 key[0].offset = 0;
1952
1953 key[1].objectid = objectid;
1954 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1955 key[1].offset = ref_objectid;
1956
1957 sizes[0] = sizeof(struct btrfs_inode_item);
1958 sizes[1] = name_len + sizeof(*ref);
1959
1960 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1961 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001962 goto fail;
1963
Chris Mason9c583092008-01-29 15:15:18 -05001964 if (objectid > root->highest_inode)
1965 root->highest_inode = objectid;
1966
Chris Mason39279cc2007-06-12 06:35:45 -04001967 inode->i_uid = current->fsuid;
1968 inode->i_gid = current->fsgid;
1969 inode->i_mode = mode;
1970 inode->i_ino = objectid;
1971 inode->i_blocks = 0;
1972 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001973 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1974 struct btrfs_inode_item);
1975 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001976
1977 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1978 struct btrfs_inode_ref);
1979 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1980 ptr = (unsigned long)(ref + 1);
1981 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1982
Chris Mason5f39d392007-10-15 16:14:19 -04001983 btrfs_mark_buffer_dirty(path->nodes[0]);
1984 btrfs_free_path(path);
1985
Chris Mason39279cc2007-06-12 06:35:45 -04001986 location = &BTRFS_I(inode)->location;
1987 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001988 location->offset = 0;
1989 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1990
Chris Mason39279cc2007-06-12 06:35:45 -04001991 insert_inode_hash(inode);
1992 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001993fail:
1994 btrfs_free_path(path);
1995 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001996}
1997
1998static inline u8 btrfs_inode_type(struct inode *inode)
1999{
2000 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2001}
2002
2003static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002004 struct dentry *dentry, struct inode *inode,
2005 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002006{
2007 int ret;
2008 struct btrfs_key key;
2009 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04002010 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002011
Chris Mason39279cc2007-06-12 06:35:45 -04002012 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002013 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2014 key.offset = 0;
2015
2016 ret = btrfs_insert_dir_item(trans, root,
2017 dentry->d_name.name, dentry->d_name.len,
2018 dentry->d_parent->d_inode->i_ino,
2019 &key, btrfs_inode_type(inode));
2020 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002021 if (add_backref) {
2022 ret = btrfs_insert_inode_ref(trans, root,
2023 dentry->d_name.name,
2024 dentry->d_name.len,
2025 inode->i_ino,
2026 dentry->d_parent->d_inode->i_ino);
2027 }
Chris Mason79c44582007-06-25 10:09:33 -04002028 parent_inode = dentry->d_parent->d_inode;
Chris Masondbe674a2008-07-17 12:54:05 -04002029 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2030 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002031 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002032 ret = btrfs_update_inode(trans, root,
2033 dentry->d_parent->d_inode);
2034 }
2035 return ret;
2036}
2037
2038static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002039 struct dentry *dentry, struct inode *inode,
2040 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002041{
Chris Mason9c583092008-01-29 15:15:18 -05002042 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04002043 if (!err) {
2044 d_instantiate(dentry, inode);
2045 return 0;
2046 }
2047 if (err > 0)
2048 err = -EEXIST;
2049 return err;
2050}
2051
Josef Bacik618e21d2007-07-11 10:18:17 -04002052static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2053 int mode, dev_t rdev)
2054{
2055 struct btrfs_trans_handle *trans;
2056 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002057 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002058 int err;
2059 int drop_inode = 0;
2060 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002061 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002062
2063 if (!new_valid_dev(rdev))
2064 return -EINVAL;
2065
Chris Mason1832a6d2007-12-21 16:27:21 -05002066 err = btrfs_check_free_space(root, 1, 0);
2067 if (err)
2068 goto fail;
2069
Josef Bacik618e21d2007-07-11 10:18:17 -04002070 trans = btrfs_start_transaction(root, 1);
2071 btrfs_set_trans_block_group(trans, dir);
2072
2073 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2074 if (err) {
2075 err = -ENOSPC;
2076 goto out_unlock;
2077 }
2078
Chris Mason9c583092008-01-29 15:15:18 -05002079 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2080 dentry->d_name.len,
2081 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002082 BTRFS_I(dir)->block_group, mode);
2083 err = PTR_ERR(inode);
2084 if (IS_ERR(inode))
2085 goto out_unlock;
2086
2087 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002088 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002089 if (err)
2090 drop_inode = 1;
2091 else {
2092 inode->i_op = &btrfs_special_inode_operations;
2093 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002094 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002095 }
2096 dir->i_sb->s_dirt = 1;
2097 btrfs_update_inode_block_group(trans, inode);
2098 btrfs_update_inode_block_group(trans, dir);
2099out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002100 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002101 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002102fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002103 if (drop_inode) {
2104 inode_dec_link_count(inode);
2105 iput(inode);
2106 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002107 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002108 return err;
2109}
2110
Chris Mason39279cc2007-06-12 06:35:45 -04002111static int btrfs_create(struct inode *dir, struct dentry *dentry,
2112 int mode, struct nameidata *nd)
2113{
2114 struct btrfs_trans_handle *trans;
2115 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002116 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002117 int err;
2118 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002119 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002120 u64 objectid;
2121
Chris Mason1832a6d2007-12-21 16:27:21 -05002122 err = btrfs_check_free_space(root, 1, 0);
2123 if (err)
2124 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002125 trans = btrfs_start_transaction(root, 1);
2126 btrfs_set_trans_block_group(trans, dir);
2127
2128 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2129 if (err) {
2130 err = -ENOSPC;
2131 goto out_unlock;
2132 }
2133
Chris Mason9c583092008-01-29 15:15:18 -05002134 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2135 dentry->d_name.len,
2136 dentry->d_parent->d_inode->i_ino,
2137 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002138 err = PTR_ERR(inode);
2139 if (IS_ERR(inode))
2140 goto out_unlock;
2141
2142 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002143 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002144 if (err)
2145 drop_inode = 1;
2146 else {
2147 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002148 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002149 inode->i_fop = &btrfs_file_operations;
2150 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002151 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2152 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002153 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002154 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2155 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002156 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002157 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002158 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002159 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002160 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002161 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002162 }
2163 dir->i_sb->s_dirt = 1;
2164 btrfs_update_inode_block_group(trans, inode);
2165 btrfs_update_inode_block_group(trans, dir);
2166out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002167 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002168 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002169fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002170 if (drop_inode) {
2171 inode_dec_link_count(inode);
2172 iput(inode);
2173 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002174 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002175 return err;
2176}
2177
2178static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2179 struct dentry *dentry)
2180{
2181 struct btrfs_trans_handle *trans;
2182 struct btrfs_root *root = BTRFS_I(dir)->root;
2183 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002184 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002185 int err;
2186 int drop_inode = 0;
2187
2188 if (inode->i_nlink == 0)
2189 return -ENOENT;
2190
Chris Mason6da6aba2007-12-18 16:15:09 -05002191#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2192 inode->i_nlink++;
2193#else
Chris Mason39279cc2007-06-12 06:35:45 -04002194 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002195#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002196 err = btrfs_check_free_space(root, 1, 0);
2197 if (err)
2198 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002199 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002200
Chris Mason39279cc2007-06-12 06:35:45 -04002201 btrfs_set_trans_block_group(trans, dir);
2202 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05002203 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002204
Chris Mason39279cc2007-06-12 06:35:45 -04002205 if (err)
2206 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002207
Chris Mason39279cc2007-06-12 06:35:45 -04002208 dir->i_sb->s_dirt = 1;
2209 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002210 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002211
Chris Mason54aa1f42007-06-22 14:16:25 -04002212 if (err)
2213 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002214
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002215 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002216 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002217fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002218 if (drop_inode) {
2219 inode_dec_link_count(inode);
2220 iput(inode);
2221 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002222 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002223 return err;
2224}
2225
Chris Mason39279cc2007-06-12 06:35:45 -04002226static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2227{
Chris Masonb9d86662008-05-02 16:13:49 -04002228 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002229 struct btrfs_trans_handle *trans;
2230 struct btrfs_root *root = BTRFS_I(dir)->root;
2231 int err = 0;
2232 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002233 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002234 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002235
Chris Mason1832a6d2007-12-21 16:27:21 -05002236 err = btrfs_check_free_space(root, 1, 0);
2237 if (err)
2238 goto out_unlock;
2239
Chris Mason39279cc2007-06-12 06:35:45 -04002240 trans = btrfs_start_transaction(root, 1);
2241 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002242
Chris Mason39279cc2007-06-12 06:35:45 -04002243 if (IS_ERR(trans)) {
2244 err = PTR_ERR(trans);
2245 goto out_unlock;
2246 }
2247
2248 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2249 if (err) {
2250 err = -ENOSPC;
2251 goto out_unlock;
2252 }
2253
Chris Mason9c583092008-01-29 15:15:18 -05002254 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2255 dentry->d_name.len,
2256 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002257 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2258 if (IS_ERR(inode)) {
2259 err = PTR_ERR(inode);
2260 goto out_fail;
2261 }
Chris Mason5f39d392007-10-15 16:14:19 -04002262
Chris Mason39279cc2007-06-12 06:35:45 -04002263 drop_on_err = 1;
2264 inode->i_op = &btrfs_dir_inode_operations;
2265 inode->i_fop = &btrfs_dir_file_operations;
2266 btrfs_set_trans_block_group(trans, inode);
2267
Chris Masondbe674a2008-07-17 12:54:05 -04002268 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002269 err = btrfs_update_inode(trans, root, inode);
2270 if (err)
2271 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002272
Chris Mason9c583092008-01-29 15:15:18 -05002273 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002274 if (err)
2275 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002276
Chris Mason39279cc2007-06-12 06:35:45 -04002277 d_instantiate(dentry, inode);
2278 drop_on_err = 0;
2279 dir->i_sb->s_dirt = 1;
2280 btrfs_update_inode_block_group(trans, inode);
2281 btrfs_update_inode_block_group(trans, dir);
2282
2283out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002284 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002285 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002286
Chris Mason39279cc2007-06-12 06:35:45 -04002287out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002288 if (drop_on_err)
2289 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002290 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002291 return err;
2292}
2293
Chris Mason3b951512008-04-17 11:29:12 -04002294static int merge_extent_mapping(struct extent_map_tree *em_tree,
2295 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002296 struct extent_map *em,
2297 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002298{
2299 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002300
Chris Masone6dcd2d2008-07-17 12:53:50 -04002301 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2302 start_diff = map_start - em->start;
2303 em->start = map_start;
2304 em->len = map_len;
2305 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2306 em->block_start += start_diff;
2307 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002308}
2309
Chris Masona52d9a82007-08-27 16:49:44 -04002310struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002311 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002312 int create)
2313{
2314 int ret;
2315 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002316 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002317 u64 extent_start = 0;
2318 u64 extent_end = 0;
2319 u64 objectid = inode->i_ino;
2320 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002321 struct btrfs_path *path;
2322 struct btrfs_root *root = BTRFS_I(inode)->root;
2323 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002324 struct extent_buffer *leaf;
2325 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002326 struct extent_map *em = NULL;
2327 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002328 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002329 struct btrfs_trans_handle *trans = NULL;
2330
2331 path = btrfs_alloc_path();
2332 BUG_ON(!path);
Chris Masona52d9a82007-08-27 16:49:44 -04002333
2334again:
Chris Masond1310b22008-01-24 16:13:08 -05002335 spin_lock(&em_tree->lock);
2336 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002337 if (em)
2338 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002339 spin_unlock(&em_tree->lock);
2340
Chris Masona52d9a82007-08-27 16:49:44 -04002341 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002342 if (em->start > start || em->start + em->len <= start)
2343 free_extent_map(em);
2344 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002345 free_extent_map(em);
2346 else
2347 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002348 }
Chris Masond1310b22008-01-24 16:13:08 -05002349 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002350 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002351 err = -ENOMEM;
2352 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002353 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002354 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002355 em->start = EXTENT_MAP_HOLE;
2356 em->len = (u64)-1;
Chris Mason179e29e2007-11-01 11:28:41 -04002357 ret = btrfs_lookup_file_extent(trans, root, path,
2358 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002359 if (ret < 0) {
2360 err = ret;
2361 goto out;
2362 }
2363
2364 if (ret != 0) {
2365 if (path->slots[0] == 0)
2366 goto not_found;
2367 path->slots[0]--;
2368 }
2369
Chris Mason5f39d392007-10-15 16:14:19 -04002370 leaf = path->nodes[0];
2371 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002372 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002373 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002374 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2375 found_type = btrfs_key_type(&found_key);
2376 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002377 found_type != BTRFS_EXTENT_DATA_KEY) {
2378 goto not_found;
2379 }
2380
Chris Mason5f39d392007-10-15 16:14:19 -04002381 found_type = btrfs_file_extent_type(leaf, item);
2382 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002383 if (found_type == BTRFS_FILE_EXTENT_REG) {
2384 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002385 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002386 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002387 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002388 em->start = start;
2389 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002390 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002391 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002392 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002393 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002394 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002395 }
2396 goto not_found_em;
2397 }
Chris Masondb945352007-10-15 16:15:53 -04002398 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2399 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002400 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002401 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002402 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002403 goto insert;
2404 }
Chris Masondb945352007-10-15 16:15:53 -04002405 bytenr += btrfs_file_extent_offset(leaf, item);
2406 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002407 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002408 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002409 goto insert;
2410 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002411 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002412 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002413 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002414 size_t size;
2415 size_t extent_offset;
2416 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002417
Chris Mason5f39d392007-10-15 16:14:19 -04002418 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2419 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002420 extent_end = (extent_start + size + root->sectorsize - 1) &
2421 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002422 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002423 em->start = start;
2424 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002425 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002426 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002427 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002428 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002429 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002430 }
2431 goto not_found_em;
2432 }
2433 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002434
2435 if (!page) {
2436 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002437 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002438 goto out;
2439 }
2440
Chris Mason70dec802008-01-29 09:59:12 -05002441 page_start = page_offset(page) + pg_offset;
2442 extent_offset = page_start - extent_start;
2443 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002444 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002445 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002446 em->len = (copy_size + root->sectorsize - 1) &
2447 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002448 map = kmap(page);
2449 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002450 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002451 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002452 copy_size);
2453 flush_dcache_page(page);
2454 } else if (create && PageUptodate(page)) {
2455 if (!trans) {
2456 kunmap(page);
2457 free_extent_map(em);
2458 em = NULL;
2459 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002460 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002461 goto again;
2462 }
Chris Mason70dec802008-01-29 09:59:12 -05002463 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002464 copy_size);
2465 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002466 }
Chris Masona52d9a82007-08-27 16:49:44 -04002467 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002468 set_extent_uptodate(io_tree, em->start,
2469 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002470 goto insert;
2471 } else {
2472 printk("unkknown found_type %d\n", found_type);
2473 WARN_ON(1);
2474 }
2475not_found:
2476 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002477 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002478not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002479 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002480insert:
2481 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002482 if (em->start > start || extent_map_end(em) <= start) {
2483 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002484 err = -EIO;
2485 goto out;
2486 }
Chris Masond1310b22008-01-24 16:13:08 -05002487
2488 err = 0;
2489 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002490 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002491 /* it is possible that someone inserted the extent into the tree
2492 * while we had the lock dropped. It is also possible that
2493 * an overlapping map exists in the tree
2494 */
Chris Masona52d9a82007-08-27 16:49:44 -04002495 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002496 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002497
2498 ret = 0;
2499
Chris Mason3b951512008-04-17 11:29:12 -04002500 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002501 if (existing && (existing->start > start ||
2502 existing->start + existing->len <= start)) {
2503 free_extent_map(existing);
2504 existing = NULL;
2505 }
Chris Mason3b951512008-04-17 11:29:12 -04002506 if (!existing) {
2507 existing = lookup_extent_mapping(em_tree, em->start,
2508 em->len);
2509 if (existing) {
2510 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002511 em, start,
2512 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002513 free_extent_map(existing);
2514 if (err) {
2515 free_extent_map(em);
2516 em = NULL;
2517 }
2518 } else {
2519 err = -EIO;
2520 printk("failing to insert %Lu %Lu\n",
2521 start, len);
2522 free_extent_map(em);
2523 em = NULL;
2524 }
2525 } else {
2526 free_extent_map(em);
2527 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002528 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002529 }
Chris Masona52d9a82007-08-27 16:49:44 -04002530 }
Chris Masond1310b22008-01-24 16:13:08 -05002531 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002532out:
2533 btrfs_free_path(path);
2534 if (trans) {
2535 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002536 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002537 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002538 }
Chris Masona52d9a82007-08-27 16:49:44 -04002539 }
Chris Masona52d9a82007-08-27 16:49:44 -04002540 if (err) {
2541 free_extent_map(em);
2542 WARN_ON(1);
2543 return ERR_PTR(err);
2544 }
2545 return em;
2546}
2547
Chris Masone1c4b742008-04-22 13:26:46 -04002548#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002549static int btrfs_get_block(struct inode *inode, sector_t iblock,
2550 struct buffer_head *bh_result, int create)
2551{
2552 struct extent_map *em;
2553 u64 start = (u64)iblock << inode->i_blkbits;
2554 struct btrfs_multi_bio *multi = NULL;
2555 struct btrfs_root *root = BTRFS_I(inode)->root;
2556 u64 len;
2557 u64 logical;
2558 u64 map_length;
2559 int ret = 0;
2560
2561 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2562
2563 if (!em || IS_ERR(em))
2564 goto out;
2565
Chris Masone1c4b742008-04-22 13:26:46 -04002566 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002567 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002568 }
Chris Mason16432982008-04-10 10:23:21 -04002569
2570 if (em->block_start == EXTENT_MAP_INLINE) {
2571 ret = -EINVAL;
2572 goto out;
2573 }
2574
Chris Mason16432982008-04-10 10:23:21 -04002575 len = em->start + em->len - start;
2576 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2577
Chris Masone1c4b742008-04-22 13:26:46 -04002578 if (em->block_start == EXTENT_MAP_HOLE ||
2579 em->block_start == EXTENT_MAP_DELALLOC) {
2580 bh_result->b_size = len;
2581 goto out;
2582 }
2583
Chris Mason16432982008-04-10 10:23:21 -04002584 logical = start - em->start;
2585 logical = em->block_start + logical;
2586
2587 map_length = len;
2588 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2589 logical, &map_length, &multi, 0);
2590 BUG_ON(ret);
2591 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2592 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002593
Chris Mason16432982008-04-10 10:23:21 -04002594 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2595 set_buffer_mapped(bh_result);
2596 kfree(multi);
2597out:
2598 free_extent_map(em);
2599 return ret;
2600}
Chris Masone1c4b742008-04-22 13:26:46 -04002601#endif
Chris Mason16432982008-04-10 10:23:21 -04002602
2603static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2604 const struct iovec *iov, loff_t offset,
2605 unsigned long nr_segs)
2606{
Chris Masone1c4b742008-04-22 13:26:46 -04002607 return -EINVAL;
2608#if 0
Chris Mason16432982008-04-10 10:23:21 -04002609 struct file *file = iocb->ki_filp;
2610 struct inode *inode = file->f_mapping->host;
2611
2612 if (rw == WRITE)
2613 return -EINVAL;
2614
2615 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2616 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002617#endif
Chris Mason16432982008-04-10 10:23:21 -04002618}
2619
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002620static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002621{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002622 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002623}
2624
Chris Mason9ebefb182007-06-15 13:50:00 -04002625int btrfs_readpage(struct file *file, struct page *page)
2626{
Chris Masond1310b22008-01-24 16:13:08 -05002627 struct extent_io_tree *tree;
2628 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002629 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002630}
Chris Mason1832a6d2007-12-21 16:27:21 -05002631
Chris Mason39279cc2007-06-12 06:35:45 -04002632static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2633{
Chris Masond1310b22008-01-24 16:13:08 -05002634 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002635
2636
2637 if (current->flags & PF_MEMALLOC) {
2638 redirty_page_for_writepage(wbc, page);
2639 unlock_page(page);
2640 return 0;
2641 }
Chris Masond1310b22008-01-24 16:13:08 -05002642 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002643 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2644}
Chris Mason39279cc2007-06-12 06:35:45 -04002645
Chris Masonb293f022007-11-01 19:45:34 -04002646static int btrfs_writepages(struct address_space *mapping,
2647 struct writeback_control *wbc)
2648{
Chris Masond1310b22008-01-24 16:13:08 -05002649 struct extent_io_tree *tree;
2650 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002651 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2652}
2653
Chris Mason3ab2fb52007-11-08 10:59:22 -05002654static int
2655btrfs_readpages(struct file *file, struct address_space *mapping,
2656 struct list_head *pages, unsigned nr_pages)
2657{
Chris Masond1310b22008-01-24 16:13:08 -05002658 struct extent_io_tree *tree;
2659 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002660 return extent_readpages(tree, mapping, pages, nr_pages,
2661 btrfs_get_extent);
2662}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002663static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002664{
Chris Masond1310b22008-01-24 16:13:08 -05002665 struct extent_io_tree *tree;
2666 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002667 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002668
Chris Masond1310b22008-01-24 16:13:08 -05002669 tree = &BTRFS_I(page->mapping->host)->io_tree;
2670 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002671 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002672 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002673 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002674 ClearPagePrivate(page);
2675 set_page_private(page, 0);
2676 page_cache_release(page);
2677 }
2678 return ret;
2679}
Chris Mason39279cc2007-06-12 06:35:45 -04002680
Chris Masone6dcd2d2008-07-17 12:53:50 -04002681static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2682{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002683 return __btrfs_releasepage(page, gfp_flags);
2684}
2685
Chris Masona52d9a82007-08-27 16:49:44 -04002686static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2687{
Chris Masond1310b22008-01-24 16:13:08 -05002688 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002689 struct btrfs_ordered_extent *ordered;
2690 u64 page_start = page_offset(page);
2691 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002692
Chris Masone6dcd2d2008-07-17 12:53:50 -04002693 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002694 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002695 if (offset) {
2696 btrfs_releasepage(page, GFP_NOFS);
2697 return;
2698 }
2699
2700 lock_extent(tree, page_start, page_end, GFP_NOFS);
2701 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2702 page_offset(page));
2703 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04002704 /*
2705 * IO on this page will never be started, so we need
2706 * to account for any ordered extents now
2707 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002708 clear_extent_bit(tree, page_start, page_end,
2709 EXTENT_DIRTY | EXTENT_DELALLOC |
2710 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04002711 btrfs_finish_ordered_io(page->mapping->host,
2712 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002713 btrfs_put_ordered_extent(ordered);
2714 lock_extent(tree, page_start, page_end, GFP_NOFS);
2715 }
2716 clear_extent_bit(tree, page_start, page_end,
2717 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2718 EXTENT_ORDERED,
2719 1, 1, GFP_NOFS);
2720 __btrfs_releasepage(page, GFP_NOFS);
2721
Chris Mason4a096752008-07-21 10:29:44 -04002722 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002723 if (PagePrivate(page)) {
Chris Masone6dcd2d2008-07-17 12:53:50 -04002724 invalidate_extent_lru(tree, page_offset(page),
2725 PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002726 ClearPagePrivate(page);
2727 set_page_private(page, 0);
2728 page_cache_release(page);
2729 }
Chris Mason39279cc2007-06-12 06:35:45 -04002730}
2731
Chris Mason9ebefb182007-06-15 13:50:00 -04002732/*
2733 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2734 * called from a page fault handler when a page is first dirtied. Hence we must
2735 * be careful to check for EOF conditions here. We set the page up correctly
2736 * for a written page which means we get ENOSPC checking when writing into
2737 * holes and correct delalloc and unwritten extent mapping on filesystems that
2738 * support these features.
2739 *
2740 * We are not allowed to take the i_mutex here so we have to play games to
2741 * protect against truncate races as the page could now be beyond EOF. Because
2742 * vmtruncate() writes the inode size before removing pages, once we have the
2743 * page lock we can determine safely if the page is beyond EOF. If it is not
2744 * beyond EOF, then the page is guaranteed safe against truncation until we
2745 * unlock the page.
2746 */
2747int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2748{
Chris Mason6da6aba2007-12-18 16:15:09 -05002749 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002750 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002751 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2752 struct btrfs_ordered_extent *ordered;
2753 char *kaddr;
2754 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002755 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002756 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002757 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002758 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04002759
Chris Mason1832a6d2007-12-21 16:27:21 -05002760 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05002761 if (ret)
2762 goto out;
2763
2764 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002765again:
Chris Mason9ebefb182007-06-15 13:50:00 -04002766 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002767 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002768 page_start = page_offset(page);
2769 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002770
Chris Mason9ebefb182007-06-15 13:50:00 -04002771 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04002772 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002773 /* page got truncated out from underneath us */
2774 goto out_unlock;
2775 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002776 wait_on_page_writeback(page);
2777
2778 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2779 set_page_extent_mapped(page);
2780
Chris Masoneb84ae02008-07-17 13:53:27 -04002781 /*
2782 * we can't set the delalloc bits if there are pending ordered
2783 * extents. Drop our locks and wait for them to finish
2784 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002785 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2786 if (ordered) {
2787 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2788 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002789 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002790 btrfs_put_ordered_extent(ordered);
2791 goto again;
2792 }
2793
2794 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
2795 page_end, GFP_NOFS);
2796 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04002797
2798 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002799 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002800 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04002801 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04002802 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04002803
Chris Masone6dcd2d2008-07-17 12:53:50 -04002804 if (zero_start != PAGE_CACHE_SIZE) {
2805 kaddr = kmap(page);
2806 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
2807 flush_dcache_page(page);
2808 kunmap(page);
2809 }
Chris Mason247e7432008-07-17 12:53:51 -04002810 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002811 set_page_dirty(page);
2812 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04002813
2814out_unlock:
2815 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002816out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002817 return ret;
2818}
2819
Chris Mason39279cc2007-06-12 06:35:45 -04002820static void btrfs_truncate(struct inode *inode)
2821{
2822 struct btrfs_root *root = BTRFS_I(inode)->root;
2823 int ret;
2824 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002825 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04002826 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002827
2828 if (!S_ISREG(inode->i_mode))
2829 return;
2830 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2831 return;
2832
2833 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04002834 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002835
Chris Mason39279cc2007-06-12 06:35:45 -04002836 trans = btrfs_start_transaction(root, 1);
2837 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04002838 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04002839
2840 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002841 ret = btrfs_truncate_in_trans(trans, root, inode,
2842 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002843 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002844 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002845
Chris Mason89ce8a62008-06-25 16:01:31 -04002846 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002847 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002848 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002849}
2850
Sven Wegener3b963622008-06-09 21:57:42 -04002851/*
2852 * Invalidate a single dcache entry at the root of the filesystem.
2853 * Needed after creation of snapshot or subvolume.
2854 */
2855void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2856 int namelen)
2857{
2858 struct dentry *alias, *entry;
2859 struct qstr qstr;
2860
2861 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2862 if (alias) {
2863 qstr.name = name;
2864 qstr.len = namelen;
2865 /* change me if btrfs ever gets a d_hash operation */
2866 qstr.hash = full_name_hash(qstr.name, qstr.len);
2867 entry = d_lookup(alias, &qstr);
2868 dput(alias);
2869 if (entry) {
2870 d_invalidate(entry);
2871 dput(entry);
2872 }
2873 }
2874}
2875
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002876int btrfs_create_subvol_root(struct btrfs_root *new_root,
2877 struct btrfs_trans_handle *trans, u64 new_dirid,
2878 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04002879{
Chris Mason39279cc2007-06-12 06:35:45 -04002880 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002881 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002882
Chris Mason9c583092008-01-29 15:15:18 -05002883 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002884 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002885 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002886 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002887 inode->i_op = &btrfs_dir_inode_operations;
2888 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002889 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002890
Chris Mason39544012007-12-12 14:38:19 -05002891 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2892 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002893 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04002894 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04002895
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002896 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002897}
2898
Chris Masonedbd8d42007-12-21 16:27:24 -05002899unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002900 struct file_ra_state *ra, struct file *file,
2901 pgoff_t offset, pgoff_t last_index)
2902{
Chris Mason8e7bf942008-04-28 09:02:36 -04002903 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04002904
2905#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04002906 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2907 return offset;
2908#else
Chris Mason86479a02007-09-10 19:58:16 -04002909 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2910 return offset + req_size;
2911#endif
2912}
2913
Chris Mason39279cc2007-06-12 06:35:45 -04002914struct inode *btrfs_alloc_inode(struct super_block *sb)
2915{
2916 struct btrfs_inode *ei;
2917
2918 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2919 if (!ei)
2920 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002921 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002922 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002923 return &ei->vfs_inode;
2924}
2925
2926void btrfs_destroy_inode(struct inode *inode)
2927{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002928 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04002929 WARN_ON(!list_empty(&inode->i_dentry));
2930 WARN_ON(inode->i_data.nrpages);
2931
Chris Masone6dcd2d2008-07-17 12:53:50 -04002932 while(1) {
2933 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
2934 if (!ordered)
2935 break;
2936 else {
2937 printk("found ordered extent %Lu %Lu\n",
2938 ordered->file_offset, ordered->len);
2939 btrfs_remove_ordered_extent(inode, ordered);
2940 btrfs_put_ordered_extent(ordered);
2941 btrfs_put_ordered_extent(ordered);
2942 }
2943 }
Chris Mason8c416c92008-01-14 15:10:26 -05002944 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002945 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2946}
2947
Chris Mason44ec0b72007-10-29 10:55:05 -04002948#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2949static void init_once(struct kmem_cache * cachep, void *foo)
2950#else
Chris Mason39279cc2007-06-12 06:35:45 -04002951static void init_once(void * foo, struct kmem_cache * cachep,
2952 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002953#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002954{
2955 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2956
2957 inode_init_once(&ei->vfs_inode);
2958}
2959
2960void btrfs_destroy_cachep(void)
2961{
2962 if (btrfs_inode_cachep)
2963 kmem_cache_destroy(btrfs_inode_cachep);
2964 if (btrfs_trans_handle_cachep)
2965 kmem_cache_destroy(btrfs_trans_handle_cachep);
2966 if (btrfs_transaction_cachep)
2967 kmem_cache_destroy(btrfs_transaction_cachep);
2968 if (btrfs_bit_radix_cachep)
2969 kmem_cache_destroy(btrfs_bit_radix_cachep);
2970 if (btrfs_path_cachep)
2971 kmem_cache_destroy(btrfs_path_cachep);
2972}
2973
Chris Mason86479a02007-09-10 19:58:16 -04002974struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002975 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002976#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2977 void (*ctor)(struct kmem_cache *, void *)
2978#else
Chris Mason92fee662007-07-25 12:31:35 -04002979 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002980 unsigned long)
2981#endif
2982 )
Chris Mason92fee662007-07-25 12:31:35 -04002983{
2984 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2985 SLAB_MEM_SPREAD | extra_flags), ctor
2986#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2987 ,NULL
2988#endif
2989 );
2990}
2991
Chris Mason39279cc2007-06-12 06:35:45 -04002992int btrfs_init_cachep(void)
2993{
Chris Mason86479a02007-09-10 19:58:16 -04002994 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002995 sizeof(struct btrfs_inode),
2996 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002997 if (!btrfs_inode_cachep)
2998 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002999 btrfs_trans_handle_cachep =
3000 btrfs_cache_create("btrfs_trans_handle_cache",
3001 sizeof(struct btrfs_trans_handle),
3002 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003003 if (!btrfs_trans_handle_cachep)
3004 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003005 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003006 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003007 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003008 if (!btrfs_transaction_cachep)
3009 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003010 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003011 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003012 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003013 if (!btrfs_path_cachep)
3014 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003015 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003016 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003017 if (!btrfs_bit_radix_cachep)
3018 goto fail;
3019 return 0;
3020fail:
3021 btrfs_destroy_cachep();
3022 return -ENOMEM;
3023}
3024
3025static int btrfs_getattr(struct vfsmount *mnt,
3026 struct dentry *dentry, struct kstat *stat)
3027{
3028 struct inode *inode = dentry->d_inode;
3029 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003030 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003031 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003032 return 0;
3033}
3034
3035static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3036 struct inode * new_dir,struct dentry *new_dentry)
3037{
3038 struct btrfs_trans_handle *trans;
3039 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3040 struct inode *new_inode = new_dentry->d_inode;
3041 struct inode *old_inode = old_dentry->d_inode;
3042 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04003043 int ret;
3044
3045 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3046 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3047 return -ENOTEMPTY;
3048 }
Chris Mason5f39d392007-10-15 16:14:19 -04003049
Chris Mason1832a6d2007-12-21 16:27:21 -05003050 ret = btrfs_check_free_space(root, 1, 0);
3051 if (ret)
3052 goto out_unlock;
3053
Chris Mason39279cc2007-06-12 06:35:45 -04003054 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003055
Chris Mason39279cc2007-06-12 06:35:45 -04003056 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003057
3058 old_dentry->d_inode->i_nlink++;
3059 old_dir->i_ctime = old_dir->i_mtime = ctime;
3060 new_dir->i_ctime = new_dir->i_mtime = ctime;
3061 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003062
Chris Mason39279cc2007-06-12 06:35:45 -04003063 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3064 if (ret)
3065 goto out_fail;
3066
3067 if (new_inode) {
3068 new_inode->i_ctime = CURRENT_TIME;
3069 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3070 if (ret)
3071 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003072 }
Chris Mason9c583092008-01-29 15:15:18 -05003073 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003074 if (ret)
3075 goto out_fail;
3076
3077out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003078 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003079out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003080 return ret;
3081}
3082
3083static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3084 const char *symname)
3085{
3086 struct btrfs_trans_handle *trans;
3087 struct btrfs_root *root = BTRFS_I(dir)->root;
3088 struct btrfs_path *path;
3089 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003090 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003091 int err;
3092 int drop_inode = 0;
3093 u64 objectid;
3094 int name_len;
3095 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003096 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003097 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003098 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003099 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003100
3101 name_len = strlen(symname) + 1;
3102 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3103 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003104
Chris Mason1832a6d2007-12-21 16:27:21 -05003105 err = btrfs_check_free_space(root, 1, 0);
3106 if (err)
3107 goto out_fail;
3108
Chris Mason39279cc2007-06-12 06:35:45 -04003109 trans = btrfs_start_transaction(root, 1);
3110 btrfs_set_trans_block_group(trans, dir);
3111
3112 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3113 if (err) {
3114 err = -ENOSPC;
3115 goto out_unlock;
3116 }
3117
Chris Mason9c583092008-01-29 15:15:18 -05003118 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3119 dentry->d_name.len,
3120 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003121 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3122 err = PTR_ERR(inode);
3123 if (IS_ERR(inode))
3124 goto out_unlock;
3125
3126 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003127 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003128 if (err)
3129 drop_inode = 1;
3130 else {
3131 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003132 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003133 inode->i_fop = &btrfs_file_operations;
3134 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003135 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3136 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003137 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003138 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3139 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04003140 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003141 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003142 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003143 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003144 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003145 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003146 }
3147 dir->i_sb->s_dirt = 1;
3148 btrfs_update_inode_block_group(trans, inode);
3149 btrfs_update_inode_block_group(trans, dir);
3150 if (drop_inode)
3151 goto out_unlock;
3152
3153 path = btrfs_alloc_path();
3154 BUG_ON(!path);
3155 key.objectid = inode->i_ino;
3156 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003157 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3158 datasize = btrfs_file_extent_calc_inline_size(name_len);
3159 err = btrfs_insert_empty_item(trans, root, path, &key,
3160 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003161 if (err) {
3162 drop_inode = 1;
3163 goto out_unlock;
3164 }
Chris Mason5f39d392007-10-15 16:14:19 -04003165 leaf = path->nodes[0];
3166 ei = btrfs_item_ptr(leaf, path->slots[0],
3167 struct btrfs_file_extent_item);
3168 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3169 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003170 BTRFS_FILE_EXTENT_INLINE);
3171 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003172 write_extent_buffer(leaf, symname, ptr, name_len);
3173 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003174 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003175
Chris Mason39279cc2007-06-12 06:35:45 -04003176 inode->i_op = &btrfs_symlink_inode_operations;
3177 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003178 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003179 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003180 err = btrfs_update_inode(trans, root, inode);
3181 if (err)
3182 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003183
3184out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003185 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003186 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003187out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003188 if (drop_inode) {
3189 inode_dec_link_count(inode);
3190 iput(inode);
3191 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003192 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003193 return err;
3194}
Chris Mason16432982008-04-10 10:23:21 -04003195
Chris Masone6dcd2d2008-07-17 12:53:50 -04003196static int btrfs_set_page_dirty(struct page *page)
3197{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003198 return __set_page_dirty_nobuffers(page);
3199}
3200
Yanfdebe2b2008-01-14 13:26:08 -05003201static int btrfs_permission(struct inode *inode, int mask,
3202 struct nameidata *nd)
3203{
3204 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3205 return -EACCES;
3206 return generic_permission(inode, mask, NULL);
3207}
Chris Mason39279cc2007-06-12 06:35:45 -04003208
3209static struct inode_operations btrfs_dir_inode_operations = {
3210 .lookup = btrfs_lookup,
3211 .create = btrfs_create,
3212 .unlink = btrfs_unlink,
3213 .link = btrfs_link,
3214 .mkdir = btrfs_mkdir,
3215 .rmdir = btrfs_rmdir,
3216 .rename = btrfs_rename,
3217 .symlink = btrfs_symlink,
3218 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003219 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003220 .setxattr = generic_setxattr,
3221 .getxattr = generic_getxattr,
3222 .listxattr = btrfs_listxattr,
3223 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003224 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003225};
Chris Mason39279cc2007-06-12 06:35:45 -04003226static struct inode_operations btrfs_dir_ro_inode_operations = {
3227 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003228 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003229};
Chris Mason39279cc2007-06-12 06:35:45 -04003230static struct file_operations btrfs_dir_file_operations = {
3231 .llseek = generic_file_llseek,
3232 .read = generic_read_dir,
3233 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003234 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003235#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003236 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003237#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003238 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003239};
3240
Chris Masond1310b22008-01-24 16:13:08 -05003241static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003242 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003243 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003244 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003245 .readpage_io_hook = btrfs_readpage_io_hook,
3246 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003247 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003248 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003249 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003250 .set_bit_hook = btrfs_set_bit_hook,
3251 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003252};
3253
Chris Mason39279cc2007-06-12 06:35:45 -04003254static struct address_space_operations btrfs_aops = {
3255 .readpage = btrfs_readpage,
3256 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003257 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003258 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003259 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003260 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003261 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003262 .invalidatepage = btrfs_invalidatepage,
3263 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003264 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003265};
3266
3267static struct address_space_operations btrfs_symlink_aops = {
3268 .readpage = btrfs_readpage,
3269 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003270 .invalidatepage = btrfs_invalidatepage,
3271 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003272};
3273
3274static struct inode_operations btrfs_file_inode_operations = {
3275 .truncate = btrfs_truncate,
3276 .getattr = btrfs_getattr,
3277 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003278 .setxattr = generic_setxattr,
3279 .getxattr = generic_getxattr,
3280 .listxattr = btrfs_listxattr,
3281 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003282 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003283};
Josef Bacik618e21d2007-07-11 10:18:17 -04003284static struct inode_operations btrfs_special_inode_operations = {
3285 .getattr = btrfs_getattr,
3286 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003287 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003288};
Chris Mason39279cc2007-06-12 06:35:45 -04003289static struct inode_operations btrfs_symlink_inode_operations = {
3290 .readlink = generic_readlink,
3291 .follow_link = page_follow_link_light,
3292 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003293 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003294};