blob: 315dd550d56013e3859668018a5e656636057198 [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 Mason065631f2008-02-20 12:07:25 -050019#include <linux/bio.h>
Chris Mason39279cc2007-06-12 06:35:45 -040020#include <linux/buffer_head.h>
21#include <linux/fs.h>
22#include <linux/pagemap.h>
23#include <linux/highmem.h>
24#include <linux/time.h>
25#include <linux/init.h>
26#include <linux/string.h>
27#include <linux/smp_lock.h>
28#include <linux/backing-dev.h>
29#include <linux/mpage.h>
30#include <linux/swap.h>
31#include <linux/writeback.h>
32#include <linux/statfs.h>
33#include <linux/compat.h>
Chris Mason9ebefb182007-06-15 13:50:00 -040034#include <linux/bit_spinlock.h>
Chris Mason92fee662007-07-25 12:31:35 -040035#include <linux/version.h>
Josef Bacik5103e942007-11-16 11:45:54 -050036#include <linux/xattr.h>
Chris Mason39279cc2007-06-12 06:35:45 -040037#include "ctree.h"
38#include "disk-io.h"
39#include "transaction.h"
40#include "btrfs_inode.h"
41#include "ioctl.h"
42#include "print-tree.h"
43
44struct btrfs_iget_args {
45 u64 ino;
46 struct btrfs_root *root;
47};
48
49static struct inode_operations btrfs_dir_inode_operations;
50static struct inode_operations btrfs_symlink_inode_operations;
51static struct inode_operations btrfs_dir_ro_inode_operations;
Josef Bacik618e21d2007-07-11 10:18:17 -040052static struct inode_operations btrfs_special_inode_operations;
Chris Mason39279cc2007-06-12 06:35:45 -040053static struct inode_operations btrfs_file_inode_operations;
54static struct address_space_operations btrfs_aops;
55static struct address_space_operations btrfs_symlink_aops;
56static struct file_operations btrfs_dir_file_operations;
Chris Masond1310b22008-01-24 16:13:08 -050057static struct extent_io_ops btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -040058
59static struct kmem_cache *btrfs_inode_cachep;
60struct kmem_cache *btrfs_trans_handle_cachep;
61struct kmem_cache *btrfs_transaction_cachep;
62struct kmem_cache *btrfs_bit_radix_cachep;
63struct kmem_cache *btrfs_path_cachep;
64
65#define S_SHIFT 12
66static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
67 [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE,
68 [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR,
69 [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV,
70 [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV,
71 [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO,
72 [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK,
73 [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK,
74};
75
Chris Mason1832a6d2007-12-21 16:27:21 -050076int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
77 int for_del)
78{
79 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
80 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
81 u64 thresh;
82 int ret = 0;
83
84 if (for_del)
Chris Masonf9ef6602008-01-03 09:22:38 -050085 thresh = total * 90;
Chris Mason1832a6d2007-12-21 16:27:21 -050086 else
Chris Masonf9ef6602008-01-03 09:22:38 -050087 thresh = total * 85;
88
89 do_div(thresh, 100);
Chris Mason1832a6d2007-12-21 16:27:21 -050090
91 spin_lock(&root->fs_info->delalloc_lock);
92 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
93 ret = -ENOSPC;
94 spin_unlock(&root->fs_info->delalloc_lock);
95 return ret;
96}
97
Chris Masonbe20aa92007-12-17 20:14:01 -050098static int cow_file_range(struct inode *inode, u64 start, u64 end)
Chris Masonb888db22007-08-27 16:49:44 -040099{
100 struct btrfs_root *root = BTRFS_I(inode)->root;
101 struct btrfs_trans_handle *trans;
Chris Masonb888db22007-08-27 16:49:44 -0400102 u64 alloc_hint = 0;
Chris Masondb945352007-10-15 16:15:53 -0400103 u64 num_bytes;
Chris Masonc59f8952007-12-17 20:14:04 -0500104 u64 cur_alloc_size;
Chris Masondb945352007-10-15 16:15:53 -0400105 u64 blocksize = root->sectorsize;
Chris Masond1310b22008-01-24 16:13:08 -0500106 u64 orig_start = start;
107 u64 orig_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500108 struct btrfs_key ins;
109 int ret;
Chris Masonb888db22007-08-27 16:49:44 -0400110
Chris Masonb888db22007-08-27 16:49:44 -0400111 trans = btrfs_start_transaction(root, 1);
Chris Masonb888db22007-08-27 16:49:44 -0400112 BUG_ON(!trans);
Chris Masonbe20aa92007-12-17 20:14:01 -0500113 btrfs_set_trans_block_group(trans, inode);
114
Chris Masondb945352007-10-15 16:15:53 -0400115 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
Chris Masonbe20aa92007-12-17 20:14:01 -0500116 num_bytes = max(blocksize, num_bytes);
Chris Masonb888db22007-08-27 16:49:44 -0400117 ret = btrfs_drop_extents(trans, root, inode,
Chris Mason3326d1b2007-10-15 16:18:25 -0400118 start, start + num_bytes, start, &alloc_hint);
Chris Masond1310b22008-01-24 16:13:08 -0500119 orig_num_bytes = num_bytes;
Chris Masondb945352007-10-15 16:15:53 -0400120
Chris Mason179e29e2007-11-01 11:28:41 -0400121 if (alloc_hint == EXTENT_MAP_INLINE)
122 goto out;
123
Chris Masonc59f8952007-12-17 20:14:04 -0500124 while(num_bytes > 0) {
125 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
126 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
127 root->root_key.objectid,
128 trans->transid,
129 inode->i_ino, start, 0,
130 alloc_hint, (u64)-1, &ins, 1);
131 if (ret) {
132 WARN_ON(1);
133 goto out;
134 }
135 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
136 start, ins.objectid, ins.offset,
137 ins.offset);
Chris Mason90692182008-02-08 13:49:28 -0500138 inode->i_blocks += ins.offset >> 9;
Chris Mason5f564062008-01-22 16:47:59 -0500139 btrfs_check_file(root, inode);
Chris Masonc59f8952007-12-17 20:14:04 -0500140 num_bytes -= cur_alloc_size;
141 alloc_hint = ins.objectid + ins.offset;
142 start += cur_alloc_size;
Chris Masonb888db22007-08-27 16:49:44 -0400143 }
Chris Masond1310b22008-01-24 16:13:08 -0500144 btrfs_drop_extent_cache(inode, orig_start,
145 orig_start + orig_num_bytes - 1);
Chris Masondc17ff82008-01-08 15:46:30 -0500146 btrfs_add_ordered_inode(inode);
Chris Mason90692182008-02-08 13:49:28 -0500147 btrfs_update_inode(trans, root, inode);
Chris Masonb888db22007-08-27 16:49:44 -0400148out:
149 btrfs_end_transaction(trans, root);
Chris Masonbe20aa92007-12-17 20:14:01 -0500150 return ret;
151}
152
153static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
154{
155 u64 extent_start;
156 u64 extent_end;
157 u64 bytenr;
158 u64 cow_end;
Chris Mason1832a6d2007-12-21 16:27:21 -0500159 u64 loops = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500160 u64 total_fs_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500161 struct btrfs_root *root = BTRFS_I(inode)->root;
162 struct extent_buffer *leaf;
163 int found_type;
164 struct btrfs_path *path;
165 struct btrfs_file_extent_item *item;
166 int ret;
167 int err;
168 struct btrfs_key found_key;
169
Chris Masonc31f8832008-01-08 15:46:31 -0500170 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masonbe20aa92007-12-17 20:14:01 -0500171 path = btrfs_alloc_path();
172 BUG_ON(!path);
173again:
174 ret = btrfs_lookup_file_extent(NULL, root, path,
175 inode->i_ino, start, 0);
176 if (ret < 0) {
177 btrfs_free_path(path);
178 return ret;
179 }
180
181 cow_end = end;
182 if (ret != 0) {
183 if (path->slots[0] == 0)
184 goto not_found;
185 path->slots[0]--;
186 }
187
188 leaf = path->nodes[0];
189 item = btrfs_item_ptr(leaf, path->slots[0],
190 struct btrfs_file_extent_item);
191
192 /* are we inside the extent that was found? */
193 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
194 found_type = btrfs_key_type(&found_key);
195 if (found_key.objectid != inode->i_ino ||
196 found_type != BTRFS_EXTENT_DATA_KEY) {
197 goto not_found;
198 }
199
200 found_type = btrfs_file_extent_type(leaf, item);
201 extent_start = found_key.offset;
202 if (found_type == BTRFS_FILE_EXTENT_REG) {
Chris Masonc31f8832008-01-08 15:46:31 -0500203 u64 extent_num_bytes;
204
205 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
206 extent_end = extent_start + extent_num_bytes;
Chris Masonbe20aa92007-12-17 20:14:01 -0500207 err = 0;
208
Chris Mason1832a6d2007-12-21 16:27:21 -0500209 if (loops && start != extent_start)
210 goto not_found;
211
Chris Masonbe20aa92007-12-17 20:14:01 -0500212 if (start < extent_start || start >= extent_end)
213 goto not_found;
214
215 cow_end = min(end, extent_end - 1);
216 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
217 if (bytenr == 0)
218 goto not_found;
219
Chris Masonc31f8832008-01-08 15:46:31 -0500220 /*
221 * we may be called by the resizer, make sure we're inside
222 * the limits of the FS
223 */
224 if (bytenr + extent_num_bytes > total_fs_bytes)
225 goto not_found;
226
Chris Masonbe20aa92007-12-17 20:14:01 -0500227 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
228 goto not_found;
229 }
230
231 start = extent_end;
Chris Masonbd098352008-01-03 13:23:19 -0500232 } else {
Chris Masonbe20aa92007-12-17 20:14:01 -0500233 goto not_found;
234 }
235loop:
236 if (start > end) {
237 btrfs_free_path(path);
238 return 0;
239 }
240 btrfs_release_path(root, path);
Chris Mason1832a6d2007-12-21 16:27:21 -0500241 loops++;
Chris Masonbe20aa92007-12-17 20:14:01 -0500242 goto again;
243
244not_found:
245 cow_file_range(inode, start, cow_end);
246 start = cow_end + 1;
247 goto loop;
248}
249
250static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
251{
252 struct btrfs_root *root = BTRFS_I(inode)->root;
253 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500254 mutex_lock(&root->fs_info->fs_mutex);
Yanb98b6762008-01-08 15:54:37 -0500255 if (btrfs_test_opt(root, NODATACOW) ||
256 btrfs_test_flag(inode, NODATACOW))
Chris Masonbe20aa92007-12-17 20:14:01 -0500257 ret = run_delalloc_nocow(inode, start, end);
258 else
259 ret = cow_file_range(inode, start, end);
Chris Mason1832a6d2007-12-21 16:27:21 -0500260
Chris Masonb888db22007-08-27 16:49:44 -0400261 mutex_unlock(&root->fs_info->fs_mutex);
262 return ret;
263}
264
Chris Mason291d6732008-01-29 15:55:23 -0500265int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500266 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500267{
Chris Masonb0c68f82008-01-31 11:05:37 -0500268 if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
Chris Mason291d6732008-01-29 15:55:23 -0500269 struct btrfs_root *root = BTRFS_I(inode)->root;
270 spin_lock(&root->fs_info->delalloc_lock);
Chris Mason90692182008-02-08 13:49:28 -0500271 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
Chris Mason291d6732008-01-29 15:55:23 -0500272 root->fs_info->delalloc_bytes += end - start + 1;
273 spin_unlock(&root->fs_info->delalloc_lock);
274 }
275 return 0;
276}
277
278int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
Chris Masonb0c68f82008-01-31 11:05:37 -0500279 unsigned long old, unsigned long bits)
Chris Mason291d6732008-01-29 15:55:23 -0500280{
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;
283 spin_lock(&root->fs_info->delalloc_lock);
Chris Masonb0c68f82008-01-31 11:05:37 -0500284 if (end - start + 1 > root->fs_info->delalloc_bytes) {
285 printk("warning: delalloc account %Lu %Lu\n",
286 end - start + 1, root->fs_info->delalloc_bytes);
287 root->fs_info->delalloc_bytes = 0;
Chris Mason90692182008-02-08 13:49:28 -0500288 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masonb0c68f82008-01-31 11:05:37 -0500289 } else {
290 root->fs_info->delalloc_bytes -= end - start + 1;
Chris Mason90692182008-02-08 13:49:28 -0500291 BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
Chris Masonb0c68f82008-01-31 11:05:37 -0500292 }
Chris Mason291d6732008-01-29 15:55:23 -0500293 spin_unlock(&root->fs_info->delalloc_lock);
294 }
295 return 0;
296}
297
Chris Mason065631f2008-02-20 12:07:25 -0500298int btrfs_submit_bio_hook(int rw, struct bio *bio)
299{
Chris Mason065631f2008-02-20 12:07:25 -0500300 struct bio_vec *bvec = bio->bi_io_vec;
301 struct inode *inode = bvec->bv_page->mapping->host;
302 struct btrfs_root *root = BTRFS_I(inode)->root;
303 struct btrfs_trans_handle *trans;
304 int ret = 0;
305
306 if (rw != WRITE)
307 return 0;
308
309 if (btrfs_test_opt(root, NODATASUM) ||
310 btrfs_test_flag(inode, NODATASUM))
311 return 0;
312
313 mutex_lock(&root->fs_info->fs_mutex);
314 trans = btrfs_start_transaction(root, 1);
315 btrfs_set_trans_block_group(trans, inode);
316 btrfs_csum_file_blocks(trans, root, inode, bio);
317 ret = btrfs_end_transaction(trans, root);
318 BUG_ON(ret);
319 mutex_unlock(&root->fs_info->fs_mutex);
320 return ret;
321}
Chris Mason6885f302008-02-20 16:11:05 -0500322
Chris Mason07157aa2007-08-30 08:50:51 -0400323int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
324{
325 int ret = 0;
326 struct inode *inode = page->mapping->host;
327 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -0500328 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400329 struct btrfs_csum_item *item;
330 struct btrfs_path *path = NULL;
Chris Masonff79f812007-10-15 16:22:25 -0400331 u32 csum;
Yanb98b6762008-01-08 15:54:37 -0500332 if (btrfs_test_opt(root, NODATASUM) ||
333 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500334 return 0;
Chris Mason07157aa2007-08-30 08:50:51 -0400335 mutex_lock(&root->fs_info->fs_mutex);
336 path = btrfs_alloc_path();
337 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
338 if (IS_ERR(item)) {
339 ret = PTR_ERR(item);
340 /* a csum that isn't present is a preallocated region. */
341 if (ret == -ENOENT || ret == -EFBIG)
342 ret = 0;
Chris Masonff79f812007-10-15 16:22:25 -0400343 csum = 0;
Chris Masonaadfeb62008-01-29 09:10:27 -0500344 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
Chris Mason07157aa2007-08-30 08:50:51 -0400345 goto out;
346 }
Chris Masonff79f812007-10-15 16:22:25 -0400347 read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
348 BTRFS_CRC32_SIZE);
Chris Masond1310b22008-01-24 16:13:08 -0500349 set_state_private(io_tree, start, csum);
Chris Mason07157aa2007-08-30 08:50:51 -0400350out:
351 if (path)
352 btrfs_free_path(path);
353 mutex_unlock(&root->fs_info->fs_mutex);
354 return ret;
355}
356
Chris Mason70dec802008-01-29 09:59:12 -0500357int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
358 struct extent_state *state)
Chris Mason07157aa2007-08-30 08:50:51 -0400359{
Chris Mason35ebb932007-10-30 16:56:53 -0400360 size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
Chris Mason07157aa2007-08-30 08:50:51 -0400361 struct inode *inode = page->mapping->host;
Chris Masond1310b22008-01-24 16:13:08 -0500362 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason07157aa2007-08-30 08:50:51 -0400363 char *kaddr;
Chris Masonaadfeb62008-01-29 09:10:27 -0500364 u64 private = ~(u32)0;
Chris Mason07157aa2007-08-30 08:50:51 -0400365 int ret;
Chris Masonff79f812007-10-15 16:22:25 -0400366 struct btrfs_root *root = BTRFS_I(inode)->root;
367 u32 csum = ~(u32)0;
Jens Axboebbf0d002007-10-19 09:23:07 -0400368 unsigned long flags;
Chris Masond1310b22008-01-24 16:13:08 -0500369
Yanb98b6762008-01-08 15:54:37 -0500370 if (btrfs_test_opt(root, NODATASUM) ||
371 btrfs_test_flag(inode, NODATASUM))
Chris Masonb6cda9b2007-12-14 15:30:32 -0500372 return 0;
Yanc2e639f2008-02-04 08:57:25 -0500373 if (state && state->start == start) {
Chris Mason70dec802008-01-29 09:59:12 -0500374 private = state->private;
375 ret = 0;
376 } else {
377 ret = get_state_private(io_tree, start, &private);
378 }
Jens Axboebbf0d002007-10-19 09:23:07 -0400379 local_irq_save(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400380 kaddr = kmap_atomic(page, KM_IRQ0);
381 if (ret) {
382 goto zeroit;
383 }
Chris Masonff79f812007-10-15 16:22:25 -0400384 csum = btrfs_csum_data(root, kaddr + offset, csum, end - start + 1);
385 btrfs_csum_final(csum, (char *)&csum);
386 if (csum != private) {
Chris Mason07157aa2007-08-30 08:50:51 -0400387 goto zeroit;
388 }
389 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400390 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400391 return 0;
392
393zeroit:
Chris Masonaadfeb62008-01-29 09:10:27 -0500394 printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
395 page->mapping->host->i_ino, (unsigned long long)start, csum,
396 private);
Chris Masondb945352007-10-15 16:15:53 -0400397 memset(kaddr + offset, 1, end - start + 1);
398 flush_dcache_page(page);
Chris Mason07157aa2007-08-30 08:50:51 -0400399 kunmap_atomic(kaddr, KM_IRQ0);
Jens Axboebbf0d002007-10-19 09:23:07 -0400400 local_irq_restore(flags);
Chris Mason07157aa2007-08-30 08:50:51 -0400401 return 0;
402}
Chris Masonb888db22007-08-27 16:49:44 -0400403
Chris Mason39279cc2007-06-12 06:35:45 -0400404void btrfs_read_locked_inode(struct inode *inode)
405{
406 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400407 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400408 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -0400409 struct btrfs_inode_timespec *tspec;
Chris Mason39279cc2007-06-12 06:35:45 -0400410 struct btrfs_root *root = BTRFS_I(inode)->root;
411 struct btrfs_key location;
412 u64 alloc_group_block;
Josef Bacik618e21d2007-07-11 10:18:17 -0400413 u32 rdev;
Chris Mason39279cc2007-06-12 06:35:45 -0400414 int ret;
415
416 path = btrfs_alloc_path();
417 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400418 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -0400419 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
Chris Masondc17ff82008-01-08 15:46:30 -0500420
Chris Mason39279cc2007-06-12 06:35:45 -0400421 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400422 if (ret)
Chris Mason39279cc2007-06-12 06:35:45 -0400423 goto make_bad;
Chris Mason39279cc2007-06-12 06:35:45 -0400424
Chris Mason5f39d392007-10-15 16:14:19 -0400425 leaf = path->nodes[0];
426 inode_item = btrfs_item_ptr(leaf, path->slots[0],
427 struct btrfs_inode_item);
428
429 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
430 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
431 inode->i_uid = btrfs_inode_uid(leaf, inode_item);
432 inode->i_gid = btrfs_inode_gid(leaf, inode_item);
433 inode->i_size = btrfs_inode_size(leaf, inode_item);
434
435 tspec = btrfs_inode_atime(inode_item);
436 inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
437 inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
438
439 tspec = btrfs_inode_mtime(inode_item);
440 inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
441 inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
442
443 tspec = btrfs_inode_ctime(inode_item);
444 inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
445 inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
446
447 inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
448 inode->i_generation = btrfs_inode_generation(leaf, inode_item);
Josef Bacik618e21d2007-07-11 10:18:17 -0400449 inode->i_rdev = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400450 rdev = btrfs_inode_rdev(leaf, inode_item);
451
452 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
Chris Mason39279cc2007-06-12 06:35:45 -0400453 BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
454 alloc_group_block);
Yanb98b6762008-01-08 15:54:37 -0500455 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
Chris Masone52ec0e2007-12-21 16:36:24 -0500456 if (!BTRFS_I(inode)->block_group) {
457 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
458 NULL, 0, 0, 0);
459 }
Chris Mason39279cc2007-06-12 06:35:45 -0400460 btrfs_free_path(path);
461 inode_item = NULL;
462
463 mutex_unlock(&root->fs_info->fs_mutex);
464
465 switch (inode->i_mode & S_IFMT) {
Chris Mason39279cc2007-06-12 06:35:45 -0400466 case S_IFREG:
467 inode->i_mapping->a_ops = &btrfs_aops;
Chris Masond1310b22008-01-24 16:13:08 -0500468 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -0400469 inode->i_fop = &btrfs_file_operations;
470 inode->i_op = &btrfs_file_inode_operations;
471 break;
472 case S_IFDIR:
473 inode->i_fop = &btrfs_dir_file_operations;
474 if (root == root->fs_info->tree_root)
475 inode->i_op = &btrfs_dir_ro_inode_operations;
476 else
477 inode->i_op = &btrfs_dir_inode_operations;
478 break;
479 case S_IFLNK:
480 inode->i_op = &btrfs_symlink_inode_operations;
481 inode->i_mapping->a_ops = &btrfs_symlink_aops;
482 break;
Josef Bacik618e21d2007-07-11 10:18:17 -0400483 default:
484 init_special_inode(inode, inode->i_mode, rdev);
485 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400486 }
487 return;
488
489make_bad:
490 btrfs_release_path(root, path);
491 btrfs_free_path(path);
492 mutex_unlock(&root->fs_info->fs_mutex);
493 make_bad_inode(inode);
494}
495
Chris Mason5f39d392007-10-15 16:14:19 -0400496static void fill_inode_item(struct extent_buffer *leaf,
497 struct btrfs_inode_item *item,
Chris Mason39279cc2007-06-12 06:35:45 -0400498 struct inode *inode)
499{
Chris Mason5f39d392007-10-15 16:14:19 -0400500 btrfs_set_inode_uid(leaf, item, inode->i_uid);
501 btrfs_set_inode_gid(leaf, item, inode->i_gid);
502 btrfs_set_inode_size(leaf, item, inode->i_size);
503 btrfs_set_inode_mode(leaf, item, inode->i_mode);
504 btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
505
506 btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
507 inode->i_atime.tv_sec);
508 btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
509 inode->i_atime.tv_nsec);
510
511 btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
512 inode->i_mtime.tv_sec);
513 btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
514 inode->i_mtime.tv_nsec);
515
516 btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
517 inode->i_ctime.tv_sec);
518 btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
519 inode->i_ctime.tv_nsec);
520
521 btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
522 btrfs_set_inode_generation(leaf, item, inode->i_generation);
523 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
Yanb98b6762008-01-08 15:54:37 -0500524 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
Chris Mason5f39d392007-10-15 16:14:19 -0400525 btrfs_set_inode_block_group(leaf, item,
Chris Mason39279cc2007-06-12 06:35:45 -0400526 BTRFS_I(inode)->block_group->key.objectid);
527}
528
Chris Masona52d9a82007-08-27 16:49:44 -0400529int btrfs_update_inode(struct btrfs_trans_handle *trans,
Chris Mason39279cc2007-06-12 06:35:45 -0400530 struct btrfs_root *root,
531 struct inode *inode)
532{
533 struct btrfs_inode_item *inode_item;
534 struct btrfs_path *path;
Chris Mason5f39d392007-10-15 16:14:19 -0400535 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400536 int ret;
537
538 path = btrfs_alloc_path();
539 BUG_ON(!path);
Chris Mason39279cc2007-06-12 06:35:45 -0400540 ret = btrfs_lookup_inode(trans, root, path,
541 &BTRFS_I(inode)->location, 1);
542 if (ret) {
543 if (ret > 0)
544 ret = -ENOENT;
545 goto failed;
546 }
547
Chris Mason5f39d392007-10-15 16:14:19 -0400548 leaf = path->nodes[0];
549 inode_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400550 struct btrfs_inode_item);
551
Chris Mason5f39d392007-10-15 16:14:19 -0400552 fill_inode_item(leaf, inode_item, inode);
553 btrfs_mark_buffer_dirty(leaf);
Josef Bacik15ee9bc2007-08-10 16:22:09 -0400554 btrfs_set_inode_last_trans(trans, inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400555 ret = 0;
556failed:
557 btrfs_release_path(root, path);
558 btrfs_free_path(path);
559 return ret;
560}
561
562
563static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
564 struct btrfs_root *root,
565 struct inode *dir,
566 struct dentry *dentry)
567{
568 struct btrfs_path *path;
569 const char *name = dentry->d_name.name;
570 int name_len = dentry->d_name.len;
571 int ret = 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400572 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400573 struct btrfs_dir_item *di;
Chris Mason5f39d392007-10-15 16:14:19 -0400574 struct btrfs_key key;
Chris Mason39279cc2007-06-12 06:35:45 -0400575
576 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400577 if (!path) {
578 ret = -ENOMEM;
579 goto err;
580 }
581
Chris Mason39279cc2007-06-12 06:35:45 -0400582 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
583 name, name_len, -1);
584 if (IS_ERR(di)) {
585 ret = PTR_ERR(di);
586 goto err;
587 }
588 if (!di) {
589 ret = -ENOENT;
590 goto err;
591 }
Chris Mason5f39d392007-10-15 16:14:19 -0400592 leaf = path->nodes[0];
593 btrfs_dir_item_key_to_cpu(leaf, di, &key);
Chris Mason39279cc2007-06-12 06:35:45 -0400594 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason54aa1f42007-06-22 14:16:25 -0400595 if (ret)
596 goto err;
Chris Mason39279cc2007-06-12 06:35:45 -0400597 btrfs_release_path(root, path);
598
599 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
Chris Mason5f39d392007-10-15 16:14:19 -0400600 key.objectid, name, name_len, -1);
Chris Mason39279cc2007-06-12 06:35:45 -0400601 if (IS_ERR(di)) {
602 ret = PTR_ERR(di);
603 goto err;
604 }
605 if (!di) {
606 ret = -ENOENT;
607 goto err;
608 }
609 ret = btrfs_delete_one_dir_name(trans, root, path, di);
Chris Mason39279cc2007-06-12 06:35:45 -0400610
611 dentry->d_inode->i_ctime = dir->i_ctime;
Chris Mason76fea002007-12-13 09:06:01 -0500612 ret = btrfs_del_inode_ref(trans, root, name, name_len,
613 dentry->d_inode->i_ino,
614 dentry->d_parent->d_inode->i_ino);
615 if (ret) {
616 printk("failed to delete reference to %.*s, "
617 "inode %lu parent %lu\n", name_len, name,
618 dentry->d_inode->i_ino,
619 dentry->d_parent->d_inode->i_ino);
Chris Mason39544012007-12-12 14:38:19 -0500620 }
Chris Mason39279cc2007-06-12 06:35:45 -0400621err:
622 btrfs_free_path(path);
623 if (!ret) {
624 dir->i_size -= name_len * 2;
Chris Mason79c44582007-06-25 10:09:33 -0400625 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -0400626 btrfs_update_inode(trans, root, dir);
Chris Mason6da6aba2007-12-18 16:15:09 -0500627#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
628 dentry->d_inode->i_nlink--;
629#else
Chris Mason39279cc2007-06-12 06:35:45 -0400630 drop_nlink(dentry->d_inode);
Chris Mason6da6aba2007-12-18 16:15:09 -0500631#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400632 ret = btrfs_update_inode(trans, root, dentry->d_inode);
Chris Mason39279cc2007-06-12 06:35:45 -0400633 dir->i_sb->s_dirt = 1;
634 }
635 return ret;
636}
637
638static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
639{
640 struct btrfs_root *root;
641 struct btrfs_trans_handle *trans;
Chris Mason2da98f02008-01-16 11:44:43 -0500642 struct inode *inode = dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -0400643 int ret;
Chris Mason1832a6d2007-12-21 16:27:21 -0500644 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400645
646 root = BTRFS_I(dir)->root;
647 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500648
649 ret = btrfs_check_free_space(root, 1, 1);
650 if (ret)
651 goto fail;
652
Chris Mason39279cc2007-06-12 06:35:45 -0400653 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -0400654
Chris Mason39279cc2007-06-12 06:35:45 -0400655 btrfs_set_trans_block_group(trans, dir);
656 ret = btrfs_unlink_trans(trans, root, dir, dentry);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400657 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -0400658
Chris Mason2da98f02008-01-16 11:44:43 -0500659 if (inode->i_nlink == 0) {
660 int found;
661 /* if the inode isn't linked anywhere,
662 * we don't need to worry about
663 * data=ordered
664 */
665 found = btrfs_del_ordered_inode(inode);
666 if (found == 1) {
667 atomic_dec(&inode->i_count);
668 }
669 }
670
Chris Mason39279cc2007-06-12 06:35:45 -0400671 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500672fail:
Chris Mason39279cc2007-06-12 06:35:45 -0400673 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400674 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500675 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -0400676 return ret;
677}
678
679static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
680{
681 struct inode *inode = dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -0500682 int err = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400683 int ret;
684 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason39279cc2007-06-12 06:35:45 -0400685 struct btrfs_trans_handle *trans;
Chris Mason1832a6d2007-12-21 16:27:21 -0500686 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400687
Yan134d4512007-10-25 15:49:25 -0400688 if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
689 return -ENOTEMPTY;
690
Chris Mason39279cc2007-06-12 06:35:45 -0400691 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -0500692 ret = btrfs_check_free_space(root, 1, 1);
693 if (ret)
694 goto fail;
695
Chris Mason39279cc2007-06-12 06:35:45 -0400696 trans = btrfs_start_transaction(root, 1);
697 btrfs_set_trans_block_group(trans, dir);
Chris Mason39279cc2007-06-12 06:35:45 -0400698
699 /* now the directory is empty */
700 err = btrfs_unlink_trans(trans, root, dir, dentry);
701 if (!err) {
702 inode->i_size = 0;
703 }
Chris Mason39544012007-12-12 14:38:19 -0500704
Chris Masond3c2fdc2007-09-17 10:58:06 -0400705 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -0400706 ret = btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -0500707fail:
Yan134d4512007-10-25 15:49:25 -0400708 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -0400709 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -0500710 btrfs_throttle(root);
Chris Mason39544012007-12-12 14:38:19 -0500711
Chris Mason39279cc2007-06-12 06:35:45 -0400712 if (ret && !err)
713 err = ret;
714 return err;
715}
716
Chris Mason39279cc2007-06-12 06:35:45 -0400717/*
Chris Mason39279cc2007-06-12 06:35:45 -0400718 * this can truncate away extent items, csum items and directory items.
719 * It starts at a high offset and removes keys until it can't find
720 * any higher than i_size.
721 *
722 * csum items that cross the new i_size are truncated to the new size
723 * as well.
724 */
725static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
726 struct btrfs_root *root,
Chris Mason85e21ba2008-01-29 15:11:36 -0500727 struct inode *inode,
728 u32 min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400729{
730 int ret;
731 struct btrfs_path *path;
732 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400733 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -0400734 u32 found_type;
Chris Mason5f39d392007-10-15 16:14:19 -0400735 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -0400736 struct btrfs_file_extent_item *fi;
737 u64 extent_start = 0;
Chris Masondb945352007-10-15 16:15:53 -0400738 u64 extent_num_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400739 u64 item_end = 0;
Chris Mason7bb86312007-12-11 09:25:06 -0500740 u64 root_gen = 0;
Chris Masond8d5f3e2007-12-11 12:42:00 -0500741 u64 root_owner = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400742 int found_extent;
743 int del_item;
Chris Mason85e21ba2008-01-29 15:11:36 -0500744 int pending_del_nr = 0;
745 int pending_del_slot = 0;
Chris Mason179e29e2007-11-01 11:28:41 -0400746 int extent_type = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400747
Chris Masona52d9a82007-08-27 16:49:44 -0400748 btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -0400749 path = btrfs_alloc_path();
Chris Mason3c69fae2007-08-07 15:52:22 -0400750 path->reada = -1;
Chris Mason39279cc2007-06-12 06:35:45 -0400751 BUG_ON(!path);
Chris Mason5f39d392007-10-15 16:14:19 -0400752
Chris Mason39279cc2007-06-12 06:35:45 -0400753 /* FIXME, add redo link to tree so we don't leak on crash */
754 key.objectid = inode->i_ino;
755 key.offset = (u64)-1;
Chris Mason5f39d392007-10-15 16:14:19 -0400756 key.type = (u8)-1;
757
Chris Mason85e21ba2008-01-29 15:11:36 -0500758 btrfs_init_path(path);
759search_again:
760 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
761 if (ret < 0) {
762 goto error;
763 }
764 if (ret > 0) {
765 BUG_ON(path->slots[0] == 0);
766 path->slots[0]--;
767 }
768
Chris Mason39279cc2007-06-12 06:35:45 -0400769 while(1) {
Chris Mason39279cc2007-06-12 06:35:45 -0400770 fi = NULL;
Chris Mason5f39d392007-10-15 16:14:19 -0400771 leaf = path->nodes[0];
772 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
773 found_type = btrfs_key_type(&found_key);
Chris Mason39279cc2007-06-12 06:35:45 -0400774
Chris Mason5f39d392007-10-15 16:14:19 -0400775 if (found_key.objectid != inode->i_ino)
Chris Mason39279cc2007-06-12 06:35:45 -0400776 break;
Chris Mason5f39d392007-10-15 16:14:19 -0400777
Chris Mason85e21ba2008-01-29 15:11:36 -0500778 if (found_type < min_type)
Chris Mason39279cc2007-06-12 06:35:45 -0400779 break;
780
Chris Mason5f39d392007-10-15 16:14:19 -0400781 item_end = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -0400782 if (found_type == BTRFS_EXTENT_DATA_KEY) {
Chris Mason5f39d392007-10-15 16:14:19 -0400783 fi = btrfs_item_ptr(leaf, path->slots[0],
Chris Mason39279cc2007-06-12 06:35:45 -0400784 struct btrfs_file_extent_item);
Chris Mason179e29e2007-11-01 11:28:41 -0400785 extent_type = btrfs_file_extent_type(leaf, fi);
786 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason5f39d392007-10-15 16:14:19 -0400787 item_end +=
Chris Masondb945352007-10-15 16:15:53 -0400788 btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason179e29e2007-11-01 11:28:41 -0400789 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
790 struct btrfs_item *item = btrfs_item_nr(leaf,
791 path->slots[0]);
792 item_end += btrfs_file_extent_inline_len(leaf,
793 item);
Chris Mason39279cc2007-06-12 06:35:45 -0400794 }
Yan008630c2007-11-07 13:31:09 -0500795 item_end--;
Chris Mason39279cc2007-06-12 06:35:45 -0400796 }
797 if (found_type == BTRFS_CSUM_ITEM_KEY) {
798 ret = btrfs_csum_truncate(trans, root, path,
799 inode->i_size);
800 BUG_ON(ret);
801 }
Yan008630c2007-11-07 13:31:09 -0500802 if (item_end < inode->i_size) {
Chris Masonb888db22007-08-27 16:49:44 -0400803 if (found_type == BTRFS_DIR_ITEM_KEY) {
804 found_type = BTRFS_INODE_ITEM_KEY;
805 } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
806 found_type = BTRFS_CSUM_ITEM_KEY;
Chris Mason85e21ba2008-01-29 15:11:36 -0500807 } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
808 found_type = BTRFS_XATTR_ITEM_KEY;
809 } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
810 found_type = BTRFS_INODE_REF_KEY;
Chris Masonb888db22007-08-27 16:49:44 -0400811 } else if (found_type) {
812 found_type--;
813 } else {
814 break;
Chris Mason39279cc2007-06-12 06:35:45 -0400815 }
Yana61721d2007-09-17 11:08:38 -0400816 btrfs_set_key_type(&key, found_type);
Chris Mason85e21ba2008-01-29 15:11:36 -0500817 goto next;
Chris Mason39279cc2007-06-12 06:35:45 -0400818 }
Chris Mason5f39d392007-10-15 16:14:19 -0400819 if (found_key.offset >= inode->i_size)
Chris Mason39279cc2007-06-12 06:35:45 -0400820 del_item = 1;
821 else
822 del_item = 0;
823 found_extent = 0;
824
825 /* FIXME, shrink the extent if the ref count is only 1 */
Chris Mason179e29e2007-11-01 11:28:41 -0400826 if (found_type != BTRFS_EXTENT_DATA_KEY)
827 goto delete;
828
829 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
Chris Mason39279cc2007-06-12 06:35:45 -0400830 u64 num_dec;
Chris Masondb945352007-10-15 16:15:53 -0400831 extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400832 if (!del_item) {
Chris Masondb945352007-10-15 16:15:53 -0400833 u64 orig_num_bytes =
834 btrfs_file_extent_num_bytes(leaf, fi);
835 extent_num_bytes = inode->i_size -
Chris Mason5f39d392007-10-15 16:14:19 -0400836 found_key.offset + root->sectorsize - 1;
Yanb1632b102008-01-30 11:54:04 -0500837 extent_num_bytes = extent_num_bytes &
838 ~((u64)root->sectorsize - 1);
Chris Masondb945352007-10-15 16:15:53 -0400839 btrfs_set_file_extent_num_bytes(leaf, fi,
840 extent_num_bytes);
841 num_dec = (orig_num_bytes -
Chris Mason90692182008-02-08 13:49:28 -0500842 extent_num_bytes);
843 if (extent_start != 0)
844 dec_i_blocks(inode, num_dec);
Chris Mason5f39d392007-10-15 16:14:19 -0400845 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400846 } else {
Chris Masondb945352007-10-15 16:15:53 -0400847 extent_num_bytes =
848 btrfs_file_extent_disk_num_bytes(leaf,
849 fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400850 /* FIXME blocksize != 4096 */
Chris Mason90692182008-02-08 13:49:28 -0500851 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
Chris Mason39279cc2007-06-12 06:35:45 -0400852 if (extent_start != 0) {
853 found_extent = 1;
Chris Mason90692182008-02-08 13:49:28 -0500854 dec_i_blocks(inode, num_dec);
Chris Mason39279cc2007-06-12 06:35:45 -0400855 }
Chris Masond8d5f3e2007-12-11 12:42:00 -0500856 root_gen = btrfs_header_generation(leaf);
857 root_owner = btrfs_header_owner(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -0400858 }
Chris Mason90692182008-02-08 13:49:28 -0500859 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
860 if (!del_item) {
861 u32 newsize = inode->i_size - found_key.offset;
862 dec_i_blocks(inode, item_end + 1 -
863 found_key.offset - newsize);
864 newsize =
865 btrfs_file_extent_calc_inline_size(newsize);
866 ret = btrfs_truncate_item(trans, root, path,
867 newsize, 1);
868 BUG_ON(ret);
869 } else {
870 dec_i_blocks(inode, item_end + 1 -
871 found_key.offset);
872 }
Chris Mason39279cc2007-06-12 06:35:45 -0400873 }
Chris Mason179e29e2007-11-01 11:28:41 -0400874delete:
Chris Mason39279cc2007-06-12 06:35:45 -0400875 if (del_item) {
Chris Mason85e21ba2008-01-29 15:11:36 -0500876 if (!pending_del_nr) {
877 /* no pending yet, add ourselves */
878 pending_del_slot = path->slots[0];
879 pending_del_nr = 1;
880 } else if (pending_del_nr &&
881 path->slots[0] + 1 == pending_del_slot) {
882 /* hop on the pending chunk */
883 pending_del_nr++;
884 pending_del_slot = path->slots[0];
885 } else {
886 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
887 }
Chris Mason39279cc2007-06-12 06:35:45 -0400888 } else {
889 break;
890 }
Chris Mason39279cc2007-06-12 06:35:45 -0400891 if (found_extent) {
892 ret = btrfs_free_extent(trans, root, extent_start,
Chris Mason7bb86312007-12-11 09:25:06 -0500893 extent_num_bytes,
Chris Masond8d5f3e2007-12-11 12:42:00 -0500894 root_owner,
Chris Mason7bb86312007-12-11 09:25:06 -0500895 root_gen, inode->i_ino,
896 found_key.offset, 0);
Chris Mason39279cc2007-06-12 06:35:45 -0400897 BUG_ON(ret);
898 }
Chris Mason85e21ba2008-01-29 15:11:36 -0500899next:
900 if (path->slots[0] == 0) {
901 if (pending_del_nr)
902 goto del_pending;
903 btrfs_release_path(root, path);
904 goto search_again;
905 }
906
907 path->slots[0]--;
908 if (pending_del_nr &&
909 path->slots[0] + 1 != pending_del_slot) {
910 struct btrfs_key debug;
911del_pending:
912 btrfs_item_key_to_cpu(path->nodes[0], &debug,
913 pending_del_slot);
914 ret = btrfs_del_items(trans, root, path,
915 pending_del_slot,
916 pending_del_nr);
917 BUG_ON(ret);
918 pending_del_nr = 0;
919 btrfs_release_path(root, path);
920 goto search_again;
921 }
Chris Mason39279cc2007-06-12 06:35:45 -0400922 }
923 ret = 0;
924error:
Chris Mason85e21ba2008-01-29 15:11:36 -0500925 if (pending_del_nr) {
926 ret = btrfs_del_items(trans, root, path, pending_del_slot,
927 pending_del_nr);
928 }
Chris Mason39279cc2007-06-12 06:35:45 -0400929 btrfs_release_path(root, path);
930 btrfs_free_path(path);
931 inode->i_sb->s_dirt = 1;
932 return ret;
933}
934
Chris Masonb888db22007-08-27 16:49:44 -0400935static int btrfs_cow_one_page(struct inode *inode, struct page *page,
Chris Masona52d9a82007-08-27 16:49:44 -0400936 size_t zero_start)
Chris Mason39279cc2007-06-12 06:35:45 -0400937{
Chris Mason39279cc2007-06-12 06:35:45 -0400938 char *kaddr;
Chris Masond1310b22008-01-24 16:13:08 -0500939 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason35ebb932007-10-30 16:56:53 -0400940 u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masonb888db22007-08-27 16:49:44 -0400941 u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Mason1832a6d2007-12-21 16:27:21 -0500942 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -0400943
Chris Mason190662b2007-12-18 16:25:45 -0500944 WARN_ON(!PageLocked(page));
Christoph Hellwigb3cfa352007-09-17 11:25:58 -0400945 set_page_extent_mapped(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400946
Chris Masond1310b22008-01-24 16:13:08 -0500947 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -0500948 set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
Chris Masonb888db22007-08-27 16:49:44 -0400949 page_end, GFP_NOFS);
Chris Mason1832a6d2007-12-21 16:27:21 -0500950
Chris Masona52d9a82007-08-27 16:49:44 -0400951 if (zero_start != PAGE_CACHE_SIZE) {
Chris Masonb888db22007-08-27 16:49:44 -0400952 kaddr = kmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400953 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
954 flush_dcache_page(page);
Chris Masonb888db22007-08-27 16:49:44 -0400955 kunmap(page);
Chris Masona52d9a82007-08-27 16:49:44 -0400956 }
Chris Masonb888db22007-08-27 16:49:44 -0400957 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -0500958 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -0400959
Chris Masona52d9a82007-08-27 16:49:44 -0400960 return ret;
961}
962
963/*
964 * taken from block_truncate_page, but does cow as it zeros out
965 * any bytes left in the last page in the file.
966 */
967static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
968{
969 struct inode *inode = mapping->host;
Chris Masondb945352007-10-15 16:15:53 -0400970 struct btrfs_root *root = BTRFS_I(inode)->root;
971 u32 blocksize = root->sectorsize;
Chris Masona52d9a82007-08-27 16:49:44 -0400972 pgoff_t index = from >> PAGE_CACHE_SHIFT;
973 unsigned offset = from & (PAGE_CACHE_SIZE-1);
974 struct page *page;
975 int ret = 0;
976 u64 page_start;
977
978 if ((offset & (blocksize - 1)) == 0)
979 goto out;
980
981 ret = -ENOMEM;
982 page = grab_cache_page(mapping, index);
983 if (!page)
984 goto out;
985 if (!PageUptodate(page)) {
986 ret = btrfs_readpage(NULL, page);
987 lock_page(page);
988 if (!PageUptodate(page)) {
989 ret = -EIO;
990 goto out;
991 }
992 }
Chris Mason35ebb932007-10-30 16:56:53 -0400993 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -0400994
Chris Masonb888db22007-08-27 16:49:44 -0400995 ret = btrfs_cow_one_page(inode, page, offset);
Chris Mason39279cc2007-06-12 06:35:45 -0400996
Chris Mason39279cc2007-06-12 06:35:45 -0400997 unlock_page(page);
998 page_cache_release(page);
999out:
1000 return ret;
1001}
1002
1003static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1004{
1005 struct inode *inode = dentry->d_inode;
1006 int err;
1007
1008 err = inode_change_ok(inode, attr);
1009 if (err)
1010 return err;
1011
1012 if (S_ISREG(inode->i_mode) &&
1013 attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1014 struct btrfs_trans_handle *trans;
1015 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05001016 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason2bf5a722007-08-30 11:54:02 -04001017
Chris Mason5f39d392007-10-15 16:14:19 -04001018 u64 mask = root->sectorsize - 1;
Chris Mason1b0f7c22008-01-30 14:33:02 -05001019 u64 hole_start = (inode->i_size + mask) & ~mask;
Chris Masonf392a932008-01-30 11:54:05 -05001020 u64 block_end = (attr->ia_size + mask) & ~mask;
Chris Mason39279cc2007-06-12 06:35:45 -04001021 u64 hole_size;
Chris Mason179e29e2007-11-01 11:28:41 -04001022 u64 alloc_hint = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001023
Chris Mason1b0f7c22008-01-30 14:33:02 -05001024 if (attr->ia_size <= hole_start)
Chris Mason39279cc2007-06-12 06:35:45 -04001025 goto out;
1026
Chris Mason1832a6d2007-12-21 16:27:21 -05001027 mutex_lock(&root->fs_info->fs_mutex);
1028 err = btrfs_check_free_space(root, 1, 0);
1029 mutex_unlock(&root->fs_info->fs_mutex);
1030 if (err)
1031 goto fail;
1032
Chris Mason39279cc2007-06-12 06:35:45 -04001033 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1034
Chris Mason1b0f7c22008-01-30 14:33:02 -05001035 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason5f564062008-01-22 16:47:59 -05001036 hole_size = block_end - hole_start;
Chris Mason39279cc2007-06-12 06:35:45 -04001037
1038 mutex_lock(&root->fs_info->fs_mutex);
1039 trans = btrfs_start_transaction(root, 1);
1040 btrfs_set_trans_block_group(trans, inode);
Chris Mason2bf5a722007-08-30 11:54:02 -04001041 err = btrfs_drop_extents(trans, root, inode,
Chris Mason1b0f7c22008-01-30 14:33:02 -05001042 hole_start, block_end, hole_start,
Chris Mason3326d1b2007-10-15 16:18:25 -04001043 &alloc_hint);
Chris Mason2bf5a722007-08-30 11:54:02 -04001044
Chris Mason179e29e2007-11-01 11:28:41 -04001045 if (alloc_hint != EXTENT_MAP_INLINE) {
1046 err = btrfs_insert_file_extent(trans, root,
1047 inode->i_ino,
Chris Mason5f564062008-01-22 16:47:59 -05001048 hole_start, 0, 0,
1049 hole_size);
Chris Masond1310b22008-01-24 16:13:08 -05001050 btrfs_drop_extent_cache(inode, hole_start,
1051 hole_size - 1);
Chris Mason5f564062008-01-22 16:47:59 -05001052 btrfs_check_file(root, inode);
Chris Mason179e29e2007-11-01 11:28:41 -04001053 }
Chris Mason39279cc2007-06-12 06:35:45 -04001054 btrfs_end_transaction(trans, root);
1055 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1b0f7c22008-01-30 14:33:02 -05001056 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
Chris Mason54aa1f42007-06-22 14:16:25 -04001057 if (err)
1058 return err;
Chris Mason39279cc2007-06-12 06:35:45 -04001059 }
1060out:
1061 err = inode_setattr(inode, attr);
Chris Mason1832a6d2007-12-21 16:27:21 -05001062fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001063 return err;
1064}
Chris Mason61295eb2008-01-14 16:24:38 -05001065
Chris Mason2da98f02008-01-16 11:44:43 -05001066void btrfs_put_inode(struct inode *inode)
Chris Mason61295eb2008-01-14 16:24:38 -05001067{
Chris Mason2da98f02008-01-16 11:44:43 -05001068 int ret;
1069
1070 if (!BTRFS_I(inode)->ordered_trans) {
Chris Mason61295eb2008-01-14 16:24:38 -05001071 return;
1072 }
Chris Mason2da98f02008-01-16 11:44:43 -05001073
1074 if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1075 mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1076 return;
1077
1078 ret = btrfs_del_ordered_inode(inode);
1079 if (ret == 1) {
1080 atomic_dec(&inode->i_count);
1081 }
Chris Mason61295eb2008-01-14 16:24:38 -05001082}
1083
Chris Mason39279cc2007-06-12 06:35:45 -04001084void btrfs_delete_inode(struct inode *inode)
1085{
1086 struct btrfs_trans_handle *trans;
1087 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001088 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04001089 int ret;
1090
1091 truncate_inode_pages(&inode->i_data, 0);
1092 if (is_bad_inode(inode)) {
1093 goto no_delete;
1094 }
Chris Mason5f39d392007-10-15 16:14:19 -04001095
Chris Mason39279cc2007-06-12 06:35:45 -04001096 inode->i_size = 0;
1097 mutex_lock(&root->fs_info->fs_mutex);
1098 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001099
Chris Mason39279cc2007-06-12 06:35:45 -04001100 btrfs_set_trans_block_group(trans, inode);
Chris Mason85e21ba2008-01-29 15:11:36 -05001101 ret = btrfs_truncate_in_trans(trans, root, inode, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001102 if (ret)
1103 goto no_delete_lock;
Chris Mason85e21ba2008-01-29 15:11:36 -05001104
Chris Masond3c2fdc2007-09-17 10:58:06 -04001105 nr = trans->blocks_used;
Chris Mason85e21ba2008-01-29 15:11:36 -05001106 clear_inode(inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001107
Chris Mason39279cc2007-06-12 06:35:45 -04001108 btrfs_end_transaction(trans, root);
1109 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001110 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001111 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001112 return;
Chris Mason54aa1f42007-06-22 14:16:25 -04001113
1114no_delete_lock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001115 nr = trans->blocks_used;
Chris Mason54aa1f42007-06-22 14:16:25 -04001116 btrfs_end_transaction(trans, root);
1117 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001118 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001119 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001120no_delete:
1121 clear_inode(inode);
1122}
1123
1124/*
1125 * this returns the key found in the dir entry in the location pointer.
1126 * If no dir entries were found, location->objectid is 0.
1127 */
1128static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1129 struct btrfs_key *location)
1130{
1131 const char *name = dentry->d_name.name;
1132 int namelen = dentry->d_name.len;
1133 struct btrfs_dir_item *di;
1134 struct btrfs_path *path;
1135 struct btrfs_root *root = BTRFS_I(dir)->root;
Yan0d9f7f32007-10-25 15:48:28 -04001136 int ret = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001137
Chris Mason39544012007-12-12 14:38:19 -05001138 if (namelen == 1 && strcmp(name, ".") == 0) {
1139 location->objectid = dir->i_ino;
1140 location->type = BTRFS_INODE_ITEM_KEY;
1141 location->offset = 0;
1142 return 0;
1143 }
Chris Mason39279cc2007-06-12 06:35:45 -04001144 path = btrfs_alloc_path();
1145 BUG_ON(!path);
Chris Mason39544012007-12-12 14:38:19 -05001146
Chris Mason7a720532007-12-13 09:06:59 -05001147 if (namelen == 2 && strcmp(name, "..") == 0) {
Chris Mason39544012007-12-12 14:38:19 -05001148 struct btrfs_key key;
1149 struct extent_buffer *leaf;
1150 u32 nritems;
1151 int slot;
1152
1153 key.objectid = dir->i_ino;
1154 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1155 key.offset = 0;
1156 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1157 BUG_ON(ret == 0);
1158 ret = 0;
1159
1160 leaf = path->nodes[0];
1161 slot = path->slots[0];
1162 nritems = btrfs_header_nritems(leaf);
1163 if (slot >= nritems)
1164 goto out_err;
1165
1166 btrfs_item_key_to_cpu(leaf, &key, slot);
1167 if (key.objectid != dir->i_ino ||
1168 key.type != BTRFS_INODE_REF_KEY) {
1169 goto out_err;
1170 }
1171 location->objectid = key.offset;
1172 location->type = BTRFS_INODE_ITEM_KEY;
1173 location->offset = 0;
1174 goto out;
1175 }
1176
Chris Mason39279cc2007-06-12 06:35:45 -04001177 di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1178 namelen, 0);
Yan0d9f7f32007-10-25 15:48:28 -04001179 if (IS_ERR(di))
1180 ret = PTR_ERR(di);
Chris Mason39279cc2007-06-12 06:35:45 -04001181 if (!di || IS_ERR(di)) {
Chris Mason39544012007-12-12 14:38:19 -05001182 goto out_err;
Chris Mason39279cc2007-06-12 06:35:45 -04001183 }
Chris Mason5f39d392007-10-15 16:14:19 -04001184 btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
Chris Mason39279cc2007-06-12 06:35:45 -04001185out:
Chris Mason39279cc2007-06-12 06:35:45 -04001186 btrfs_free_path(path);
1187 return ret;
Chris Mason39544012007-12-12 14:38:19 -05001188out_err:
1189 location->objectid = 0;
1190 goto out;
Chris Mason39279cc2007-06-12 06:35:45 -04001191}
1192
1193/*
1194 * when we hit a tree root in a directory, the btrfs part of the inode
1195 * needs to be changed to reflect the root directory of the tree root. This
1196 * is kind of like crossing a mount point.
1197 */
1198static int fixup_tree_root_location(struct btrfs_root *root,
1199 struct btrfs_key *location,
Josef Bacik58176a92007-08-29 15:47:34 -04001200 struct btrfs_root **sub_root,
1201 struct dentry *dentry)
Chris Mason39279cc2007-06-12 06:35:45 -04001202{
1203 struct btrfs_path *path;
1204 struct btrfs_root_item *ri;
1205
1206 if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1207 return 0;
1208 if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1209 return 0;
1210
1211 path = btrfs_alloc_path();
1212 BUG_ON(!path);
1213 mutex_lock(&root->fs_info->fs_mutex);
1214
Josef Bacik58176a92007-08-29 15:47:34 -04001215 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1216 dentry->d_name.name,
1217 dentry->d_name.len);
Chris Mason39279cc2007-06-12 06:35:45 -04001218 if (IS_ERR(*sub_root))
1219 return PTR_ERR(*sub_root);
1220
1221 ri = &(*sub_root)->root_item;
1222 location->objectid = btrfs_root_dirid(ri);
Chris Mason39279cc2007-06-12 06:35:45 -04001223 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1224 location->offset = 0;
1225
1226 btrfs_free_path(path);
1227 mutex_unlock(&root->fs_info->fs_mutex);
1228 return 0;
1229}
1230
1231static int btrfs_init_locked_inode(struct inode *inode, void *p)
1232{
1233 struct btrfs_iget_args *args = p;
1234 inode->i_ino = args->ino;
1235 BTRFS_I(inode)->root = args->root;
Chris Mason90692182008-02-08 13:49:28 -05001236 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001237 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1238 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001239 inode->i_mapping, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04001240 return 0;
1241}
1242
1243static int btrfs_find_actor(struct inode *inode, void *opaque)
1244{
1245 struct btrfs_iget_args *args = opaque;
1246 return (args->ino == inode->i_ino &&
1247 args->root == BTRFS_I(inode)->root);
1248}
1249
Chris Masondc17ff82008-01-08 15:46:30 -05001250struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1251 u64 root_objectid)
1252{
1253 struct btrfs_iget_args args;
1254 args.ino = objectid;
1255 args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1256
1257 if (!args.root)
1258 return NULL;
1259
1260 return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1261}
1262
Chris Mason39279cc2007-06-12 06:35:45 -04001263struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1264 struct btrfs_root *root)
1265{
1266 struct inode *inode;
1267 struct btrfs_iget_args args;
1268 args.ino = objectid;
1269 args.root = root;
1270
1271 inode = iget5_locked(s, objectid, btrfs_find_actor,
1272 btrfs_init_locked_inode,
1273 (void *)&args);
1274 return inode;
1275}
1276
1277static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1278 struct nameidata *nd)
1279{
1280 struct inode * inode;
1281 struct btrfs_inode *bi = BTRFS_I(dir);
1282 struct btrfs_root *root = bi->root;
1283 struct btrfs_root *sub_root = root;
1284 struct btrfs_key location;
1285 int ret;
1286
1287 if (dentry->d_name.len > BTRFS_NAME_LEN)
1288 return ERR_PTR(-ENAMETOOLONG);
Chris Mason5f39d392007-10-15 16:14:19 -04001289
Chris Mason39279cc2007-06-12 06:35:45 -04001290 mutex_lock(&root->fs_info->fs_mutex);
1291 ret = btrfs_inode_by_name(dir, dentry, &location);
1292 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason5f39d392007-10-15 16:14:19 -04001293
Chris Mason39279cc2007-06-12 06:35:45 -04001294 if (ret < 0)
1295 return ERR_PTR(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001296
Chris Mason39279cc2007-06-12 06:35:45 -04001297 inode = NULL;
1298 if (location.objectid) {
Josef Bacik58176a92007-08-29 15:47:34 -04001299 ret = fixup_tree_root_location(root, &location, &sub_root,
1300 dentry);
Chris Mason39279cc2007-06-12 06:35:45 -04001301 if (ret < 0)
1302 return ERR_PTR(ret);
1303 if (ret > 0)
1304 return ERR_PTR(-ENOENT);
1305 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1306 sub_root);
1307 if (!inode)
1308 return ERR_PTR(-EACCES);
1309 if (inode->i_state & I_NEW) {
1310 /* the inode and parent dir are two different roots */
1311 if (sub_root != root) {
1312 igrab(inode);
1313 sub_root->inode = inode;
1314 }
1315 BTRFS_I(inode)->root = sub_root;
1316 memcpy(&BTRFS_I(inode)->location, &location,
1317 sizeof(location));
1318 btrfs_read_locked_inode(inode);
1319 unlock_new_inode(inode);
1320 }
1321 }
1322 return d_splice_alias(inode, dentry);
1323}
1324
Chris Mason39279cc2007-06-12 06:35:45 -04001325static unsigned char btrfs_filetype_table[] = {
1326 DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1327};
1328
1329static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1330{
Chris Mason6da6aba2007-12-18 16:15:09 -05001331 struct inode *inode = filp->f_dentry->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04001332 struct btrfs_root *root = BTRFS_I(inode)->root;
1333 struct btrfs_item *item;
1334 struct btrfs_dir_item *di;
1335 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001336 struct btrfs_key found_key;
Chris Mason39279cc2007-06-12 06:35:45 -04001337 struct btrfs_path *path;
1338 int ret;
1339 u32 nritems;
Chris Mason5f39d392007-10-15 16:14:19 -04001340 struct extent_buffer *leaf;
Chris Mason39279cc2007-06-12 06:35:45 -04001341 int slot;
1342 int advance;
1343 unsigned char d_type;
1344 int over = 0;
1345 u32 di_cur;
1346 u32 di_total;
1347 u32 di_len;
1348 int key_type = BTRFS_DIR_INDEX_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001349 char tmp_name[32];
1350 char *name_ptr;
1351 int name_len;
Chris Mason39279cc2007-06-12 06:35:45 -04001352
1353 /* FIXME, use a real flag for deciding about the key type */
1354 if (root->fs_info->tree_root == root)
1355 key_type = BTRFS_DIR_ITEM_KEY;
Chris Mason5f39d392007-10-15 16:14:19 -04001356
Chris Mason39544012007-12-12 14:38:19 -05001357 /* special case for "." */
1358 if (filp->f_pos == 0) {
1359 over = filldir(dirent, ".", 1,
1360 1, inode->i_ino,
1361 DT_DIR);
1362 if (over)
1363 return 0;
1364 filp->f_pos = 1;
1365 }
1366
Chris Mason39279cc2007-06-12 06:35:45 -04001367 mutex_lock(&root->fs_info->fs_mutex);
1368 key.objectid = inode->i_ino;
Chris Mason39544012007-12-12 14:38:19 -05001369 path = btrfs_alloc_path();
1370 path->reada = 2;
1371
1372 /* special case for .., just use the back ref */
1373 if (filp->f_pos == 1) {
1374 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1375 key.offset = 0;
1376 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1377 BUG_ON(ret == 0);
1378 leaf = path->nodes[0];
1379 slot = path->slots[0];
1380 nritems = btrfs_header_nritems(leaf);
1381 if (slot >= nritems) {
1382 btrfs_release_path(root, path);
1383 goto read_dir_items;
1384 }
1385 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1386 btrfs_release_path(root, path);
1387 if (found_key.objectid != key.objectid ||
1388 found_key.type != BTRFS_INODE_REF_KEY)
1389 goto read_dir_items;
1390 over = filldir(dirent, "..", 2,
1391 2, found_key.offset, DT_DIR);
1392 if (over)
1393 goto nopos;
1394 filp->f_pos = 2;
1395 }
1396
1397read_dir_items:
Chris Mason39279cc2007-06-12 06:35:45 -04001398 btrfs_set_key_type(&key, key_type);
1399 key.offset = filp->f_pos;
Chris Mason5f39d392007-10-15 16:14:19 -04001400
Chris Mason39279cc2007-06-12 06:35:45 -04001401 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1402 if (ret < 0)
1403 goto err;
1404 advance = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001405 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -04001406 leaf = path->nodes[0];
1407 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001408 slot = path->slots[0];
1409 if (advance || slot >= nritems) {
1410 if (slot >= nritems -1) {
Chris Mason39279cc2007-06-12 06:35:45 -04001411 ret = btrfs_next_leaf(root, path);
1412 if (ret)
1413 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001414 leaf = path->nodes[0];
1415 nritems = btrfs_header_nritems(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04001416 slot = path->slots[0];
1417 } else {
1418 slot++;
1419 path->slots[0]++;
1420 }
1421 }
1422 advance = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001423 item = btrfs_item_nr(leaf, slot);
1424 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1425
1426 if (found_key.objectid != key.objectid)
Chris Mason39279cc2007-06-12 06:35:45 -04001427 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001428 if (btrfs_key_type(&found_key) != key_type)
Chris Mason39279cc2007-06-12 06:35:45 -04001429 break;
Chris Mason5f39d392007-10-15 16:14:19 -04001430 if (found_key.offset < filp->f_pos)
Chris Mason39279cc2007-06-12 06:35:45 -04001431 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001432
1433 filp->f_pos = found_key.offset;
Chris Mason39279cc2007-06-12 06:35:45 -04001434 advance = 1;
1435 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1436 di_cur = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001437 di_total = btrfs_item_size(leaf, item);
Chris Mason39279cc2007-06-12 06:35:45 -04001438 while(di_cur < di_total) {
Chris Mason5f39d392007-10-15 16:14:19 -04001439 struct btrfs_key location;
1440
1441 name_len = btrfs_dir_name_len(leaf, di);
1442 if (name_len < 32) {
1443 name_ptr = tmp_name;
1444 } else {
1445 name_ptr = kmalloc(name_len, GFP_NOFS);
1446 BUG_ON(!name_ptr);
1447 }
1448 read_extent_buffer(leaf, name_ptr,
1449 (unsigned long)(di + 1), name_len);
1450
1451 d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1452 btrfs_dir_item_key_to_cpu(leaf, di, &location);
Chris Mason5f39d392007-10-15 16:14:19 -04001453 over = filldir(dirent, name_ptr, name_len,
1454 found_key.offset,
1455 location.objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001456 d_type);
Chris Mason5f39d392007-10-15 16:14:19 -04001457
1458 if (name_ptr != tmp_name)
1459 kfree(name_ptr);
1460
Chris Mason39279cc2007-06-12 06:35:45 -04001461 if (over)
1462 goto nopos;
Josef Bacik5103e942007-11-16 11:45:54 -05001463 di_len = btrfs_dir_name_len(leaf, di) +
1464 btrfs_dir_data_len(leaf, di) +sizeof(*di);
Chris Mason39279cc2007-06-12 06:35:45 -04001465 di_cur += di_len;
1466 di = (struct btrfs_dir_item *)((char *)di + di_len);
1467 }
1468 }
Yan Zheng5e591a02008-02-19 11:41:02 -05001469 if (key_type == BTRFS_DIR_INDEX_KEY)
1470 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1471 else
1472 filp->f_pos++;
Chris Mason39279cc2007-06-12 06:35:45 -04001473nopos:
1474 ret = 0;
1475err:
1476 btrfs_release_path(root, path);
1477 btrfs_free_path(path);
1478 mutex_unlock(&root->fs_info->fs_mutex);
1479 return ret;
1480}
1481
1482int btrfs_write_inode(struct inode *inode, int wait)
1483{
1484 struct btrfs_root *root = BTRFS_I(inode)->root;
1485 struct btrfs_trans_handle *trans;
1486 int ret = 0;
1487
1488 if (wait) {
1489 mutex_lock(&root->fs_info->fs_mutex);
1490 trans = btrfs_start_transaction(root, 1);
1491 btrfs_set_trans_block_group(trans, inode);
1492 ret = btrfs_commit_transaction(trans, root);
1493 mutex_unlock(&root->fs_info->fs_mutex);
1494 }
1495 return ret;
1496}
1497
1498/*
Chris Mason54aa1f42007-06-22 14:16:25 -04001499 * This is somewhat expensive, updating the tree every time the
Chris Mason39279cc2007-06-12 06:35:45 -04001500 * inode changes. But, it is most likely to find the inode in cache.
1501 * FIXME, needs more benchmarking...there are no reasons other than performance
1502 * to keep or drop this code.
1503 */
1504void btrfs_dirty_inode(struct inode *inode)
1505{
1506 struct btrfs_root *root = BTRFS_I(inode)->root;
1507 struct btrfs_trans_handle *trans;
1508
1509 mutex_lock(&root->fs_info->fs_mutex);
1510 trans = btrfs_start_transaction(root, 1);
1511 btrfs_set_trans_block_group(trans, inode);
1512 btrfs_update_inode(trans, root, inode);
1513 btrfs_end_transaction(trans, root);
1514 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04001515}
1516
1517static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1518 struct btrfs_root *root,
Chris Mason9c583092008-01-29 15:15:18 -05001519 const char *name, int name_len,
1520 u64 ref_objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001521 u64 objectid,
1522 struct btrfs_block_group_cache *group,
1523 int mode)
1524{
1525 struct inode *inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001526 struct btrfs_inode_item *inode_item;
Chris Mason39279cc2007-06-12 06:35:45 -04001527 struct btrfs_key *location;
Chris Mason5f39d392007-10-15 16:14:19 -04001528 struct btrfs_path *path;
Chris Mason9c583092008-01-29 15:15:18 -05001529 struct btrfs_inode_ref *ref;
1530 struct btrfs_key key[2];
1531 u32 sizes[2];
1532 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04001533 int ret;
1534 int owner;
1535
Chris Mason5f39d392007-10-15 16:14:19 -04001536 path = btrfs_alloc_path();
1537 BUG_ON(!path);
1538
Chris Mason39279cc2007-06-12 06:35:45 -04001539 inode = new_inode(root->fs_info->sb);
1540 if (!inode)
1541 return ERR_PTR(-ENOMEM);
1542
Chris Masond1310b22008-01-24 16:13:08 -05001543 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1544 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masonb888db22007-08-27 16:49:44 -04001545 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001546 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001547 BTRFS_I(inode)->root = root;
Chris Masonb888db22007-08-27 16:49:44 -04001548
Chris Mason39279cc2007-06-12 06:35:45 -04001549 if (mode & S_IFDIR)
1550 owner = 0;
1551 else
1552 owner = 1;
1553 group = btrfs_find_block_group(root, group, 0, 0, owner);
1554 BTRFS_I(inode)->block_group = group;
Yanb98b6762008-01-08 15:54:37 -05001555 BTRFS_I(inode)->flags = 0;
Chris Mason9c583092008-01-29 15:15:18 -05001556
1557 key[0].objectid = objectid;
1558 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1559 key[0].offset = 0;
1560
1561 key[1].objectid = objectid;
1562 btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1563 key[1].offset = ref_objectid;
1564
1565 sizes[0] = sizeof(struct btrfs_inode_item);
1566 sizes[1] = name_len + sizeof(*ref);
1567
1568 ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1569 if (ret != 0)
Chris Mason5f39d392007-10-15 16:14:19 -04001570 goto fail;
1571
Chris Mason9c583092008-01-29 15:15:18 -05001572 if (objectid > root->highest_inode)
1573 root->highest_inode = objectid;
1574
Chris Mason39279cc2007-06-12 06:35:45 -04001575 inode->i_uid = current->fsuid;
1576 inode->i_gid = current->fsgid;
1577 inode->i_mode = mode;
1578 inode->i_ino = objectid;
1579 inode->i_blocks = 0;
1580 inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
Chris Mason5f39d392007-10-15 16:14:19 -04001581 inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1582 struct btrfs_inode_item);
1583 fill_inode_item(path->nodes[0], inode_item, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001584
1585 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1586 struct btrfs_inode_ref);
1587 btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1588 ptr = (unsigned long)(ref + 1);
1589 write_extent_buffer(path->nodes[0], name, ptr, name_len);
1590
Chris Mason5f39d392007-10-15 16:14:19 -04001591 btrfs_mark_buffer_dirty(path->nodes[0]);
1592 btrfs_free_path(path);
1593
Chris Mason39279cc2007-06-12 06:35:45 -04001594 location = &BTRFS_I(inode)->location;
1595 location->objectid = objectid;
Chris Mason39279cc2007-06-12 06:35:45 -04001596 location->offset = 0;
1597 btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1598
Chris Mason39279cc2007-06-12 06:35:45 -04001599 insert_inode_hash(inode);
1600 return inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001601fail:
1602 btrfs_free_path(path);
1603 return ERR_PTR(ret);
Chris Mason39279cc2007-06-12 06:35:45 -04001604}
1605
1606static inline u8 btrfs_inode_type(struct inode *inode)
1607{
1608 return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1609}
1610
1611static int btrfs_add_link(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001612 struct dentry *dentry, struct inode *inode,
1613 int add_backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001614{
1615 int ret;
1616 struct btrfs_key key;
1617 struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
Chris Mason79c44582007-06-25 10:09:33 -04001618 struct inode *parent_inode;
Chris Mason5f39d392007-10-15 16:14:19 -04001619
Chris Mason39279cc2007-06-12 06:35:45 -04001620 key.objectid = inode->i_ino;
Chris Mason39279cc2007-06-12 06:35:45 -04001621 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1622 key.offset = 0;
1623
1624 ret = btrfs_insert_dir_item(trans, root,
1625 dentry->d_name.name, dentry->d_name.len,
1626 dentry->d_parent->d_inode->i_ino,
1627 &key, btrfs_inode_type(inode));
1628 if (ret == 0) {
Chris Mason9c583092008-01-29 15:15:18 -05001629 if (add_backref) {
1630 ret = btrfs_insert_inode_ref(trans, root,
1631 dentry->d_name.name,
1632 dentry->d_name.len,
1633 inode->i_ino,
1634 dentry->d_parent->d_inode->i_ino);
1635 }
Chris Mason79c44582007-06-25 10:09:33 -04001636 parent_inode = dentry->d_parent->d_inode;
1637 parent_inode->i_size += dentry->d_name.len * 2;
1638 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
Chris Mason39279cc2007-06-12 06:35:45 -04001639 ret = btrfs_update_inode(trans, root,
1640 dentry->d_parent->d_inode);
1641 }
1642 return ret;
1643}
1644
1645static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
Chris Mason9c583092008-01-29 15:15:18 -05001646 struct dentry *dentry, struct inode *inode,
1647 int backref)
Chris Mason39279cc2007-06-12 06:35:45 -04001648{
Chris Mason9c583092008-01-29 15:15:18 -05001649 int err = btrfs_add_link(trans, dentry, inode, backref);
Chris Mason39279cc2007-06-12 06:35:45 -04001650 if (!err) {
1651 d_instantiate(dentry, inode);
1652 return 0;
1653 }
1654 if (err > 0)
1655 err = -EEXIST;
1656 return err;
1657}
1658
Josef Bacik618e21d2007-07-11 10:18:17 -04001659static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1660 int mode, dev_t rdev)
1661{
1662 struct btrfs_trans_handle *trans;
1663 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001664 struct inode *inode = NULL;
Josef Bacik618e21d2007-07-11 10:18:17 -04001665 int err;
1666 int drop_inode = 0;
1667 u64 objectid;
Chris Mason1832a6d2007-12-21 16:27:21 -05001668 unsigned long nr = 0;
Josef Bacik618e21d2007-07-11 10:18:17 -04001669
1670 if (!new_valid_dev(rdev))
1671 return -EINVAL;
1672
1673 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001674 err = btrfs_check_free_space(root, 1, 0);
1675 if (err)
1676 goto fail;
1677
Josef Bacik618e21d2007-07-11 10:18:17 -04001678 trans = btrfs_start_transaction(root, 1);
1679 btrfs_set_trans_block_group(trans, dir);
1680
1681 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1682 if (err) {
1683 err = -ENOSPC;
1684 goto out_unlock;
1685 }
1686
Chris Mason9c583092008-01-29 15:15:18 -05001687 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1688 dentry->d_name.len,
1689 dentry->d_parent->d_inode->i_ino, objectid,
Josef Bacik618e21d2007-07-11 10:18:17 -04001690 BTRFS_I(dir)->block_group, mode);
1691 err = PTR_ERR(inode);
1692 if (IS_ERR(inode))
1693 goto out_unlock;
1694
1695 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001696 err = btrfs_add_nondir(trans, dentry, inode, 0);
Josef Bacik618e21d2007-07-11 10:18:17 -04001697 if (err)
1698 drop_inode = 1;
1699 else {
1700 inode->i_op = &btrfs_special_inode_operations;
1701 init_special_inode(inode, inode->i_mode, rdev);
Yan1b4ab1b2007-08-29 09:11:44 -04001702 btrfs_update_inode(trans, root, inode);
Josef Bacik618e21d2007-07-11 10:18:17 -04001703 }
1704 dir->i_sb->s_dirt = 1;
1705 btrfs_update_inode_block_group(trans, inode);
1706 btrfs_update_inode_block_group(trans, dir);
1707out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001708 nr = trans->blocks_used;
Josef Bacik618e21d2007-07-11 10:18:17 -04001709 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001710fail:
Josef Bacik618e21d2007-07-11 10:18:17 -04001711 mutex_unlock(&root->fs_info->fs_mutex);
1712
1713 if (drop_inode) {
1714 inode_dec_link_count(inode);
1715 iput(inode);
1716 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001717 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001718 btrfs_throttle(root);
Josef Bacik618e21d2007-07-11 10:18:17 -04001719 return err;
1720}
1721
Chris Mason39279cc2007-06-12 06:35:45 -04001722static int btrfs_create(struct inode *dir, struct dentry *dentry,
1723 int mode, struct nameidata *nd)
1724{
1725 struct btrfs_trans_handle *trans;
1726 struct btrfs_root *root = BTRFS_I(dir)->root;
Chris Mason1832a6d2007-12-21 16:27:21 -05001727 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04001728 int err;
1729 int drop_inode = 0;
Chris Mason1832a6d2007-12-21 16:27:21 -05001730 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001731 u64 objectid;
1732
1733 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001734 err = btrfs_check_free_space(root, 1, 0);
1735 if (err)
1736 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001737 trans = btrfs_start_transaction(root, 1);
1738 btrfs_set_trans_block_group(trans, dir);
1739
1740 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1741 if (err) {
1742 err = -ENOSPC;
1743 goto out_unlock;
1744 }
1745
Chris Mason9c583092008-01-29 15:15:18 -05001746 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1747 dentry->d_name.len,
1748 dentry->d_parent->d_inode->i_ino,
1749 objectid, BTRFS_I(dir)->block_group, mode);
Chris Mason39279cc2007-06-12 06:35:45 -04001750 err = PTR_ERR(inode);
1751 if (IS_ERR(inode))
1752 goto out_unlock;
1753
1754 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05001755 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001756 if (err)
1757 drop_inode = 1;
1758 else {
1759 inode->i_mapping->a_ops = &btrfs_aops;
1760 inode->i_fop = &btrfs_file_operations;
1761 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05001762 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1763 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04001764 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05001765 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001766 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04001767 }
1768 dir->i_sb->s_dirt = 1;
1769 btrfs_update_inode_block_group(trans, inode);
1770 btrfs_update_inode_block_group(trans, dir);
1771out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001772 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001773 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001774fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001775 mutex_unlock(&root->fs_info->fs_mutex);
1776
1777 if (drop_inode) {
1778 inode_dec_link_count(inode);
1779 iput(inode);
1780 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001781 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001782 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001783 return err;
1784}
1785
1786static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1787 struct dentry *dentry)
1788{
1789 struct btrfs_trans_handle *trans;
1790 struct btrfs_root *root = BTRFS_I(dir)->root;
1791 struct inode *inode = old_dentry->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05001792 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001793 int err;
1794 int drop_inode = 0;
1795
1796 if (inode->i_nlink == 0)
1797 return -ENOENT;
1798
Chris Mason6da6aba2007-12-18 16:15:09 -05001799#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1800 inode->i_nlink++;
1801#else
Chris Mason39279cc2007-06-12 06:35:45 -04001802 inc_nlink(inode);
Chris Mason6da6aba2007-12-18 16:15:09 -05001803#endif
Chris Mason39279cc2007-06-12 06:35:45 -04001804 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001805 err = btrfs_check_free_space(root, 1, 0);
1806 if (err)
1807 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04001808 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001809
Chris Mason39279cc2007-06-12 06:35:45 -04001810 btrfs_set_trans_block_group(trans, dir);
1811 atomic_inc(&inode->i_count);
Chris Mason9c583092008-01-29 15:15:18 -05001812 err = btrfs_add_nondir(trans, dentry, inode, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04001813
Chris Mason39279cc2007-06-12 06:35:45 -04001814 if (err)
1815 drop_inode = 1;
Chris Mason5f39d392007-10-15 16:14:19 -04001816
Chris Mason39279cc2007-06-12 06:35:45 -04001817 dir->i_sb->s_dirt = 1;
1818 btrfs_update_inode_block_group(trans, dir);
Chris Mason54aa1f42007-06-22 14:16:25 -04001819 err = btrfs_update_inode(trans, root, inode);
Chris Mason5f39d392007-10-15 16:14:19 -04001820
Chris Mason54aa1f42007-06-22 14:16:25 -04001821 if (err)
1822 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001823
Chris Masond3c2fdc2007-09-17 10:58:06 -04001824 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001825 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05001826fail:
Chris Mason39279cc2007-06-12 06:35:45 -04001827 mutex_unlock(&root->fs_info->fs_mutex);
1828
1829 if (drop_inode) {
1830 inode_dec_link_count(inode);
1831 iput(inode);
1832 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04001833 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001834 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001835 return err;
1836}
1837
Chris Mason39279cc2007-06-12 06:35:45 -04001838static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1839{
1840 struct inode *inode;
1841 struct btrfs_trans_handle *trans;
1842 struct btrfs_root *root = BTRFS_I(dir)->root;
1843 int err = 0;
1844 int drop_on_err = 0;
1845 u64 objectid;
Chris Masond3c2fdc2007-09-17 10:58:06 -04001846 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04001847
1848 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05001849 err = btrfs_check_free_space(root, 1, 0);
1850 if (err)
1851 goto out_unlock;
1852
Chris Mason39279cc2007-06-12 06:35:45 -04001853 trans = btrfs_start_transaction(root, 1);
1854 btrfs_set_trans_block_group(trans, dir);
Chris Mason5f39d392007-10-15 16:14:19 -04001855
Chris Mason39279cc2007-06-12 06:35:45 -04001856 if (IS_ERR(trans)) {
1857 err = PTR_ERR(trans);
1858 goto out_unlock;
1859 }
1860
1861 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1862 if (err) {
1863 err = -ENOSPC;
1864 goto out_unlock;
1865 }
1866
Chris Mason9c583092008-01-29 15:15:18 -05001867 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1868 dentry->d_name.len,
1869 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04001870 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1871 if (IS_ERR(inode)) {
1872 err = PTR_ERR(inode);
1873 goto out_fail;
1874 }
Chris Mason5f39d392007-10-15 16:14:19 -04001875
Chris Mason39279cc2007-06-12 06:35:45 -04001876 drop_on_err = 1;
1877 inode->i_op = &btrfs_dir_inode_operations;
1878 inode->i_fop = &btrfs_dir_file_operations;
1879 btrfs_set_trans_block_group(trans, inode);
1880
Chris Mason39544012007-12-12 14:38:19 -05001881 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04001882 err = btrfs_update_inode(trans, root, inode);
1883 if (err)
1884 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001885
Chris Mason9c583092008-01-29 15:15:18 -05001886 err = btrfs_add_link(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04001887 if (err)
1888 goto out_fail;
Chris Mason5f39d392007-10-15 16:14:19 -04001889
Chris Mason39279cc2007-06-12 06:35:45 -04001890 d_instantiate(dentry, inode);
1891 drop_on_err = 0;
1892 dir->i_sb->s_dirt = 1;
1893 btrfs_update_inode_block_group(trans, inode);
1894 btrfs_update_inode_block_group(trans, dir);
1895
1896out_fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04001897 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04001898 btrfs_end_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04001899
Chris Mason39279cc2007-06-12 06:35:45 -04001900out_unlock:
1901 mutex_unlock(&root->fs_info->fs_mutex);
1902 if (drop_on_err)
1903 iput(inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001904 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05001905 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04001906 return err;
1907}
1908
Chris Masona52d9a82007-08-27 16:49:44 -04001909struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
Chris Mason70dec802008-01-29 09:59:12 -05001910 size_t pg_offset, u64 start, u64 len,
Chris Masona52d9a82007-08-27 16:49:44 -04001911 int create)
1912{
1913 int ret;
1914 int err = 0;
Chris Masondb945352007-10-15 16:15:53 -04001915 u64 bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04001916 u64 extent_start = 0;
1917 u64 extent_end = 0;
1918 u64 objectid = inode->i_ino;
1919 u32 found_type;
Chris Masona52d9a82007-08-27 16:49:44 -04001920 struct btrfs_path *path;
1921 struct btrfs_root *root = BTRFS_I(inode)->root;
1922 struct btrfs_file_extent_item *item;
Chris Mason5f39d392007-10-15 16:14:19 -04001923 struct extent_buffer *leaf;
1924 struct btrfs_key found_key;
Chris Masona52d9a82007-08-27 16:49:44 -04001925 struct extent_map *em = NULL;
1926 struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
Chris Masond1310b22008-01-24 16:13:08 -05001927 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04001928 struct btrfs_trans_handle *trans = NULL;
1929
1930 path = btrfs_alloc_path();
1931 BUG_ON(!path);
1932 mutex_lock(&root->fs_info->fs_mutex);
1933
1934again:
Chris Masond1310b22008-01-24 16:13:08 -05001935 spin_lock(&em_tree->lock);
1936 em = lookup_extent_mapping(em_tree, start, len);
1937 spin_unlock(&em_tree->lock);
1938
Chris Masona52d9a82007-08-27 16:49:44 -04001939 if (em) {
Chris Mason56b453c2008-01-03 09:08:27 -05001940 if (em->start > start) {
Chris Masond1310b22008-01-24 16:13:08 -05001941 printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1942 start, len, em->start, em->len);
Chris Mason56b453c2008-01-03 09:08:27 -05001943 WARN_ON(1);
1944 }
Chris Mason70dec802008-01-29 09:59:12 -05001945 if (em->block_start == EXTENT_MAP_INLINE && page)
1946 free_extent_map(em);
1947 else
1948 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001949 }
Chris Masond1310b22008-01-24 16:13:08 -05001950 em = alloc_extent_map(GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04001951 if (!em) {
Chris Masond1310b22008-01-24 16:13:08 -05001952 err = -ENOMEM;
1953 goto out;
Chris Masona52d9a82007-08-27 16:49:44 -04001954 }
Chris Masond1310b22008-01-24 16:13:08 -05001955
1956 em->start = EXTENT_MAP_HOLE;
1957 em->len = (u64)-1;
Chris Masona52d9a82007-08-27 16:49:44 -04001958 em->bdev = inode->i_sb->s_bdev;
Chris Mason179e29e2007-11-01 11:28:41 -04001959 ret = btrfs_lookup_file_extent(trans, root, path,
1960 objectid, start, trans != NULL);
Chris Masona52d9a82007-08-27 16:49:44 -04001961 if (ret < 0) {
1962 err = ret;
1963 goto out;
1964 }
1965
1966 if (ret != 0) {
1967 if (path->slots[0] == 0)
1968 goto not_found;
1969 path->slots[0]--;
1970 }
1971
Chris Mason5f39d392007-10-15 16:14:19 -04001972 leaf = path->nodes[0];
1973 item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masona52d9a82007-08-27 16:49:44 -04001974 struct btrfs_file_extent_item);
Chris Masona52d9a82007-08-27 16:49:44 -04001975 /* are we inside the extent that was found? */
Chris Mason5f39d392007-10-15 16:14:19 -04001976 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1977 found_type = btrfs_key_type(&found_key);
1978 if (found_key.objectid != objectid ||
Chris Masona52d9a82007-08-27 16:49:44 -04001979 found_type != BTRFS_EXTENT_DATA_KEY) {
1980 goto not_found;
1981 }
1982
Chris Mason5f39d392007-10-15 16:14:19 -04001983 found_type = btrfs_file_extent_type(leaf, item);
1984 extent_start = found_key.offset;
Chris Masona52d9a82007-08-27 16:49:44 -04001985 if (found_type == BTRFS_FILE_EXTENT_REG) {
1986 extent_end = extent_start +
Chris Masondb945352007-10-15 16:15:53 -04001987 btrfs_file_extent_num_bytes(leaf, item);
Chris Masona52d9a82007-08-27 16:49:44 -04001988 err = 0;
Chris Masonb888db22007-08-27 16:49:44 -04001989 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04001990 em->start = start;
1991 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05001992 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04001993 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05001994 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04001995 } else {
Chris Masond1310b22008-01-24 16:13:08 -05001996 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04001997 }
1998 goto not_found_em;
1999 }
Chris Masondb945352007-10-15 16:15:53 -04002000 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2001 if (bytenr == 0) {
Chris Masona52d9a82007-08-27 16:49:44 -04002002 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002003 em->len = extent_end - extent_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002004 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002005 goto insert;
2006 }
Chris Masondb945352007-10-15 16:15:53 -04002007 bytenr += btrfs_file_extent_offset(leaf, item);
2008 em->block_start = bytenr;
Chris Masona52d9a82007-08-27 16:49:44 -04002009 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002010 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002011 goto insert;
2012 } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
Chris Mason70dec802008-01-29 09:59:12 -05002013 u64 page_start;
Chris Mason5f39d392007-10-15 16:14:19 -04002014 unsigned long ptr;
Chris Masona52d9a82007-08-27 16:49:44 -04002015 char *map;
Chris Mason3326d1b2007-10-15 16:18:25 -04002016 size_t size;
2017 size_t extent_offset;
2018 size_t copy_size;
Chris Masona52d9a82007-08-27 16:49:44 -04002019
Chris Mason5f39d392007-10-15 16:14:19 -04002020 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2021 path->slots[0]));
Chris Masond1310b22008-01-24 16:13:08 -05002022 extent_end = (extent_start + size + root->sectorsize - 1) &
2023 ~((u64)root->sectorsize - 1);
Chris Masonb888db22007-08-27 16:49:44 -04002024 if (start < extent_start || start >= extent_end) {
Chris Masona52d9a82007-08-27 16:49:44 -04002025 em->start = start;
2026 if (start < extent_start) {
Chris Masond1310b22008-01-24 16:13:08 -05002027 if (start + len <= extent_start)
Chris Masonb888db22007-08-27 16:49:44 -04002028 goto not_found;
Chris Masond1310b22008-01-24 16:13:08 -05002029 em->len = extent_end - extent_start;
Chris Masona52d9a82007-08-27 16:49:44 -04002030 } else {
Chris Masond1310b22008-01-24 16:13:08 -05002031 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002032 }
2033 goto not_found_em;
2034 }
2035 em->block_start = EXTENT_MAP_INLINE;
Yan689f9342007-10-29 11:41:07 -04002036
2037 if (!page) {
2038 em->start = extent_start;
Chris Masond1310b22008-01-24 16:13:08 -05002039 em->len = size;
Yan689f9342007-10-29 11:41:07 -04002040 goto out;
2041 }
2042
Chris Mason70dec802008-01-29 09:59:12 -05002043 page_start = page_offset(page) + pg_offset;
2044 extent_offset = page_start - extent_start;
2045 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
Yan689f9342007-10-29 11:41:07 -04002046 size - extent_offset);
Chris Mason3326d1b2007-10-15 16:18:25 -04002047 em->start = extent_start + extent_offset;
Chris Mason70dec802008-01-29 09:59:12 -05002048 em->len = (copy_size + root->sectorsize - 1) &
2049 ~((u64)root->sectorsize - 1);
Yan689f9342007-10-29 11:41:07 -04002050 map = kmap(page);
2051 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
Chris Mason179e29e2007-11-01 11:28:41 -04002052 if (create == 0 && !PageUptodate(page)) {
Chris Mason70dec802008-01-29 09:59:12 -05002053 read_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002054 copy_size);
2055 flush_dcache_page(page);
2056 } else if (create && PageUptodate(page)) {
2057 if (!trans) {
2058 kunmap(page);
2059 free_extent_map(em);
2060 em = NULL;
2061 btrfs_release_path(root, path);
2062 trans = btrfs_start_transaction(root, 1);
2063 goto again;
2064 }
Chris Mason70dec802008-01-29 09:59:12 -05002065 write_extent_buffer(leaf, map + pg_offset, ptr,
Chris Mason179e29e2007-11-01 11:28:41 -04002066 copy_size);
2067 btrfs_mark_buffer_dirty(leaf);
Chris Masona52d9a82007-08-27 16:49:44 -04002068 }
Chris Masona52d9a82007-08-27 16:49:44 -04002069 kunmap(page);
Chris Masond1310b22008-01-24 16:13:08 -05002070 set_extent_uptodate(io_tree, em->start,
2071 extent_map_end(em) - 1, GFP_NOFS);
Chris Masona52d9a82007-08-27 16:49:44 -04002072 goto insert;
2073 } else {
2074 printk("unkknown found_type %d\n", found_type);
2075 WARN_ON(1);
2076 }
2077not_found:
2078 em->start = start;
Chris Masond1310b22008-01-24 16:13:08 -05002079 em->len = len;
Chris Masona52d9a82007-08-27 16:49:44 -04002080not_found_em:
Chris Mason5f39d392007-10-15 16:14:19 -04002081 em->block_start = EXTENT_MAP_HOLE;
Chris Masona52d9a82007-08-27 16:49:44 -04002082insert:
2083 btrfs_release_path(root, path);
Chris Masond1310b22008-01-24 16:13:08 -05002084 if (em->start > start || extent_map_end(em) <= start) {
2085 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002086 err = -EIO;
2087 goto out;
2088 }
Chris Masond1310b22008-01-24 16:13:08 -05002089
2090 err = 0;
2091 spin_lock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002092 ret = add_extent_mapping(em_tree, em);
2093 if (ret == -EEXIST) {
2094 free_extent_map(em);
Chris Masond1310b22008-01-24 16:13:08 -05002095 em = lookup_extent_mapping(em_tree, start, len);
2096 if (!em) {
Chris Masona52d9a82007-08-27 16:49:44 -04002097 err = -EIO;
Chris Masond1310b22008-01-24 16:13:08 -05002098 printk("failing to insert %Lu %Lu\n", start, len);
Chris Masona52d9a82007-08-27 16:49:44 -04002099 }
Chris Masona52d9a82007-08-27 16:49:44 -04002100 }
Chris Masond1310b22008-01-24 16:13:08 -05002101 spin_unlock(&em_tree->lock);
Chris Masona52d9a82007-08-27 16:49:44 -04002102out:
2103 btrfs_free_path(path);
2104 if (trans) {
2105 ret = btrfs_end_transaction(trans, root);
2106 if (!err)
2107 err = ret;
2108 }
2109 mutex_unlock(&root->fs_info->fs_mutex);
2110 if (err) {
2111 free_extent_map(em);
2112 WARN_ON(1);
2113 return ERR_PTR(err);
2114 }
2115 return em;
2116}
2117
Christoph Hellwigd396c6f52007-09-10 20:02:30 -04002118static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
Chris Mason39279cc2007-06-12 06:35:45 -04002119{
Christoph Hellwigd396c6f52007-09-10 20:02:30 -04002120 return extent_bmap(mapping, iblock, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002121}
2122
Chris Mason9ebefb182007-06-15 13:50:00 -04002123int btrfs_readpage(struct file *file, struct page *page)
2124{
Chris Masond1310b22008-01-24 16:13:08 -05002125 struct extent_io_tree *tree;
2126 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002127 return extent_read_full_page(tree, page, btrfs_get_extent);
Chris Mason39279cc2007-06-12 06:35:45 -04002128}
Chris Mason1832a6d2007-12-21 16:27:21 -05002129
Chris Mason39279cc2007-06-12 06:35:45 -04002130static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2131{
Chris Masond1310b22008-01-24 16:13:08 -05002132 struct extent_io_tree *tree;
Chris Masonb888db22007-08-27 16:49:44 -04002133
2134
2135 if (current->flags & PF_MEMALLOC) {
2136 redirty_page_for_writepage(wbc, page);
2137 unlock_page(page);
2138 return 0;
2139 }
Chris Masond1310b22008-01-24 16:13:08 -05002140 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002141 return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2142}
Chris Mason39279cc2007-06-12 06:35:45 -04002143
Chris Masonb293f022007-11-01 19:45:34 -04002144static int btrfs_writepages(struct address_space *mapping,
2145 struct writeback_control *wbc)
2146{
Chris Masond1310b22008-01-24 16:13:08 -05002147 struct extent_io_tree *tree;
2148 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Masonb293f022007-11-01 19:45:34 -04002149 return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2150}
2151
Chris Mason3ab2fb52007-11-08 10:59:22 -05002152static int
2153btrfs_readpages(struct file *file, struct address_space *mapping,
2154 struct list_head *pages, unsigned nr_pages)
2155{
Chris Masond1310b22008-01-24 16:13:08 -05002156 struct extent_io_tree *tree;
2157 tree = &BTRFS_I(mapping->host)->io_tree;
Chris Mason3ab2fb52007-11-08 10:59:22 -05002158 return extent_readpages(tree, mapping, pages, nr_pages,
2159 btrfs_get_extent);
2160}
2161
Chris Mason70dec802008-01-29 09:59:12 -05002162static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
Chris Masona52d9a82007-08-27 16:49:44 -04002163{
Chris Masond1310b22008-01-24 16:13:08 -05002164 struct extent_io_tree *tree;
2165 struct extent_map_tree *map;
Chris Masona52d9a82007-08-27 16:49:44 -04002166 int ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002167
Chris Masond1310b22008-01-24 16:13:08 -05002168 tree = &BTRFS_I(page->mapping->host)->io_tree;
2169 map = &BTRFS_I(page->mapping->host)->extent_tree;
Chris Mason70dec802008-01-29 09:59:12 -05002170 ret = try_release_extent_mapping(map, tree, page, gfp_flags);
Chris Masona52d9a82007-08-27 16:49:44 -04002171 if (ret == 1) {
2172 ClearPagePrivate(page);
2173 set_page_private(page, 0);
2174 page_cache_release(page);
2175 }
2176 return ret;
2177}
Chris Mason39279cc2007-06-12 06:35:45 -04002178
Chris Masona52d9a82007-08-27 16:49:44 -04002179static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2180{
Chris Masond1310b22008-01-24 16:13:08 -05002181 struct extent_io_tree *tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002182
Chris Masond1310b22008-01-24 16:13:08 -05002183 tree = &BTRFS_I(page->mapping->host)->io_tree;
Chris Masona52d9a82007-08-27 16:49:44 -04002184 extent_invalidatepage(tree, page, offset);
2185 btrfs_releasepage(page, GFP_NOFS);
Chris Mason39279cc2007-06-12 06:35:45 -04002186}
2187
Chris Mason9ebefb182007-06-15 13:50:00 -04002188/*
2189 * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2190 * called from a page fault handler when a page is first dirtied. Hence we must
2191 * be careful to check for EOF conditions here. We set the page up correctly
2192 * for a written page which means we get ENOSPC checking when writing into
2193 * holes and correct delalloc and unwritten extent mapping on filesystems that
2194 * support these features.
2195 *
2196 * We are not allowed to take the i_mutex here so we have to play games to
2197 * protect against truncate races as the page could now be beyond EOF. Because
2198 * vmtruncate() writes the inode size before removing pages, once we have the
2199 * page lock we can determine safely if the page is beyond EOF. If it is not
2200 * beyond EOF, then the page is guaranteed safe against truncation until we
2201 * unlock the page.
2202 */
2203int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2204{
Chris Mason6da6aba2007-12-18 16:15:09 -05002205 struct inode *inode = fdentry(vma->vm_file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002206 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Mason9ebefb182007-06-15 13:50:00 -04002207 unsigned long end;
2208 loff_t size;
Chris Mason1832a6d2007-12-21 16:27:21 -05002209 int ret;
Chris Masona52d9a82007-08-27 16:49:44 -04002210 u64 page_start;
Chris Mason9ebefb182007-06-15 13:50:00 -04002211
Chris Mason1832a6d2007-12-21 16:27:21 -05002212 mutex_lock(&root->fs_info->fs_mutex);
2213 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
Chris Mason8f662a72008-01-02 10:01:11 -05002214 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002215 if (ret)
2216 goto out;
2217
2218 ret = -EINVAL;
2219
Chris Mason9ebefb182007-06-15 13:50:00 -04002220 lock_page(page);
2221 wait_on_page_writeback(page);
Chris Mason9ebefb182007-06-15 13:50:00 -04002222 size = i_size_read(inode);
Chris Mason35ebb932007-10-30 16:56:53 -04002223 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Masona52d9a82007-08-27 16:49:44 -04002224
Chris Mason9ebefb182007-06-15 13:50:00 -04002225 if ((page->mapping != inode->i_mapping) ||
Chris Masona52d9a82007-08-27 16:49:44 -04002226 (page_start > size)) {
Chris Mason9ebefb182007-06-15 13:50:00 -04002227 /* page got truncated out from underneath us */
2228 goto out_unlock;
2229 }
2230
2231 /* page is wholly or partially inside EOF */
Chris Masona52d9a82007-08-27 16:49:44 -04002232 if (page_start + PAGE_CACHE_SIZE > size)
Chris Mason9ebefb182007-06-15 13:50:00 -04002233 end = size & ~PAGE_CACHE_MASK;
2234 else
2235 end = PAGE_CACHE_SIZE;
2236
Chris Masonb888db22007-08-27 16:49:44 -04002237 ret = btrfs_cow_one_page(inode, page, end);
Chris Mason9ebefb182007-06-15 13:50:00 -04002238
2239out_unlock:
2240 unlock_page(page);
Chris Mason1832a6d2007-12-21 16:27:21 -05002241out:
Chris Mason9ebefb182007-06-15 13:50:00 -04002242 return ret;
2243}
2244
Chris Mason39279cc2007-06-12 06:35:45 -04002245static void btrfs_truncate(struct inode *inode)
2246{
2247 struct btrfs_root *root = BTRFS_I(inode)->root;
2248 int ret;
2249 struct btrfs_trans_handle *trans;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002250 unsigned long nr;
Chris Mason39279cc2007-06-12 06:35:45 -04002251
2252 if (!S_ISREG(inode->i_mode))
2253 return;
2254 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2255 return;
2256
2257 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2258
2259 mutex_lock(&root->fs_info->fs_mutex);
2260 trans = btrfs_start_transaction(root, 1);
2261 btrfs_set_trans_block_group(trans, inode);
2262
2263 /* FIXME, add redo link to tree so we don't leak on crash */
Chris Mason85e21ba2008-01-29 15:11:36 -05002264 ret = btrfs_truncate_in_trans(trans, root, inode,
2265 BTRFS_EXTENT_DATA_KEY);
Chris Mason39279cc2007-06-12 06:35:45 -04002266 btrfs_update_inode(trans, root, inode);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002267 nr = trans->blocks_used;
Chris Mason5f39d392007-10-15 16:14:19 -04002268
Chris Mason39279cc2007-06-12 06:35:45 -04002269 ret = btrfs_end_transaction(trans, root);
2270 BUG_ON(ret);
2271 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002272 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002273 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002274}
2275
Chris Mason4313b392008-01-03 09:08:48 -05002276static int noinline create_subvol(struct btrfs_root *root, char *name,
2277 int namelen)
Chris Mason39279cc2007-06-12 06:35:45 -04002278{
2279 struct btrfs_trans_handle *trans;
2280 struct btrfs_key key;
2281 struct btrfs_root_item root_item;
2282 struct btrfs_inode_item *inode_item;
Chris Mason5f39d392007-10-15 16:14:19 -04002283 struct extent_buffer *leaf;
Chris Masondc17ff82008-01-08 15:46:30 -05002284 struct btrfs_root *new_root = root;
Chris Mason39279cc2007-06-12 06:35:45 -04002285 struct inode *inode;
2286 struct inode *dir;
2287 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002288 int err;
Chris Mason39279cc2007-06-12 06:35:45 -04002289 u64 objectid;
2290 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
Chris Masond3c2fdc2007-09-17 10:58:06 -04002291 unsigned long nr = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002292
2293 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002294 ret = btrfs_check_free_space(root, 1, 0);
2295 if (ret)
2296 goto fail_commit;
2297
Chris Mason39279cc2007-06-12 06:35:45 -04002298 trans = btrfs_start_transaction(root, 1);
2299 BUG_ON(!trans);
2300
Chris Mason7bb86312007-12-11 09:25:06 -05002301 ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2302 0, &objectid);
2303 if (ret)
2304 goto fail;
2305
2306 leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2307 objectid, trans->transid, 0, 0,
2308 0, 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002309 if (IS_ERR(leaf))
2310 return PTR_ERR(leaf);
2311
2312 btrfs_set_header_nritems(leaf, 0);
2313 btrfs_set_header_level(leaf, 0);
Chris Masondb945352007-10-15 16:15:53 -04002314 btrfs_set_header_bytenr(leaf, leaf->start);
Chris Mason5f39d392007-10-15 16:14:19 -04002315 btrfs_set_header_generation(leaf, trans->transid);
Chris Mason7bb86312007-12-11 09:25:06 -05002316 btrfs_set_header_owner(leaf, objectid);
2317
Chris Mason5f39d392007-10-15 16:14:19 -04002318 write_extent_buffer(leaf, root->fs_info->fsid,
2319 (unsigned long)btrfs_header_fsid(leaf),
2320 BTRFS_FSID_SIZE);
2321 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002322
2323 inode_item = &root_item.inode;
2324 memset(inode_item, 0, sizeof(*inode_item));
Chris Mason5f39d392007-10-15 16:14:19 -04002325 inode_item->generation = cpu_to_le64(1);
2326 inode_item->size = cpu_to_le64(3);
2327 inode_item->nlink = cpu_to_le32(1);
2328 inode_item->nblocks = cpu_to_le64(1);
2329 inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
Chris Mason39279cc2007-06-12 06:35:45 -04002330
Chris Masondb945352007-10-15 16:15:53 -04002331 btrfs_set_root_bytenr(&root_item, leaf->start);
2332 btrfs_set_root_level(&root_item, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002333 btrfs_set_root_refs(&root_item, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002334 btrfs_set_root_used(&root_item, 0);
2335
Chris Mason5eda7b52007-06-22 14:16:25 -04002336 memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2337 root_item.drop_level = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04002338
2339 free_extent_buffer(leaf);
2340 leaf = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002341
Chris Mason39279cc2007-06-12 06:35:45 -04002342 btrfs_set_root_dirid(&root_item, new_dirid);
2343
2344 key.objectid = objectid;
2345 key.offset = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002346 btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2347 ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2348 &root_item);
Chris Mason54aa1f42007-06-22 14:16:25 -04002349 if (ret)
2350 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002351
2352 /*
2353 * insert the directory item
2354 */
2355 key.offset = (u64)-1;
2356 dir = root->fs_info->sb->s_root->d_inode;
2357 ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2358 name, namelen, dir->i_ino, &key,
2359 BTRFS_FT_DIR);
Chris Mason54aa1f42007-06-22 14:16:25 -04002360 if (ret)
2361 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002362
Chris Mason39544012007-12-12 14:38:19 -05002363 ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2364 name, namelen, objectid,
2365 root->fs_info->sb->s_root->d_inode->i_ino);
2366 if (ret)
2367 goto fail;
2368
Chris Mason39279cc2007-06-12 06:35:45 -04002369 ret = btrfs_commit_transaction(trans, root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002370 if (ret)
2371 goto fail_commit;
Chris Mason39279cc2007-06-12 06:35:45 -04002372
Josef Bacik58176a92007-08-29 15:47:34 -04002373 new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
Chris Mason39279cc2007-06-12 06:35:45 -04002374 BUG_ON(!new_root);
2375
2376 trans = btrfs_start_transaction(new_root, 1);
2377 BUG_ON(!trans);
2378
Chris Mason9c583092008-01-29 15:15:18 -05002379 inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2380 new_dirid,
Chris Mason39279cc2007-06-12 06:35:45 -04002381 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
Chris Mason54aa1f42007-06-22 14:16:25 -04002382 if (IS_ERR(inode))
2383 goto fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002384 inode->i_op = &btrfs_dir_inode_operations;
2385 inode->i_fop = &btrfs_dir_file_operations;
Chris Mason34088782007-06-12 11:36:58 -04002386 new_root->inode = inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002387
Chris Mason39544012007-12-12 14:38:19 -05002388 ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2389 new_dirid);
Chris Mason39279cc2007-06-12 06:35:45 -04002390 inode->i_nlink = 1;
Chris Mason39544012007-12-12 14:38:19 -05002391 inode->i_size = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002392 ret = btrfs_update_inode(trans, new_root, inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002393 if (ret)
2394 goto fail;
2395fail:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002396 nr = trans->blocks_used;
Chris Masondc17ff82008-01-08 15:46:30 -05002397 err = btrfs_commit_transaction(trans, new_root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002398 if (err && !ret)
2399 ret = err;
2400fail_commit:
Chris Mason39279cc2007-06-12 06:35:45 -04002401 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002402 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002403 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002404 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002405}
2406
2407static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2408{
Chris Mason3063d292008-01-08 15:46:30 -05002409 struct btrfs_pending_snapshot *pending_snapshot;
Chris Mason39279cc2007-06-12 06:35:45 -04002410 struct btrfs_trans_handle *trans;
Chris Mason39279cc2007-06-12 06:35:45 -04002411 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04002412 int err;
Chris Mason1832a6d2007-12-21 16:27:21 -05002413 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002414
2415 if (!root->ref_cows)
2416 return -EINVAL;
2417
2418 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002419 ret = btrfs_check_free_space(root, 1, 0);
2420 if (ret)
2421 goto fail_unlock;
2422
Chris Mason3063d292008-01-08 15:46:30 -05002423 pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2424 if (!pending_snapshot) {
2425 ret = -ENOMEM;
2426 goto fail_unlock;
2427 }
Yanfb4bc1e2008-01-17 11:59:51 -05002428 pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
Chris Mason3063d292008-01-08 15:46:30 -05002429 if (!pending_snapshot->name) {
2430 ret = -ENOMEM;
2431 kfree(pending_snapshot);
2432 goto fail_unlock;
2433 }
Yanfb4bc1e2008-01-17 11:59:51 -05002434 memcpy(pending_snapshot->name, name, namelen);
2435 pending_snapshot->name[namelen] = '\0';
Chris Mason39279cc2007-06-12 06:35:45 -04002436 trans = btrfs_start_transaction(root, 1);
2437 BUG_ON(!trans);
Chris Mason3063d292008-01-08 15:46:30 -05002438 pending_snapshot->root = root;
2439 list_add(&pending_snapshot->list,
2440 &trans->transaction->pending_snapshots);
Chris Mason39279cc2007-06-12 06:35:45 -04002441 ret = btrfs_update_inode(trans, root, root->inode);
Chris Mason54aa1f42007-06-22 14:16:25 -04002442 err = btrfs_commit_transaction(trans, root);
Chris Mason5f39d392007-10-15 16:14:19 -04002443
Chris Mason1832a6d2007-12-21 16:27:21 -05002444fail_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002445 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masond3c2fdc2007-09-17 10:58:06 -04002446 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002447 btrfs_throttle(root);
Chris Mason54aa1f42007-06-22 14:16:25 -04002448 return ret;
Chris Mason39279cc2007-06-12 06:35:45 -04002449}
2450
Chris Masonedbd8d42007-12-21 16:27:24 -05002451unsigned long btrfs_force_ra(struct address_space *mapping,
Chris Mason86479a02007-09-10 19:58:16 -04002452 struct file_ra_state *ra, struct file *file,
2453 pgoff_t offset, pgoff_t last_index)
2454{
2455 pgoff_t req_size;
2456
2457#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2458 req_size = last_index - offset + 1;
2459 offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2460 return offset;
2461#else
2462 req_size = min(last_index - offset + 1, (pgoff_t)128);
2463 page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2464 return offset + req_size;
2465#endif
2466}
2467
2468int btrfs_defrag_file(struct file *file) {
Chris Mason6da6aba2007-12-18 16:15:09 -05002469 struct inode *inode = fdentry(file)->d_inode;
Chris Mason1832a6d2007-12-21 16:27:21 -05002470 struct btrfs_root *root = BTRFS_I(inode)->root;
Chris Masond1310b22008-01-24 16:13:08 -05002471 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason86479a02007-09-10 19:58:16 -04002472 struct page *page;
2473 unsigned long last_index;
2474 unsigned long ra_index = 0;
2475 u64 page_start;
2476 u64 page_end;
2477 unsigned long i;
Chris Mason1832a6d2007-12-21 16:27:21 -05002478 int ret;
2479
2480 mutex_lock(&root->fs_info->fs_mutex);
2481 ret = btrfs_check_free_space(root, inode->i_size, 0);
2482 mutex_unlock(&root->fs_info->fs_mutex);
2483 if (ret)
2484 return -ENOSPC;
Chris Mason86479a02007-09-10 19:58:16 -04002485
2486 mutex_lock(&inode->i_mutex);
2487 last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2488 for (i = 0; i <= last_index; i++) {
2489 if (i == ra_index) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002490 ra_index = btrfs_force_ra(inode->i_mapping,
2491 &file->f_ra,
2492 file, ra_index, last_index);
Chris Mason86479a02007-09-10 19:58:16 -04002493 }
2494 page = grab_cache_page(inode->i_mapping, i);
2495 if (!page)
2496 goto out_unlock;
2497 if (!PageUptodate(page)) {
2498 btrfs_readpage(NULL, page);
2499 lock_page(page);
2500 if (!PageUptodate(page)) {
2501 unlock_page(page);
2502 page_cache_release(page);
2503 goto out_unlock;
2504 }
2505 }
Chris Mason35ebb932007-10-30 16:56:53 -04002506 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
Chris Mason86479a02007-09-10 19:58:16 -04002507 page_end = page_start + PAGE_CACHE_SIZE - 1;
2508
Chris Masond1310b22008-01-24 16:13:08 -05002509 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masond1310b22008-01-24 16:13:08 -05002510 set_extent_delalloc(io_tree, page_start,
Chris Mason86479a02007-09-10 19:58:16 -04002511 page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002512
Chris Masond1310b22008-01-24 16:13:08 -05002513 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Mason86479a02007-09-10 19:58:16 -04002514 set_page_dirty(page);
2515 unlock_page(page);
2516 page_cache_release(page);
2517 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2518 }
2519
2520out_unlock:
2521 mutex_unlock(&inode->i_mutex);
2522 return 0;
2523}
2524
Chris Masonedbd8d42007-12-21 16:27:24 -05002525static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2526{
2527 u64 new_size;
2528 u64 old_size;
2529 struct btrfs_ioctl_vol_args *vol_args;
2530 struct btrfs_trans_handle *trans;
2531 char *sizestr;
2532 int ret = 0;
2533 int namelen;
2534 int mod = 0;
2535
2536 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2537
2538 if (!vol_args)
2539 return -ENOMEM;
2540
2541 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2542 ret = -EFAULT;
2543 goto out;
2544 }
2545 namelen = strlen(vol_args->name);
2546 if (namelen > BTRFS_VOL_NAME_MAX) {
2547 ret = -EINVAL;
2548 goto out;
2549 }
2550
2551 sizestr = vol_args->name;
2552 if (!strcmp(sizestr, "max"))
2553 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2554 else {
2555 if (sizestr[0] == '-') {
2556 mod = -1;
2557 sizestr++;
2558 } else if (sizestr[0] == '+') {
2559 mod = 1;
2560 sizestr++;
2561 }
2562 new_size = btrfs_parse_size(sizestr);
2563 if (new_size == 0) {
2564 ret = -EINVAL;
2565 goto out;
2566 }
2567 }
2568
2569 mutex_lock(&root->fs_info->fs_mutex);
2570 old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2571
2572 if (mod < 0) {
2573 if (new_size > old_size) {
2574 ret = -EINVAL;
2575 goto out_unlock;
2576 }
2577 new_size = old_size - new_size;
2578 } else if (mod > 0) {
2579 new_size = old_size + new_size;
2580 }
2581
2582 if (new_size < 256 * 1024 * 1024) {
2583 ret = -EINVAL;
2584 goto out_unlock;
2585 }
2586 if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2587 ret = -EFBIG;
2588 goto out_unlock;
2589 }
Chris Masonf9ef6602008-01-03 09:22:38 -05002590
2591 do_div(new_size, root->sectorsize);
2592 new_size *= root->sectorsize;
Chris Masonedbd8d42007-12-21 16:27:24 -05002593
2594printk("new size is %Lu\n", new_size);
2595 if (new_size > old_size) {
2596 trans = btrfs_start_transaction(root, 1);
2597 ret = btrfs_grow_extent_tree(trans, root, new_size);
2598 btrfs_commit_transaction(trans, root);
2599 } else {
2600 ret = btrfs_shrink_extent_tree(root, new_size);
2601 }
2602
2603out_unlock:
2604 mutex_unlock(&root->fs_info->fs_mutex);
2605out:
2606 kfree(vol_args);
2607 return ret;
2608}
2609
Chris Mason4313b392008-01-03 09:08:48 -05002610static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2611 void __user *arg)
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002612{
Chris Mason4aec2b52007-12-18 16:25:45 -05002613 struct btrfs_ioctl_vol_args *vol_args;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002614 struct btrfs_dir_item *di;
2615 struct btrfs_path *path;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002616 u64 root_dirid;
Chris Mason4aec2b52007-12-18 16:25:45 -05002617 int namelen;
2618 int ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002619
Chris Mason4aec2b52007-12-18 16:25:45 -05002620 vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
Chris Mason5f39d392007-10-15 16:14:19 -04002621
Chris Mason4aec2b52007-12-18 16:25:45 -05002622 if (!vol_args)
2623 return -ENOMEM;
2624
2625 if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2626 ret = -EFAULT;
2627 goto out;
2628 }
2629
2630 namelen = strlen(vol_args->name);
2631 if (namelen > BTRFS_VOL_NAME_MAX) {
2632 ret = -EINVAL;
2633 goto out;
2634 }
2635 if (strchr(vol_args->name, '/')) {
2636 ret = -EINVAL;
2637 goto out;
2638 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002639
2640 path = btrfs_alloc_path();
Chris Mason4aec2b52007-12-18 16:25:45 -05002641 if (!path) {
2642 ret = -ENOMEM;
2643 goto out;
2644 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002645
2646 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2647 mutex_lock(&root->fs_info->fs_mutex);
2648 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2649 path, root_dirid,
Chris Mason4aec2b52007-12-18 16:25:45 -05002650 vol_args->name, namelen, 0);
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002651 mutex_unlock(&root->fs_info->fs_mutex);
2652 btrfs_free_path(path);
Chris Mason4aec2b52007-12-18 16:25:45 -05002653
2654 if (di && !IS_ERR(di)) {
2655 ret = -EEXIST;
2656 goto out;
2657 }
2658
2659 if (IS_ERR(di)) {
2660 ret = PTR_ERR(di);
2661 goto out;
2662 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002663
2664 if (root == root->fs_info->tree_root)
Chris Mason4aec2b52007-12-18 16:25:45 -05002665 ret = create_subvol(root, vol_args->name, namelen);
2666 else
2667 ret = create_snapshot(root, vol_args->name, namelen);
2668out:
2669 kfree(vol_args);
2670 return ret;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002671}
2672
2673static int btrfs_ioctl_defrag(struct file *file)
Chris Mason39279cc2007-06-12 06:35:45 -04002674{
Chris Mason6da6aba2007-12-18 16:15:09 -05002675 struct inode *inode = fdentry(file)->d_inode;
Chris Mason39279cc2007-06-12 06:35:45 -04002676 struct btrfs_root *root = BTRFS_I(inode)->root;
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002677
2678 switch (inode->i_mode & S_IFMT) {
2679 case S_IFDIR:
2680 mutex_lock(&root->fs_info->fs_mutex);
2681 btrfs_defrag_root(root, 0);
2682 btrfs_defrag_root(root->fs_info->extent_root, 0);
2683 mutex_unlock(&root->fs_info->fs_mutex);
2684 break;
2685 case S_IFREG:
2686 btrfs_defrag_file(file);
2687 break;
2688 }
2689
2690 return 0;
2691}
2692
2693long btrfs_ioctl(struct file *file, unsigned int
2694 cmd, unsigned long arg)
2695{
Chris Mason6da6aba2007-12-18 16:15:09 -05002696 struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
Chris Mason39279cc2007-06-12 06:35:45 -04002697
2698 switch (cmd) {
2699 case BTRFS_IOC_SNAP_CREATE:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002700 return btrfs_ioctl_snap_create(root, (void __user *)arg);
Chris Mason6702ed42007-08-07 16:15:09 -04002701 case BTRFS_IOC_DEFRAG:
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002702 return btrfs_ioctl_defrag(file);
Chris Masonedbd8d42007-12-21 16:27:24 -05002703 case BTRFS_IOC_RESIZE:
2704 return btrfs_ioctl_resize(root, (void __user *)arg);
Chris Mason39279cc2007-06-12 06:35:45 -04002705 }
Christoph Hellwigd03581f2007-09-14 10:22:57 -04002706
2707 return -ENOTTY;
Chris Mason39279cc2007-06-12 06:35:45 -04002708}
2709
Chris Mason39279cc2007-06-12 06:35:45 -04002710/*
2711 * Called inside transaction, so use GFP_NOFS
2712 */
2713struct inode *btrfs_alloc_inode(struct super_block *sb)
2714{
2715 struct btrfs_inode *ei;
2716
2717 ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2718 if (!ei)
2719 return NULL;
Josef Bacik15ee9bc2007-08-10 16:22:09 -04002720 ei->last_trans = 0;
Chris Masondc17ff82008-01-08 15:46:30 -05002721 ei->ordered_trans = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002722 return &ei->vfs_inode;
2723}
2724
2725void btrfs_destroy_inode(struct inode *inode)
2726{
2727 WARN_ON(!list_empty(&inode->i_dentry));
2728 WARN_ON(inode->i_data.nrpages);
2729
Chris Mason8c416c92008-01-14 15:10:26 -05002730 btrfs_drop_extent_cache(inode, 0, (u64)-1);
Chris Mason39279cc2007-06-12 06:35:45 -04002731 kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2732}
2733
Chris Mason44ec0b72007-10-29 10:55:05 -04002734#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2735static void init_once(struct kmem_cache * cachep, void *foo)
2736#else
Chris Mason39279cc2007-06-12 06:35:45 -04002737static void init_once(void * foo, struct kmem_cache * cachep,
2738 unsigned long flags)
Chris Mason44ec0b72007-10-29 10:55:05 -04002739#endif
Chris Mason39279cc2007-06-12 06:35:45 -04002740{
2741 struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2742
2743 inode_init_once(&ei->vfs_inode);
2744}
2745
2746void btrfs_destroy_cachep(void)
2747{
2748 if (btrfs_inode_cachep)
2749 kmem_cache_destroy(btrfs_inode_cachep);
2750 if (btrfs_trans_handle_cachep)
2751 kmem_cache_destroy(btrfs_trans_handle_cachep);
2752 if (btrfs_transaction_cachep)
2753 kmem_cache_destroy(btrfs_transaction_cachep);
2754 if (btrfs_bit_radix_cachep)
2755 kmem_cache_destroy(btrfs_bit_radix_cachep);
2756 if (btrfs_path_cachep)
2757 kmem_cache_destroy(btrfs_path_cachep);
2758}
2759
Chris Mason86479a02007-09-10 19:58:16 -04002760struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
Chris Mason92fee662007-07-25 12:31:35 -04002761 unsigned long extra_flags,
Chris Mason44ec0b72007-10-29 10:55:05 -04002762#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2763 void (*ctor)(struct kmem_cache *, void *)
2764#else
Chris Mason92fee662007-07-25 12:31:35 -04002765 void (*ctor)(void *, struct kmem_cache *,
Chris Mason44ec0b72007-10-29 10:55:05 -04002766 unsigned long)
2767#endif
2768 )
Chris Mason92fee662007-07-25 12:31:35 -04002769{
2770 return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2771 SLAB_MEM_SPREAD | extra_flags), ctor
2772#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2773 ,NULL
2774#endif
2775 );
2776}
2777
Chris Mason39279cc2007-06-12 06:35:45 -04002778int btrfs_init_cachep(void)
2779{
Chris Mason86479a02007-09-10 19:58:16 -04002780 btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
Chris Mason92fee662007-07-25 12:31:35 -04002781 sizeof(struct btrfs_inode),
2782 0, init_once);
Chris Mason39279cc2007-06-12 06:35:45 -04002783 if (!btrfs_inode_cachep)
2784 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002785 btrfs_trans_handle_cachep =
2786 btrfs_cache_create("btrfs_trans_handle_cache",
2787 sizeof(struct btrfs_trans_handle),
2788 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002789 if (!btrfs_trans_handle_cachep)
2790 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002791 btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
Chris Mason39279cc2007-06-12 06:35:45 -04002792 sizeof(struct btrfs_transaction),
Chris Mason92fee662007-07-25 12:31:35 -04002793 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002794 if (!btrfs_transaction_cachep)
2795 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002796 btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
Yan23223582007-09-17 11:08:52 -04002797 sizeof(struct btrfs_path),
Chris Mason92fee662007-07-25 12:31:35 -04002798 0, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002799 if (!btrfs_path_cachep)
2800 goto fail;
Chris Mason86479a02007-09-10 19:58:16 -04002801 btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
Chris Mason92fee662007-07-25 12:31:35 -04002802 SLAB_DESTROY_BY_RCU, NULL);
Chris Mason39279cc2007-06-12 06:35:45 -04002803 if (!btrfs_bit_radix_cachep)
2804 goto fail;
2805 return 0;
2806fail:
2807 btrfs_destroy_cachep();
2808 return -ENOMEM;
2809}
2810
2811static int btrfs_getattr(struct vfsmount *mnt,
2812 struct dentry *dentry, struct kstat *stat)
2813{
2814 struct inode *inode = dentry->d_inode;
2815 generic_fillattr(inode, stat);
Chris Masond6667462008-01-03 14:51:00 -05002816 stat->blksize = PAGE_CACHE_SIZE;
Chris Mason90692182008-02-08 13:49:28 -05002817 stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
Chris Mason39279cc2007-06-12 06:35:45 -04002818 return 0;
2819}
2820
2821static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2822 struct inode * new_dir,struct dentry *new_dentry)
2823{
2824 struct btrfs_trans_handle *trans;
2825 struct btrfs_root *root = BTRFS_I(old_dir)->root;
2826 struct inode *new_inode = new_dentry->d_inode;
2827 struct inode *old_inode = old_dentry->d_inode;
2828 struct timespec ctime = CURRENT_TIME;
2829 struct btrfs_path *path;
Chris Mason39279cc2007-06-12 06:35:45 -04002830 int ret;
2831
2832 if (S_ISDIR(old_inode->i_mode) && new_inode &&
2833 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2834 return -ENOTEMPTY;
2835 }
Chris Mason5f39d392007-10-15 16:14:19 -04002836
Chris Mason39279cc2007-06-12 06:35:45 -04002837 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002838 ret = btrfs_check_free_space(root, 1, 0);
2839 if (ret)
2840 goto out_unlock;
2841
Chris Mason39279cc2007-06-12 06:35:45 -04002842 trans = btrfs_start_transaction(root, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002843
Chris Mason39279cc2007-06-12 06:35:45 -04002844 btrfs_set_trans_block_group(trans, new_dir);
2845 path = btrfs_alloc_path();
2846 if (!path) {
2847 ret = -ENOMEM;
2848 goto out_fail;
2849 }
2850
2851 old_dentry->d_inode->i_nlink++;
2852 old_dir->i_ctime = old_dir->i_mtime = ctime;
2853 new_dir->i_ctime = new_dir->i_mtime = ctime;
2854 old_inode->i_ctime = ctime;
Chris Mason5f39d392007-10-15 16:14:19 -04002855
Chris Mason39279cc2007-06-12 06:35:45 -04002856 ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2857 if (ret)
2858 goto out_fail;
2859
2860 if (new_inode) {
2861 new_inode->i_ctime = CURRENT_TIME;
2862 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2863 if (ret)
2864 goto out_fail;
Chris Mason39279cc2007-06-12 06:35:45 -04002865 }
Chris Mason9c583092008-01-29 15:15:18 -05002866 ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
Chris Mason39279cc2007-06-12 06:35:45 -04002867 if (ret)
2868 goto out_fail;
2869
2870out_fail:
2871 btrfs_free_path(path);
2872 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002873out_unlock:
Chris Mason39279cc2007-06-12 06:35:45 -04002874 mutex_unlock(&root->fs_info->fs_mutex);
2875 return ret;
2876}
2877
2878static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2879 const char *symname)
2880{
2881 struct btrfs_trans_handle *trans;
2882 struct btrfs_root *root = BTRFS_I(dir)->root;
2883 struct btrfs_path *path;
2884 struct btrfs_key key;
Chris Mason1832a6d2007-12-21 16:27:21 -05002885 struct inode *inode = NULL;
Chris Mason39279cc2007-06-12 06:35:45 -04002886 int err;
2887 int drop_inode = 0;
2888 u64 objectid;
2889 int name_len;
2890 int datasize;
Chris Mason5f39d392007-10-15 16:14:19 -04002891 unsigned long ptr;
Chris Mason39279cc2007-06-12 06:35:45 -04002892 struct btrfs_file_extent_item *ei;
Chris Mason5f39d392007-10-15 16:14:19 -04002893 struct extent_buffer *leaf;
Chris Mason1832a6d2007-12-21 16:27:21 -05002894 unsigned long nr = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002895
2896 name_len = strlen(symname) + 1;
2897 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2898 return -ENAMETOOLONG;
Chris Mason1832a6d2007-12-21 16:27:21 -05002899
Chris Mason39279cc2007-06-12 06:35:45 -04002900 mutex_lock(&root->fs_info->fs_mutex);
Chris Mason1832a6d2007-12-21 16:27:21 -05002901 err = btrfs_check_free_space(root, 1, 0);
2902 if (err)
2903 goto out_fail;
2904
Chris Mason39279cc2007-06-12 06:35:45 -04002905 trans = btrfs_start_transaction(root, 1);
2906 btrfs_set_trans_block_group(trans, dir);
2907
2908 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2909 if (err) {
2910 err = -ENOSPC;
2911 goto out_unlock;
2912 }
2913
Chris Mason9c583092008-01-29 15:15:18 -05002914 inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2915 dentry->d_name.len,
2916 dentry->d_parent->d_inode->i_ino, objectid,
Chris Mason39279cc2007-06-12 06:35:45 -04002917 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2918 err = PTR_ERR(inode);
2919 if (IS_ERR(inode))
2920 goto out_unlock;
2921
2922 btrfs_set_trans_block_group(trans, inode);
Chris Mason9c583092008-01-29 15:15:18 -05002923 err = btrfs_add_nondir(trans, dentry, inode, 0);
Chris Mason39279cc2007-06-12 06:35:45 -04002924 if (err)
2925 drop_inode = 1;
2926 else {
2927 inode->i_mapping->a_ops = &btrfs_aops;
2928 inode->i_fop = &btrfs_file_operations;
2929 inode->i_op = &btrfs_file_inode_operations;
Chris Masond1310b22008-01-24 16:13:08 -05002930 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2931 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
Chris Masona52d9a82007-08-27 16:49:44 -04002932 inode->i_mapping, GFP_NOFS);
Chris Mason90692182008-02-08 13:49:28 -05002933 BTRFS_I(inode)->delalloc_bytes = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002934 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
Chris Mason39279cc2007-06-12 06:35:45 -04002935 }
2936 dir->i_sb->s_dirt = 1;
2937 btrfs_update_inode_block_group(trans, inode);
2938 btrfs_update_inode_block_group(trans, dir);
2939 if (drop_inode)
2940 goto out_unlock;
2941
2942 path = btrfs_alloc_path();
2943 BUG_ON(!path);
2944 key.objectid = inode->i_ino;
2945 key.offset = 0;
Chris Mason39279cc2007-06-12 06:35:45 -04002946 btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2947 datasize = btrfs_file_extent_calc_inline_size(name_len);
2948 err = btrfs_insert_empty_item(trans, root, path, &key,
2949 datasize);
Chris Mason54aa1f42007-06-22 14:16:25 -04002950 if (err) {
2951 drop_inode = 1;
2952 goto out_unlock;
2953 }
Chris Mason5f39d392007-10-15 16:14:19 -04002954 leaf = path->nodes[0];
2955 ei = btrfs_item_ptr(leaf, path->slots[0],
2956 struct btrfs_file_extent_item);
2957 btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2958 btrfs_set_file_extent_type(leaf, ei,
Chris Mason39279cc2007-06-12 06:35:45 -04002959 BTRFS_FILE_EXTENT_INLINE);
2960 ptr = btrfs_file_extent_inline_start(ei);
Chris Mason5f39d392007-10-15 16:14:19 -04002961 write_extent_buffer(leaf, symname, ptr, name_len);
2962 btrfs_mark_buffer_dirty(leaf);
Chris Mason39279cc2007-06-12 06:35:45 -04002963 btrfs_free_path(path);
Chris Mason5f39d392007-10-15 16:14:19 -04002964
Chris Mason39279cc2007-06-12 06:35:45 -04002965 inode->i_op = &btrfs_symlink_inode_operations;
2966 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2967 inode->i_size = name_len - 1;
Chris Mason54aa1f42007-06-22 14:16:25 -04002968 err = btrfs_update_inode(trans, root, inode);
2969 if (err)
2970 drop_inode = 1;
Chris Mason39279cc2007-06-12 06:35:45 -04002971
2972out_unlock:
Chris Masond3c2fdc2007-09-17 10:58:06 -04002973 nr = trans->blocks_used;
Chris Mason39279cc2007-06-12 06:35:45 -04002974 btrfs_end_transaction(trans, root);
Chris Mason1832a6d2007-12-21 16:27:21 -05002975out_fail:
Chris Mason39279cc2007-06-12 06:35:45 -04002976 mutex_unlock(&root->fs_info->fs_mutex);
Chris Mason39279cc2007-06-12 06:35:45 -04002977 if (drop_inode) {
2978 inode_dec_link_count(inode);
2979 iput(inode);
2980 }
Chris Masond3c2fdc2007-09-17 10:58:06 -04002981 btrfs_btree_balance_dirty(root, nr);
Chris Masone2008b62008-01-08 15:46:30 -05002982 btrfs_throttle(root);
Chris Mason39279cc2007-06-12 06:35:45 -04002983 return err;
2984}
Yanfdebe2b2008-01-14 13:26:08 -05002985static int btrfs_permission(struct inode *inode, int mask,
2986 struct nameidata *nd)
2987{
2988 if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
2989 return -EACCES;
2990 return generic_permission(inode, mask, NULL);
2991}
Chris Mason39279cc2007-06-12 06:35:45 -04002992
2993static struct inode_operations btrfs_dir_inode_operations = {
2994 .lookup = btrfs_lookup,
2995 .create = btrfs_create,
2996 .unlink = btrfs_unlink,
2997 .link = btrfs_link,
2998 .mkdir = btrfs_mkdir,
2999 .rmdir = btrfs_rmdir,
3000 .rename = btrfs_rename,
3001 .symlink = btrfs_symlink,
3002 .setattr = btrfs_setattr,
Josef Bacik618e21d2007-07-11 10:18:17 -04003003 .mknod = btrfs_mknod,
Josef Bacik5103e942007-11-16 11:45:54 -05003004 .setxattr = generic_setxattr,
3005 .getxattr = generic_getxattr,
3006 .listxattr = btrfs_listxattr,
3007 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003008 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003009};
Chris Mason39279cc2007-06-12 06:35:45 -04003010static struct inode_operations btrfs_dir_ro_inode_operations = {
3011 .lookup = btrfs_lookup,
Yanfdebe2b2008-01-14 13:26:08 -05003012 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003013};
Chris Mason39279cc2007-06-12 06:35:45 -04003014static struct file_operations btrfs_dir_file_operations = {
3015 .llseek = generic_file_llseek,
3016 .read = generic_read_dir,
3017 .readdir = btrfs_readdir,
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003018 .unlocked_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003019#ifdef CONFIG_COMPAT
Christoph Hellwig34287aa2007-09-14 10:22:47 -04003020 .compat_ioctl = btrfs_ioctl,
Chris Mason39279cc2007-06-12 06:35:45 -04003021#endif
3022};
3023
Chris Masond1310b22008-01-24 16:13:08 -05003024static struct extent_io_ops btrfs_extent_io_ops = {
Chris Mason07157aa2007-08-30 08:50:51 -04003025 .fill_delalloc = run_delalloc_range,
Chris Mason065631f2008-02-20 12:07:25 -05003026 .submit_bio_hook = btrfs_submit_bio_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003027 .readpage_io_hook = btrfs_readpage_io_hook,
3028 .readpage_end_io_hook = btrfs_readpage_end_io_hook,
Chris Masonb0c68f82008-01-31 11:05:37 -05003029 .set_bit_hook = btrfs_set_bit_hook,
3030 .clear_bit_hook = btrfs_clear_bit_hook,
Chris Mason07157aa2007-08-30 08:50:51 -04003031};
3032
Chris Mason39279cc2007-06-12 06:35:45 -04003033static struct address_space_operations btrfs_aops = {
3034 .readpage = btrfs_readpage,
3035 .writepage = btrfs_writepage,
Chris Masonb293f022007-11-01 19:45:34 -04003036 .writepages = btrfs_writepages,
Chris Mason3ab2fb52007-11-08 10:59:22 -05003037 .readpages = btrfs_readpages,
Chris Mason39279cc2007-06-12 06:35:45 -04003038 .sync_page = block_sync_page,
Chris Mason39279cc2007-06-12 06:35:45 -04003039 .bmap = btrfs_bmap,
Chris Masona52d9a82007-08-27 16:49:44 -04003040 .invalidatepage = btrfs_invalidatepage,
3041 .releasepage = btrfs_releasepage,
3042 .set_page_dirty = __set_page_dirty_nobuffers,
Chris Mason39279cc2007-06-12 06:35:45 -04003043};
3044
3045static struct address_space_operations btrfs_symlink_aops = {
3046 .readpage = btrfs_readpage,
3047 .writepage = btrfs_writepage,
Chris Mason2bf5a722007-08-30 11:54:02 -04003048 .invalidatepage = btrfs_invalidatepage,
3049 .releasepage = btrfs_releasepage,
Chris Mason39279cc2007-06-12 06:35:45 -04003050};
3051
3052static struct inode_operations btrfs_file_inode_operations = {
3053 .truncate = btrfs_truncate,
3054 .getattr = btrfs_getattr,
3055 .setattr = btrfs_setattr,
Josef Bacik5103e942007-11-16 11:45:54 -05003056 .setxattr = generic_setxattr,
3057 .getxattr = generic_getxattr,
3058 .listxattr = btrfs_listxattr,
3059 .removexattr = generic_removexattr,
Yanfdebe2b2008-01-14 13:26:08 -05003060 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003061};
Josef Bacik618e21d2007-07-11 10:18:17 -04003062static struct inode_operations btrfs_special_inode_operations = {
3063 .getattr = btrfs_getattr,
3064 .setattr = btrfs_setattr,
Yanfdebe2b2008-01-14 13:26:08 -05003065 .permission = btrfs_permission,
Josef Bacik618e21d2007-07-11 10:18:17 -04003066};
Chris Mason39279cc2007-06-12 06:35:45 -04003067static struct inode_operations btrfs_symlink_inode_operations = {
3068 .readlink = generic_readlink,
3069 .follow_link = page_follow_link_light,
3070 .put_link = page_put_link,
Yanfdebe2b2008-01-14 13:26:08 -05003071 .permission = btrfs_permission,
Chris Mason39279cc2007-06-12 06:35:45 -04003072};