blob: 8d371d6fe5516e17d40851fae6511fe1d9b677dd [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;
Chris Masonf4219502008-07-22 11:18:09 -0400488
Chris Mason247e7432008-07-17 12:53:51 -0400489 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 Masonf4219502008-07-22 11:18:09 -0400505 struct extent_map *em_orig;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400506 u64 alloc_hint = 0;
Chris Masone5a22172008-07-18 20:42:20 -0400507 u64 clear_start;
508 u64 clear_end;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400509 struct list_head list;
510 struct btrfs_key ins;
Chris Masonf4219502008-07-22 11:18:09 -0400511 struct rb_node *rb;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400512 int ret;
513
514 ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
Chris Masonba1da2f2008-07-17 12:54:15 -0400515 if (!ret)
Chris Masone6dcd2d2008-07-17 12:53:50 -0400516 return 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400517
Chris Masonf9295742008-07-17 12:54:14 -0400518 trans = btrfs_join_transaction(root, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400519
520 ordered_extent = btrfs_lookup_ordered_extent(inode, start);
521 BUG_ON(!ordered_extent);
522
523 lock_extent(io_tree, ordered_extent->file_offset,
524 ordered_extent->file_offset + ordered_extent->len - 1,
525 GFP_NOFS);
526
527 INIT_LIST_HEAD(&list);
528
529 ins.objectid = ordered_extent->start;
530 ins.offset = ordered_extent->len;
531 ins.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masone5a22172008-07-18 20:42:20 -0400532
Chris Masone6dcd2d2008-07-17 12:53:50 -0400533 ret = btrfs_alloc_reserved_extent(trans, root, root->root_key.objectid,
534 trans->transid, inode->i_ino,
535 ordered_extent->file_offset, &ins);
536 BUG_ON(ret);
Chris Masonee6e6502008-07-17 12:54:40 -0400537
538 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Masone5a22172008-07-18 20:42:20 -0400539
Chris Masonf4219502008-07-22 11:18:09 -0400540 spin_lock(&em_tree->lock);
541 clear_start = ordered_extent->file_offset;
542 clear_end = ordered_extent->file_offset + ordered_extent->len;
543 em = lookup_extent_mapping(em_tree, clear_start,
544 ordered_extent->len);
545 em_orig = em;
546 while(em && clear_start < extent_map_end(em) && clear_end > em->start) {
547 clear_bit(EXTENT_FLAG_PINNED, &em->flags);
548 rb = rb_next(&em->rb_node);
549 if (!rb)
550 break;
551 em = rb_entry(rb, struct extent_map, rb_node);
552 }
553 free_extent_map(em_orig);
554 spin_unlock(&em_tree->lock);
555
Chris Masone6dcd2d2008-07-17 12:53:50 -0400556 ret = btrfs_drop_extents(trans, root, inode,
557 ordered_extent->file_offset,
558 ordered_extent->file_offset +
559 ordered_extent->len,
560 ordered_extent->file_offset, &alloc_hint);
561 BUG_ON(ret);
562 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
563 ordered_extent->file_offset,
564 ordered_extent->start,
565 ordered_extent->len,
566 ordered_extent->len, 0);
567 BUG_ON(ret);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400568
Chris Masone6dcd2d2008-07-17 12:53:50 -0400569 btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
570 ordered_extent->file_offset +
571 ordered_extent->len - 1);
Chris Masonee6e6502008-07-17 12:54:40 -0400572 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
573
Chris Masone6dcd2d2008-07-17 12:53:50 -0400574 inode->i_blocks += ordered_extent->len >> 9;
575 unlock_extent(io_tree, ordered_extent->file_offset,
576 ordered_extent->file_offset + ordered_extent->len - 1,
577 GFP_NOFS);
578 add_pending_csums(trans, inode, ordered_extent->file_offset,
579 &ordered_extent->list);
580
Chris Masondbe674a2008-07-17 12:54:05 -0400581 btrfs_ordered_update_i_size(inode, ordered_extent);
Chris Masone6dcd2d2008-07-17 12:53:50 -0400582 btrfs_remove_ordered_extent(inode, ordered_extent);
Chris Mason7f3c74f2008-07-18 12:01:11 -0400583
Chris Masone6dcd2d2008-07-17 12:53:50 -0400584 /* once for us */
585 btrfs_put_ordered_extent(ordered_extent);
586 /* once for the tree */
587 btrfs_put_ordered_extent(ordered_extent);
588
589 btrfs_update_inode(trans, root, inode);
590 btrfs_end_transaction(trans, root);
591 return 0;
592}
593
Chris Mason211f90e2008-07-18 11:56:15 -0400594int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
595 struct extent_state *state, int uptodate)
596{
597 return btrfs_finish_ordered_io(page->mapping->host, start, end);
598}
599
Chris Mason07157aa2007-08-30 08:50:51 -0400600int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
601{
602 int ret = 0;
603 struct inode *inode = page->mapping->host;
604 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500605 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400606 struct btrfs_csum_item *item;
607 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400608 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400609
Yanb98b6762008-01-08 15:54:37 -0500610 if (btrfs_test_opt(root, NODATASUM) ||
611 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500612 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400613
Chris Mason89642222008-07-24 09:41:53 -0400614 /*
615 * It is possible there is an ordered extent that has
616 * not yet finished for this range in the file. If so,
617 * that extent will have a csum cached, and it will insert
618 * the sum after all the blocks in the extent are fully
619 * on disk. So, look for an ordered extent and use the
620 * sum if found. We have to do this before looking in the
621 * btree because csum items are pre-inserted based on
622 * the file size. btrfs_lookup_csum might find an item
623 * that still hasn't been fully filled.
624 */
625 ret = btrfs_find_ordered_sum(inode, start, &csum);
626 if (ret == 0)
627 goto found;
628
629 ret = 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400630 path = btrfs_alloc_path();
631 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
632 if (IS_ERR(item)) {
633 ret = PTR_ERR(item);
634 /* a csum that isn't present is a preallocated region. */
635 if (ret == -ENOENT || ret == -EFBIG)
636 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400637 csum = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -0400638 printk("no csum found for inode %lu start %Lu\n", inode->i_ino,
639 start);
Chris Mason07157aa2007-08-30 08:50:51 -0400640 goto out;
641 }
Chris Masonff79f812007-10-15 16:22:25 -0400642 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
643 BTRFS_CRC32_SIZE);
Chris Masonba1da2f2008-07-17 12:54:15 -0400644found:
Chris Masond1310b22008-01-24 16:13:08 -0500645 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400646out:
647 if (path)
648 btrfs_free_path(path);
Chris Mason07157aa2007-08-30 08:50:51 -0400649 return ret;
650}
651
Chris Mason7e383262008-04-09 16:28:12 -0400652struct io_failure_record {
653 struct page *page;
654 u64 start;
655 u64 len;
656 u64 logical;
657 int last_mirror;
658};
659
Chris Mason1259ab72008-05-12 13:39:03 -0400660int btrfs_io_failed_hook(struct bio *failed_bio,
661 struct page *page, u64 start, u64 end,
662 struct extent_state *state)
Chris Mason7e383262008-04-09 16:28:12 -0400663{
664 struct io_failure_record *failrec = NULL;
665 u64 private;
666 struct extent_map *em;
667 struct inode *inode = page->mapping->host;
668 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400669 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400670 struct bio *bio;
671 int num_copies;
672 int ret;
Chris Mason1259ab72008-05-12 13:39:03 -0400673 int rw;
Chris Mason7e383262008-04-09 16:28:12 -0400674 u64 logical;
675
676 ret = get_state_private(failure_tree, start, &private);
677 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400678 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
679 if (!failrec)
680 return -ENOMEM;
681 failrec->start = start;
682 failrec->len = end - start + 1;
683 failrec->last_mirror = 0;
684
Chris Mason3b951512008-04-17 11:29:12 -0400685 spin_lock(&em_tree->lock);
686 em = lookup_extent_mapping(em_tree, start, failrec->len);
687 if (em->start > start || em->start + em->len < start) {
688 free_extent_map(em);
689 em = NULL;
690 }
691 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400692
693 if (!em || IS_ERR(em)) {
694 kfree(failrec);
695 return -EIO;
696 }
697 logical = start - em->start;
698 logical = em->block_start + logical;
699 failrec->logical = logical;
700 free_extent_map(em);
701 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
702 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400703 set_state_private(failure_tree, start,
704 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400705 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400706 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400707 }
708 num_copies = btrfs_num_copies(
709 &BTRFS_I(inode)->root->fs_info->mapping_tree,
710 failrec->logical, failrec->len);
711 failrec->last_mirror++;
712 if (!state) {
713 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
714 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
715 failrec->start,
716 EXTENT_LOCKED);
717 if (state && state->start != failrec->start)
718 state = NULL;
719 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
720 }
721 if (!state || failrec->last_mirror > num_copies) {
722 set_state_private(failure_tree, failrec->start, 0);
723 clear_extent_bits(failure_tree, failrec->start,
724 failrec->start + failrec->len - 1,
725 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
726 kfree(failrec);
727 return -EIO;
728 }
729 bio = bio_alloc(GFP_NOFS, 1);
730 bio->bi_private = state;
731 bio->bi_end_io = failed_bio->bi_end_io;
732 bio->bi_sector = failrec->logical >> 9;
733 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400734 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400735 bio_add_page(bio, page, failrec->len, start - page_offset(page));
Chris Mason1259ab72008-05-12 13:39:03 -0400736 if (failed_bio->bi_rw & (1 << BIO_RW))
737 rw = WRITE;
738 else
739 rw = READ;
740
741 BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
742 failrec->last_mirror);
743 return 0;
744}
745
746int btrfs_clean_io_failures(struct inode *inode, u64 start)
747{
748 u64 private;
749 u64 private_failure;
750 struct io_failure_record *failure;
751 int ret;
752
753 private = 0;
754 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
755 (u64)-1, 1, EXTENT_DIRTY)) {
756 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
757 start, &private_failure);
758 if (ret == 0) {
759 failure = (struct io_failure_record *)(unsigned long)
760 private_failure;
761 set_state_private(&BTRFS_I(inode)->io_failure_tree,
762 failure->start, 0);
763 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
764 failure->start,
765 failure->start + failure->len - 1,
766 EXTENT_DIRTY | EXTENT_LOCKED,
767 GFP_NOFS);
768 kfree(failure);
769 }
770 }
Chris Mason7e383262008-04-09 16:28:12 -0400771 return 0;
772}
773
Chris Mason70dec802008-01-29 09:59:12 -0500774int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
775 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400776{
Chris Mason35ebb932007-10-30 16:56:53 -0400777 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400778 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500779 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400780 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500781 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400782 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400783 struct btrfs_root *root = BTRFS_I(inode)->root;
784 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400785 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500786
Yanb98b6762008-01-08 15:54:37 -0500787 if (btrfs_test_opt(root, NODATASUM) ||
788 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500789 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500790 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500791 private = state->private;
792 ret = 0;
793 } else {
794 ret = get_state_private(io_tree, start, &private);
795 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400796 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400797 kaddr = kmap_atomic(page, KM_IRQ0);
798 if (ret) {
799 goto zeroit;
800 }
Chris Masonff79f812007-10-15 16:22:25 -0400801 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
802 btrfs_csum_final(csum, (char *)&csum);
803 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400804 goto zeroit;
805 }
806 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400807 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400808
809 /* if the io failure tree for this inode is non-empty,
810 * check to see if we've recovered from a failed IO
811 */
Chris Mason1259ab72008-05-12 13:39:03 -0400812 btrfs_clean_io_failures(inode, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400813 return 0;
814
815zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500816 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
817 page->mapping->host->i_ino, (unsigned long long)start, csum,
818 private);
Chris Masondb945352007-10-15 16:15:53 -0400819 memset(kaddr + offset, 1, end - start + 1);
820 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400821 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400822 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400823 if (private == 0)
824 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400825 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400826}
Chris Masonb888db22007-08-27 16:49:44 -0400827
Chris Mason39279cc2007-06-12 06:35:45 -0400828void btrfs_read_locked_inode(struct inode *inode)
829{
830 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400831 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400832 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400833 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400834 struct btrfs_root *root = BTRFS_I(inode)->root;
835 struct btrfs_key location;
836 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400837 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400838 int ret;
839
840 path = btrfs_alloc_path();
841 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400842 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500843
Chris Mason39279cc2007-06-12 06:35:45 -0400844 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400845 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400846 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400847
Chris Mason5f39d392007-10-15 16:14:19 -0400848 leaf = path->nodes[0];
849 inode_item = btrfs_item_ptr(leaf, path->slots[0],
850 struct btrfs_inode_item);
851
852 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
853 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
854 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
855 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
Chris Masondbe674a2008-07-17 12:54:05 -0400856 btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -0400857
858 tspec = btrfs_inode_atime(inode_item);
859 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
860 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
861
862 tspec = btrfs_inode_mtime(inode_item);
863 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
864 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
865
866 tspec = btrfs_inode_ctime(inode_item);
867 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
868 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
869
870 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
871 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400872 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400873 rdev = btrfs_inode_rdev(leaf, inode_item);
874
Josef Bacikaec74772008-07-24 12:12:38 -0400875 BTRFS_I(inode)->index_cnt = (u64)-1;
876
Chris Mason5f39d392007-10-15 16:14:19 -0400877 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400878 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
879 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500880 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500881 if (!BTRFS_I(inode)->block_group) {
882 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400883 NULL, 0,
884 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500885 }
Chris Mason39279cc2007-06-12 06:35:45 -0400886 btrfs_free_path(path);
887 inode_item = NULL;
888
Chris Mason39279cc2007-06-12 06:35:45 -0400889 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400890 case S_IFREG:
891 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400892 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500893 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400894 inode->i_fop = &btrfs_file_operations;
895 inode->i_op = &btrfs_file_inode_operations;
896 break;
897 case S_IFDIR:
898 inode->i_fop = &btrfs_dir_file_operations;
899 if (root == root->fs_info->tree_root)
900 inode->i_op = &btrfs_dir_ro_inode_operations;
901 else
902 inode->i_op = &btrfs_dir_inode_operations;
903 break;
904 case S_IFLNK:
905 inode->i_op = &btrfs_symlink_inode_operations;
906 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400907 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400908 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400909 default:
910 init_special_inode(inode, inode->i_mode, rdev);
911 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400912 }
913 return;
914
915make_bad:
Chris Mason39279cc2007-06-12 06:35:45 -0400916 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -0400917 make_bad_inode(inode);
918}
919
Chris Mason5f39d392007-10-15 16:14:19 -0400920static void fill_inode_item(struct extent_buffer *leaf,
921 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400922 struct inode *inode)
923{
Chris Mason5f39d392007-10-15 16:14:19 -0400924 btrfs_set_inode_uid(leaf, item, inode->i_uid);
925 btrfs_set_inode_gid(leaf, item, inode->i_gid);
Chris Masondbe674a2008-07-17 12:54:05 -0400926 btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
Chris Mason5f39d392007-10-15 16:14:19 -0400927 btrfs_set_inode_mode(leaf, item, inode->i_mode);
928 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
929
930 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
931 inode->i_atime.tv_sec);
932 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
933 inode->i_atime.tv_nsec);
934
935 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
936 inode->i_mtime.tv_sec);
937 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
938 inode->i_mtime.tv_nsec);
939
940 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
941 inode->i_ctime.tv_sec);
942 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
943 inode->i_ctime.tv_nsec);
944
945 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
946 btrfs_set_inode_generation(leaf, item, inode->i_generation);
947 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500948 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400949 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400950 BTRFS_I(inode)->block_group->key.objectid);
951}
952
Chris Masonba1da2f2008-07-17 12:54:15 -0400953int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400954 struct btrfs_root *root,
955 struct inode *inode)
956{
957 struct btrfs_inode_item *inode_item;
958 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400959 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400960 int ret;
961
962 path = btrfs_alloc_path();
963 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400964 ret = btrfs_lookup_inode(trans, root, path,
965 &BTRFS_I(inode)->location, 1);
966 if (ret) {
967 if (ret > 0)
968 ret = -ENOENT;
969 goto failed;
970 }
971
Chris Mason5f39d392007-10-15 16:14:19 -0400972 leaf = path->nodes[0];
973 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400974 struct btrfs_inode_item);
975
Chris Mason5f39d392007-10-15 16:14:19 -0400976 fill_inode_item(leaf, inode_item, inode);
977 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400978 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400979 ret = 0;
980failed:
Chris Mason39279cc2007-06-12 06:35:45 -0400981 btrfs_free_path(path);
982 return ret;
983}
984
985
986static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
987 struct btrfs_root *root,
988 struct inode *dir,
989 struct dentry *dentry)
990{
991 struct btrfs_path *path;
992 const char *name = dentry->d_name.name;
993 int name_len = dentry->d_name.len;
994 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400995 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400996 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400997 struct btrfs_key key;
Josef Bacikaec74772008-07-24 12:12:38 -0400998 u64 index;
Chris Mason39279cc2007-06-12 06:35:45 -0400999
1000 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001001 if (!path) {
1002 ret = -ENOMEM;
1003 goto err;
1004 }
1005
Chris Mason39279cc2007-06-12 06:35:45 -04001006 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1007 name, name_len, -1);
1008 if (IS_ERR(di)) {
1009 ret = PTR_ERR(di);
1010 goto err;
1011 }
1012 if (!di) {
1013 ret = -ENOENT;
1014 goto err;
1015 }
Chris Mason5f39d392007-10-15 16:14:19 -04001016 leaf = path->nodes[0];
1017 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -04001018 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -04001019 if (ret)
1020 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -04001021 btrfs_release_path(root, path);
1022
Josef Bacikaec74772008-07-24 12:12:38 -04001023 ret = btrfs_del_inode_ref(trans, root, name, name_len,
1024 dentry->d_inode->i_ino,
1025 dentry->d_parent->d_inode->i_ino, &index);
1026 if (ret) {
1027 printk("failed to delete reference to %.*s, "
1028 "inode %lu parent %lu\n", name_len, name,
1029 dentry->d_inode->i_ino,
1030 dentry->d_parent->d_inode->i_ino);
1031 goto err;
1032 }
1033
Chris Mason39279cc2007-06-12 06:35:45 -04001034 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04001035 index, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -04001036 if (IS_ERR(di)) {
1037 ret = PTR_ERR(di);
1038 goto err;
1039 }
1040 if (!di) {
1041 ret = -ENOENT;
1042 goto err;
1043 }
1044 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason925baed2008-06-25 16:01:30 -04001045 btrfs_release_path(root, path);
Chris Mason39279cc2007-06-12 06:35:45 -04001046
1047 dentry->d_inode->i_ctime = dir->i_ctime;
1048err:
1049 btrfs_free_path(path);
1050 if (!ret) {
Chris Masondbe674a2008-07-17 12:54:05 -04001051 btrfs_i_size_write(dir, dir->i_size - name_len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04001052 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001053 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -05001054#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1055 dentry->d_inode->i_nlink--;
1056#else
Chris Mason39279cc2007-06-12 06:35:45 -04001057 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001058#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001059 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -04001060 dir->i_sb->s_dirt = 1;
1061 }
1062 return ret;
1063}
1064
1065static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1066{
1067 struct btrfs_root *root;
1068 struct btrfs_trans_handle *trans;
1069 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -05001070 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001071
1072 root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001073
1074 ret = btrfs_check_free_space(root, 1, 1);
1075 if (ret)
1076 goto fail;
1077
Chris Mason39279cc2007-06-12 06:35:45 -04001078 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001079
Chris Mason39279cc2007-06-12 06:35:45 -04001080 btrfs_set_trans_block_group(trans, dir);
1081 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001082 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04001083
Chris Mason89ce8a62008-06-25 16:01:31 -04001084 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001085fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001086 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001087 return ret;
1088}
1089
1090static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1091{
1092 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001093 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001094 int ret;
1095 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04001096 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -05001097 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001098
Chris Mason925baed2008-06-25 16:01:30 -04001099 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
Yan134d4512007-10-25 15:49:25 -04001100 return -ENOTEMPTY;
Chris Mason925baed2008-06-25 16:01:30 -04001101 }
Yan134d4512007-10-25 15:49:25 -04001102
Chris Mason1832a6d2007-12-21 16:27:21 -05001103 ret = btrfs_check_free_space(root, 1, 1);
1104 if (ret)
1105 goto fail;
1106
Chris Mason39279cc2007-06-12 06:35:45 -04001107 trans = btrfs_start_transaction(root, 1);
1108 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -04001109
1110 /* now the directory is empty */
1111 err = btrfs_unlink_trans(trans, root, dir, dentry);
1112 if (!err) {
Chris Masondbe674a2008-07-17 12:54:05 -04001113 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001114 }
Chris Mason39544012007-12-12 14:38:19 -05001115
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001116 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04001117 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001118fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001119 btrfs_btree_balance_dirty(root, nr);
Chris Mason39544012007-12-12 14:38:19 -05001120
Chris Mason39279cc2007-06-12 06:35:45 -04001121 if (ret && !err)
1122 err = ret;
1123 return err;
1124}
1125
Chris Mason39279cc2007-06-12 06:35:45 -04001126/*
Chris Mason39279cc2007-06-12 06:35:45 -04001127 * this can truncate away extent items, csum items and directory items.
1128 * It starts at a high offset and removes keys until it can't find
1129 * any higher than i_size.
1130 *
1131 * csum items that cross the new i_size are truncated to the new size
1132 * as well.
1133 */
1134static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
1135 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -05001136 struct inode *inode,
1137 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001138{
1139 int ret;
1140 struct btrfs_path *path;
1141 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001142 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001143 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -04001144 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001145 struct btrfs_file_extent_item *fi;
1146 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -04001147 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001148 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001149 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001150 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001151 int found_extent;
1152 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -05001153 int pending_del_nr = 0;
1154 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -04001155 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -04001156 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001157
Chris Mason3b951512008-04-17 11:29:12 -04001158 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04001159 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -04001160 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -04001161 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -04001162
Chris Mason39279cc2007-06-12 06:35:45 -04001163 /* FIXME, add redo link to tree so we don't leak on crash */
1164 key.objectid = inode->i_ino;
1165 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -04001166 key.type = (u8)-1;
1167
Chris Mason85e21ba2008-01-29 15:11:36 -05001168 btrfs_init_path(path);
1169search_again:
1170 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1171 if (ret < 0) {
1172 goto error;
1173 }
1174 if (ret > 0) {
1175 BUG_ON(path->slots[0] == 0);
1176 path->slots[0]--;
1177 }
1178
Chris Mason39279cc2007-06-12 06:35:45 -04001179 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001180 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -04001181 leaf = path->nodes[0];
1182 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1183 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -04001184
Chris Mason5f39d392007-10-15 16:14:19 -04001185 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -04001186 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001187
Chris Mason85e21ba2008-01-29 15:11:36 -05001188 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001189 break;
1190
Chris Mason5f39d392007-10-15 16:14:19 -04001191 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001192 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -04001193 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -04001194 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -04001195 extent_type = btrfs_file_extent_type(leaf, fi);
1196 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -04001197 item_end +=
Chris Masondb945352007-10-15 16:15:53 -04001198 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -04001199 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1200 struct btrfs_item *item = btrfs_item_nr(leaf,
1201 path->slots[0]);
1202 item_end += btrfs_file_extent_inline_len(leaf,
1203 item);
Chris Mason39279cc2007-06-12 06:35:45 -04001204 }
Yan008630c2007-11-07 13:31:09 -05001205 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -04001206 }
1207 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1208 ret = btrfs_csum_truncate(trans, root, path,
1209 inode->i_size);
1210 BUG_ON(ret);
1211 }
Yan008630c2007-11-07 13:31:09 -05001212 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -04001213 if (found_type == BTRFS_DIR_ITEM_KEY) {
1214 found_type = BTRFS_INODE_ITEM_KEY;
1215 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1216 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -05001217 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1218 found_type = BTRFS_XATTR_ITEM_KEY;
1219 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1220 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -04001221 } else if (found_type) {
1222 found_type--;
1223 } else {
1224 break;
Chris Mason39279cc2007-06-12 06:35:45 -04001225 }
Yana61721d2007-09-17 11:08:38 -04001226 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -05001227 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -04001228 }
Chris Mason5f39d392007-10-15 16:14:19 -04001229 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001230 del_item = 1;
1231 else
1232 del_item = 0;
1233 found_extent = 0;
1234
1235 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001236 if (found_type != BTRFS_EXTENT_DATA_KEY)
1237 goto delete;
1238
1239 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001240 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001241 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001242 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001243 u64 orig_num_bytes =
1244 btrfs_file_extent_num_bytes(leaf, fi);
1245 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001246 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001247 extent_num_bytes = extent_num_bytes &
1248 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001249 btrfs_set_file_extent_num_bytes(leaf, fi,
1250 extent_num_bytes);
1251 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001252 extent_num_bytes);
1253 if (extent_start != 0)
1254 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001255 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001256 } else {
Chris Masondb945352007-10-15 16:15:53 -04001257 extent_num_bytes =
1258 btrfs_file_extent_disk_num_bytes(leaf,
1259 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001260 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001261 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001262 if (extent_start != 0) {
1263 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001264 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001265 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001266 root_gen = btrfs_header_generation(leaf);
1267 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001268 }
Chris Mason90692182008-02-08 13:49:28 -05001269 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1270 if (!del_item) {
1271 u32 newsize = inode->i_size - found_key.offset;
1272 dec_i_blocks(inode, item_end + 1 -
1273 found_key.offset - newsize);
1274 newsize =
1275 btrfs_file_extent_calc_inline_size(newsize);
1276 ret = btrfs_truncate_item(trans, root, path,
1277 newsize, 1);
1278 BUG_ON(ret);
1279 } else {
1280 dec_i_blocks(inode, item_end + 1 -
1281 found_key.offset);
1282 }
Chris Mason39279cc2007-06-12 06:35:45 -04001283 }
Chris Mason179e29e2007-11-01 11:28:41 -04001284delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001285 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001286 if (!pending_del_nr) {
1287 /* no pending yet, add ourselves */
1288 pending_del_slot = path->slots[0];
1289 pending_del_nr = 1;
1290 } else if (pending_del_nr &&
1291 path->slots[0] + 1 == pending_del_slot) {
1292 /* hop on the pending chunk */
1293 pending_del_nr++;
1294 pending_del_slot = path->slots[0];
1295 } else {
1296 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1297 }
Chris Mason39279cc2007-06-12 06:35:45 -04001298 } else {
1299 break;
1300 }
Chris Mason39279cc2007-06-12 06:35:45 -04001301 if (found_extent) {
1302 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001303 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001304 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001305 root_gen, inode->i_ino,
1306 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001307 BUG_ON(ret);
1308 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001309next:
1310 if (path->slots[0] == 0) {
1311 if (pending_del_nr)
1312 goto del_pending;
1313 btrfs_release_path(root, path);
1314 goto search_again;
1315 }
1316
1317 path->slots[0]--;
1318 if (pending_del_nr &&
1319 path->slots[0] + 1 != pending_del_slot) {
1320 struct btrfs_key debug;
1321del_pending:
1322 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1323 pending_del_slot);
1324 ret = btrfs_del_items(trans, root, path,
1325 pending_del_slot,
1326 pending_del_nr);
1327 BUG_ON(ret);
1328 pending_del_nr = 0;
1329 btrfs_release_path(root, path);
1330 goto search_again;
1331 }
Chris Mason39279cc2007-06-12 06:35:45 -04001332 }
1333 ret = 0;
1334error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001335 if (pending_del_nr) {
1336 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1337 pending_del_nr);
1338 }
Chris Mason39279cc2007-06-12 06:35:45 -04001339 btrfs_free_path(path);
1340 inode->i_sb->s_dirt = 1;
1341 return ret;
1342}
1343
Chris Masona52d9a82007-08-27 16:49:44 -04001344/*
1345 * taken from block_truncate_page, but does cow as it zeros out
1346 * any bytes left in the last page in the file.
1347 */
1348static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1349{
1350 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001351 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001352 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1353 struct btrfs_ordered_extent *ordered;
1354 char *kaddr;
Chris Masondb945352007-10-15 16:15:53 -04001355 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001356 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1357 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1358 struct page *page;
1359 int ret = 0;
1360 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001361 u64 page_end;
Chris Masona52d9a82007-08-27 16:49:44 -04001362
1363 if ((offset & (blocksize - 1)) == 0)
1364 goto out;
1365
1366 ret = -ENOMEM;
Chris Mason211c17f2008-05-15 09:13:45 -04001367again:
Chris Masona52d9a82007-08-27 16:49:44 -04001368 page = grab_cache_page(mapping, index);
1369 if (!page)
1370 goto out;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001371
1372 page_start = page_offset(page);
1373 page_end = page_start + PAGE_CACHE_SIZE - 1;
1374
Chris Masona52d9a82007-08-27 16:49:44 -04001375 if (!PageUptodate(page)) {
1376 ret = btrfs_readpage(NULL, page);
1377 lock_page(page);
Chris Mason211c17f2008-05-15 09:13:45 -04001378 if (page->mapping != mapping) {
1379 unlock_page(page);
1380 page_cache_release(page);
1381 goto again;
1382 }
Chris Masona52d9a82007-08-27 16:49:44 -04001383 if (!PageUptodate(page)) {
1384 ret = -EIO;
Chris Mason89642222008-07-24 09:41:53 -04001385 goto out_unlock;
Chris Masona52d9a82007-08-27 16:49:44 -04001386 }
1387 }
Chris Mason211c17f2008-05-15 09:13:45 -04001388 wait_on_page_writeback(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001389
1390 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1391 set_page_extent_mapped(page);
1392
1393 ordered = btrfs_lookup_ordered_extent(inode, page_start);
1394 if (ordered) {
1395 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1396 unlock_page(page);
1397 page_cache_release(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04001398 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001399 btrfs_put_ordered_extent(ordered);
1400 goto again;
1401 }
1402
1403 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
1404 page_end, GFP_NOFS);
1405 ret = 0;
1406 if (offset != PAGE_CACHE_SIZE) {
1407 kaddr = kmap(page);
1408 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1409 flush_dcache_page(page);
1410 kunmap(page);
1411 }
Chris Mason247e7432008-07-17 12:53:51 -04001412 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04001413 set_page_dirty(page);
1414 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001415
Chris Mason89642222008-07-24 09:41:53 -04001416out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04001417 unlock_page(page);
1418 page_cache_release(page);
1419out:
1420 return ret;
1421}
1422
1423static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1424{
1425 struct inode *inode = dentry->d_inode;
1426 int err;
1427
1428 err = inode_change_ok(inode, attr);
1429 if (err)
1430 return err;
1431
1432 if (S_ISREG(inode->i_mode) &&
1433 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1434 struct btrfs_trans_handle *trans;
1435 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001436 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001437
Chris Mason5f39d392007-10-15 16:14:19 -04001438 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001439 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001440 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001441 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001442 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001443
Chris Mason1b0f7c22008-01-30 14:33:02 -05001444 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001445 goto out;
1446
Chris Mason1832a6d2007-12-21 16:27:21 -05001447 err = btrfs_check_free_space(root, 1, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05001448 if (err)
1449 goto fail;
1450
Chris Mason39279cc2007-06-12 06:35:45 -04001451 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1452
Chris Mason5f564062008-01-22 16:47:59 -05001453 hole_size = block_end - hole_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04001454 btrfs_wait_ordered_range(inode, hole_start, hole_size);
1455 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001456
Chris Mason39279cc2007-06-12 06:35:45 -04001457 trans = btrfs_start_transaction(root, 1);
1458 btrfs_set_trans_block_group(trans, inode);
Chris Masonee6e6502008-07-17 12:54:40 -04001459 mutex_lock(&BTRFS_I(inode)->extent_mutex);
Chris Mason2bf5a722007-08-30 11:54:02 -04001460 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001461 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001462 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001463
Chris Mason179e29e2007-11-01 11:28:41 -04001464 if (alloc_hint != EXTENT_MAP_INLINE) {
1465 err = btrfs_insert_file_extent(trans, root,
1466 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001467 hole_start, 0, 0,
Sage Weilf2eb0a22008-05-02 14:43:14 -04001468 hole_size, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001469 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001470 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001471 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001472 }
Chris Masonee6e6502008-07-17 12:54:40 -04001473 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001474 btrfs_end_transaction(trans, root);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001475 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001476 if (err)
1477 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001478 }
1479out:
1480 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001481fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001482 return err;
1483}
Chris Mason61295eb2008-01-14 16:24:38 -05001484
Chris Mason39279cc2007-06-12 06:35:45 -04001485void btrfs_delete_inode(struct inode *inode)
1486{
1487 struct btrfs_trans_handle *trans;
1488 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001489 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001490 int ret;
1491
1492 truncate_inode_pages(&inode->i_data, 0);
1493 if (is_bad_inode(inode)) {
1494 goto no_delete;
1495 }
Chris Mason4a096752008-07-21 10:29:44 -04001496 btrfs_wait_ordered_range(inode, 0, (u64)-1);
Chris Mason5f39d392007-10-15 16:14:19 -04001497
Chris Masondbe674a2008-07-17 12:54:05 -04001498 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001499 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001500
Chris Mason39279cc2007-06-12 06:35:45 -04001501 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001502 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001503 if (ret)
1504 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001505
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001506 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001507 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001508
Chris Mason39279cc2007-06-12 06:35:45 -04001509 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001510 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001511 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001512
1513no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001514 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001515 btrfs_end_transaction(trans, root);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001516 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04001517no_delete:
1518 clear_inode(inode);
1519}
1520
1521/*
1522 * this returns the key found in the dir entry in the location pointer.
1523 * If no dir entries were found, location->objectid is 0.
1524 */
1525static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1526 struct btrfs_key *location)
1527{
1528 const char *name = dentry->d_name.name;
1529 int namelen = dentry->d_name.len;
1530 struct btrfs_dir_item *di;
1531 struct btrfs_path *path;
1532 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001533 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001534
Chris Mason39544012007-12-12 14:38:19 -05001535 if (namelen == 1 && strcmp(name, ".") == 0) {
1536 location->objectid = dir->i_ino;
1537 location->type = BTRFS_INODE_ITEM_KEY;
1538 location->offset = 0;
1539 return 0;
1540 }
Chris Mason39279cc2007-06-12 06:35:45 -04001541 path = btrfs_alloc_path();
1542 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001543
Chris Mason7a720532007-12-13 09:06:59 -05001544 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001545 struct btrfs_key key;
1546 struct extent_buffer *leaf;
1547 u32 nritems;
1548 int slot;
1549
1550 key.objectid = dir->i_ino;
1551 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1552 key.offset = 0;
1553 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1554 BUG_ON(ret == 0);
1555 ret = 0;
1556
1557 leaf = path->nodes[0];
1558 slot = path->slots[0];
1559 nritems = btrfs_header_nritems(leaf);
1560 if (slot >= nritems)
1561 goto out_err;
1562
1563 btrfs_item_key_to_cpu(leaf, &key, slot);
1564 if (key.objectid != dir->i_ino ||
1565 key.type != BTRFS_INODE_REF_KEY) {
1566 goto out_err;
1567 }
1568 location->objectid = key.offset;
1569 location->type = BTRFS_INODE_ITEM_KEY;
1570 location->offset = 0;
1571 goto out;
1572 }
1573
Chris Mason39279cc2007-06-12 06:35:45 -04001574 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1575 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001576 if (IS_ERR(di))
1577 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001578 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001579 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001580 }
Chris Mason5f39d392007-10-15 16:14:19 -04001581 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001582out:
Chris Mason39279cc2007-06-12 06:35:45 -04001583 btrfs_free_path(path);
1584 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001585out_err:
1586 location->objectid = 0;
1587 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001588}
1589
1590/*
1591 * when we hit a tree root in a directory, the btrfs part of the inode
1592 * needs to be changed to reflect the root directory of the tree root. This
1593 * is kind of like crossing a mount point.
1594 */
1595static int fixup_tree_root_location(struct btrfs_root *root,
1596 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001597 struct btrfs_root **sub_root,
1598 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001599{
1600 struct btrfs_path *path;
1601 struct btrfs_root_item *ri;
1602
1603 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1604 return 0;
1605 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1606 return 0;
1607
1608 path = btrfs_alloc_path();
1609 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -04001610
Josef Bacik58176a92007-08-29 15:47:34 -04001611 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1612 dentry->d_name.name,
1613 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001614 if (IS_ERR(*sub_root))
1615 return PTR_ERR(*sub_root);
1616
1617 ri = &(*sub_root)->root_item;
1618 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001619 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1620 location->offset = 0;
1621
1622 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001623 return 0;
1624}
1625
1626static int btrfs_init_locked_inode(struct inode *inode, void *p)
1627{
1628 struct btrfs_iget_args *args = p;
1629 inode->i_ino = args->ino;
1630 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001631 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04001632 BTRFS_I(inode)->disk_i_size = 0;
Josef Bacikaec74772008-07-24 12:12:38 -04001633 BTRFS_I(inode)->index_cnt = (u64)-1;
Chris Masond1310b22008-01-24 16:13:08 -05001634 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1635 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001636 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001637 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1638 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04001639 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04001640 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04001641 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001642 return 0;
1643}
1644
1645static int btrfs_find_actor(struct inode *inode, void *opaque)
1646{
1647 struct btrfs_iget_args *args = opaque;
1648 return (args->ino == inode->i_ino &&
1649 args->root == BTRFS_I(inode)->root);
1650}
1651
Chris Masondc17ff82008-01-08 15:46:30 -05001652struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1653 u64 root_objectid)
1654{
1655 struct btrfs_iget_args args;
1656 args.ino = objectid;
1657 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1658
1659 if (!args.root)
1660 return NULL;
1661
1662 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1663}
1664
Chris Mason39279cc2007-06-12 06:35:45 -04001665struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1666 struct btrfs_root *root)
1667{
1668 struct inode *inode;
1669 struct btrfs_iget_args args;
1670 args.ino = objectid;
1671 args.root = root;
1672
1673 inode = iget5_locked(s, objectid, btrfs_find_actor,
1674 btrfs_init_locked_inode,
1675 (void *)&args);
1676 return inode;
1677}
1678
1679static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1680 struct nameidata *nd)
1681{
1682 struct inode * inode;
1683 struct btrfs_inode *bi = BTRFS_I(dir);
1684 struct btrfs_root *root = bi->root;
1685 struct btrfs_root *sub_root = root;
1686 struct btrfs_key location;
1687 int ret;
1688
1689 if (dentry->d_name.len > BTRFS_NAME_LEN)
1690 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001691
Chris Mason39279cc2007-06-12 06:35:45 -04001692 ret = btrfs_inode_by_name(dir, dentry, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001693
Chris Mason39279cc2007-06-12 06:35:45 -04001694 if (ret < 0)
1695 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001696
Chris Mason39279cc2007-06-12 06:35:45 -04001697 inode = NULL;
1698 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001699 ret = fixup_tree_root_location(root, &location, &sub_root,
1700 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001701 if (ret < 0)
1702 return ERR_PTR(ret);
1703 if (ret > 0)
1704 return ERR_PTR(-ENOENT);
1705 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1706 sub_root);
1707 if (!inode)
1708 return ERR_PTR(-EACCES);
1709 if (inode->i_state & I_NEW) {
1710 /* the inode and parent dir are two different roots */
1711 if (sub_root != root) {
1712 igrab(inode);
1713 sub_root->inode = inode;
1714 }
1715 BTRFS_I(inode)->root = sub_root;
1716 memcpy(&BTRFS_I(inode)->location, &location,
1717 sizeof(location));
1718 btrfs_read_locked_inode(inode);
1719 unlock_new_inode(inode);
1720 }
1721 }
1722 return d_splice_alias(inode, dentry);
1723}
1724
Chris Mason39279cc2007-06-12 06:35:45 -04001725static unsigned char btrfs_filetype_table[] = {
1726 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1727};
1728
1729static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1730{
Chris Mason6da6aba2007-12-18 16:15:09 -05001731 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001732 struct btrfs_root *root = BTRFS_I(inode)->root;
1733 struct btrfs_item *item;
1734 struct btrfs_dir_item *di;
1735 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001736 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001737 struct btrfs_path *path;
1738 int ret;
1739 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001740 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001741 int slot;
1742 int advance;
1743 unsigned char d_type;
1744 int over = 0;
1745 u32 di_cur;
1746 u32 di_total;
1747 u32 di_len;
1748 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001749 char tmp_name[32];
1750 char *name_ptr;
1751 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001752
1753 /* FIXME, use a real flag for deciding about the key type */
1754 if (root->fs_info->tree_root == root)
1755 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001756
Chris Mason39544012007-12-12 14:38:19 -05001757 /* special case for "." */
1758 if (filp->f_pos == 0) {
1759 over = filldir(dirent, ".", 1,
1760 1, inode->i_ino,
1761 DT_DIR);
1762 if (over)
1763 return 0;
1764 filp->f_pos = 1;
1765 }
1766
Chris Mason39279cc2007-06-12 06:35:45 -04001767 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001768 path = btrfs_alloc_path();
1769 path->reada = 2;
1770
1771 /* special case for .., just use the back ref */
1772 if (filp->f_pos == 1) {
1773 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1774 key.offset = 0;
1775 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1776 BUG_ON(ret == 0);
1777 leaf = path->nodes[0];
1778 slot = path->slots[0];
1779 nritems = btrfs_header_nritems(leaf);
1780 if (slot >= nritems) {
1781 btrfs_release_path(root, path);
1782 goto read_dir_items;
1783 }
1784 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1785 btrfs_release_path(root, path);
1786 if (found_key.objectid != key.objectid ||
1787 found_key.type != BTRFS_INODE_REF_KEY)
1788 goto read_dir_items;
1789 over = filldir(dirent, "..", 2,
1790 2, found_key.offset, DT_DIR);
1791 if (over)
1792 goto nopos;
1793 filp->f_pos = 2;
1794 }
1795
1796read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001797 btrfs_set_key_type(&key, key_type);
1798 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001799
Chris Mason39279cc2007-06-12 06:35:45 -04001800 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1801 if (ret < 0)
1802 goto err;
1803 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001804 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001805 leaf = path->nodes[0];
1806 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001807 slot = path->slots[0];
1808 if (advance || slot >= nritems) {
1809 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001810 ret = btrfs_next_leaf(root, path);
1811 if (ret)
1812 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001813 leaf = path->nodes[0];
1814 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001815 slot = path->slots[0];
1816 } else {
1817 slot++;
1818 path->slots[0]++;
1819 }
1820 }
1821 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001822 item = btrfs_item_nr(leaf, slot);
1823 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1824
1825 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001826 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001827 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001828 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001829 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001830 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001831
1832 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001833 advance = 1;
1834 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1835 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001836 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001837 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001838 struct btrfs_key location;
1839
1840 name_len = btrfs_dir_name_len(leaf, di);
1841 if (name_len < 32) {
1842 name_ptr = tmp_name;
1843 } else {
1844 name_ptr = kmalloc(name_len, GFP_NOFS);
1845 BUG_ON(!name_ptr);
1846 }
1847 read_extent_buffer(leaf, name_ptr,
1848 (unsigned long)(di + 1), name_len);
1849
1850 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1851 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001852 over = filldir(dirent, name_ptr, name_len,
1853 found_key.offset,
1854 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001855 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001856
1857 if (name_ptr != tmp_name)
1858 kfree(name_ptr);
1859
Chris Mason39279cc2007-06-12 06:35:45 -04001860 if (over)
1861 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001862 di_len = btrfs_dir_name_len(leaf, di) +
1863 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001864 di_cur += di_len;
1865 di = (struct btrfs_dir_item *)((char *)di + di_len);
1866 }
1867 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001868 if (key_type == BTRFS_DIR_INDEX_KEY)
1869 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1870 else
1871 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001872nopos:
1873 ret = 0;
1874err:
Chris Mason39279cc2007-06-12 06:35:45 -04001875 btrfs_free_path(path);
Chris Mason39279cc2007-06-12 06:35:45 -04001876 return ret;
1877}
1878
1879int btrfs_write_inode(struct inode *inode, int wait)
1880{
1881 struct btrfs_root *root = BTRFS_I(inode)->root;
1882 struct btrfs_trans_handle *trans;
1883 int ret = 0;
1884
1885 if (wait) {
Chris Masonf9295742008-07-17 12:54:14 -04001886 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001887 btrfs_set_trans_block_group(trans, inode);
1888 ret = btrfs_commit_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001889 }
1890 return ret;
1891}
1892
1893/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001894 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001895 * inode changes. But, it is most likely to find the inode in cache.
1896 * FIXME, needs more benchmarking...there are no reasons other than performance
1897 * to keep or drop this code.
1898 */
1899void btrfs_dirty_inode(struct inode *inode)
1900{
1901 struct btrfs_root *root = BTRFS_I(inode)->root;
1902 struct btrfs_trans_handle *trans;
1903
Chris Masonf9295742008-07-17 12:54:14 -04001904 trans = btrfs_join_transaction(root, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04001905 btrfs_set_trans_block_group(trans, inode);
1906 btrfs_update_inode(trans, root, inode);
1907 btrfs_end_transaction(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04001908}
1909
Josef Bacikaec74772008-07-24 12:12:38 -04001910static int btrfs_set_inode_index_count(struct inode *inode)
1911{
1912 struct btrfs_root *root = BTRFS_I(inode)->root;
1913 struct btrfs_key key, found_key;
1914 struct btrfs_path *path;
1915 struct extent_buffer *leaf;
1916 int ret;
1917
1918 key.objectid = inode->i_ino;
1919 btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
1920 key.offset = (u64)-1;
1921
1922 path = btrfs_alloc_path();
1923 if (!path)
1924 return -ENOMEM;
1925
1926 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1927 if (ret < 0)
1928 goto out;
1929 /* FIXME: we should be able to handle this */
1930 if (ret == 0)
1931 goto out;
1932 ret = 0;
1933
1934 /*
1935 * MAGIC NUMBER EXPLANATION:
1936 * since we search a directory based on f_pos we have to start at 2
1937 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
1938 * else has to start at 2
1939 */
1940 if (path->slots[0] == 0) {
1941 BTRFS_I(inode)->index_cnt = 2;
1942 goto out;
1943 }
1944
1945 path->slots[0]--;
1946
1947 leaf = path->nodes[0];
1948 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1949
1950 if (found_key.objectid != inode->i_ino ||
1951 btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
1952 BTRFS_I(inode)->index_cnt = 2;
1953 goto out;
1954 }
1955
1956 BTRFS_I(inode)->index_cnt = found_key.offset + 1;
1957out:
1958 btrfs_free_path(path);
1959 return ret;
1960}
1961
1962static int btrfs_set_inode_index(struct inode *dir, struct inode *inode)
1963{
1964 int ret = 0;
1965
1966 if (BTRFS_I(dir)->index_cnt == (u64)-1) {
1967 ret = btrfs_set_inode_index_count(dir);
1968 if (ret)
1969 return ret;
1970 }
1971
1972 BTRFS_I(inode)->index = BTRFS_I(dir)->index_cnt;
1973 BTRFS_I(dir)->index_cnt++;
1974
1975 return ret;
1976}
1977
Chris Mason39279cc2007-06-12 06:35:45 -04001978static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1979 struct btrfs_root *root,
Josef Bacikaec74772008-07-24 12:12:38 -04001980 struct inode *dir,
Chris Mason9c583092008-01-29 15:15:18 -05001981 const char *name, int name_len,
1982 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001983 u64 objectid,
1984 struct btrfs_block_group_cache *group,
1985 int mode)
1986{
1987 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001988 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001989 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001990 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001991 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001992 struct btrfs_inode_ref *ref;
1993 struct btrfs_key key[2];
1994 u32 sizes[2];
1995 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001996 int ret;
1997 int owner;
1998
Chris Mason5f39d392007-10-15 16:14:19 -04001999 path = btrfs_alloc_path();
2000 BUG_ON(!path);
2001
Chris Mason39279cc2007-06-12 06:35:45 -04002002 inode = new_inode(root->fs_info->sb);
2003 if (!inode)
2004 return ERR_PTR(-ENOMEM);
2005
Josef Bacikaec74772008-07-24 12:12:38 -04002006 if (dir) {
2007 ret = btrfs_set_inode_index(dir, inode);
2008 if (ret)
2009 return ERR_PTR(ret);
2010 } else {
2011 BTRFS_I(inode)->index = 0;
2012 }
2013 /*
2014 * index_cnt is ignored for everything but a dir,
2015 * btrfs_get_inode_index_count has an explanation for the magic
2016 * number
2017 */
2018 BTRFS_I(inode)->index_cnt = 2;
2019
Chris Masond1310b22008-01-24 16:13:08 -05002020 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2021 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04002022 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002023 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2024 inode->i_mapping, GFP_NOFS);
Chris Masonba1da2f2008-07-17 12:54:15 -04002025 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason1b1e2132008-06-25 16:01:31 -04002026 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002027 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002028 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002029 BTRFS_I(inode)->disk_i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002030 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04002031
Chris Mason39279cc2007-06-12 06:35:45 -04002032 if (mode & S_IFDIR)
2033 owner = 0;
2034 else
2035 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04002036 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002037 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04002038 if (!new_inode_group) {
2039 printk("find_block group failed\n");
2040 new_inode_group = group;
2041 }
2042 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05002043 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05002044
2045 key[0].objectid = objectid;
2046 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2047 key[0].offset = 0;
2048
2049 key[1].objectid = objectid;
2050 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2051 key[1].offset = ref_objectid;
2052
2053 sizes[0] = sizeof(struct btrfs_inode_item);
2054 sizes[1] = name_len + sizeof(*ref);
2055
2056 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2057 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04002058 goto fail;
2059
Chris Mason9c583092008-01-29 15:15:18 -05002060 if (objectid > root->highest_inode)
2061 root->highest_inode = objectid;
2062
Chris Mason39279cc2007-06-12 06:35:45 -04002063 inode->i_uid = current->fsuid;
2064 inode->i_gid = current->fsgid;
2065 inode->i_mode = mode;
2066 inode->i_ino = objectid;
2067 inode->i_blocks = 0;
2068 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04002069 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2070 struct btrfs_inode_item);
2071 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002072
2073 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2074 struct btrfs_inode_ref);
2075 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
Josef Bacikaec74772008-07-24 12:12:38 -04002076 btrfs_set_inode_ref_index(path->nodes[0], ref, BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002077 ptr = (unsigned long)(ref + 1);
2078 write_extent_buffer(path->nodes[0], name, ptr, name_len);
2079
Chris Mason5f39d392007-10-15 16:14:19 -04002080 btrfs_mark_buffer_dirty(path->nodes[0]);
2081 btrfs_free_path(path);
2082
Chris Mason39279cc2007-06-12 06:35:45 -04002083 location = &BTRFS_I(inode)->location;
2084 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04002085 location->offset = 0;
2086 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2087
Chris Mason39279cc2007-06-12 06:35:45 -04002088 insert_inode_hash(inode);
2089 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002090fail:
Josef Bacikaec74772008-07-24 12:12:38 -04002091 if (dir)
2092 BTRFS_I(dir)->index_cnt--;
Chris Mason5f39d392007-10-15 16:14:19 -04002093 btrfs_free_path(path);
2094 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04002095}
2096
2097static inline u8 btrfs_inode_type(struct inode *inode)
2098{
2099 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2100}
2101
2102static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002103 struct dentry *dentry, struct inode *inode,
2104 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002105{
2106 int ret;
2107 struct btrfs_key key;
2108 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Josef Bacikaec74772008-07-24 12:12:38 -04002109 struct inode *parent_inode = dentry->d_parent->d_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04002110
Chris Mason39279cc2007-06-12 06:35:45 -04002111 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04002112 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2113 key.offset = 0;
2114
2115 ret = btrfs_insert_dir_item(trans, root,
2116 dentry->d_name.name, dentry->d_name.len,
2117 dentry->d_parent->d_inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002118 &key, btrfs_inode_type(inode),
2119 BTRFS_I(inode)->index);
Chris Mason39279cc2007-06-12 06:35:45 -04002120 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05002121 if (add_backref) {
2122 ret = btrfs_insert_inode_ref(trans, root,
2123 dentry->d_name.name,
2124 dentry->d_name.len,
2125 inode->i_ino,
Josef Bacikaec74772008-07-24 12:12:38 -04002126 parent_inode->i_ino,
2127 BTRFS_I(inode)->index);
Chris Mason9c583092008-01-29 15:15:18 -05002128 }
Chris Masondbe674a2008-07-17 12:54:05 -04002129 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2130 dentry->d_name.len * 2);
Chris Mason79c44582007-06-25 10:09:33 -04002131 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04002132 ret = btrfs_update_inode(trans, root,
2133 dentry->d_parent->d_inode);
2134 }
2135 return ret;
2136}
2137
2138static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05002139 struct dentry *dentry, struct inode *inode,
2140 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04002141{
Chris Mason9c583092008-01-29 15:15:18 -05002142 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04002143 if (!err) {
2144 d_instantiate(dentry, inode);
2145 return 0;
2146 }
2147 if (err > 0)
2148 err = -EEXIST;
2149 return err;
2150}
2151
Josef Bacik618e21d2007-07-11 10:18:17 -04002152static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2153 int mode, dev_t rdev)
2154{
2155 struct btrfs_trans_handle *trans;
2156 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002157 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04002158 int err;
2159 int drop_inode = 0;
2160 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05002161 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04002162
2163 if (!new_valid_dev(rdev))
2164 return -EINVAL;
2165
Chris Mason1832a6d2007-12-21 16:27:21 -05002166 err = btrfs_check_free_space(root, 1, 0);
2167 if (err)
2168 goto fail;
2169
Josef Bacik618e21d2007-07-11 10:18:17 -04002170 trans = btrfs_start_transaction(root, 1);
2171 btrfs_set_trans_block_group(trans, dir);
2172
2173 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2174 if (err) {
2175 err = -ENOSPC;
2176 goto out_unlock;
2177 }
2178
Josef Bacikaec74772008-07-24 12:12:38 -04002179 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002180 dentry->d_name.len,
2181 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04002182 BTRFS_I(dir)->block_group, mode);
2183 err = PTR_ERR(inode);
2184 if (IS_ERR(inode))
2185 goto out_unlock;
2186
2187 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002188 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04002189 if (err)
2190 drop_inode = 1;
2191 else {
2192 inode->i_op = &btrfs_special_inode_operations;
2193 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04002194 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04002195 }
2196 dir->i_sb->s_dirt = 1;
2197 btrfs_update_inode_block_group(trans, inode);
2198 btrfs_update_inode_block_group(trans, dir);
2199out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002200 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002201 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002202fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04002203 if (drop_inode) {
2204 inode_dec_link_count(inode);
2205 iput(inode);
2206 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002207 btrfs_btree_balance_dirty(root, nr);
Josef Bacik618e21d2007-07-11 10:18:17 -04002208 return err;
2209}
2210
Chris Mason39279cc2007-06-12 06:35:45 -04002211static int btrfs_create(struct inode *dir, struct dentry *dentry,
2212 int mode, struct nameidata *nd)
2213{
2214 struct btrfs_trans_handle *trans;
2215 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05002216 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002217 int err;
2218 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05002219 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002220 u64 objectid;
2221
Chris Mason1832a6d2007-12-21 16:27:21 -05002222 err = btrfs_check_free_space(root, 1, 0);
2223 if (err)
2224 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002225 trans = btrfs_start_transaction(root, 1);
2226 btrfs_set_trans_block_group(trans, dir);
2227
2228 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2229 if (err) {
2230 err = -ENOSPC;
2231 goto out_unlock;
2232 }
2233
Josef Bacikaec74772008-07-24 12:12:38 -04002234 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002235 dentry->d_name.len,
2236 dentry->d_parent->d_inode->i_ino,
2237 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04002238 err = PTR_ERR(inode);
2239 if (IS_ERR(inode))
2240 goto out_unlock;
2241
2242 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002243 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002244 if (err)
2245 drop_inode = 1;
2246 else {
2247 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04002248 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04002249 inode->i_fop = &btrfs_file_operations;
2250 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002251 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2252 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002253 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04002254 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
2255 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04002256 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04002257 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05002258 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04002259 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002260 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04002261 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04002262 }
2263 dir->i_sb->s_dirt = 1;
2264 btrfs_update_inode_block_group(trans, inode);
2265 btrfs_update_inode_block_group(trans, dir);
2266out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002267 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002268 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002269fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002270 if (drop_inode) {
2271 inode_dec_link_count(inode);
2272 iput(inode);
2273 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002274 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002275 return err;
2276}
2277
2278static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2279 struct dentry *dentry)
2280{
2281 struct btrfs_trans_handle *trans;
2282 struct btrfs_root *root = BTRFS_I(dir)->root;
2283 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002284 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002285 int err;
2286 int drop_inode = 0;
2287
2288 if (inode->i_nlink == 0)
2289 return -ENOENT;
2290
Chris Mason6da6aba2007-12-18 16:15:09 -05002291#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2292 inode->i_nlink++;
2293#else
Chris Mason39279cc2007-06-12 06:35:45 -04002294 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002295#endif
Chris Mason1832a6d2007-12-21 16:27:21 -05002296 err = btrfs_check_free_space(root, 1, 0);
2297 if (err)
2298 goto fail;
Josef Bacikaec74772008-07-24 12:12:38 -04002299 err = btrfs_set_inode_index(dir, inode);
2300 if (err)
2301 goto fail;
2302
Chris Mason39279cc2007-06-12 06:35:45 -04002303 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002304
Chris Mason39279cc2007-06-12 06:35:45 -04002305 btrfs_set_trans_block_group(trans, dir);
2306 atomic_inc(&inode->i_count);
Josef Bacikaec74772008-07-24 12:12:38 -04002307
Chris Mason9c583092008-01-29 15:15:18 -05002308 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002309
Chris Mason39279cc2007-06-12 06:35:45 -04002310 if (err)
2311 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002312
Chris Mason39279cc2007-06-12 06:35:45 -04002313 dir->i_sb->s_dirt = 1;
2314 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002315 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002316
Chris Mason54aa1f42007-06-22 14:16:25 -04002317 if (err)
2318 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002319
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002320 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002321 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002322fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002323 if (drop_inode) {
2324 inode_dec_link_count(inode);
2325 iput(inode);
2326 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002327 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002328 return err;
2329}
2330
Chris Mason39279cc2007-06-12 06:35:45 -04002331static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2332{
Chris Masonb9d86662008-05-02 16:13:49 -04002333 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002334 struct btrfs_trans_handle *trans;
2335 struct btrfs_root *root = BTRFS_I(dir)->root;
2336 int err = 0;
2337 int drop_on_err = 0;
Chris Masonb9d86662008-05-02 16:13:49 -04002338 u64 objectid = 0;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002339 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002340
Chris Mason1832a6d2007-12-21 16:27:21 -05002341 err = btrfs_check_free_space(root, 1, 0);
2342 if (err)
2343 goto out_unlock;
2344
Chris Mason39279cc2007-06-12 06:35:45 -04002345 trans = btrfs_start_transaction(root, 1);
2346 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002347
Chris Mason39279cc2007-06-12 06:35:45 -04002348 if (IS_ERR(trans)) {
2349 err = PTR_ERR(trans);
2350 goto out_unlock;
2351 }
2352
2353 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2354 if (err) {
2355 err = -ENOSPC;
2356 goto out_unlock;
2357 }
2358
Josef Bacikaec74772008-07-24 12:12:38 -04002359 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05002360 dentry->d_name.len,
2361 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002362 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2363 if (IS_ERR(inode)) {
2364 err = PTR_ERR(inode);
2365 goto out_fail;
2366 }
Chris Mason5f39d392007-10-15 16:14:19 -04002367
Chris Mason39279cc2007-06-12 06:35:45 -04002368 drop_on_err = 1;
2369 inode->i_op = &btrfs_dir_inode_operations;
2370 inode->i_fop = &btrfs_dir_file_operations;
2371 btrfs_set_trans_block_group(trans, inode);
2372
Chris Masondbe674a2008-07-17 12:54:05 -04002373 btrfs_i_size_write(inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002374 err = btrfs_update_inode(trans, root, inode);
2375 if (err)
2376 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002377
Chris Mason9c583092008-01-29 15:15:18 -05002378 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002379 if (err)
2380 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002381
Chris Mason39279cc2007-06-12 06:35:45 -04002382 d_instantiate(dentry, inode);
2383 drop_on_err = 0;
2384 dir->i_sb->s_dirt = 1;
2385 btrfs_update_inode_block_group(trans, inode);
2386 btrfs_update_inode_block_group(trans, dir);
2387
2388out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002389 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04002390 btrfs_end_transaction_throttle(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002391
Chris Mason39279cc2007-06-12 06:35:45 -04002392out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002393 if (drop_on_err)
2394 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002395 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002396 return err;
2397}
2398
Chris Mason3b951512008-04-17 11:29:12 -04002399static int merge_extent_mapping(struct extent_map_tree *em_tree,
2400 struct extent_map *existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002401 struct extent_map *em,
2402 u64 map_start, u64 map_len)
Chris Mason3b951512008-04-17 11:29:12 -04002403{
2404 u64 start_diff;
Chris Mason3b951512008-04-17 11:29:12 -04002405
Chris Masone6dcd2d2008-07-17 12:53:50 -04002406 BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2407 start_diff = map_start - em->start;
2408 em->start = map_start;
2409 em->len = map_len;
2410 if (em->block_start < EXTENT_MAP_LAST_BYTE)
2411 em->block_start += start_diff;
2412 return add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002413}
2414
Chris Masona52d9a82007-08-27 16:49:44 -04002415struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002416 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002417 int create)
2418{
2419 int ret;
2420 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002421 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002422 u64 extent_start = 0;
2423 u64 extent_end = 0;
2424 u64 objectid = inode->i_ino;
2425 u32 found_type;
Chris Masonf4219502008-07-22 11:18:09 -04002426 struct btrfs_path *path = NULL;
Chris Masona52d9a82007-08-27 16:49:44 -04002427 struct btrfs_root *root = BTRFS_I(inode)->root;
2428 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002429 struct extent_buffer *leaf;
2430 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002431 struct extent_map *em = NULL;
2432 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002433 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002434 struct btrfs_trans_handle *trans = NULL;
2435
Chris Masona52d9a82007-08-27 16:49:44 -04002436again:
Chris Masond1310b22008-01-24 16:13:08 -05002437 spin_lock(&em_tree->lock);
2438 em = lookup_extent_mapping(em_tree, start, len);
Chris Masona061fc82008-05-07 11:43:44 -04002439 if (em)
2440 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002441 spin_unlock(&em_tree->lock);
2442
Chris Masona52d9a82007-08-27 16:49:44 -04002443 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002444 if (em->start > start || em->start + em->len <= start)
2445 free_extent_map(em);
2446 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002447 free_extent_map(em);
2448 else
2449 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002450 }
Chris Masond1310b22008-01-24 16:13:08 -05002451 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002452 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002453 err = -ENOMEM;
2454 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002455 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002456 em->bdev = root->fs_info->fs_devices->latest_bdev;
Chris Masond1310b22008-01-24 16:13:08 -05002457 em->start = EXTENT_MAP_HOLE;
2458 em->len = (u64)-1;
Chris Masonf4219502008-07-22 11:18:09 -04002459
2460 if (!path) {
2461 path = btrfs_alloc_path();
2462 BUG_ON(!path);
2463 }
2464
Chris Mason179e29e2007-11-01 11:28:41 -04002465 ret = btrfs_lookup_file_extent(trans, root, path,
2466 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002467 if (ret < 0) {
2468 err = ret;
2469 goto out;
2470 }
2471
2472 if (ret != 0) {
2473 if (path->slots[0] == 0)
2474 goto not_found;
2475 path->slots[0]--;
2476 }
2477
Chris Mason5f39d392007-10-15 16:14:19 -04002478 leaf = path->nodes[0];
2479 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002480 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002481 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002482 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2483 found_type = btrfs_key_type(&found_key);
2484 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002485 found_type != BTRFS_EXTENT_DATA_KEY) {
2486 goto not_found;
2487 }
2488
Chris Mason5f39d392007-10-15 16:14:19 -04002489 found_type = btrfs_file_extent_type(leaf, item);
2490 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002491 if (found_type == BTRFS_FILE_EXTENT_REG) {
2492 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002493 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002494 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002495 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002496 em->start = start;
2497 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002498 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002499 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002500 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002501 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002502 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002503 }
2504 goto not_found_em;
2505 }
Chris Masondb945352007-10-15 16:15:53 -04002506 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2507 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002508 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002509 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002510 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002511 goto insert;
2512 }
Chris Masondb945352007-10-15 16:15:53 -04002513 bytenr += btrfs_file_extent_offset(leaf, item);
2514 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002515 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002516 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002517 goto insert;
2518 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002519 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002520 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002521 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002522 size_t size;
2523 size_t extent_offset;
2524 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002525
Chris Mason5f39d392007-10-15 16:14:19 -04002526 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2527 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002528 extent_end = (extent_start + size + root->sectorsize - 1) &
2529 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002530 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002531 em->start = start;
2532 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002533 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002534 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002535 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002536 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002537 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002538 }
2539 goto not_found_em;
2540 }
2541 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002542
2543 if (!page) {
2544 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002545 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002546 goto out;
2547 }
2548
Chris Mason70dec802008-01-29 09:59:12 -05002549 page_start = page_offset(page) + pg_offset;
2550 extent_offset = page_start - extent_start;
2551 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002552 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002553 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002554 em->len = (copy_size + root->sectorsize - 1) &
2555 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002556 map = kmap(page);
2557 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002558 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002559 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002560 copy_size);
2561 flush_dcache_page(page);
2562 } else if (create && PageUptodate(page)) {
2563 if (!trans) {
2564 kunmap(page);
2565 free_extent_map(em);
2566 em = NULL;
2567 btrfs_release_path(root, path);
Chris Masonf9295742008-07-17 12:54:14 -04002568 trans = btrfs_join_transaction(root, 1);
Chris Mason179e29e2007-11-01 11:28:41 -04002569 goto again;
2570 }
Chris Mason70dec802008-01-29 09:59:12 -05002571 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002572 copy_size);
2573 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002574 }
Chris Masona52d9a82007-08-27 16:49:44 -04002575 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002576 set_extent_uptodate(io_tree, em->start,
2577 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002578 goto insert;
2579 } else {
2580 printk("unkknown found_type %d\n", found_type);
2581 WARN_ON(1);
2582 }
2583not_found:
2584 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002585 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002586not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002587 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002588insert:
2589 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002590 if (em->start > start || extent_map_end(em) <= start) {
2591 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002592 err = -EIO;
2593 goto out;
2594 }
Chris Masond1310b22008-01-24 16:13:08 -05002595
2596 err = 0;
2597 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002598 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002599 /* it is possible that someone inserted the extent into the tree
2600 * while we had the lock dropped. It is also possible that
2601 * an overlapping map exists in the tree
2602 */
Chris Masona52d9a82007-08-27 16:49:44 -04002603 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002604 struct extent_map *existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002605
2606 ret = 0;
2607
Chris Mason3b951512008-04-17 11:29:12 -04002608 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002609 if (existing && (existing->start > start ||
2610 existing->start + existing->len <= start)) {
2611 free_extent_map(existing);
2612 existing = NULL;
2613 }
Chris Mason3b951512008-04-17 11:29:12 -04002614 if (!existing) {
2615 existing = lookup_extent_mapping(em_tree, em->start,
2616 em->len);
2617 if (existing) {
2618 err = merge_extent_mapping(em_tree, existing,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002619 em, start,
2620 root->sectorsize);
Chris Mason3b951512008-04-17 11:29:12 -04002621 free_extent_map(existing);
2622 if (err) {
2623 free_extent_map(em);
2624 em = NULL;
2625 }
2626 } else {
2627 err = -EIO;
2628 printk("failing to insert %Lu %Lu\n",
2629 start, len);
2630 free_extent_map(em);
2631 em = NULL;
2632 }
2633 } else {
2634 free_extent_map(em);
2635 em = existing;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002636 err = 0;
Chris Masona52d9a82007-08-27 16:49:44 -04002637 }
Chris Masona52d9a82007-08-27 16:49:44 -04002638 }
Chris Masond1310b22008-01-24 16:13:08 -05002639 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002640out:
Chris Masonf4219502008-07-22 11:18:09 -04002641 if (path)
2642 btrfs_free_path(path);
Chris Masona52d9a82007-08-27 16:49:44 -04002643 if (trans) {
2644 ret = btrfs_end_transaction(trans, root);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002645 if (!err) {
Chris Masona52d9a82007-08-27 16:49:44 -04002646 err = ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002647 }
Chris Masona52d9a82007-08-27 16:49:44 -04002648 }
Chris Masona52d9a82007-08-27 16:49:44 -04002649 if (err) {
2650 free_extent_map(em);
2651 WARN_ON(1);
2652 return ERR_PTR(err);
2653 }
2654 return em;
2655}
2656
Chris Masone1c4b742008-04-22 13:26:46 -04002657#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002658static int btrfs_get_block(struct inode *inode, sector_t iblock,
2659 struct buffer_head *bh_result, int create)
2660{
2661 struct extent_map *em;
2662 u64 start = (u64)iblock << inode->i_blkbits;
2663 struct btrfs_multi_bio *multi = NULL;
2664 struct btrfs_root *root = BTRFS_I(inode)->root;
2665 u64 len;
2666 u64 logical;
2667 u64 map_length;
2668 int ret = 0;
2669
2670 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2671
2672 if (!em || IS_ERR(em))
2673 goto out;
2674
Chris Masone1c4b742008-04-22 13:26:46 -04002675 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002676 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002677 }
Chris Mason16432982008-04-10 10:23:21 -04002678
2679 if (em->block_start == EXTENT_MAP_INLINE) {
2680 ret = -EINVAL;
2681 goto out;
2682 }
2683
Chris Mason16432982008-04-10 10:23:21 -04002684 len = em->start + em->len - start;
2685 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2686
Chris Masone1c4b742008-04-22 13:26:46 -04002687 if (em->block_start == EXTENT_MAP_HOLE ||
2688 em->block_start == EXTENT_MAP_DELALLOC) {
2689 bh_result->b_size = len;
2690 goto out;
2691 }
2692
Chris Mason16432982008-04-10 10:23:21 -04002693 logical = start - em->start;
2694 logical = em->block_start + logical;
2695
2696 map_length = len;
2697 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2698 logical, &map_length, &multi, 0);
2699 BUG_ON(ret);
2700 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2701 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002702
Chris Mason16432982008-04-10 10:23:21 -04002703 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2704 set_buffer_mapped(bh_result);
2705 kfree(multi);
2706out:
2707 free_extent_map(em);
2708 return ret;
2709}
Chris Masone1c4b742008-04-22 13:26:46 -04002710#endif
Chris Mason16432982008-04-10 10:23:21 -04002711
2712static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2713 const struct iovec *iov, loff_t offset,
2714 unsigned long nr_segs)
2715{
Chris Masone1c4b742008-04-22 13:26:46 -04002716 return -EINVAL;
2717#if 0
Chris Mason16432982008-04-10 10:23:21 -04002718 struct file *file = iocb->ki_filp;
2719 struct inode *inode = file->f_mapping->host;
2720
2721 if (rw == WRITE)
2722 return -EINVAL;
2723
2724 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2725 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002726#endif
Chris Mason16432982008-04-10 10:23:21 -04002727}
2728
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002729static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002730{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002731 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002732}
2733
Chris Mason9ebefb182007-06-15 13:50:00 -04002734int btrfs_readpage(struct file *file, struct page *page)
2735{
Chris Masond1310b22008-01-24 16:13:08 -05002736 struct extent_io_tree *tree;
2737 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002738 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002739}
Chris Mason1832a6d2007-12-21 16:27:21 -05002740
Chris Mason39279cc2007-06-12 06:35:45 -04002741static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2742{
Chris Masond1310b22008-01-24 16:13:08 -05002743 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002744
2745
2746 if (current->flags & PF_MEMALLOC) {
2747 redirty_page_for_writepage(wbc, page);
2748 unlock_page(page);
2749 return 0;
2750 }
Chris Masond1310b22008-01-24 16:13:08 -05002751 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002752 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2753}
Chris Mason39279cc2007-06-12 06:35:45 -04002754
Chris Masonf4219502008-07-22 11:18:09 -04002755int btrfs_writepages(struct address_space *mapping,
2756 struct writeback_control *wbc)
Chris Masonb293f022007-11-01 19:45:34 -04002757{
Chris Masond1310b22008-01-24 16:13:08 -05002758 struct extent_io_tree *tree;
2759 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002760 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2761}
2762
Chris Mason3ab2fb52007-11-08 10:59:22 -05002763static int
2764btrfs_readpages(struct file *file, struct address_space *mapping,
2765 struct list_head *pages, unsigned nr_pages)
2766{
Chris Masond1310b22008-01-24 16:13:08 -05002767 struct extent_io_tree *tree;
2768 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002769 return extent_readpages(tree, mapping, pages, nr_pages,
2770 btrfs_get_extent);
2771}
Chris Masone6dcd2d2008-07-17 12:53:50 -04002772static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002773{
Chris Masond1310b22008-01-24 16:13:08 -05002774 struct extent_io_tree *tree;
2775 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002776 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002777
Chris Masond1310b22008-01-24 16:13:08 -05002778 tree = &BTRFS_I(page->mapping->host)->io_tree;
2779 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002780 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002781 if (ret == 1) {
2782 ClearPagePrivate(page);
2783 set_page_private(page, 0);
2784 page_cache_release(page);
2785 }
2786 return ret;
2787}
Chris Mason39279cc2007-06-12 06:35:45 -04002788
Chris Masone6dcd2d2008-07-17 12:53:50 -04002789static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2790{
Chris Masone6dcd2d2008-07-17 12:53:50 -04002791 return __btrfs_releasepage(page, gfp_flags);
2792}
2793
Chris Masona52d9a82007-08-27 16:49:44 -04002794static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2795{
Chris Masond1310b22008-01-24 16:13:08 -05002796 struct extent_io_tree *tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002797 struct btrfs_ordered_extent *ordered;
2798 u64 page_start = page_offset(page);
2799 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002800
Chris Masone6dcd2d2008-07-17 12:53:50 -04002801 wait_on_page_writeback(page);
Chris Masond1310b22008-01-24 16:13:08 -05002802 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002803 if (offset) {
2804 btrfs_releasepage(page, GFP_NOFS);
2805 return;
2806 }
2807
2808 lock_extent(tree, page_start, page_end, GFP_NOFS);
2809 ordered = btrfs_lookup_ordered_extent(page->mapping->host,
2810 page_offset(page));
2811 if (ordered) {
Chris Masoneb84ae02008-07-17 13:53:27 -04002812 /*
2813 * IO on this page will never be started, so we need
2814 * to account for any ordered extents now
2815 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002816 clear_extent_bit(tree, page_start, page_end,
2817 EXTENT_DIRTY | EXTENT_DELALLOC |
2818 EXTENT_LOCKED, 1, 0, GFP_NOFS);
Chris Mason211f90e2008-07-18 11:56:15 -04002819 btrfs_finish_ordered_io(page->mapping->host,
2820 page_start, page_end);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002821 btrfs_put_ordered_extent(ordered);
2822 lock_extent(tree, page_start, page_end, GFP_NOFS);
2823 }
2824 clear_extent_bit(tree, page_start, page_end,
2825 EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
2826 EXTENT_ORDERED,
2827 1, 1, GFP_NOFS);
2828 __btrfs_releasepage(page, GFP_NOFS);
2829
Chris Mason4a096752008-07-21 10:29:44 -04002830 ClearPageChecked(page);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002831 if (PagePrivate(page)) {
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002832 ClearPagePrivate(page);
2833 set_page_private(page, 0);
2834 page_cache_release(page);
2835 }
Chris Mason39279cc2007-06-12 06:35:45 -04002836}
2837
Chris Mason9ebefb182007-06-15 13:50:00 -04002838/*
2839 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2840 * called from a page fault handler when a page is first dirtied. Hence we must
2841 * be careful to check for EOF conditions here. We set the page up correctly
2842 * for a written page which means we get ENOSPC checking when writing into
2843 * holes and correct delalloc and unwritten extent mapping on filesystems that
2844 * support these features.
2845 *
2846 * We are not allowed to take the i_mutex here so we have to play games to
2847 * protect against truncate races as the page could now be beyond EOF. Because
2848 * vmtruncate() writes the inode size before removing pages, once we have the
2849 * page lock we can determine safely if the page is beyond EOF. If it is not
2850 * beyond EOF, then the page is guaranteed safe against truncation until we
2851 * unlock the page.
2852 */
2853int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2854{
Chris Mason6da6aba2007-12-18 16:15:09 -05002855 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002856 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002857 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2858 struct btrfs_ordered_extent *ordered;
2859 char *kaddr;
2860 unsigned long zero_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002861 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002862 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002863 u64 page_start;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002864 u64 page_end;
Chris Mason9ebefb182007-06-15 13:50:00 -04002865
Chris Mason1832a6d2007-12-21 16:27:21 -05002866 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason1832a6d2007-12-21 16:27:21 -05002867 if (ret)
2868 goto out;
2869
2870 ret = -EINVAL;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002871again:
Chris Mason9ebefb182007-06-15 13:50:00 -04002872 lock_page(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002873 size = i_size_read(inode);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002874 page_start = page_offset(page);
2875 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masona52d9a82007-08-27 16:49:44 -04002876
Chris Mason9ebefb182007-06-15 13:50:00 -04002877 if ((page->mapping != inode->i_mapping) ||
Chris Masone6dcd2d2008-07-17 12:53:50 -04002878 (page_start >= size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002879 /* page got truncated out from underneath us */
2880 goto out_unlock;
2881 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002882 wait_on_page_writeback(page);
2883
2884 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2885 set_page_extent_mapped(page);
2886
Chris Masoneb84ae02008-07-17 13:53:27 -04002887 /*
2888 * we can't set the delalloc bits if there are pending ordered
2889 * extents. Drop our locks and wait for them to finish
2890 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002891 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2892 if (ordered) {
2893 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2894 unlock_page(page);
Chris Masoneb84ae02008-07-17 13:53:27 -04002895 btrfs_start_ordered_extent(inode, ordered, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002896 btrfs_put_ordered_extent(ordered);
2897 goto again;
2898 }
2899
2900 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
2901 page_end, GFP_NOFS);
2902 ret = 0;
Chris Mason9ebefb182007-06-15 13:50:00 -04002903
2904 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002905 if (page_start + PAGE_CACHE_SIZE > size)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002906 zero_start = size & ~PAGE_CACHE_MASK;
Chris Mason9ebefb182007-06-15 13:50:00 -04002907 else
Chris Masone6dcd2d2008-07-17 12:53:50 -04002908 zero_start = PAGE_CACHE_SIZE;
Chris Mason9ebefb182007-06-15 13:50:00 -04002909
Chris Masone6dcd2d2008-07-17 12:53:50 -04002910 if (zero_start != PAGE_CACHE_SIZE) {
2911 kaddr = kmap(page);
2912 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
2913 flush_dcache_page(page);
2914 kunmap(page);
2915 }
Chris Mason247e7432008-07-17 12:53:51 -04002916 ClearPageChecked(page);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002917 set_page_dirty(page);
2918 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason9ebefb182007-06-15 13:50:00 -04002919
2920out_unlock:
2921 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002922out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002923 return ret;
2924}
2925
Chris Mason39279cc2007-06-12 06:35:45 -04002926static void btrfs_truncate(struct inode *inode)
2927{
2928 struct btrfs_root *root = BTRFS_I(inode)->root;
2929 int ret;
2930 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002931 unsigned long nr;
Chris Masondbe674a2008-07-17 12:54:05 -04002932 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002933
2934 if (!S_ISREG(inode->i_mode))
2935 return;
2936 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2937 return;
2938
2939 btrfs_truncate_page(inode->i_mapping, inode->i_size);
Chris Mason4a096752008-07-21 10:29:44 -04002940 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002941
Chris Mason39279cc2007-06-12 06:35:45 -04002942 trans = btrfs_start_transaction(root, 1);
2943 btrfs_set_trans_block_group(trans, inode);
Chris Masondbe674a2008-07-17 12:54:05 -04002944 btrfs_i_size_write(inode, inode->i_size);
Chris Mason39279cc2007-06-12 06:35:45 -04002945
2946 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002947 ret = btrfs_truncate_in_trans(trans, root, inode,
2948 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002949 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002950 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002951
Chris Mason89ce8a62008-06-25 16:01:31 -04002952 ret = btrfs_end_transaction_throttle(trans, root);
Chris Mason39279cc2007-06-12 06:35:45 -04002953 BUG_ON(ret);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002954 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04002955}
2956
Sven Wegener3b963622008-06-09 21:57:42 -04002957/*
2958 * Invalidate a single dcache entry at the root of the filesystem.
2959 * Needed after creation of snapshot or subvolume.
2960 */
2961void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
2962 int namelen)
2963{
2964 struct dentry *alias, *entry;
2965 struct qstr qstr;
2966
2967 alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
2968 if (alias) {
2969 qstr.name = name;
2970 qstr.len = namelen;
2971 /* change me if btrfs ever gets a d_hash operation */
2972 qstr.hash = full_name_hash(qstr.name, qstr.len);
2973 entry = d_lookup(alias, &qstr);
2974 dput(alias);
2975 if (entry) {
2976 d_invalidate(entry);
2977 dput(entry);
2978 }
2979 }
2980}
2981
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002982int btrfs_create_subvol_root(struct btrfs_root *new_root,
2983 struct btrfs_trans_handle *trans, u64 new_dirid,
2984 struct btrfs_block_group_cache *block_group)
Chris Mason39279cc2007-06-12 06:35:45 -04002985{
Chris Mason39279cc2007-06-12 06:35:45 -04002986 struct inode *inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002987
Josef Bacikaec74772008-07-24 12:12:38 -04002988 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002989 new_dirid, block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002990 if (IS_ERR(inode))
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002991 return PTR_ERR(inode);
Chris Mason39279cc2007-06-12 06:35:45 -04002992 inode->i_op = &btrfs_dir_inode_operations;
2993 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002994 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002995
Chris Mason39279cc2007-06-12 06:35:45 -04002996 inode->i_nlink = 1;
Chris Masondbe674a2008-07-17 12:54:05 -04002997 btrfs_i_size_write(inode, 0);
Sven Wegener3b963622008-06-09 21:57:42 -04002998
Christoph Hellwigf46b5a62008-06-11 21:53:53 -04002999 return btrfs_update_inode(trans, new_root, inode);
Chris Mason39279cc2007-06-12 06:35:45 -04003000}
3001
Chris Masonedbd8d42007-12-21 16:27:24 -05003002unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04003003 struct file_ra_state *ra, struct file *file,
3004 pgoff_t offset, pgoff_t last_index)
3005{
Chris Mason8e7bf942008-04-28 09:02:36 -04003006 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04003007
3008#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04003009 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3010 return offset;
3011#else
Chris Mason86479a02007-09-10 19:58:16 -04003012 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3013 return offset + req_size;
3014#endif
3015}
3016
Chris Mason39279cc2007-06-12 06:35:45 -04003017struct inode *btrfs_alloc_inode(struct super_block *sb)
3018{
3019 struct btrfs_inode *ei;
3020
3021 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3022 if (!ei)
3023 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003024 ei->last_trans = 0;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003025 btrfs_ordered_inode_tree_init(&ei->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003026 return &ei->vfs_inode;
3027}
3028
3029void btrfs_destroy_inode(struct inode *inode)
3030{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003031 struct btrfs_ordered_extent *ordered;
Chris Mason39279cc2007-06-12 06:35:45 -04003032 WARN_ON(!list_empty(&inode->i_dentry));
3033 WARN_ON(inode->i_data.nrpages);
3034
Chris Masone6dcd2d2008-07-17 12:53:50 -04003035 while(1) {
3036 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3037 if (!ordered)
3038 break;
3039 else {
3040 printk("found ordered extent %Lu %Lu\n",
3041 ordered->file_offset, ordered->len);
3042 btrfs_remove_ordered_extent(inode, ordered);
3043 btrfs_put_ordered_extent(ordered);
3044 btrfs_put_ordered_extent(ordered);
3045 }
3046 }
Chris Mason8c416c92008-01-14 15:10:26 -05003047 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003048 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3049}
3050
Chris Mason44ec0b72007-10-29 10:55:05 -04003051#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3052static void init_once(struct kmem_cache * cachep, void *foo)
3053#else
Chris Mason39279cc2007-06-12 06:35:45 -04003054static void init_once(void * foo, struct kmem_cache * cachep,
3055 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003056#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003057{
3058 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3059
3060 inode_init_once(&ei->vfs_inode);
3061}
3062
3063void btrfs_destroy_cachep(void)
3064{
3065 if (btrfs_inode_cachep)
3066 kmem_cache_destroy(btrfs_inode_cachep);
3067 if (btrfs_trans_handle_cachep)
3068 kmem_cache_destroy(btrfs_trans_handle_cachep);
3069 if (btrfs_transaction_cachep)
3070 kmem_cache_destroy(btrfs_transaction_cachep);
3071 if (btrfs_bit_radix_cachep)
3072 kmem_cache_destroy(btrfs_bit_radix_cachep);
3073 if (btrfs_path_cachep)
3074 kmem_cache_destroy(btrfs_path_cachep);
3075}
3076
Chris Mason86479a02007-09-10 19:58:16 -04003077struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003078 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04003079#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3080 void (*ctor)(struct kmem_cache *, void *)
3081#else
Chris Mason92fee662007-07-25 12:31:35 -04003082 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003083 unsigned long)
3084#endif
3085 )
Chris Mason92fee662007-07-25 12:31:35 -04003086{
3087 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3088 SLAB_MEM_SPREAD | extra_flags), ctor
3089#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3090 ,NULL
3091#endif
3092 );
3093}
3094
Chris Mason39279cc2007-06-12 06:35:45 -04003095int btrfs_init_cachep(void)
3096{
Chris Mason86479a02007-09-10 19:58:16 -04003097 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003098 sizeof(struct btrfs_inode),
3099 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003100 if (!btrfs_inode_cachep)
3101 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003102 btrfs_trans_handle_cachep =
3103 btrfs_cache_create("btrfs_trans_handle_cache",
3104 sizeof(struct btrfs_trans_handle),
3105 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003106 if (!btrfs_trans_handle_cachep)
3107 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003108 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003109 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003110 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003111 if (!btrfs_transaction_cachep)
3112 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003113 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003114 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003115 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003116 if (!btrfs_path_cachep)
3117 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003118 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003119 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003120 if (!btrfs_bit_radix_cachep)
3121 goto fail;
3122 return 0;
3123fail:
3124 btrfs_destroy_cachep();
3125 return -ENOMEM;
3126}
3127
3128static int btrfs_getattr(struct vfsmount *mnt,
3129 struct dentry *dentry, struct kstat *stat)
3130{
3131 struct inode *inode = dentry->d_inode;
3132 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003133 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003134 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003135 return 0;
3136}
3137
3138static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3139 struct inode * new_dir,struct dentry *new_dentry)
3140{
3141 struct btrfs_trans_handle *trans;
3142 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3143 struct inode *new_inode = new_dentry->d_inode;
3144 struct inode *old_inode = old_dentry->d_inode;
3145 struct timespec ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04003146 int ret;
3147
3148 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3149 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3150 return -ENOTEMPTY;
3151 }
Chris Mason5f39d392007-10-15 16:14:19 -04003152
Chris Mason1832a6d2007-12-21 16:27:21 -05003153 ret = btrfs_check_free_space(root, 1, 0);
3154 if (ret)
3155 goto out_unlock;
3156
Chris Mason39279cc2007-06-12 06:35:45 -04003157 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003158
Chris Mason39279cc2007-06-12 06:35:45 -04003159 btrfs_set_trans_block_group(trans, new_dir);
Chris Mason39279cc2007-06-12 06:35:45 -04003160
3161 old_dentry->d_inode->i_nlink++;
3162 old_dir->i_ctime = old_dir->i_mtime = ctime;
3163 new_dir->i_ctime = new_dir->i_mtime = ctime;
3164 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003165
Chris Mason39279cc2007-06-12 06:35:45 -04003166 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3167 if (ret)
3168 goto out_fail;
3169
3170 if (new_inode) {
3171 new_inode->i_ctime = CURRENT_TIME;
3172 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3173 if (ret)
3174 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003175 }
Josef Bacikaec74772008-07-24 12:12:38 -04003176 ret = btrfs_set_inode_index(new_dir, old_inode);
3177 if (ret)
3178 goto out_fail;
3179
Chris Mason9c583092008-01-29 15:15:18 -05003180 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003181 if (ret)
3182 goto out_fail;
3183
3184out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003185 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003186out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003187 return ret;
3188}
3189
3190static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3191 const char *symname)
3192{
3193 struct btrfs_trans_handle *trans;
3194 struct btrfs_root *root = BTRFS_I(dir)->root;
3195 struct btrfs_path *path;
3196 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003197 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003198 int err;
3199 int drop_inode = 0;
3200 u64 objectid;
3201 int name_len;
3202 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003203 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003204 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003205 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003206 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003207
3208 name_len = strlen(symname) + 1;
3209 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3210 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003211
Chris Mason1832a6d2007-12-21 16:27:21 -05003212 err = btrfs_check_free_space(root, 1, 0);
3213 if (err)
3214 goto out_fail;
3215
Chris Mason39279cc2007-06-12 06:35:45 -04003216 trans = btrfs_start_transaction(root, 1);
3217 btrfs_set_trans_block_group(trans, dir);
3218
3219 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3220 if (err) {
3221 err = -ENOSPC;
3222 goto out_unlock;
3223 }
3224
Josef Bacikaec74772008-07-24 12:12:38 -04003225 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
Chris Mason9c583092008-01-29 15:15:18 -05003226 dentry->d_name.len,
3227 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003228 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3229 err = PTR_ERR(inode);
3230 if (IS_ERR(inode))
3231 goto out_unlock;
3232
3233 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003234 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003235 if (err)
3236 drop_inode = 1;
3237 else {
3238 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003239 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003240 inode->i_fop = &btrfs_file_operations;
3241 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003242 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3243 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003244 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003245 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3246 inode->i_mapping, GFP_NOFS);
Chris Mason1b1e2132008-06-25 16:01:31 -04003247 mutex_init(&BTRFS_I(inode)->csum_mutex);
Chris Masonee6e6502008-07-17 12:54:40 -04003248 mutex_init(&BTRFS_I(inode)->extent_mutex);
Chris Mason90692182008-02-08 13:49:28 -05003249 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masondbe674a2008-07-17 12:54:05 -04003250 BTRFS_I(inode)->disk_i_size = 0;
Chris Masond1310b22008-01-24 16:13:08 -05003251 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Masonba1da2f2008-07-17 12:54:15 -04003252 btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
Chris Mason39279cc2007-06-12 06:35:45 -04003253 }
3254 dir->i_sb->s_dirt = 1;
3255 btrfs_update_inode_block_group(trans, inode);
3256 btrfs_update_inode_block_group(trans, dir);
3257 if (drop_inode)
3258 goto out_unlock;
3259
3260 path = btrfs_alloc_path();
3261 BUG_ON(!path);
3262 key.objectid = inode->i_ino;
3263 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003264 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3265 datasize = btrfs_file_extent_calc_inline_size(name_len);
3266 err = btrfs_insert_empty_item(trans, root, path, &key,
3267 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003268 if (err) {
3269 drop_inode = 1;
3270 goto out_unlock;
3271 }
Chris Mason5f39d392007-10-15 16:14:19 -04003272 leaf = path->nodes[0];
3273 ei = btrfs_item_ptr(leaf, path->slots[0],
3274 struct btrfs_file_extent_item);
3275 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3276 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003277 BTRFS_FILE_EXTENT_INLINE);
3278 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003279 write_extent_buffer(leaf, symname, ptr, name_len);
3280 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003281 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003282
Chris Mason39279cc2007-06-12 06:35:45 -04003283 inode->i_op = &btrfs_symlink_inode_operations;
3284 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003285 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masondbe674a2008-07-17 12:54:05 -04003286 btrfs_i_size_write(inode, name_len - 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04003287 err = btrfs_update_inode(trans, root, inode);
3288 if (err)
3289 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003290
3291out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003292 nr = trans->blocks_used;
Chris Mason89ce8a62008-06-25 16:01:31 -04003293 btrfs_end_transaction_throttle(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003294out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003295 if (drop_inode) {
3296 inode_dec_link_count(inode);
3297 iput(inode);
3298 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003299 btrfs_btree_balance_dirty(root, nr);
Chris Mason39279cc2007-06-12 06:35:45 -04003300 return err;
3301}
Chris Mason16432982008-04-10 10:23:21 -04003302
Chris Masone6dcd2d2008-07-17 12:53:50 -04003303static int btrfs_set_page_dirty(struct page *page)
3304{
Chris Masone6dcd2d2008-07-17 12:53:50 -04003305 return __set_page_dirty_nobuffers(page);
3306}
3307
Yanfdebe2b2008-01-14 13:26:08 -05003308static int btrfs_permission(struct inode *inode, int mask,
3309 struct nameidata *nd)
3310{
3311 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3312 return -EACCES;
3313 return generic_permission(inode, mask, NULL);
3314}
Chris Mason39279cc2007-06-12 06:35:45 -04003315
3316static struct inode_operations btrfs_dir_inode_operations = {
3317 .lookup = btrfs_lookup,
3318 .create = btrfs_create,
3319 .unlink = btrfs_unlink,
3320 .link = btrfs_link,
3321 .mkdir = btrfs_mkdir,
3322 .rmdir = btrfs_rmdir,
3323 .rename = btrfs_rename,
3324 .symlink = btrfs_symlink,
3325 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003326 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003327 .setxattr = generic_setxattr,
3328 .getxattr = generic_getxattr,
3329 .listxattr = btrfs_listxattr,
3330 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003331 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003332};
Chris Mason39279cc2007-06-12 06:35:45 -04003333static struct inode_operations btrfs_dir_ro_inode_operations = {
3334 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003335 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003336};
Chris Mason39279cc2007-06-12 06:35:45 -04003337static struct file_operations btrfs_dir_file_operations = {
3338 .llseek = generic_file_llseek,
3339 .read = generic_read_dir,
3340 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003341 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003342#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003343 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003344#endif
Sage Weil6bf13c02008-06-10 10:07:39 -04003345 .release = btrfs_release_file,
Chris Mason39279cc2007-06-12 06:35:45 -04003346};
3347
Chris Masond1310b22008-01-24 16:13:08 -05003348static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003349 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003350 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003351 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003352 .readpage_io_hook = btrfs_readpage_io_hook,
3353 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003354 .writepage_end_io_hook = btrfs_writepage_end_io_hook,
Chris Mason247e7432008-07-17 12:53:51 -04003355 .writepage_start_hook = btrfs_writepage_start_hook,
Chris Mason1259ab72008-05-12 13:39:03 -04003356 .readpage_io_failed_hook = btrfs_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003357 .set_bit_hook = btrfs_set_bit_hook,
3358 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003359};
3360
Chris Mason39279cc2007-06-12 06:35:45 -04003361static struct address_space_operations btrfs_aops = {
3362 .readpage = btrfs_readpage,
3363 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003364 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003365 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003366 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003367 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003368 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003369 .invalidatepage = btrfs_invalidatepage,
3370 .releasepage = btrfs_releasepage,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003371 .set_page_dirty = btrfs_set_page_dirty,
Chris Mason39279cc2007-06-12 06:35:45 -04003372};
3373
3374static struct address_space_operations btrfs_symlink_aops = {
3375 .readpage = btrfs_readpage,
3376 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003377 .invalidatepage = btrfs_invalidatepage,
3378 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003379};
3380
3381static struct inode_operations btrfs_file_inode_operations = {
3382 .truncate = btrfs_truncate,
3383 .getattr = btrfs_getattr,
3384 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003385 .setxattr = generic_setxattr,
3386 .getxattr = generic_getxattr,
3387 .listxattr = btrfs_listxattr,
3388 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003389 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003390};
Josef Bacik618e21d2007-07-11 10:18:17 -04003391static struct inode_operations btrfs_special_inode_operations = {
3392 .getattr = btrfs_getattr,
3393 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003394 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003395};
Chris Mason39279cc2007-06-12 06:35:45 -04003396static struct inode_operations btrfs_symlink_inode_operations = {
3397 .readlink = generic_readlink,
3398 .follow_link = page_follow_link_light,
3399 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003400 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003401};