blob: 1a74b5018699d5c5d181afe5e75e84e739ac6e82 [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>
22#include <linux/fs.h>
23#include <linux/pagemap.h>
24#include <linux/highmem.h>
25#include <linux/time.h>
26#include <linux/init.h>
27#include <linux/string.h>
28#include <linux/smp_lock.h>
29#include <linux/backing-dev.h>
30#include <linux/mpage.h>
31#include <linux/swap.h>
32#include <linux/writeback.h>
33#include <linux/statfs.h>
34#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040035#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040036#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050037#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040038#include "ctree.h"
39#include "disk-io.h"
40#include "transaction.h"
41#include "btrfs_inode.h"
42#include "ioctl.h"
43#include "print-tree.h"
Chris Mason0b86a832008-03-24 15:01:56 -040044#include "volumes.h"
Chris Mason39279cc2007-06-12 06:35:45 -040045
46struct btrfs_iget_args {
47 u64 ino;
48 struct btrfs_root *root;
49};
50
51static struct inode_operations btrfs_dir_inode_operations;
52static struct inode_operations btrfs_symlink_inode_operations;
53static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040054static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040055static struct inode_operations btrfs_file_inode_operations;
56static struct address_space_operations btrfs_aops;
57static struct address_space_operations btrfs_symlink_aops;
58static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050059static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040060
61static struct kmem_cache *btrfs_inode_cachep;
62struct kmem_cache *btrfs_trans_handle_cachep;
63struct kmem_cache *btrfs_transaction_cachep;
64struct kmem_cache *btrfs_bit_radix_cachep;
65struct kmem_cache *btrfs_path_cachep;
66
67#define S_SHIFT 12
68static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
69 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
70 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
71 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
72 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
73 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
74 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
75 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
76};
77
Chris Mason1832a6d2007-12-21 16:27:21 -050078int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
79 int for_del)
80{
81 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
82 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
83 u64 thresh;
Chris Masonbcbfce82008-04-22 13:26:47 -040084 unsigned long flags;
Chris Mason1832a6d2007-12-21 16:27:21 -050085 int ret = 0;
86
87 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050088 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050089 else
Chris Masonf9ef6602008-01-03 09:22:38 -050090 thresh = total * 85;
91
92 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050093
Chris Masonbcbfce82008-04-22 13:26:47 -040094 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -050095 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
96 ret = -ENOSPC;
Chris Masonbcbfce82008-04-22 13:26:47 -040097 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason1832a6d2007-12-21 16:27:21 -050098 return ret;
99}
100
Chris Masonbe20aa92007-12-17 20:14:01 -0500101static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -0400102{
103 struct btrfs_root *root = BTRFS_I(inode)->root;
104 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400105 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400106 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500107 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400108 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500109 u64 orig_start = start;
110 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500111 struct btrfs_key ins;
112 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400113
Chris Masonb888db22007-08-27 16:49:44 -0400114 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400115 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500116 btrfs_set_trans_block_group(trans, inode);
117
Chris Masondb945352007-10-15 16:15:53 -0400118 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500119 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400120 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400121 start, start + num_bytes, start, &alloc_hint);
Chris Masond1310b22008-01-24 16:13:08 -0500122 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400123
Chris Mason179e29e2007-11-01 11:28:41 -0400124 if (alloc_hint == EXTENT_MAP_INLINE)
125 goto out;
126
Chris Mason3b951512008-04-17 11:29:12 -0400127 BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
128
Chris Masonc59f8952007-12-17 20:14:04 -0500129 while(num_bytes > 0) {
130 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
131 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
Chris Mason98d20f62008-04-14 09:46:10 -0400132 root->sectorsize,
Chris Masonc59f8952007-12-17 20:14:04 -0500133 root->root_key.objectid,
134 trans->transid,
135 inode->i_ino, start, 0,
136 alloc_hint, (u64)-1, &ins, 1);
137 if (ret) {
138 WARN_ON(1);
139 goto out;
140 }
Chris Mason98d20f62008-04-14 09:46:10 -0400141 cur_alloc_size = ins.offset;
Chris Masonc59f8952007-12-17 20:14:04 -0500142 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
143 start, ins.objectid, ins.offset,
144 ins.offset);
Chris Mason90692182008-02-08 13:49:28 -0500145 inode->i_blocks += ins.offset >> 9;
Chris Mason5f564062008-01-22 16:47:59 -0500146 btrfs_check_file(root, inode);
Chris Mason3b951512008-04-17 11:29:12 -0400147 if (num_bytes < cur_alloc_size) {
148 printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
149 cur_alloc_size);
150 break;
151 }
Chris Masonc59f8952007-12-17 20:14:04 -0500152 num_bytes -= cur_alloc_size;
153 alloc_hint = ins.objectid + ins.offset;
154 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400155 }
Chris Masond1310b22008-01-24 16:13:08 -0500156 btrfs_drop_extent_cache(inode, orig_start,
157 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500158 btrfs_add_ordered_inode(inode);
Chris Mason90692182008-02-08 13:49:28 -0500159 btrfs_update_inode(trans, root, inode);
Chris Masonb888db22007-08-27 16:49:44 -0400160out:
161 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500162 return ret;
163}
164
165static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
166{
167 u64 extent_start;
168 u64 extent_end;
169 u64 bytenr;
170 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500171 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500172 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500173 struct btrfs_root *root = BTRFS_I(inode)->root;
174 struct extent_buffer *leaf;
175 int found_type;
176 struct btrfs_path *path;
177 struct btrfs_file_extent_item *item;
178 int ret;
179 int err;
180 struct btrfs_key found_key;
181
Chris Masonc31f8832008-01-08 15:46:31 -0500182 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500183 path = btrfs_alloc_path();
184 BUG_ON(!path);
185again:
186 ret = btrfs_lookup_file_extent(NULL, root, path,
187 inode->i_ino, start, 0);
188 if (ret < 0) {
189 btrfs_free_path(path);
190 return ret;
191 }
192
193 cow_end = end;
194 if (ret != 0) {
195 if (path->slots[0] == 0)
196 goto not_found;
197 path->slots[0]--;
198 }
199
200 leaf = path->nodes[0];
201 item = btrfs_item_ptr(leaf, path->slots[0],
202 struct btrfs_file_extent_item);
203
204 /* are we inside the extent that was found? */
205 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
206 found_type = btrfs_key_type(&found_key);
207 if (found_key.objectid != inode->i_ino ||
208 found_type != BTRFS_EXTENT_DATA_KEY) {
209 goto not_found;
210 }
211
212 found_type = btrfs_file_extent_type(leaf, item);
213 extent_start = found_key.offset;
214 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500215 u64 extent_num_bytes;
216
217 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
218 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500219 err = 0;
220
Chris Mason1832a6d2007-12-21 16:27:21 -0500221 if (loops && start != extent_start)
222 goto not_found;
223
Chris Masonbe20aa92007-12-17 20:14:01 -0500224 if (start < extent_start || start >= extent_end)
225 goto not_found;
226
227 cow_end = min(end, extent_end - 1);
228 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
229 if (bytenr == 0)
230 goto not_found;
231
Chris Masonc31f8832008-01-08 15:46:31 -0500232 /*
233 * we may be called by the resizer, make sure we're inside
234 * the limits of the FS
235 */
236 if (bytenr + extent_num_bytes > total_fs_bytes)
237 goto not_found;
238
Chris Masonbe20aa92007-12-17 20:14:01 -0500239 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
240 goto not_found;
241 }
242
243 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500244 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500245 goto not_found;
246 }
247loop:
248 if (start > end) {
249 btrfs_free_path(path);
250 return 0;
251 }
252 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500253 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500254 goto again;
255
256not_found:
257 cow_file_range(inode, start, cow_end);
258 start = cow_end + 1;
259 goto loop;
260}
261
262static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
263{
264 struct btrfs_root *root = BTRFS_I(inode)->root;
265 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500266 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500267 if (btrfs_test_opt(root, NODATACOW) ||
268 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500269 ret = run_delalloc_nocow(inode, start, end);
270 else
271 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500272
Chris Masonb888db22007-08-27 16:49:44 -0400273 mutex_unlock(&root->fs_info->fs_mutex);
274 return ret;
275}
276
Chris Mason291d6732008-01-29 15:55:23 -0500277int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500278 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500279{
Chris Masonbcbfce82008-04-22 13:26:47 -0400280 unsigned long flags;
Chris Masonb0c68f82008-01-31 11:05:37 -0500281 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500282 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400283 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Mason90692182008-02-08 13:49:28 -0500284 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500285 root->fs_info->delalloc_bytes += end - start + 1;
Chris Masonbcbfce82008-04-22 13:26:47 -0400286 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500287 }
288 return 0;
289}
290
291int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500292 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500293{
Chris Masonb0c68f82008-01-31 11:05:37 -0500294 if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500295 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masonbcbfce82008-04-22 13:26:47 -0400296 unsigned long flags;
297
298 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
Chris Masonb0c68f82008-01-31 11:05:37 -0500299 if (end - start + 1 > root->fs_info->delalloc_bytes) {
300 printk("warning: delalloc account %Lu %Lu\n",
301 end - start + 1, root->fs_info->delalloc_bytes);
302 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500303 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500304 } else {
305 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500306 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500307 }
Chris Masonbcbfce82008-04-22 13:26:47 -0400308 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
Chris Mason291d6732008-01-29 15:55:23 -0500309 }
310 return 0;
311}
312
Chris Mason239b14b2008-03-24 15:02:07 -0400313int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
314 size_t size, struct bio *bio)
315{
316 struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
317 struct btrfs_mapping_tree *map_tree;
Chris Mason239b14b2008-03-24 15:02:07 -0400318 u64 logical = bio->bi_sector << 9;
Chris Mason239b14b2008-03-24 15:02:07 -0400319 u64 length = 0;
320 u64 map_length;
Chris Mason239b14b2008-03-24 15:02:07 -0400321 int ret;
322
Chris Masonf2d8d742008-04-21 10:03:05 -0400323 length = bio->bi_size;
Chris Mason239b14b2008-03-24 15:02:07 -0400324 map_tree = &root->fs_info->mapping_tree;
325 map_length = length;
Chris Masoncea9e442008-04-09 16:28:12 -0400326 ret = btrfs_map_block(map_tree, READ, logical,
Chris Masonf1885912008-04-09 16:28:12 -0400327 &map_length, NULL, 0);
Chris Masoncea9e442008-04-09 16:28:12 -0400328
Chris Mason239b14b2008-03-24 15:02:07 -0400329 if (map_length < length + size) {
Chris Mason239b14b2008-03-24 15:02:07 -0400330 return 1;
331 }
332 return 0;
333}
334
Chris Mason44b8bd72008-04-16 11:14:51 -0400335int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
Chris Masonf1885912008-04-09 16:28:12 -0400336 int mirror_num)
Chris Mason065631f2008-02-20 12:07:25 -0500337{
Chris Mason065631f2008-02-20 12:07:25 -0500338 struct btrfs_root *root = BTRFS_I(inode)->root;
339 struct btrfs_trans_handle *trans;
340 int ret = 0;
Chris Masone0156402008-04-16 11:15:20 -0400341 char *sums = NULL;
342
343 ret = btrfs_csum_one_bio(root, bio, &sums);
344 BUG_ON(ret);
Chris Mason065631f2008-02-20 12:07:25 -0500345
Chris Mason44b8bd72008-04-16 11:14:51 -0400346 mutex_lock(&root->fs_info->fs_mutex);
347 trans = btrfs_start_transaction(root, 1);
Chris Masone0156402008-04-16 11:15:20 -0400348
Chris Mason44b8bd72008-04-16 11:14:51 -0400349 btrfs_set_trans_block_group(trans, inode);
Chris Masone0156402008-04-16 11:15:20 -0400350 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
351
Chris Mason44b8bd72008-04-16 11:14:51 -0400352 ret = btrfs_end_transaction(trans, root);
353 BUG_ON(ret);
354 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masone0156402008-04-16 11:15:20 -0400355
356 kfree(sums);
357
Chris Mason44b8bd72008-04-16 11:14:51 -0400358 return btrfs_map_bio(root, rw, bio, mirror_num);
359}
360
361int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
362 int mirror_num)
363{
364 struct btrfs_root *root = BTRFS_I(inode)->root;
365 int ret = 0;
366
Chris Mason22c59942008-04-09 16:28:12 -0400367 if (!(rw & (1 << BIO_RW))) {
368 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
369 BUG_ON(ret);
Chris Mason0b86a832008-03-24 15:01:56 -0400370 goto mapit;
371 }
Chris Mason065631f2008-02-20 12:07:25 -0500372
373 if (btrfs_test_opt(root, NODATASUM) ||
Chris Mason0b86a832008-03-24 15:01:56 -0400374 btrfs_test_flag(inode, NODATASUM)) {
375 goto mapit;
376 }
Chris Mason065631f2008-02-20 12:07:25 -0500377
Chris Mason44b8bd72008-04-16 11:14:51 -0400378 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
379 inode, rw, bio, mirror_num,
380 __btrfs_submit_bio_hook);
Chris Mason0b86a832008-03-24 15:01:56 -0400381mapit:
Chris Masonf1885912008-04-09 16:28:12 -0400382 return btrfs_map_bio(root, rw, bio, mirror_num);
Chris Mason065631f2008-02-20 12:07:25 -0500383}
Chris Mason6885f302008-02-20 16:11:05 -0500384
Chris Mason07157aa2007-08-30 08:50:51 -0400385int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
386{
387 int ret = 0;
388 struct inode *inode = page->mapping->host;
389 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500390 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400391 struct btrfs_csum_item *item;
392 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400393 u32 csum;
Chris Mason699122f2008-04-16 12:59:22 -0400394
Yanb98b6762008-01-08 15:54:37 -0500395 if (btrfs_test_opt(root, NODATASUM) ||
396 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500397 return 0;
Chris Mason699122f2008-04-16 12:59:22 -0400398
Chris Mason07157aa2007-08-30 08:50:51 -0400399 mutex_lock(&root->fs_info->fs_mutex);
400 path = btrfs_alloc_path();
401 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
402 if (IS_ERR(item)) {
403 ret = PTR_ERR(item);
404 /* a csum that isn't present is a preallocated region. */
405 if (ret == -ENOENT || ret == -EFBIG)
406 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400407 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500408 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400409 goto out;
410 }
Chris Masonff79f812007-10-15 16:22:25 -0400411 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
412 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500413 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400414out:
415 if (path)
416 btrfs_free_path(path);
417 mutex_unlock(&root->fs_info->fs_mutex);
418 return ret;
419}
420
Chris Mason7e383262008-04-09 16:28:12 -0400421struct io_failure_record {
422 struct page *page;
423 u64 start;
424 u64 len;
425 u64 logical;
426 int last_mirror;
427};
428
429int btrfs_readpage_io_failed_hook(struct bio *failed_bio,
430 struct page *page, u64 start, u64 end,
431 struct extent_state *state)
432{
433 struct io_failure_record *failrec = NULL;
434 u64 private;
435 struct extent_map *em;
436 struct inode *inode = page->mapping->host;
437 struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
Chris Mason3b951512008-04-17 11:29:12 -0400438 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Mason7e383262008-04-09 16:28:12 -0400439 struct bio *bio;
440 int num_copies;
441 int ret;
442 u64 logical;
443
444 ret = get_state_private(failure_tree, start, &private);
445 if (ret) {
Chris Mason7e383262008-04-09 16:28:12 -0400446 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
447 if (!failrec)
448 return -ENOMEM;
449 failrec->start = start;
450 failrec->len = end - start + 1;
451 failrec->last_mirror = 0;
452
Chris Mason3b951512008-04-17 11:29:12 -0400453 spin_lock(&em_tree->lock);
454 em = lookup_extent_mapping(em_tree, start, failrec->len);
455 if (em->start > start || em->start + em->len < start) {
456 free_extent_map(em);
457 em = NULL;
458 }
459 spin_unlock(&em_tree->lock);
Chris Mason7e383262008-04-09 16:28:12 -0400460
461 if (!em || IS_ERR(em)) {
462 kfree(failrec);
463 return -EIO;
464 }
465 logical = start - em->start;
466 logical = em->block_start + logical;
467 failrec->logical = logical;
468 free_extent_map(em);
469 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
470 EXTENT_DIRTY, GFP_NOFS);
Chris Mason587f7702008-04-11 12:16:46 -0400471 set_state_private(failure_tree, start,
472 (u64)(unsigned long)failrec);
Chris Mason7e383262008-04-09 16:28:12 -0400473 } else {
Chris Mason587f7702008-04-11 12:16:46 -0400474 failrec = (struct io_failure_record *)(unsigned long)private;
Chris Mason7e383262008-04-09 16:28:12 -0400475 }
476 num_copies = btrfs_num_copies(
477 &BTRFS_I(inode)->root->fs_info->mapping_tree,
478 failrec->logical, failrec->len);
479 failrec->last_mirror++;
480 if (!state) {
481 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
482 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
483 failrec->start,
484 EXTENT_LOCKED);
485 if (state && state->start != failrec->start)
486 state = NULL;
487 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
488 }
489 if (!state || failrec->last_mirror > num_copies) {
490 set_state_private(failure_tree, failrec->start, 0);
491 clear_extent_bits(failure_tree, failrec->start,
492 failrec->start + failrec->len - 1,
493 EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
494 kfree(failrec);
495 return -EIO;
496 }
497 bio = bio_alloc(GFP_NOFS, 1);
498 bio->bi_private = state;
499 bio->bi_end_io = failed_bio->bi_end_io;
500 bio->bi_sector = failrec->logical >> 9;
501 bio->bi_bdev = failed_bio->bi_bdev;
Chris Masone1c4b742008-04-22 13:26:46 -0400502 bio->bi_size = 0;
Chris Mason7e383262008-04-09 16:28:12 -0400503 bio_add_page(bio, page, failrec->len, start - page_offset(page));
504 btrfs_submit_bio_hook(inode, READ, bio, failrec->last_mirror);
505 return 0;
506}
507
Chris Mason70dec802008-01-29 09:59:12 -0500508int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
509 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400510{
Chris Mason35ebb932007-10-30 16:56:53 -0400511 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400512 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500513 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400514 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500515 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400516 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400517 struct btrfs_root *root = BTRFS_I(inode)->root;
518 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400519 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500520
Yanb98b6762008-01-08 15:54:37 -0500521 if (btrfs_test_opt(root, NODATASUM) ||
522 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500523 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500524 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500525 private = state->private;
526 ret = 0;
527 } else {
528 ret = get_state_private(io_tree, start, &private);
529 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400530 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400531 kaddr = kmap_atomic(page, KM_IRQ0);
532 if (ret) {
533 goto zeroit;
534 }
Chris Masonff79f812007-10-15 16:22:25 -0400535 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
536 btrfs_csum_final(csum, (char *)&csum);
537 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400538 goto zeroit;
539 }
540 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400541 local_irq_restore(flags);
Chris Mason7e383262008-04-09 16:28:12 -0400542
543 /* if the io failure tree for this inode is non-empty,
544 * check to see if we've recovered from a failed IO
545 */
546 private = 0;
547 if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
548 (u64)-1, 1, EXTENT_DIRTY)) {
549 u64 private_failure;
550 struct io_failure_record *failure;
551 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
552 start, &private_failure);
553 if (ret == 0) {
Chris Mason587f7702008-04-11 12:16:46 -0400554 failure = (struct io_failure_record *)(unsigned long)
555 private_failure;
Chris Mason7e383262008-04-09 16:28:12 -0400556 set_state_private(&BTRFS_I(inode)->io_failure_tree,
557 failure->start, 0);
558 clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
559 failure->start,
560 failure->start + failure->len - 1,
561 EXTENT_DIRTY | EXTENT_LOCKED,
562 GFP_NOFS);
563 kfree(failure);
564 }
565 }
Chris Mason07157aa2007-08-30 08:50:51 -0400566 return 0;
567
568zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500569 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
570 page->mapping->host->i_ino, (unsigned long long)start, csum,
571 private);
Chris Masondb945352007-10-15 16:15:53 -0400572 memset(kaddr + offset, 1, end - start + 1);
573 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400574 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400575 local_irq_restore(flags);
Chris Mason3b951512008-04-17 11:29:12 -0400576 if (private == 0)
577 return 0;
Chris Mason7e383262008-04-09 16:28:12 -0400578 return -EIO;
Chris Mason07157aa2007-08-30 08:50:51 -0400579}
Chris Masonb888db22007-08-27 16:49:44 -0400580
Chris Mason39279cc2007-06-12 06:35:45 -0400581void btrfs_read_locked_inode(struct inode *inode)
582{
583 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400584 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400585 struct btrfs_inode_item *inode_item;
Chris Mason0b86a832008-03-24 15:01:56 -0400586 struct btrfs_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400587 struct btrfs_root *root = BTRFS_I(inode)->root;
588 struct btrfs_key location;
589 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400590 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400591 int ret;
592
593 path = btrfs_alloc_path();
594 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400595 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400596 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500597
Chris Mason39279cc2007-06-12 06:35:45 -0400598 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400599 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400600 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400601
Chris Mason5f39d392007-10-15 16:14:19 -0400602 leaf = path->nodes[0];
603 inode_item = btrfs_item_ptr(leaf, path->slots[0],
604 struct btrfs_inode_item);
605
606 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
607 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
608 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
609 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
610 inode->i_size = btrfs_inode_size(leaf, inode_item);
611
612 tspec = btrfs_inode_atime(inode_item);
613 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
614 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
615
616 tspec = btrfs_inode_mtime(inode_item);
617 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
618 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
619
620 tspec = btrfs_inode_ctime(inode_item);
621 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
622 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
623
624 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
625 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400626 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400627 rdev = btrfs_inode_rdev(leaf, inode_item);
628
629 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400630 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
631 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500632 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500633 if (!BTRFS_I(inode)->block_group) {
634 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
Chris Mason0b86a832008-03-24 15:01:56 -0400635 NULL, 0,
636 BTRFS_BLOCK_GROUP_METADATA, 0);
Chris Masone52ec0e2007-12-21 16:36:24 -0500637 }
Chris Mason39279cc2007-06-12 06:35:45 -0400638 btrfs_free_path(path);
639 inode_item = NULL;
640
641 mutex_unlock(&root->fs_info->fs_mutex);
642
643 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400644 case S_IFREG:
645 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -0400646 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Masond1310b22008-01-24 16:13:08 -0500647 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400648 inode->i_fop = &btrfs_file_operations;
649 inode->i_op = &btrfs_file_inode_operations;
650 break;
651 case S_IFDIR:
652 inode->i_fop = &btrfs_dir_file_operations;
653 if (root == root->fs_info->tree_root)
654 inode->i_op = &btrfs_dir_ro_inode_operations;
655 else
656 inode->i_op = &btrfs_dir_inode_operations;
657 break;
658 case S_IFLNK:
659 inode->i_op = &btrfs_symlink_inode_operations;
660 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -0400661 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -0400662 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400663 default:
664 init_special_inode(inode, inode->i_mode, rdev);
665 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400666 }
667 return;
668
669make_bad:
670 btrfs_release_path(root, path);
671 btrfs_free_path(path);
672 mutex_unlock(&root->fs_info->fs_mutex);
673 make_bad_inode(inode);
674}
675
Chris Mason5f39d392007-10-15 16:14:19 -0400676static void fill_inode_item(struct extent_buffer *leaf,
677 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400678 struct inode *inode)
679{
Chris Mason5f39d392007-10-15 16:14:19 -0400680 btrfs_set_inode_uid(leaf, item, inode->i_uid);
681 btrfs_set_inode_gid(leaf, item, inode->i_gid);
682 btrfs_set_inode_size(leaf, item, inode->i_size);
683 btrfs_set_inode_mode(leaf, item, inode->i_mode);
684 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
685
686 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
687 inode->i_atime.tv_sec);
688 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
689 inode->i_atime.tv_nsec);
690
691 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
692 inode->i_mtime.tv_sec);
693 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
694 inode->i_mtime.tv_nsec);
695
696 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
697 inode->i_ctime.tv_sec);
698 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
699 inode->i_ctime.tv_nsec);
700
701 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
702 btrfs_set_inode_generation(leaf, item, inode->i_generation);
703 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500704 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400705 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400706 BTRFS_I(inode)->block_group->key.objectid);
707}
708
Chris Masona52d9a82007-08-27 16:49:44 -0400709int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400710 struct btrfs_root *root,
711 struct inode *inode)
712{
713 struct btrfs_inode_item *inode_item;
714 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400715 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400716 int ret;
717
718 path = btrfs_alloc_path();
719 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400720 ret = btrfs_lookup_inode(trans, root, path,
721 &BTRFS_I(inode)->location, 1);
722 if (ret) {
723 if (ret > 0)
724 ret = -ENOENT;
725 goto failed;
726 }
727
Chris Mason5f39d392007-10-15 16:14:19 -0400728 leaf = path->nodes[0];
729 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400730 struct btrfs_inode_item);
731
Chris Mason5f39d392007-10-15 16:14:19 -0400732 fill_inode_item(leaf, inode_item, inode);
733 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400734 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400735 ret = 0;
736failed:
737 btrfs_release_path(root, path);
738 btrfs_free_path(path);
739 return ret;
740}
741
742
743static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
744 struct btrfs_root *root,
745 struct inode *dir,
746 struct dentry *dentry)
747{
748 struct btrfs_path *path;
749 const char *name = dentry->d_name.name;
750 int name_len = dentry->d_name.len;
751 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400752 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400753 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400754 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400755
756 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400757 if (!path) {
758 ret = -ENOMEM;
759 goto err;
760 }
761
Chris Mason39279cc2007-06-12 06:35:45 -0400762 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
763 name, name_len, -1);
764 if (IS_ERR(di)) {
765 ret = PTR_ERR(di);
766 goto err;
767 }
768 if (!di) {
769 ret = -ENOENT;
770 goto err;
771 }
Chris Mason5f39d392007-10-15 16:14:19 -0400772 leaf = path->nodes[0];
773 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400774 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400775 if (ret)
776 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400777 btrfs_release_path(root, path);
778
779 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400780 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400781 if (IS_ERR(di)) {
782 ret = PTR_ERR(di);
783 goto err;
784 }
785 if (!di) {
786 ret = -ENOENT;
787 goto err;
788 }
789 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400790
791 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500792 ret = btrfs_del_inode_ref(trans, root, name, name_len,
793 dentry->d_inode->i_ino,
794 dentry->d_parent->d_inode->i_ino);
795 if (ret) {
796 printk("failed to delete reference to %.*s, "
797 "inode %lu parent %lu\n", name_len, name,
798 dentry->d_inode->i_ino,
799 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500800 }
Chris Mason39279cc2007-06-12 06:35:45 -0400801err:
802 btrfs_free_path(path);
803 if (!ret) {
804 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400805 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400806 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500807#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
808 dentry->d_inode->i_nlink--;
809#else
Chris Mason39279cc2007-06-12 06:35:45 -0400810 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500811#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400812 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400813 dir->i_sb->s_dirt = 1;
814 }
815 return ret;
816}
817
818static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
819{
820 struct btrfs_root *root;
821 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500822 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400823 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500824 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400825
826 root = BTRFS_I(dir)->root;
827 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500828
829 ret = btrfs_check_free_space(root, 1, 1);
830 if (ret)
831 goto fail;
832
Chris Mason39279cc2007-06-12 06:35:45 -0400833 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400834
Chris Mason39279cc2007-06-12 06:35:45 -0400835 btrfs_set_trans_block_group(trans, dir);
836 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400837 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400838
Chris Mason2da98f02008-01-16 11:44:43 -0500839 if (inode->i_nlink == 0) {
840 int found;
841 /* if the inode isn't linked anywhere,
842 * we don't need to worry about
843 * data=ordered
844 */
845 found = btrfs_del_ordered_inode(inode);
846 if (found == 1) {
847 atomic_dec(&inode->i_count);
848 }
849 }
850
Chris Mason39279cc2007-06-12 06:35:45 -0400851 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500852fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400853 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400854 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500855 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400856 return ret;
857}
858
859static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
860{
861 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500862 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400863 int ret;
864 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400865 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500866 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400867
Yan134d4512007-10-25 15:49:25 -0400868 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
869 return -ENOTEMPTY;
870
Chris Mason39279cc2007-06-12 06:35:45 -0400871 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500872 ret = btrfs_check_free_space(root, 1, 1);
873 if (ret)
874 goto fail;
875
Chris Mason39279cc2007-06-12 06:35:45 -0400876 trans = btrfs_start_transaction(root, 1);
877 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400878
879 /* now the directory is empty */
880 err = btrfs_unlink_trans(trans, root, dir, dentry);
881 if (!err) {
882 inode->i_size = 0;
883 }
Chris Mason39544012007-12-12 14:38:19 -0500884
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400885 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400886 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500887fail:
Yan134d4512007-10-25 15:49:25 -0400888 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -0400889 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500890 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500891
Chris Mason39279cc2007-06-12 06:35:45 -0400892 if (ret && !err)
893 err = ret;
894 return err;
895}
896
Chris Mason39279cc2007-06-12 06:35:45 -0400897/*
Chris Mason39279cc2007-06-12 06:35:45 -0400898 * this can truncate away extent items, csum items and directory items.
899 * It starts at a high offset and removes keys until it can't find
900 * any higher than i_size.
901 *
902 * csum items that cross the new i_size are truncated to the new size
903 * as well.
904 */
905static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
906 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500907 struct inode *inode,
908 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400909{
910 int ret;
911 struct btrfs_path *path;
912 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400913 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400914 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400915 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400916 struct btrfs_file_extent_item *fi;
917 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400918 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400919 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500920 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500921 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400922 int found_extent;
923 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500924 int pending_del_nr = 0;
925 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400926 int extent_type = -1;
Chris Mason3b951512008-04-17 11:29:12 -0400927 u64 mask = root->sectorsize - 1;
Chris Mason39279cc2007-06-12 06:35:45 -0400928
Chris Mason3b951512008-04-17 11:29:12 -0400929 btrfs_drop_extent_cache(inode, inode->i_size & (~mask), (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400930 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400931 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400932 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400933
Chris Mason39279cc2007-06-12 06:35:45 -0400934 /* FIXME, add redo link to tree so we don't leak on crash */
935 key.objectid = inode->i_ino;
936 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400937 key.type = (u8)-1;
938
Chris Mason85e21ba2008-01-29 15:11:36 -0500939 btrfs_init_path(path);
940search_again:
941 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
942 if (ret < 0) {
943 goto error;
944 }
945 if (ret > 0) {
946 BUG_ON(path->slots[0] == 0);
947 path->slots[0]--;
948 }
949
Chris Mason39279cc2007-06-12 06:35:45 -0400950 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400951 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400952 leaf = path->nodes[0];
953 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
954 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400955
Chris Mason5f39d392007-10-15 16:14:19 -0400956 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400957 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400958
Chris Mason85e21ba2008-01-29 15:11:36 -0500959 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400960 break;
961
Chris Mason5f39d392007-10-15 16:14:19 -0400962 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400963 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400964 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400965 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400966 extent_type = btrfs_file_extent_type(leaf, fi);
967 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400968 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400969 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400970 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
971 struct btrfs_item *item = btrfs_item_nr(leaf,
972 path->slots[0]);
973 item_end += btrfs_file_extent_inline_len(leaf,
974 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400975 }
Yan008630c2007-11-07 13:31:09 -0500976 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400977 }
978 if (found_type == BTRFS_CSUM_ITEM_KEY) {
979 ret = btrfs_csum_truncate(trans, root, path,
980 inode->i_size);
981 BUG_ON(ret);
982 }
Yan008630c2007-11-07 13:31:09 -0500983 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400984 if (found_type == BTRFS_DIR_ITEM_KEY) {
985 found_type = BTRFS_INODE_ITEM_KEY;
986 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
987 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500988 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
989 found_type = BTRFS_XATTR_ITEM_KEY;
990 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
991 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400992 } else if (found_type) {
993 found_type--;
994 } else {
995 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400996 }
Yana61721d2007-09-17 11:08:38 -0400997 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500998 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400999 }
Chris Mason5f39d392007-10-15 16:14:19 -04001000 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -04001001 del_item = 1;
1002 else
1003 del_item = 0;
1004 found_extent = 0;
1005
1006 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -04001007 if (found_type != BTRFS_EXTENT_DATA_KEY)
1008 goto delete;
1009
1010 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -04001011 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -04001012 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001013 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -04001014 u64 orig_num_bytes =
1015 btrfs_file_extent_num_bytes(leaf, fi);
1016 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -04001017 found_key.offset + root->sectorsize - 1;
Yanb1632b12008-01-30 11:54:04 -05001018 extent_num_bytes = extent_num_bytes &
1019 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -04001020 btrfs_set_file_extent_num_bytes(leaf, fi,
1021 extent_num_bytes);
1022 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -05001023 extent_num_bytes);
1024 if (extent_start != 0)
1025 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -04001026 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001027 } else {
Chris Masondb945352007-10-15 16:15:53 -04001028 extent_num_bytes =
1029 btrfs_file_extent_disk_num_bytes(leaf,
1030 fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001031 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -05001032 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -04001033 if (extent_start != 0) {
1034 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -05001035 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -04001036 }
Chris Masond8d5f3e2007-12-11 12:42:00 -05001037 root_gen = btrfs_header_generation(leaf);
1038 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001039 }
Chris Mason90692182008-02-08 13:49:28 -05001040 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1041 if (!del_item) {
1042 u32 newsize = inode->i_size - found_key.offset;
1043 dec_i_blocks(inode, item_end + 1 -
1044 found_key.offset - newsize);
1045 newsize =
1046 btrfs_file_extent_calc_inline_size(newsize);
1047 ret = btrfs_truncate_item(trans, root, path,
1048 newsize, 1);
1049 BUG_ON(ret);
1050 } else {
1051 dec_i_blocks(inode, item_end + 1 -
1052 found_key.offset);
1053 }
Chris Mason39279cc2007-06-12 06:35:45 -04001054 }
Chris Mason179e29e2007-11-01 11:28:41 -04001055delete:
Chris Mason39279cc2007-06-12 06:35:45 -04001056 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -05001057 if (!pending_del_nr) {
1058 /* no pending yet, add ourselves */
1059 pending_del_slot = path->slots[0];
1060 pending_del_nr = 1;
1061 } else if (pending_del_nr &&
1062 path->slots[0] + 1 == pending_del_slot) {
1063 /* hop on the pending chunk */
1064 pending_del_nr++;
1065 pending_del_slot = path->slots[0];
1066 } else {
1067 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1068 }
Chris Mason39279cc2007-06-12 06:35:45 -04001069 } else {
1070 break;
1071 }
Chris Mason39279cc2007-06-12 06:35:45 -04001072 if (found_extent) {
1073 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -05001074 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -05001075 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -05001076 root_gen, inode->i_ino,
1077 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001078 BUG_ON(ret);
1079 }
Chris Mason85e21ba2008-01-29 15:11:36 -05001080next:
1081 if (path->slots[0] == 0) {
1082 if (pending_del_nr)
1083 goto del_pending;
1084 btrfs_release_path(root, path);
1085 goto search_again;
1086 }
1087
1088 path->slots[0]--;
1089 if (pending_del_nr &&
1090 path->slots[0] + 1 != pending_del_slot) {
1091 struct btrfs_key debug;
1092del_pending:
1093 btrfs_item_key_to_cpu(path->nodes[0], &debug,
1094 pending_del_slot);
1095 ret = btrfs_del_items(trans, root, path,
1096 pending_del_slot,
1097 pending_del_nr);
1098 BUG_ON(ret);
1099 pending_del_nr = 0;
1100 btrfs_release_path(root, path);
1101 goto search_again;
1102 }
Chris Mason39279cc2007-06-12 06:35:45 -04001103 }
1104 ret = 0;
1105error:
Chris Mason85e21ba2008-01-29 15:11:36 -05001106 if (pending_del_nr) {
1107 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1108 pending_del_nr);
1109 }
Chris Mason39279cc2007-06-12 06:35:45 -04001110 btrfs_release_path(root, path);
1111 btrfs_free_path(path);
1112 inode->i_sb->s_dirt = 1;
1113 return ret;
1114}
1115
Chris Masonb888db22007-08-27 16:49:44 -04001116static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -04001117 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001118{
Chris Mason39279cc2007-06-12 06:35:45 -04001119 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -05001120 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -04001121 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -04001122 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -05001123 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001124
Chris Mason190662b2007-12-18 16:25:45 -05001125 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -04001126 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001127
Chris Masond1310b22008-01-24 16:13:08 -05001128 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05001129 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -04001130 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -05001131
Chris Masona52d9a82007-08-27 16:49:44 -04001132 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -04001133 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001134 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
1135 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -04001136 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -04001137 }
Chris Masonb888db22007-08-27 16:49:44 -04001138 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -05001139 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001140
Chris Masona52d9a82007-08-27 16:49:44 -04001141 return ret;
1142}
1143
1144/*
1145 * taken from block_truncate_page, but does cow as it zeros out
1146 * any bytes left in the last page in the file.
1147 */
1148static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1149{
1150 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -04001151 struct btrfs_root *root = BTRFS_I(inode)->root;
1152 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -04001153 pgoff_t index = from >> PAGE_CACHE_SHIFT;
1154 unsigned offset = from & (PAGE_CACHE_SIZE-1);
1155 struct page *page;
1156 int ret = 0;
1157 u64 page_start;
1158
1159 if ((offset & (blocksize - 1)) == 0)
1160 goto out;
1161
1162 ret = -ENOMEM;
1163 page = grab_cache_page(mapping, index);
1164 if (!page)
1165 goto out;
1166 if (!PageUptodate(page)) {
1167 ret = btrfs_readpage(NULL, page);
1168 lock_page(page);
1169 if (!PageUptodate(page)) {
1170 ret = -EIO;
1171 goto out;
1172 }
1173 }
Chris Mason35ebb932007-10-30 16:56:53 -04001174 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04001175
Chris Masonb888db22007-08-27 16:49:44 -04001176 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -04001177
Chris Mason39279cc2007-06-12 06:35:45 -04001178 unlock_page(page);
1179 page_cache_release(page);
1180out:
1181 return ret;
1182}
1183
1184static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1185{
1186 struct inode *inode = dentry->d_inode;
1187 int err;
1188
1189 err = inode_change_ok(inode, attr);
1190 if (err)
1191 return err;
1192
1193 if (S_ISREG(inode->i_mode) &&
1194 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1195 struct btrfs_trans_handle *trans;
1196 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001197 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001198
Chris Mason5f39d392007-10-15 16:14:19 -04001199 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001200 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001201 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001202 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001203 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001204
Chris Mason1b0f7c22008-01-30 14:33:02 -05001205 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001206 goto out;
1207
Chris Mason1832a6d2007-12-21 16:27:21 -05001208 mutex_lock(&root->fs_info->fs_mutex);
1209 err = btrfs_check_free_space(root, 1, 0);
1210 mutex_unlock(&root->fs_info->fs_mutex);
1211 if (err)
1212 goto fail;
1213
Chris Mason39279cc2007-06-12 06:35:45 -04001214 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1215
Chris Mason1b0f7c22008-01-30 14:33:02 -05001216 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001217 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001218
1219 mutex_lock(&root->fs_info->fs_mutex);
1220 trans = btrfs_start_transaction(root, 1);
1221 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001222 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001223 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001224 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001225
Chris Mason179e29e2007-11-01 11:28:41 -04001226 if (alloc_hint != EXTENT_MAP_INLINE) {
1227 err = btrfs_insert_file_extent(trans, root,
1228 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001229 hole_start, 0, 0,
1230 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001231 btrfs_drop_extent_cache(inode, hole_start,
Chris Mason3b951512008-04-17 11:29:12 -04001232 (u64)-1);
Chris Mason5f564062008-01-22 16:47:59 -05001233 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001234 }
Chris Mason39279cc2007-06-12 06:35:45 -04001235 btrfs_end_transaction(trans, root);
1236 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001237 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001238 if (err)
1239 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001240 }
1241out:
1242 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001243fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001244 return err;
1245}
Chris Mason61295eb2008-01-14 16:24:38 -05001246
Chris Mason2da98f02008-01-16 11:44:43 -05001247void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001248{
Chris Mason2da98f02008-01-16 11:44:43 -05001249 int ret;
1250
1251 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001252 return;
1253 }
Chris Mason2da98f02008-01-16 11:44:43 -05001254
1255 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1256 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1257 return;
1258
1259 ret = btrfs_del_ordered_inode(inode);
1260 if (ret == 1) {
1261 atomic_dec(&inode->i_count);
1262 }
Chris Mason61295eb2008-01-14 16:24:38 -05001263}
1264
Chris Mason39279cc2007-06-12 06:35:45 -04001265void btrfs_delete_inode(struct inode *inode)
1266{
1267 struct btrfs_trans_handle *trans;
1268 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001269 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001270 int ret;
1271
1272 truncate_inode_pages(&inode->i_data, 0);
1273 if (is_bad_inode(inode)) {
1274 goto no_delete;
1275 }
Chris Mason5f39d392007-10-15 16:14:19 -04001276
Chris Mason39279cc2007-06-12 06:35:45 -04001277 inode->i_size = 0;
1278 mutex_lock(&root->fs_info->fs_mutex);
1279 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001280
Chris Mason39279cc2007-06-12 06:35:45 -04001281 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001282 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001283 if (ret)
1284 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001285
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001286 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001287 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001288
Chris Mason39279cc2007-06-12 06:35:45 -04001289 btrfs_end_transaction(trans, root);
1290 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001291 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001292 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001293 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001294
1295no_delete_lock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001296 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001297 btrfs_end_transaction(trans, root);
1298 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001299 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001300 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001301no_delete:
1302 clear_inode(inode);
1303}
1304
1305/*
1306 * this returns the key found in the dir entry in the location pointer.
1307 * If no dir entries were found, location->objectid is 0.
1308 */
1309static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1310 struct btrfs_key *location)
1311{
1312 const char *name = dentry->d_name.name;
1313 int namelen = dentry->d_name.len;
1314 struct btrfs_dir_item *di;
1315 struct btrfs_path *path;
1316 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001317 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001318
Chris Mason39544012007-12-12 14:38:19 -05001319 if (namelen == 1 && strcmp(name, ".") == 0) {
1320 location->objectid = dir->i_ino;
1321 location->type = BTRFS_INODE_ITEM_KEY;
1322 location->offset = 0;
1323 return 0;
1324 }
Chris Mason39279cc2007-06-12 06:35:45 -04001325 path = btrfs_alloc_path();
1326 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001327
Chris Mason7a720532007-12-13 09:06:59 -05001328 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001329 struct btrfs_key key;
1330 struct extent_buffer *leaf;
1331 u32 nritems;
1332 int slot;
1333
1334 key.objectid = dir->i_ino;
1335 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1336 key.offset = 0;
1337 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1338 BUG_ON(ret == 0);
1339 ret = 0;
1340
1341 leaf = path->nodes[0];
1342 slot = path->slots[0];
1343 nritems = btrfs_header_nritems(leaf);
1344 if (slot >= nritems)
1345 goto out_err;
1346
1347 btrfs_item_key_to_cpu(leaf, &key, slot);
1348 if (key.objectid != dir->i_ino ||
1349 key.type != BTRFS_INODE_REF_KEY) {
1350 goto out_err;
1351 }
1352 location->objectid = key.offset;
1353 location->type = BTRFS_INODE_ITEM_KEY;
1354 location->offset = 0;
1355 goto out;
1356 }
1357
Chris Mason39279cc2007-06-12 06:35:45 -04001358 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1359 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001360 if (IS_ERR(di))
1361 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001362 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001363 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001364 }
Chris Mason5f39d392007-10-15 16:14:19 -04001365 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001366out:
Chris Mason39279cc2007-06-12 06:35:45 -04001367 btrfs_free_path(path);
1368 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001369out_err:
1370 location->objectid = 0;
1371 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001372}
1373
1374/*
1375 * when we hit a tree root in a directory, the btrfs part of the inode
1376 * needs to be changed to reflect the root directory of the tree root. This
1377 * is kind of like crossing a mount point.
1378 */
1379static int fixup_tree_root_location(struct btrfs_root *root,
1380 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001381 struct btrfs_root **sub_root,
1382 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001383{
1384 struct btrfs_path *path;
1385 struct btrfs_root_item *ri;
1386
1387 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1388 return 0;
1389 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1390 return 0;
1391
1392 path = btrfs_alloc_path();
1393 BUG_ON(!path);
1394 mutex_lock(&root->fs_info->fs_mutex);
1395
Josef Bacik58176a92007-08-29 15:47:34 -04001396 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1397 dentry->d_name.name,
1398 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001399 if (IS_ERR(*sub_root))
1400 return PTR_ERR(*sub_root);
1401
1402 ri = &(*sub_root)->root_item;
1403 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001404 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1405 location->offset = 0;
1406
1407 btrfs_free_path(path);
1408 mutex_unlock(&root->fs_info->fs_mutex);
1409 return 0;
1410}
1411
1412static int btrfs_init_locked_inode(struct inode *inode, void *p)
1413{
1414 struct btrfs_iget_args *args = p;
1415 inode->i_ino = args->ino;
1416 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001417 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001418 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1419 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001420 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001421 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1422 inode->i_mapping, GFP_NOFS);
Chris Mason81d7ed22008-04-25 08:51:48 -04001423 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001424 return 0;
1425}
1426
1427static int btrfs_find_actor(struct inode *inode, void *opaque)
1428{
1429 struct btrfs_iget_args *args = opaque;
1430 return (args->ino == inode->i_ino &&
1431 args->root == BTRFS_I(inode)->root);
1432}
1433
Chris Masondc17ff82008-01-08 15:46:30 -05001434struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1435 u64 root_objectid)
1436{
1437 struct btrfs_iget_args args;
1438 args.ino = objectid;
1439 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1440
1441 if (!args.root)
1442 return NULL;
1443
1444 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1445}
1446
Chris Mason39279cc2007-06-12 06:35:45 -04001447struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1448 struct btrfs_root *root)
1449{
1450 struct inode *inode;
1451 struct btrfs_iget_args args;
1452 args.ino = objectid;
1453 args.root = root;
1454
1455 inode = iget5_locked(s, objectid, btrfs_find_actor,
1456 btrfs_init_locked_inode,
1457 (void *)&args);
1458 return inode;
1459}
1460
1461static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1462 struct nameidata *nd)
1463{
1464 struct inode * inode;
1465 struct btrfs_inode *bi = BTRFS_I(dir);
1466 struct btrfs_root *root = bi->root;
1467 struct btrfs_root *sub_root = root;
1468 struct btrfs_key location;
1469 int ret;
1470
1471 if (dentry->d_name.len > BTRFS_NAME_LEN)
1472 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001473
Chris Mason39279cc2007-06-12 06:35:45 -04001474 mutex_lock(&root->fs_info->fs_mutex);
1475 ret = btrfs_inode_by_name(dir, dentry, &location);
1476 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001477
Chris Mason39279cc2007-06-12 06:35:45 -04001478 if (ret < 0)
1479 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001480
Chris Mason39279cc2007-06-12 06:35:45 -04001481 inode = NULL;
1482 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001483 ret = fixup_tree_root_location(root, &location, &sub_root,
1484 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001485 if (ret < 0)
1486 return ERR_PTR(ret);
1487 if (ret > 0)
1488 return ERR_PTR(-ENOENT);
1489 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1490 sub_root);
1491 if (!inode)
1492 return ERR_PTR(-EACCES);
1493 if (inode->i_state & I_NEW) {
1494 /* the inode and parent dir are two different roots */
1495 if (sub_root != root) {
1496 igrab(inode);
1497 sub_root->inode = inode;
1498 }
1499 BTRFS_I(inode)->root = sub_root;
1500 memcpy(&BTRFS_I(inode)->location, &location,
1501 sizeof(location));
1502 btrfs_read_locked_inode(inode);
1503 unlock_new_inode(inode);
1504 }
1505 }
1506 return d_splice_alias(inode, dentry);
1507}
1508
Chris Mason39279cc2007-06-12 06:35:45 -04001509static unsigned char btrfs_filetype_table[] = {
1510 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1511};
1512
1513static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1514{
Chris Mason6da6aba2007-12-18 16:15:09 -05001515 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001516 struct btrfs_root *root = BTRFS_I(inode)->root;
1517 struct btrfs_item *item;
1518 struct btrfs_dir_item *di;
1519 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001520 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001521 struct btrfs_path *path;
1522 int ret;
1523 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001524 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001525 int slot;
1526 int advance;
1527 unsigned char d_type;
1528 int over = 0;
1529 u32 di_cur;
1530 u32 di_total;
1531 u32 di_len;
1532 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001533 char tmp_name[32];
1534 char *name_ptr;
1535 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001536
1537 /* FIXME, use a real flag for deciding about the key type */
1538 if (root->fs_info->tree_root == root)
1539 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001540
Chris Mason39544012007-12-12 14:38:19 -05001541 /* special case for "." */
1542 if (filp->f_pos == 0) {
1543 over = filldir(dirent, ".", 1,
1544 1, inode->i_ino,
1545 DT_DIR);
1546 if (over)
1547 return 0;
1548 filp->f_pos = 1;
1549 }
1550
Chris Mason39279cc2007-06-12 06:35:45 -04001551 mutex_lock(&root->fs_info->fs_mutex);
1552 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001553 path = btrfs_alloc_path();
1554 path->reada = 2;
1555
1556 /* special case for .., just use the back ref */
1557 if (filp->f_pos == 1) {
1558 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1559 key.offset = 0;
1560 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1561 BUG_ON(ret == 0);
1562 leaf = path->nodes[0];
1563 slot = path->slots[0];
1564 nritems = btrfs_header_nritems(leaf);
1565 if (slot >= nritems) {
1566 btrfs_release_path(root, path);
1567 goto read_dir_items;
1568 }
1569 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1570 btrfs_release_path(root, path);
1571 if (found_key.objectid != key.objectid ||
1572 found_key.type != BTRFS_INODE_REF_KEY)
1573 goto read_dir_items;
1574 over = filldir(dirent, "..", 2,
1575 2, found_key.offset, DT_DIR);
1576 if (over)
1577 goto nopos;
1578 filp->f_pos = 2;
1579 }
1580
1581read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001582 btrfs_set_key_type(&key, key_type);
1583 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001584
Chris Mason39279cc2007-06-12 06:35:45 -04001585 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1586 if (ret < 0)
1587 goto err;
1588 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001589 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001590 leaf = path->nodes[0];
1591 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001592 slot = path->slots[0];
1593 if (advance || slot >= nritems) {
1594 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001595 ret = btrfs_next_leaf(root, path);
1596 if (ret)
1597 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001598 leaf = path->nodes[0];
1599 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001600 slot = path->slots[0];
1601 } else {
1602 slot++;
1603 path->slots[0]++;
1604 }
1605 }
1606 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001607 item = btrfs_item_nr(leaf, slot);
1608 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1609
1610 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001611 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001612 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001613 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001614 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001615 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001616
1617 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001618 advance = 1;
1619 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1620 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001621 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001622 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001623 struct btrfs_key location;
1624
1625 name_len = btrfs_dir_name_len(leaf, di);
1626 if (name_len < 32) {
1627 name_ptr = tmp_name;
1628 } else {
1629 name_ptr = kmalloc(name_len, GFP_NOFS);
1630 BUG_ON(!name_ptr);
1631 }
1632 read_extent_buffer(leaf, name_ptr,
1633 (unsigned long)(di + 1), name_len);
1634
1635 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1636 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001637 over = filldir(dirent, name_ptr, name_len,
1638 found_key.offset,
1639 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001640 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001641
1642 if (name_ptr != tmp_name)
1643 kfree(name_ptr);
1644
Chris Mason39279cc2007-06-12 06:35:45 -04001645 if (over)
1646 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001647 di_len = btrfs_dir_name_len(leaf, di) +
1648 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001649 di_cur += di_len;
1650 di = (struct btrfs_dir_item *)((char *)di + di_len);
1651 }
1652 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001653 if (key_type == BTRFS_DIR_INDEX_KEY)
1654 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1655 else
1656 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001657nopos:
1658 ret = 0;
1659err:
1660 btrfs_release_path(root, path);
1661 btrfs_free_path(path);
1662 mutex_unlock(&root->fs_info->fs_mutex);
1663 return ret;
1664}
1665
1666int btrfs_write_inode(struct inode *inode, int wait)
1667{
1668 struct btrfs_root *root = BTRFS_I(inode)->root;
1669 struct btrfs_trans_handle *trans;
1670 int ret = 0;
1671
1672 if (wait) {
1673 mutex_lock(&root->fs_info->fs_mutex);
1674 trans = btrfs_start_transaction(root, 1);
1675 btrfs_set_trans_block_group(trans, inode);
1676 ret = btrfs_commit_transaction(trans, root);
1677 mutex_unlock(&root->fs_info->fs_mutex);
1678 }
1679 return ret;
1680}
1681
1682/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001683 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001684 * inode changes. But, it is most likely to find the inode in cache.
1685 * FIXME, needs more benchmarking...there are no reasons other than performance
1686 * to keep or drop this code.
1687 */
1688void btrfs_dirty_inode(struct inode *inode)
1689{
1690 struct btrfs_root *root = BTRFS_I(inode)->root;
1691 struct btrfs_trans_handle *trans;
1692
1693 mutex_lock(&root->fs_info->fs_mutex);
1694 trans = btrfs_start_transaction(root, 1);
1695 btrfs_set_trans_block_group(trans, inode);
1696 btrfs_update_inode(trans, root, inode);
1697 btrfs_end_transaction(trans, root);
1698 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001699}
1700
1701static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1702 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001703 const char *name, int name_len,
1704 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001705 u64 objectid,
1706 struct btrfs_block_group_cache *group,
1707 int mode)
1708{
1709 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001710 struct btrfs_inode_item *inode_item;
Chris Mason6324fbf2008-03-24 15:01:59 -04001711 struct btrfs_block_group_cache *new_inode_group;
Chris Mason39279cc2007-06-12 06:35:45 -04001712 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001713 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001714 struct btrfs_inode_ref *ref;
1715 struct btrfs_key key[2];
1716 u32 sizes[2];
1717 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001718 int ret;
1719 int owner;
1720
Chris Mason5f39d392007-10-15 16:14:19 -04001721 path = btrfs_alloc_path();
1722 BUG_ON(!path);
1723
Chris Mason39279cc2007-06-12 06:35:45 -04001724 inode = new_inode(root->fs_info->sb);
1725 if (!inode)
1726 return ERR_PTR(-ENOMEM);
1727
Chris Masond1310b22008-01-24 16:13:08 -05001728 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1729 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001730 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001731 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1732 inode->i_mapping, GFP_NOFS);
Chris Mason81d7ed22008-04-25 08:51:48 -04001733 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Mason90692182008-02-08 13:49:28 -05001734 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001735 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001736
Chris Mason39279cc2007-06-12 06:35:45 -04001737 if (mode & S_IFDIR)
1738 owner = 0;
1739 else
1740 owner = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001741 new_inode_group = btrfs_find_block_group(root, group, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001742 BTRFS_BLOCK_GROUP_METADATA, owner);
Chris Mason6324fbf2008-03-24 15:01:59 -04001743 if (!new_inode_group) {
1744 printk("find_block group failed\n");
1745 new_inode_group = group;
1746 }
1747 BTRFS_I(inode)->block_group = new_inode_group;
Yanb98b6762008-01-08 15:54:37 -05001748 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001749
1750 key[0].objectid = objectid;
1751 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1752 key[0].offset = 0;
1753
1754 key[1].objectid = objectid;
1755 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1756 key[1].offset = ref_objectid;
1757
1758 sizes[0] = sizeof(struct btrfs_inode_item);
1759 sizes[1] = name_len + sizeof(*ref);
1760
1761 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1762 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001763 goto fail;
1764
Chris Mason9c583092008-01-29 15:15:18 -05001765 if (objectid > root->highest_inode)
1766 root->highest_inode = objectid;
1767
Chris Mason39279cc2007-06-12 06:35:45 -04001768 inode->i_uid = current->fsuid;
1769 inode->i_gid = current->fsgid;
1770 inode->i_mode = mode;
1771 inode->i_ino = objectid;
1772 inode->i_blocks = 0;
1773 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001774 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1775 struct btrfs_inode_item);
1776 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001777
1778 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1779 struct btrfs_inode_ref);
1780 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1781 ptr = (unsigned long)(ref + 1);
1782 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1783
Chris Mason5f39d392007-10-15 16:14:19 -04001784 btrfs_mark_buffer_dirty(path->nodes[0]);
1785 btrfs_free_path(path);
1786
Chris Mason39279cc2007-06-12 06:35:45 -04001787 location = &BTRFS_I(inode)->location;
1788 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001789 location->offset = 0;
1790 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1791
Chris Mason39279cc2007-06-12 06:35:45 -04001792 insert_inode_hash(inode);
1793 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001794fail:
1795 btrfs_free_path(path);
1796 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001797}
1798
1799static inline u8 btrfs_inode_type(struct inode *inode)
1800{
1801 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1802}
1803
1804static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001805 struct dentry *dentry, struct inode *inode,
1806 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001807{
1808 int ret;
1809 struct btrfs_key key;
1810 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001811 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001812
Chris Mason39279cc2007-06-12 06:35:45 -04001813 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001814 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1815 key.offset = 0;
1816
1817 ret = btrfs_insert_dir_item(trans, root,
1818 dentry->d_name.name, dentry->d_name.len,
1819 dentry->d_parent->d_inode->i_ino,
1820 &key, btrfs_inode_type(inode));
1821 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001822 if (add_backref) {
1823 ret = btrfs_insert_inode_ref(trans, root,
1824 dentry->d_name.name,
1825 dentry->d_name.len,
1826 inode->i_ino,
1827 dentry->d_parent->d_inode->i_ino);
1828 }
Chris Mason79c44582007-06-25 10:09:33 -04001829 parent_inode = dentry->d_parent->d_inode;
1830 parent_inode->i_size += dentry->d_name.len * 2;
1831 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001832 ret = btrfs_update_inode(trans, root,
1833 dentry->d_parent->d_inode);
1834 }
1835 return ret;
1836}
1837
1838static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001839 struct dentry *dentry, struct inode *inode,
1840 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001841{
Chris Mason9c583092008-01-29 15:15:18 -05001842 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001843 if (!err) {
1844 d_instantiate(dentry, inode);
1845 return 0;
1846 }
1847 if (err > 0)
1848 err = -EEXIST;
1849 return err;
1850}
1851
Josef Bacik618e21d2007-07-11 10:18:17 -04001852static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1853 int mode, dev_t rdev)
1854{
1855 struct btrfs_trans_handle *trans;
1856 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001857 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001858 int err;
1859 int drop_inode = 0;
1860 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001861 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001862
1863 if (!new_valid_dev(rdev))
1864 return -EINVAL;
1865
1866 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001867 err = btrfs_check_free_space(root, 1, 0);
1868 if (err)
1869 goto fail;
1870
Josef Bacik618e21d2007-07-11 10:18:17 -04001871 trans = btrfs_start_transaction(root, 1);
1872 btrfs_set_trans_block_group(trans, dir);
1873
1874 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1875 if (err) {
1876 err = -ENOSPC;
1877 goto out_unlock;
1878 }
1879
Chris Mason9c583092008-01-29 15:15:18 -05001880 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1881 dentry->d_name.len,
1882 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001883 BTRFS_I(dir)->block_group, mode);
1884 err = PTR_ERR(inode);
1885 if (IS_ERR(inode))
1886 goto out_unlock;
1887
1888 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001889 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001890 if (err)
1891 drop_inode = 1;
1892 else {
1893 inode->i_op = &btrfs_special_inode_operations;
1894 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001895 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001896 }
1897 dir->i_sb->s_dirt = 1;
1898 btrfs_update_inode_block_group(trans, inode);
1899 btrfs_update_inode_block_group(trans, dir);
1900out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001901 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001902 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001903fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001904 mutex_unlock(&root->fs_info->fs_mutex);
1905
1906 if (drop_inode) {
1907 inode_dec_link_count(inode);
1908 iput(inode);
1909 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001910 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001911 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001912 return err;
1913}
1914
Chris Mason39279cc2007-06-12 06:35:45 -04001915static int btrfs_create(struct inode *dir, struct dentry *dentry,
1916 int mode, struct nameidata *nd)
1917{
1918 struct btrfs_trans_handle *trans;
1919 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001920 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001921 int err;
1922 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001923 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001924 u64 objectid;
1925
1926 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001927 err = btrfs_check_free_space(root, 1, 0);
1928 if (err)
1929 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001930 trans = btrfs_start_transaction(root, 1);
1931 btrfs_set_trans_block_group(trans, dir);
1932
1933 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1934 if (err) {
1935 err = -ENOSPC;
1936 goto out_unlock;
1937 }
1938
Chris Mason9c583092008-01-29 15:15:18 -05001939 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1940 dentry->d_name.len,
1941 dentry->d_parent->d_inode->i_ino,
1942 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001943 err = PTR_ERR(inode);
1944 if (IS_ERR(inode))
1945 goto out_unlock;
1946
1947 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001948 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001949 if (err)
1950 drop_inode = 1;
1951 else {
1952 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04001953 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04001954 inode->i_fop = &btrfs_file_operations;
1955 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001956 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1957 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001958 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04001959 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1960 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001961 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason81d7ed22008-04-25 08:51:48 -04001962 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Masond1310b22008-01-24 16:13:08 -05001963 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001964 }
1965 dir->i_sb->s_dirt = 1;
1966 btrfs_update_inode_block_group(trans, inode);
1967 btrfs_update_inode_block_group(trans, dir);
1968out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001969 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001970 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001971fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001972 mutex_unlock(&root->fs_info->fs_mutex);
1973
1974 if (drop_inode) {
1975 inode_dec_link_count(inode);
1976 iput(inode);
1977 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001978 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001979 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001980 return err;
1981}
1982
1983static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1984 struct dentry *dentry)
1985{
1986 struct btrfs_trans_handle *trans;
1987 struct btrfs_root *root = BTRFS_I(dir)->root;
1988 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001989 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001990 int err;
1991 int drop_inode = 0;
1992
1993 if (inode->i_nlink == 0)
1994 return -ENOENT;
1995
Chris Mason6da6aba2007-12-18 16:15:09 -05001996#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1997 inode->i_nlink++;
1998#else
Chris Mason39279cc2007-06-12 06:35:45 -04001999 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05002000#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002001 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002002 err = btrfs_check_free_space(root, 1, 0);
2003 if (err)
2004 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002005 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002006
Chris Mason39279cc2007-06-12 06:35:45 -04002007 btrfs_set_trans_block_group(trans, dir);
2008 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05002009 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002010
Chris Mason39279cc2007-06-12 06:35:45 -04002011 if (err)
2012 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04002013
Chris Mason39279cc2007-06-12 06:35:45 -04002014 dir->i_sb->s_dirt = 1;
2015 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04002016 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04002017
Chris Mason54aa1f42007-06-22 14:16:25 -04002018 if (err)
2019 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002020
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002021 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002022 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002023fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002024 mutex_unlock(&root->fs_info->fs_mutex);
2025
2026 if (drop_inode) {
2027 inode_dec_link_count(inode);
2028 iput(inode);
2029 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002030 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002031 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002032 return err;
2033}
2034
Chris Mason39279cc2007-06-12 06:35:45 -04002035static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2036{
2037 struct inode *inode;
2038 struct btrfs_trans_handle *trans;
2039 struct btrfs_root *root = BTRFS_I(dir)->root;
2040 int err = 0;
2041 int drop_on_err = 0;
2042 u64 objectid;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002043 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002044
2045 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002046 err = btrfs_check_free_space(root, 1, 0);
2047 if (err)
2048 goto out_unlock;
2049
Chris Mason39279cc2007-06-12 06:35:45 -04002050 trans = btrfs_start_transaction(root, 1);
2051 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04002052
Chris Mason39279cc2007-06-12 06:35:45 -04002053 if (IS_ERR(trans)) {
2054 err = PTR_ERR(trans);
2055 goto out_unlock;
2056 }
2057
2058 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2059 if (err) {
2060 err = -ENOSPC;
2061 goto out_unlock;
2062 }
2063
Chris Mason9c583092008-01-29 15:15:18 -05002064 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2065 dentry->d_name.len,
2066 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002067 BTRFS_I(dir)->block_group, S_IFDIR | mode);
2068 if (IS_ERR(inode)) {
2069 err = PTR_ERR(inode);
2070 goto out_fail;
2071 }
Chris Mason5f39d392007-10-15 16:14:19 -04002072
Chris Mason39279cc2007-06-12 06:35:45 -04002073 drop_on_err = 1;
2074 inode->i_op = &btrfs_dir_inode_operations;
2075 inode->i_fop = &btrfs_dir_file_operations;
2076 btrfs_set_trans_block_group(trans, inode);
2077
Chris Mason39544012007-12-12 14:38:19 -05002078 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002079 err = btrfs_update_inode(trans, root, inode);
2080 if (err)
2081 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002082
Chris Mason9c583092008-01-29 15:15:18 -05002083 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002084 if (err)
2085 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04002086
Chris Mason39279cc2007-06-12 06:35:45 -04002087 d_instantiate(dentry, inode);
2088 drop_on_err = 0;
2089 dir->i_sb->s_dirt = 1;
2090 btrfs_update_inode_block_group(trans, inode);
2091 btrfs_update_inode_block_group(trans, dir);
2092
2093out_fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002094 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002095 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002096
Chris Mason39279cc2007-06-12 06:35:45 -04002097out_unlock:
2098 mutex_unlock(&root->fs_info->fs_mutex);
2099 if (drop_on_err)
2100 iput(inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002101 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002102 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002103 return err;
2104}
2105
Chris Mason3b951512008-04-17 11:29:12 -04002106static int merge_extent_mapping(struct extent_map_tree *em_tree,
2107 struct extent_map *existing,
2108 struct extent_map *em)
2109{
2110 u64 start_diff;
2111 u64 new_end;
2112 int ret = 0;
2113 int real_blocks = existing->block_start < EXTENT_MAP_LAST_BYTE;
2114
2115 if (real_blocks && em->block_start >= EXTENT_MAP_LAST_BYTE)
2116 goto invalid;
2117
2118 if (!real_blocks && em->block_start != existing->block_start)
2119 goto invalid;
2120
2121 new_end = max(existing->start + existing->len, em->start + em->len);
2122
2123 if (existing->start >= em->start) {
2124 if (em->start + em->len < existing->start)
2125 goto invalid;
2126
2127 start_diff = existing->start - em->start;
2128 if (real_blocks && em->block_start + start_diff !=
2129 existing->block_start)
2130 goto invalid;
2131
2132 em->len = new_end - em->start;
2133
2134 remove_extent_mapping(em_tree, existing);
2135 /* free for the tree */
2136 free_extent_map(existing);
2137 ret = add_extent_mapping(em_tree, em);
2138
2139 } else if (em->start > existing->start) {
2140
2141 if (existing->start + existing->len < em->start)
2142 goto invalid;
2143
2144 start_diff = em->start - existing->start;
2145 if (real_blocks && existing->block_start + start_diff !=
2146 em->block_start)
2147 goto invalid;
2148
2149 remove_extent_mapping(em_tree, existing);
2150 em->block_start = existing->block_start;
2151 em->start = existing->start;
2152 em->len = new_end - existing->start;
2153 free_extent_map(existing);
2154
2155 ret = add_extent_mapping(em_tree, em);
2156 } else {
2157 goto invalid;
2158 }
2159 return ret;
2160
2161invalid:
2162 printk("invalid extent map merge [%Lu %Lu %Lu] [%Lu %Lu %Lu]\n",
2163 existing->start, existing->len, existing->block_start,
2164 em->start, em->len, em->block_start);
2165 return -EIO;
2166}
2167
Chris Masona52d9a82007-08-27 16:49:44 -04002168struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05002169 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04002170 int create)
2171{
2172 int ret;
2173 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04002174 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002175 u64 extent_start = 0;
2176 u64 extent_end = 0;
2177 u64 objectid = inode->i_ino;
2178 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04002179 struct btrfs_path *path;
2180 struct btrfs_root *root = BTRFS_I(inode)->root;
2181 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04002182 struct extent_buffer *leaf;
2183 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04002184 struct extent_map *em = NULL;
2185 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05002186 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002187 struct btrfs_trans_handle *trans = NULL;
2188
2189 path = btrfs_alloc_path();
2190 BUG_ON(!path);
2191 mutex_lock(&root->fs_info->fs_mutex);
2192
2193again:
Chris Masond1310b22008-01-24 16:13:08 -05002194 spin_lock(&em_tree->lock);
2195 em = lookup_extent_mapping(em_tree, start, len);
2196 spin_unlock(&em_tree->lock);
2197
Chris Masona52d9a82007-08-27 16:49:44 -04002198 if (em) {
Chris Masone1c4b742008-04-22 13:26:46 -04002199 if (em->start > start || em->start + em->len <= start)
2200 free_extent_map(em);
2201 else if (em->block_start == EXTENT_MAP_INLINE && page)
Chris Mason70dec802008-01-29 09:59:12 -05002202 free_extent_map(em);
2203 else
2204 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002205 }
Chris Masond1310b22008-01-24 16:13:08 -05002206 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002207 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05002208 err = -ENOMEM;
2209 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04002210 }
Chris Masond1310b22008-01-24 16:13:08 -05002211
2212 em->start = EXTENT_MAP_HOLE;
2213 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04002214 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04002215 ret = btrfs_lookup_file_extent(trans, root, path,
2216 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04002217 if (ret < 0) {
2218 err = ret;
2219 goto out;
2220 }
2221
2222 if (ret != 0) {
2223 if (path->slots[0] == 0)
2224 goto not_found;
2225 path->slots[0]--;
2226 }
2227
Chris Mason5f39d392007-10-15 16:14:19 -04002228 leaf = path->nodes[0];
2229 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04002230 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04002231 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04002232 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2233 found_type = btrfs_key_type(&found_key);
2234 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04002235 found_type != BTRFS_EXTENT_DATA_KEY) {
2236 goto not_found;
2237 }
2238
Chris Mason5f39d392007-10-15 16:14:19 -04002239 found_type = btrfs_file_extent_type(leaf, item);
2240 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04002241 if (found_type == BTRFS_FILE_EXTENT_REG) {
2242 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04002243 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04002244 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04002245 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002246 em->start = start;
2247 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002248 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002249 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002250 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002251 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002252 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002253 }
2254 goto not_found_em;
2255 }
Chris Masondb945352007-10-15 16:15:53 -04002256 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2257 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002258 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002259 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002260 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002261 goto insert;
2262 }
Chris Masondb945352007-10-15 16:15:53 -04002263 bytenr += btrfs_file_extent_offset(leaf, item);
2264 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002265 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002266 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002267 goto insert;
2268 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002269 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002270 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002271 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002272 size_t size;
2273 size_t extent_offset;
2274 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002275
Chris Mason5f39d392007-10-15 16:14:19 -04002276 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2277 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002278 extent_end = (extent_start + size + root->sectorsize - 1) &
2279 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002280 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002281 em->start = start;
2282 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002283 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002284 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002285 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002286 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002287 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002288 }
2289 goto not_found_em;
2290 }
2291 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002292
2293 if (!page) {
2294 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002295 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002296 goto out;
2297 }
2298
Chris Mason70dec802008-01-29 09:59:12 -05002299 page_start = page_offset(page) + pg_offset;
2300 extent_offset = page_start - extent_start;
2301 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002302 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002303 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002304 em->len = (copy_size + root->sectorsize - 1) &
2305 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002306 map = kmap(page);
2307 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002308 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002309 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002310 copy_size);
2311 flush_dcache_page(page);
2312 } else if (create && PageUptodate(page)) {
2313 if (!trans) {
2314 kunmap(page);
2315 free_extent_map(em);
2316 em = NULL;
2317 btrfs_release_path(root, path);
2318 trans = btrfs_start_transaction(root, 1);
2319 goto again;
2320 }
Chris Mason70dec802008-01-29 09:59:12 -05002321 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002322 copy_size);
2323 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002324 }
Chris Masona52d9a82007-08-27 16:49:44 -04002325 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002326 set_extent_uptodate(io_tree, em->start,
2327 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002328 goto insert;
2329 } else {
2330 printk("unkknown found_type %d\n", found_type);
2331 WARN_ON(1);
2332 }
2333not_found:
2334 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002335 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002336not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002337 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002338insert:
2339 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002340 if (em->start > start || extent_map_end(em) <= start) {
2341 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002342 err = -EIO;
2343 goto out;
2344 }
Chris Masond1310b22008-01-24 16:13:08 -05002345
2346 err = 0;
2347 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002348 ret = add_extent_mapping(em_tree, em);
Chris Mason3b951512008-04-17 11:29:12 -04002349 /* it is possible that someone inserted the extent into the tree
2350 * while we had the lock dropped. It is also possible that
2351 * an overlapping map exists in the tree
2352 */
Chris Masona52d9a82007-08-27 16:49:44 -04002353 if (ret == -EEXIST) {
Chris Mason3b951512008-04-17 11:29:12 -04002354 struct extent_map *existing;
2355 existing = lookup_extent_mapping(em_tree, start, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002356 if (existing && (existing->start > start ||
2357 existing->start + existing->len <= start)) {
2358 free_extent_map(existing);
2359 existing = NULL;
2360 }
Chris Mason3b951512008-04-17 11:29:12 -04002361 if (!existing) {
2362 existing = lookup_extent_mapping(em_tree, em->start,
2363 em->len);
2364 if (existing) {
2365 err = merge_extent_mapping(em_tree, existing,
2366 em);
2367 free_extent_map(existing);
2368 if (err) {
2369 free_extent_map(em);
2370 em = NULL;
2371 }
2372 } else {
2373 err = -EIO;
2374 printk("failing to insert %Lu %Lu\n",
2375 start, len);
2376 free_extent_map(em);
2377 em = NULL;
2378 }
2379 } else {
2380 free_extent_map(em);
2381 em = existing;
Chris Masona52d9a82007-08-27 16:49:44 -04002382 }
Chris Masona52d9a82007-08-27 16:49:44 -04002383 }
Chris Masond1310b22008-01-24 16:13:08 -05002384 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002385out:
2386 btrfs_free_path(path);
2387 if (trans) {
2388 ret = btrfs_end_transaction(trans, root);
2389 if (!err)
2390 err = ret;
2391 }
2392 mutex_unlock(&root->fs_info->fs_mutex);
2393 if (err) {
2394 free_extent_map(em);
2395 WARN_ON(1);
2396 return ERR_PTR(err);
2397 }
2398 return em;
2399}
2400
Chris Masone1c4b742008-04-22 13:26:46 -04002401#if 0 /* waiting for O_DIRECT reads */
Chris Mason16432982008-04-10 10:23:21 -04002402static int btrfs_get_block(struct inode *inode, sector_t iblock,
2403 struct buffer_head *bh_result, int create)
2404{
2405 struct extent_map *em;
2406 u64 start = (u64)iblock << inode->i_blkbits;
2407 struct btrfs_multi_bio *multi = NULL;
2408 struct btrfs_root *root = BTRFS_I(inode)->root;
2409 u64 len;
2410 u64 logical;
2411 u64 map_length;
2412 int ret = 0;
2413
2414 em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2415
2416 if (!em || IS_ERR(em))
2417 goto out;
2418
Chris Masone1c4b742008-04-22 13:26:46 -04002419 if (em->start > start || em->start + em->len <= start) {
Chris Mason16432982008-04-10 10:23:21 -04002420 goto out;
Chris Masone1c4b742008-04-22 13:26:46 -04002421 }
Chris Mason16432982008-04-10 10:23:21 -04002422
2423 if (em->block_start == EXTENT_MAP_INLINE) {
2424 ret = -EINVAL;
2425 goto out;
2426 }
2427
Chris Mason16432982008-04-10 10:23:21 -04002428 len = em->start + em->len - start;
2429 len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2430
Chris Masone1c4b742008-04-22 13:26:46 -04002431 if (em->block_start == EXTENT_MAP_HOLE ||
2432 em->block_start == EXTENT_MAP_DELALLOC) {
2433 bh_result->b_size = len;
2434 goto out;
2435 }
2436
Chris Mason16432982008-04-10 10:23:21 -04002437 logical = start - em->start;
2438 logical = em->block_start + logical;
2439
2440 map_length = len;
2441 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2442 logical, &map_length, &multi, 0);
2443 BUG_ON(ret);
2444 bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2445 bh_result->b_size = min(map_length, len);
Chris Masone1c4b742008-04-22 13:26:46 -04002446
Chris Mason16432982008-04-10 10:23:21 -04002447 bh_result->b_bdev = multi->stripes[0].dev->bdev;
2448 set_buffer_mapped(bh_result);
2449 kfree(multi);
2450out:
2451 free_extent_map(em);
2452 return ret;
2453}
Chris Masone1c4b742008-04-22 13:26:46 -04002454#endif
Chris Mason16432982008-04-10 10:23:21 -04002455
2456static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
2457 const struct iovec *iov, loff_t offset,
2458 unsigned long nr_segs)
2459{
Chris Masone1c4b742008-04-22 13:26:46 -04002460 return -EINVAL;
2461#if 0
Chris Mason16432982008-04-10 10:23:21 -04002462 struct file *file = iocb->ki_filp;
2463 struct inode *inode = file->f_mapping->host;
2464
2465 if (rw == WRITE)
2466 return -EINVAL;
2467
2468 return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
2469 offset, nr_segs, btrfs_get_block, NULL);
Chris Masone1c4b742008-04-22 13:26:46 -04002470#endif
Chris Mason16432982008-04-10 10:23:21 -04002471}
2472
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002473static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002474{
Christoph Hellwigd396c6f2007-09-10 20:02:30 -04002475 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002476}
2477
Chris Mason9ebefb182007-06-15 13:50:00 -04002478int btrfs_readpage(struct file *file, struct page *page)
2479{
Chris Masond1310b22008-01-24 16:13:08 -05002480 struct extent_io_tree *tree;
2481 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002482 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002483}
Chris Mason1832a6d2007-12-21 16:27:21 -05002484
Chris Mason39279cc2007-06-12 06:35:45 -04002485static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2486{
Chris Masond1310b22008-01-24 16:13:08 -05002487 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002488
2489
2490 if (current->flags & PF_MEMALLOC) {
2491 redirty_page_for_writepage(wbc, page);
2492 unlock_page(page);
2493 return 0;
2494 }
Chris Masond1310b22008-01-24 16:13:08 -05002495 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002496 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2497}
Chris Mason39279cc2007-06-12 06:35:45 -04002498
Chris Masonb293f022007-11-01 19:45:34 -04002499static int btrfs_writepages(struct address_space *mapping,
2500 struct writeback_control *wbc)
2501{
Chris Masond1310b22008-01-24 16:13:08 -05002502 struct extent_io_tree *tree;
2503 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002504 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2505}
2506
Chris Mason3ab2fb52007-11-08 10:59:22 -05002507static int
2508btrfs_readpages(struct file *file, struct address_space *mapping,
2509 struct list_head *pages, unsigned nr_pages)
2510{
Chris Masond1310b22008-01-24 16:13:08 -05002511 struct extent_io_tree *tree;
2512 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002513 return extent_readpages(tree, mapping, pages, nr_pages,
2514 btrfs_get_extent);
2515}
2516
Chris Mason70dec802008-01-29 09:59:12 -05002517static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002518{
Chris Masond1310b22008-01-24 16:13:08 -05002519 struct extent_io_tree *tree;
2520 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002521 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002522
Chris Masond1310b22008-01-24 16:13:08 -05002523 tree = &BTRFS_I(page->mapping->host)->io_tree;
2524 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002525 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002526 if (ret == 1) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002527 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Masona52d9a82007-08-27 16:49:44 -04002528 ClearPagePrivate(page);
2529 set_page_private(page, 0);
2530 page_cache_release(page);
2531 }
2532 return ret;
2533}
Chris Mason39279cc2007-06-12 06:35:45 -04002534
Chris Masona52d9a82007-08-27 16:49:44 -04002535static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2536{
Chris Masond1310b22008-01-24 16:13:08 -05002537 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002538
Chris Masond1310b22008-01-24 16:13:08 -05002539 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002540 extent_invalidatepage(tree, page, offset);
2541 btrfs_releasepage(page, GFP_NOFS);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002542 if (PagePrivate(page)) {
Chris Mason4ef64ea2008-04-21 08:52:50 -04002543 invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
Chris Mason9ad6b7b2008-04-18 16:11:30 -04002544 ClearPagePrivate(page);
2545 set_page_private(page, 0);
2546 page_cache_release(page);
2547 }
Chris Mason39279cc2007-06-12 06:35:45 -04002548}
2549
Chris Mason9ebefb182007-06-15 13:50:00 -04002550/*
2551 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2552 * called from a page fault handler when a page is first dirtied. Hence we must
2553 * be careful to check for EOF conditions here. We set the page up correctly
2554 * for a written page which means we get ENOSPC checking when writing into
2555 * holes and correct delalloc and unwritten extent mapping on filesystems that
2556 * support these features.
2557 *
2558 * We are not allowed to take the i_mutex here so we have to play games to
2559 * protect against truncate races as the page could now be beyond EOF. Because
2560 * vmtruncate() writes the inode size before removing pages, once we have the
2561 * page lock we can determine safely if the page is beyond EOF. If it is not
2562 * beyond EOF, then the page is guaranteed safe against truncation until we
2563 * unlock the page.
2564 */
2565int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2566{
Chris Mason6da6aba2007-12-18 16:15:09 -05002567 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002568 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002569 unsigned long end;
2570 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002571 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002572 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002573
Chris Mason1832a6d2007-12-21 16:27:21 -05002574 mutex_lock(&root->fs_info->fs_mutex);
2575 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002576 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002577 if (ret)
2578 goto out;
2579
2580 ret = -EINVAL;
2581
Chris Mason9ebefb182007-06-15 13:50:00 -04002582 lock_page(page);
2583 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002584 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002585 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002586
Chris Mason9ebefb182007-06-15 13:50:00 -04002587 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002588 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002589 /* page got truncated out from underneath us */
2590 goto out_unlock;
2591 }
2592
2593 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002594 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002595 end = size & ~PAGE_CACHE_MASK;
2596 else
2597 end = PAGE_CACHE_SIZE;
2598
Chris Masonb888db22007-08-27 16:49:44 -04002599 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002600
2601out_unlock:
2602 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002603out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002604 return ret;
2605}
2606
Chris Mason39279cc2007-06-12 06:35:45 -04002607static void btrfs_truncate(struct inode *inode)
2608{
2609 struct btrfs_root *root = BTRFS_I(inode)->root;
2610 int ret;
2611 struct btrfs_trans_handle *trans;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002612 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002613
2614 if (!S_ISREG(inode->i_mode))
2615 return;
2616 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2617 return;
2618
2619 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2620
2621 mutex_lock(&root->fs_info->fs_mutex);
2622 trans = btrfs_start_transaction(root, 1);
2623 btrfs_set_trans_block_group(trans, inode);
2624
2625 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002626 ret = btrfs_truncate_in_trans(trans, root, inode,
2627 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002628 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002629 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002630
Chris Mason39279cc2007-06-12 06:35:45 -04002631 ret = btrfs_end_transaction(trans, root);
2632 BUG_ON(ret);
2633 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002634 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002635 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002636}
2637
Chris Mason4313b392008-01-03 09:08:48 -05002638static int noinline create_subvol(struct btrfs_root *root, char *name,
2639 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002640{
2641 struct btrfs_trans_handle *trans;
2642 struct btrfs_key key;
2643 struct btrfs_root_item root_item;
2644 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002645 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002646 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002647 struct inode *inode;
2648 struct inode *dir;
2649 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002650 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002651 u64 objectid;
2652 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002653 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002654
2655 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002656 ret = btrfs_check_free_space(root, 1, 0);
2657 if (ret)
2658 goto fail_commit;
2659
Chris Mason39279cc2007-06-12 06:35:45 -04002660 trans = btrfs_start_transaction(root, 1);
2661 BUG_ON(!trans);
2662
Chris Mason7bb86312007-12-11 09:25:06 -05002663 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2664 0, &objectid);
2665 if (ret)
2666 goto fail;
2667
2668 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2669 objectid, trans->transid, 0, 0,
2670 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002671 if (IS_ERR(leaf))
2672 return PTR_ERR(leaf);
2673
2674 btrfs_set_header_nritems(leaf, 0);
2675 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002676 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002677 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002678 btrfs_set_header_owner(leaf, objectid);
2679
Chris Mason5f39d392007-10-15 16:14:19 -04002680 write_extent_buffer(leaf, root->fs_info->fsid,
2681 (unsigned long)btrfs_header_fsid(leaf),
2682 BTRFS_FSID_SIZE);
2683 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002684
2685 inode_item = &root_item.inode;
2686 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002687 inode_item->generation = cpu_to_le64(1);
2688 inode_item->size = cpu_to_le64(3);
2689 inode_item->nlink = cpu_to_le32(1);
2690 inode_item->nblocks = cpu_to_le64(1);
2691 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002692
Chris Masondb945352007-10-15 16:15:53 -04002693 btrfs_set_root_bytenr(&root_item, leaf->start);
2694 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002695 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002696 btrfs_set_root_used(&root_item, 0);
2697
Chris Mason5eda7b52007-06-22 14:16:25 -04002698 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2699 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002700
2701 free_extent_buffer(leaf);
2702 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002703
Chris Mason39279cc2007-06-12 06:35:45 -04002704 btrfs_set_root_dirid(&root_item, new_dirid);
2705
2706 key.objectid = objectid;
2707 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002708 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2709 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2710 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002711 if (ret)
2712 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002713
2714 /*
2715 * insert the directory item
2716 */
2717 key.offset = (u64)-1;
2718 dir = root->fs_info->sb->s_root->d_inode;
2719 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2720 name, namelen, dir->i_ino, &key,
2721 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002722 if (ret)
2723 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002724
Chris Mason39544012007-12-12 14:38:19 -05002725 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2726 name, namelen, objectid,
2727 root->fs_info->sb->s_root->d_inode->i_ino);
2728 if (ret)
2729 goto fail;
2730
Chris Mason39279cc2007-06-12 06:35:45 -04002731 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002732 if (ret)
2733 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002734
Josef Bacik58176a92007-08-29 15:47:34 -04002735 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002736 BUG_ON(!new_root);
2737
2738 trans = btrfs_start_transaction(new_root, 1);
2739 BUG_ON(!trans);
2740
Chris Mason9c583092008-01-29 15:15:18 -05002741 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2742 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002743 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002744 if (IS_ERR(inode))
2745 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002746 inode->i_op = &btrfs_dir_inode_operations;
2747 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002748 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002749
Chris Mason39544012007-12-12 14:38:19 -05002750 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2751 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002752 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002753 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002754 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002755 if (ret)
2756 goto fail;
2757fail:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002758 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002759 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002760 if (err && !ret)
2761 ret = err;
2762fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002763 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002764 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002765 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002766 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002767}
2768
2769static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2770{
Chris Mason3063d292008-01-08 15:46:30 -05002771 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002772 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002773 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002774 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002775 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002776
2777 if (!root->ref_cows)
2778 return -EINVAL;
2779
2780 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002781 ret = btrfs_check_free_space(root, 1, 0);
2782 if (ret)
2783 goto fail_unlock;
2784
Chris Mason3063d292008-01-08 15:46:30 -05002785 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2786 if (!pending_snapshot) {
2787 ret = -ENOMEM;
2788 goto fail_unlock;
2789 }
Yanfb4bc1e2008-01-17 11:59:51 -05002790 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002791 if (!pending_snapshot->name) {
2792 ret = -ENOMEM;
2793 kfree(pending_snapshot);
2794 goto fail_unlock;
2795 }
Yanfb4bc1e2008-01-17 11:59:51 -05002796 memcpy(pending_snapshot->name, name, namelen);
2797 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002798 trans = btrfs_start_transaction(root, 1);
2799 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002800 pending_snapshot->root = root;
2801 list_add(&pending_snapshot->list,
2802 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002803 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002804 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002805
Chris Mason1832a6d2007-12-21 16:27:21 -05002806fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002807 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04002808 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002809 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002810 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002811}
2812
Chris Masonedbd8d42007-12-21 16:27:24 -05002813unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002814 struct file_ra_state *ra, struct file *file,
2815 pgoff_t offset, pgoff_t last_index)
2816{
Chris Mason8e7bf942008-04-28 09:02:36 -04002817 pgoff_t req_size = last_index - offset + 1;
Chris Mason86479a02007-09-10 19:58:16 -04002818
2819#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
Chris Mason86479a02007-09-10 19:58:16 -04002820 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2821 return offset;
2822#else
Chris Mason86479a02007-09-10 19:58:16 -04002823 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2824 return offset + req_size;
2825#endif
2826}
2827
2828int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002829 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002830 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002831 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002832 struct page *page;
2833 unsigned long last_index;
Chris Mason8e7bf942008-04-28 09:02:36 -04002834 unsigned long ra_pages = root->fs_info->bdi.ra_pages;
2835 unsigned long total_read = 0;
Chris Mason86479a02007-09-10 19:58:16 -04002836 u64 page_start;
2837 u64 page_end;
2838 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002839 int ret;
2840
2841 mutex_lock(&root->fs_info->fs_mutex);
2842 ret = btrfs_check_free_space(root, inode->i_size, 0);
2843 mutex_unlock(&root->fs_info->fs_mutex);
2844 if (ret)
2845 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002846
2847 mutex_lock(&inode->i_mutex);
2848 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2849 for (i = 0; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04002850 if (total_read % ra_pages == 0) {
2851 btrfs_force_ra(inode->i_mapping, &file->f_ra, file, i,
2852 min(last_index, i + ra_pages - 1));
Chris Mason86479a02007-09-10 19:58:16 -04002853 }
Chris Mason8e7bf942008-04-28 09:02:36 -04002854 total_read++;
Chris Mason86479a02007-09-10 19:58:16 -04002855 page = grab_cache_page(inode->i_mapping, i);
2856 if (!page)
2857 goto out_unlock;
2858 if (!PageUptodate(page)) {
2859 btrfs_readpage(NULL, page);
2860 lock_page(page);
2861 if (!PageUptodate(page)) {
2862 unlock_page(page);
2863 page_cache_release(page);
2864 goto out_unlock;
2865 }
2866 }
Chris Mason35ebb932007-10-30 16:56:53 -04002867 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002868 page_end = page_start + PAGE_CACHE_SIZE - 1;
2869
Chris Masond1310b22008-01-24 16:13:08 -05002870 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002871 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002872 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002873
Chris Masond1310b22008-01-24 16:13:08 -05002874 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002875 set_page_dirty(page);
2876 unlock_page(page);
2877 page_cache_release(page);
2878 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2879 }
2880
2881out_unlock:
2882 mutex_unlock(&inode->i_mutex);
2883 return 0;
2884}
2885
Chris Masonedbd8d42007-12-21 16:27:24 -05002886static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2887{
2888 u64 new_size;
2889 u64 old_size;
Chris Mason8f18cf12008-04-25 16:53:30 -04002890 u64 devid = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05002891 struct btrfs_ioctl_vol_args *vol_args;
2892 struct btrfs_trans_handle *trans;
Chris Mason8f18cf12008-04-25 16:53:30 -04002893 struct btrfs_device *device = NULL;
Chris Masonedbd8d42007-12-21 16:27:24 -05002894 char *sizestr;
Chris Mason8f18cf12008-04-25 16:53:30 -04002895 char *devstr = NULL;
Chris Masonedbd8d42007-12-21 16:27:24 -05002896 int ret = 0;
2897 int namelen;
2898 int mod = 0;
2899
2900 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2901
2902 if (!vol_args)
2903 return -ENOMEM;
2904
2905 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2906 ret = -EFAULT;
2907 goto out;
2908 }
2909 namelen = strlen(vol_args->name);
2910 if (namelen > BTRFS_VOL_NAME_MAX) {
2911 ret = -EINVAL;
2912 goto out;
2913 }
2914
Chris Mason8f18cf12008-04-25 16:53:30 -04002915 mutex_lock(&root->fs_info->fs_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05002916 sizestr = vol_args->name;
Chris Mason8f18cf12008-04-25 16:53:30 -04002917 devstr = strchr(sizestr, ':');
2918 if (devstr) {
2919 char *end;
2920 sizestr = devstr + 1;
2921 *devstr = '\0';
2922 devstr = vol_args->name;
2923 devid = simple_strtoull(devstr, &end, 10);
2924printk("resizing devid %Lu\n", devid);
2925 }
2926 device = btrfs_find_device(root, devid, NULL);
2927 if (!device) {
2928 printk("resizer unable to find device %Lu\n", devid);
2929 ret = -EINVAL;
2930 goto out_unlock;
2931 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002932 if (!strcmp(sizestr, "max"))
Chris Mason8f18cf12008-04-25 16:53:30 -04002933 new_size = device->bdev->bd_inode->i_size;
Chris Masonedbd8d42007-12-21 16:27:24 -05002934 else {
2935 if (sizestr[0] == '-') {
2936 mod = -1;
2937 sizestr++;
2938 } else if (sizestr[0] == '+') {
2939 mod = 1;
2940 sizestr++;
2941 }
2942 new_size = btrfs_parse_size(sizestr);
2943 if (new_size == 0) {
2944 ret = -EINVAL;
Chris Mason8f18cf12008-04-25 16:53:30 -04002945 goto out_unlock;
Chris Masonedbd8d42007-12-21 16:27:24 -05002946 }
2947 }
2948
Chris Mason8f18cf12008-04-25 16:53:30 -04002949 old_size = device->total_bytes;
Chris Masonedbd8d42007-12-21 16:27:24 -05002950
2951 if (mod < 0) {
2952 if (new_size > old_size) {
2953 ret = -EINVAL;
2954 goto out_unlock;
2955 }
2956 new_size = old_size - new_size;
2957 } else if (mod > 0) {
2958 new_size = old_size + new_size;
2959 }
2960
2961 if (new_size < 256 * 1024 * 1024) {
2962 ret = -EINVAL;
2963 goto out_unlock;
2964 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002965 if (new_size > device->bdev->bd_inode->i_size) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002966 ret = -EFBIG;
2967 goto out_unlock;
2968 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002969
2970 do_div(new_size, root->sectorsize);
2971 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002972
Chris Mason8f18cf12008-04-25 16:53:30 -04002973printk("new size for %s is %llu\n", device->name, (unsigned long long)new_size);
2974
Chris Masonedbd8d42007-12-21 16:27:24 -05002975 if (new_size > old_size) {
2976 trans = btrfs_start_transaction(root, 1);
Chris Mason8f18cf12008-04-25 16:53:30 -04002977 ret = btrfs_grow_device(trans, device, new_size);
Chris Masonedbd8d42007-12-21 16:27:24 -05002978 btrfs_commit_transaction(trans, root);
2979 } else {
Chris Mason8f18cf12008-04-25 16:53:30 -04002980 ret = btrfs_shrink_device(device, new_size);
Chris Masonedbd8d42007-12-21 16:27:24 -05002981 }
2982
2983out_unlock:
2984 mutex_unlock(&root->fs_info->fs_mutex);
2985out:
2986 kfree(vol_args);
2987 return ret;
2988}
2989
Chris Mason4313b392008-01-03 09:08:48 -05002990static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2991 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002992{
Chris Mason4aec2b52007-12-18 16:25:45 -05002993 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002994 struct btrfs_dir_item *di;
2995 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002996 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002997 int namelen;
2998 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002999
Chris Mason4aec2b52007-12-18 16:25:45 -05003000 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04003001
Chris Mason4aec2b52007-12-18 16:25:45 -05003002 if (!vol_args)
3003 return -ENOMEM;
3004
3005 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3006 ret = -EFAULT;
3007 goto out;
3008 }
3009
3010 namelen = strlen(vol_args->name);
3011 if (namelen > BTRFS_VOL_NAME_MAX) {
3012 ret = -EINVAL;
3013 goto out;
3014 }
3015 if (strchr(vol_args->name, '/')) {
3016 ret = -EINVAL;
3017 goto out;
3018 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003019
3020 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05003021 if (!path) {
3022 ret = -ENOMEM;
3023 goto out;
3024 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003025
3026 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
3027 mutex_lock(&root->fs_info->fs_mutex);
3028 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
3029 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05003030 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003031 mutex_unlock(&root->fs_info->fs_mutex);
3032 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05003033
3034 if (di && !IS_ERR(di)) {
3035 ret = -EEXIST;
3036 goto out;
3037 }
3038
3039 if (IS_ERR(di)) {
3040 ret = PTR_ERR(di);
3041 goto out;
3042 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003043
3044 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05003045 ret = create_subvol(root, vol_args->name, namelen);
3046 else
3047 ret = create_snapshot(root, vol_args->name, namelen);
3048out:
3049 kfree(vol_args);
3050 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003051}
3052
3053static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04003054{
Chris Mason6da6aba2007-12-18 16:15:09 -05003055 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04003056 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003057
3058 switch (inode->i_mode & S_IFMT) {
3059 case S_IFDIR:
3060 mutex_lock(&root->fs_info->fs_mutex);
3061 btrfs_defrag_root(root, 0);
3062 btrfs_defrag_root(root->fs_info->extent_root, 0);
3063 mutex_unlock(&root->fs_info->fs_mutex);
3064 break;
3065 case S_IFREG:
3066 btrfs_defrag_file(file);
3067 break;
3068 }
3069
3070 return 0;
3071}
3072
Chris Mason788f20e2008-04-28 15:29:42 -04003073long btrfs_ioctl_add_dev(struct btrfs_root *root, void __user *arg)
3074{
3075 struct btrfs_ioctl_vol_args *vol_args;
3076 int ret;
3077
3078 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
3079
3080 if (!vol_args)
3081 return -ENOMEM;
3082
3083 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
3084 ret = -EFAULT;
3085 goto out;
3086 }
3087 ret = btrfs_init_new_device(root, vol_args->name);
3088
3089out:
3090 kfree(vol_args);
3091 return ret;
3092}
3093
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003094long btrfs_ioctl(struct file *file, unsigned int
3095 cmd, unsigned long arg)
3096{
Chris Mason6da6aba2007-12-18 16:15:09 -05003097 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04003098
3099 switch (cmd) {
3100 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003101 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04003102 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003103 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05003104 case BTRFS_IOC_RESIZE:
3105 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason788f20e2008-04-28 15:29:42 -04003106 case BTRFS_IOC_ADD_DEV:
3107 return btrfs_ioctl_add_dev(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04003108 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04003109
3110 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04003111}
3112
Chris Mason39279cc2007-06-12 06:35:45 -04003113/*
3114 * Called inside transaction, so use GFP_NOFS
3115 */
3116struct inode *btrfs_alloc_inode(struct super_block *sb)
3117{
3118 struct btrfs_inode *ei;
3119
3120 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3121 if (!ei)
3122 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04003123 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05003124 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003125 return &ei->vfs_inode;
3126}
3127
3128void btrfs_destroy_inode(struct inode *inode)
3129{
3130 WARN_ON(!list_empty(&inode->i_dentry));
3131 WARN_ON(inode->i_data.nrpages);
3132
Chris Mason8c416c92008-01-14 15:10:26 -05003133 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04003134 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3135}
3136
Chris Mason44ec0b72007-10-29 10:55:05 -04003137#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3138static void init_once(struct kmem_cache * cachep, void *foo)
3139#else
Chris Mason39279cc2007-06-12 06:35:45 -04003140static void init_once(void * foo, struct kmem_cache * cachep,
3141 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04003142#endif
Chris Mason39279cc2007-06-12 06:35:45 -04003143{
3144 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3145
3146 inode_init_once(&ei->vfs_inode);
3147}
3148
3149void btrfs_destroy_cachep(void)
3150{
3151 if (btrfs_inode_cachep)
3152 kmem_cache_destroy(btrfs_inode_cachep);
3153 if (btrfs_trans_handle_cachep)
3154 kmem_cache_destroy(btrfs_trans_handle_cachep);
3155 if (btrfs_transaction_cachep)
3156 kmem_cache_destroy(btrfs_transaction_cachep);
3157 if (btrfs_bit_radix_cachep)
3158 kmem_cache_destroy(btrfs_bit_radix_cachep);
3159 if (btrfs_path_cachep)
3160 kmem_cache_destroy(btrfs_path_cachep);
3161}
3162
Chris Mason86479a02007-09-10 19:58:16 -04003163struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04003164 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04003165#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3166 void (*ctor)(struct kmem_cache *, void *)
3167#else
Chris Mason92fee662007-07-25 12:31:35 -04003168 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04003169 unsigned long)
3170#endif
3171 )
Chris Mason92fee662007-07-25 12:31:35 -04003172{
3173 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3174 SLAB_MEM_SPREAD | extra_flags), ctor
3175#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3176 ,NULL
3177#endif
3178 );
3179}
3180
Chris Mason39279cc2007-06-12 06:35:45 -04003181int btrfs_init_cachep(void)
3182{
Chris Mason86479a02007-09-10 19:58:16 -04003183 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04003184 sizeof(struct btrfs_inode),
3185 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04003186 if (!btrfs_inode_cachep)
3187 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003188 btrfs_trans_handle_cachep =
3189 btrfs_cache_create("btrfs_trans_handle_cache",
3190 sizeof(struct btrfs_trans_handle),
3191 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003192 if (!btrfs_trans_handle_cachep)
3193 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003194 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04003195 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04003196 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003197 if (!btrfs_transaction_cachep)
3198 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003199 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04003200 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04003201 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003202 if (!btrfs_path_cachep)
3203 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04003204 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04003205 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04003206 if (!btrfs_bit_radix_cachep)
3207 goto fail;
3208 return 0;
3209fail:
3210 btrfs_destroy_cachep();
3211 return -ENOMEM;
3212}
3213
3214static int btrfs_getattr(struct vfsmount *mnt,
3215 struct dentry *dentry, struct kstat *stat)
3216{
3217 struct inode *inode = dentry->d_inode;
3218 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05003219 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05003220 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04003221 return 0;
3222}
3223
3224static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3225 struct inode * new_dir,struct dentry *new_dentry)
3226{
3227 struct btrfs_trans_handle *trans;
3228 struct btrfs_root *root = BTRFS_I(old_dir)->root;
3229 struct inode *new_inode = new_dentry->d_inode;
3230 struct inode *old_inode = old_dentry->d_inode;
3231 struct timespec ctime = CURRENT_TIME;
3232 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04003233 int ret;
3234
3235 if (S_ISDIR(old_inode->i_mode) && new_inode &&
3236 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3237 return -ENOTEMPTY;
3238 }
Chris Mason5f39d392007-10-15 16:14:19 -04003239
Chris Mason39279cc2007-06-12 06:35:45 -04003240 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003241 ret = btrfs_check_free_space(root, 1, 0);
3242 if (ret)
3243 goto out_unlock;
3244
Chris Mason39279cc2007-06-12 06:35:45 -04003245 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003246
Chris Mason39279cc2007-06-12 06:35:45 -04003247 btrfs_set_trans_block_group(trans, new_dir);
3248 path = btrfs_alloc_path();
3249 if (!path) {
3250 ret = -ENOMEM;
3251 goto out_fail;
3252 }
3253
3254 old_dentry->d_inode->i_nlink++;
3255 old_dir->i_ctime = old_dir->i_mtime = ctime;
3256 new_dir->i_ctime = new_dir->i_mtime = ctime;
3257 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04003258
Chris Mason39279cc2007-06-12 06:35:45 -04003259 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
3260 if (ret)
3261 goto out_fail;
3262
3263 if (new_inode) {
3264 new_inode->i_ctime = CURRENT_TIME;
3265 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
3266 if (ret)
3267 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04003268 }
Chris Mason9c583092008-01-29 15:15:18 -05003269 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04003270 if (ret)
3271 goto out_fail;
3272
3273out_fail:
3274 btrfs_free_path(path);
3275 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003276out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04003277 mutex_unlock(&root->fs_info->fs_mutex);
3278 return ret;
3279}
3280
3281static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3282 const char *symname)
3283{
3284 struct btrfs_trans_handle *trans;
3285 struct btrfs_root *root = BTRFS_I(dir)->root;
3286 struct btrfs_path *path;
3287 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05003288 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04003289 int err;
3290 int drop_inode = 0;
3291 u64 objectid;
3292 int name_len;
3293 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04003294 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04003295 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04003296 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05003297 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003298
3299 name_len = strlen(symname) + 1;
3300 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3301 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05003302
Chris Mason39279cc2007-06-12 06:35:45 -04003303 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05003304 err = btrfs_check_free_space(root, 1, 0);
3305 if (err)
3306 goto out_fail;
3307
Chris Mason39279cc2007-06-12 06:35:45 -04003308 trans = btrfs_start_transaction(root, 1);
3309 btrfs_set_trans_block_group(trans, dir);
3310
3311 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3312 if (err) {
3313 err = -ENOSPC;
3314 goto out_unlock;
3315 }
3316
Chris Mason9c583092008-01-29 15:15:18 -05003317 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
3318 dentry->d_name.len,
3319 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04003320 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
3321 err = PTR_ERR(inode);
3322 if (IS_ERR(inode))
3323 goto out_unlock;
3324
3325 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05003326 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04003327 if (err)
3328 drop_inode = 1;
3329 else {
3330 inode->i_mapping->a_ops = &btrfs_aops;
Chris Mason04160082008-03-26 10:28:07 -04003331 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003332 inode->i_fop = &btrfs_file_operations;
3333 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05003334 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3335 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04003336 inode->i_mapping, GFP_NOFS);
Chris Mason7e383262008-04-09 16:28:12 -04003337 extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3338 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05003339 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason81d7ed22008-04-25 08:51:48 -04003340 atomic_set(&BTRFS_I(inode)->ordered_writeback, 0);
Chris Masond1310b22008-01-24 16:13:08 -05003341 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04003342 }
3343 dir->i_sb->s_dirt = 1;
3344 btrfs_update_inode_block_group(trans, inode);
3345 btrfs_update_inode_block_group(trans, dir);
3346 if (drop_inode)
3347 goto out_unlock;
3348
3349 path = btrfs_alloc_path();
3350 BUG_ON(!path);
3351 key.objectid = inode->i_ino;
3352 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04003353 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3354 datasize = btrfs_file_extent_calc_inline_size(name_len);
3355 err = btrfs_insert_empty_item(trans, root, path, &key,
3356 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04003357 if (err) {
3358 drop_inode = 1;
3359 goto out_unlock;
3360 }
Chris Mason5f39d392007-10-15 16:14:19 -04003361 leaf = path->nodes[0];
3362 ei = btrfs_item_ptr(leaf, path->slots[0],
3363 struct btrfs_file_extent_item);
3364 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3365 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04003366 BTRFS_FILE_EXTENT_INLINE);
3367 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04003368 write_extent_buffer(leaf, symname, ptr, name_len);
3369 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04003370 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04003371
Chris Mason39279cc2007-06-12 06:35:45 -04003372 inode->i_op = &btrfs_symlink_inode_operations;
3373 inode->i_mapping->a_ops = &btrfs_symlink_aops;
Chris Mason04160082008-03-26 10:28:07 -04003374 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
Chris Mason39279cc2007-06-12 06:35:45 -04003375 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04003376 err = btrfs_update_inode(trans, root, inode);
3377 if (err)
3378 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04003379
3380out_unlock:
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003381 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04003382 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05003383out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04003384 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04003385 if (drop_inode) {
3386 inode_dec_link_count(inode);
3387 iput(inode);
3388 }
Chris Masond3c2fdcf2007-09-17 10:58:06 -04003389 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05003390 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04003391 return err;
3392}
Chris Mason16432982008-04-10 10:23:21 -04003393
Yanfdebe2b2008-01-14 13:26:08 -05003394static int btrfs_permission(struct inode *inode, int mask,
3395 struct nameidata *nd)
3396{
3397 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3398 return -EACCES;
3399 return generic_permission(inode, mask, NULL);
3400}
Chris Mason39279cc2007-06-12 06:35:45 -04003401
3402static struct inode_operations btrfs_dir_inode_operations = {
3403 .lookup = btrfs_lookup,
3404 .create = btrfs_create,
3405 .unlink = btrfs_unlink,
3406 .link = btrfs_link,
3407 .mkdir = btrfs_mkdir,
3408 .rmdir = btrfs_rmdir,
3409 .rename = btrfs_rename,
3410 .symlink = btrfs_symlink,
3411 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003412 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003413 .setxattr = generic_setxattr,
3414 .getxattr = generic_getxattr,
3415 .listxattr = btrfs_listxattr,
3416 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003417 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003418};
Chris Mason39279cc2007-06-12 06:35:45 -04003419static struct inode_operations btrfs_dir_ro_inode_operations = {
3420 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003421 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003422};
Chris Mason39279cc2007-06-12 06:35:45 -04003423static struct file_operations btrfs_dir_file_operations = {
3424 .llseek = generic_file_llseek,
3425 .read = generic_read_dir,
3426 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003427 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003428#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003429 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003430#endif
3431};
3432
Chris Masond1310b22008-01-24 16:13:08 -05003433static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003434 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003435 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason239b14b2008-03-24 15:02:07 -04003436 .merge_bio_hook = btrfs_merge_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003437 .readpage_io_hook = btrfs_readpage_io_hook,
3438 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Mason7e383262008-04-09 16:28:12 -04003439 .readpage_io_failed_hook = btrfs_readpage_io_failed_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003440 .set_bit_hook = btrfs_set_bit_hook,
3441 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003442};
3443
Chris Mason39279cc2007-06-12 06:35:45 -04003444static struct address_space_operations btrfs_aops = {
3445 .readpage = btrfs_readpage,
3446 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003447 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003448 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003449 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003450 .bmap = btrfs_bmap,
Chris Mason16432982008-04-10 10:23:21 -04003451 .direct_IO = btrfs_direct_IO,
Chris Masona52d9a82007-08-27 16:49:44 -04003452 .invalidatepage = btrfs_invalidatepage,
3453 .releasepage = btrfs_releasepage,
3454 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003455};
3456
3457static struct address_space_operations btrfs_symlink_aops = {
3458 .readpage = btrfs_readpage,
3459 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003460 .invalidatepage = btrfs_invalidatepage,
3461 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003462};
3463
3464static struct inode_operations btrfs_file_inode_operations = {
3465 .truncate = btrfs_truncate,
3466 .getattr = btrfs_getattr,
3467 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003468 .setxattr = generic_setxattr,
3469 .getxattr = generic_getxattr,
3470 .listxattr = btrfs_listxattr,
3471 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003472 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003473};
Josef Bacik618e21d2007-07-11 10:18:17 -04003474static struct inode_operations btrfs_special_inode_operations = {
3475 .getattr = btrfs_getattr,
3476 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003477 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003478};
Chris Mason39279cc2007-06-12 06:35:45 -04003479static struct inode_operations btrfs_symlink_inode_operations = {
3480 .readlink = generic_readlink,
3481 .follow_link = page_follow_link_light,
3482 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003483 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003484};